SPB Git

Repository Public

FoldersScreen.swift 6.1 KB 143 lines swift

1// SP Notes iOS2// File: Features/Folders/FoldersScreen.swift3// Description: Browsing the sections and their nested folders on iPhone, the recursive tree rows shared with the iPad sidebar,4//              and the small sheet that names a new folder or renames one.5//6// Author: Simon-Pierre Boucher7// Contact: contact@spboucher.ai8import SPNotesCore9import SwiftUI1011struct FoldersScreen: View {12    @Environment(AppModel.self) private var app13    @Environment(ShellState.self) private var shell14    @State private var tree: [String: [FolderNode]] = [:]1516    var body: some View {17        List {18            ForEach(NoteSection.editable, id: \.self) { section in19                Section {20                    NavigationLink(value: Destination.list(.section(section))) {21                        FolderRow(title: String(localized: "Tout dans \(NoteSection.title(section))"), symbol: NoteSection.symbol(section))22                    }23                    .accessibilityIdentifier("section-\(section)")24                    ForEach(tree[section] ?? []) { node in FolderTreeRow(node: node, depth: 0) }25                } header: {26                    HStack {27                        Text(NoteSection.title(section))28                        Spacer()29                        Button { shell.sheet = .newFolder(parentId: nil, section: section) } label: {30                            Image(systemName: "folder.badge.plus").frame(minWidth: 44, minHeight: 32, alignment: .trailing)31                        }32                        .accessibilityLabel("Nouveau dossier dans \(NoteSection.title(section))")33                        .accessibilityIdentifier("new-folder-\(section)")34                    }35                }36            }37        }38        .listStyle(.insetGrouped)39        .scrollContentBackground(.hidden)40        .background(Color.spBackground)41        .navigationTitle("Dossiers")42        .task(id: app.feed.structure) { tree = FolderTree.build((try? await app.store.folderTree()) ?? []) }43    }44}4546/// One folder of the tree with its children behind a disclosure. On iPhone a tap opens the folder.47struct FolderTreeRow: View {48    let node: FolderNode49    let depth: Int50    @Environment(AppModel.self) private var app51    @Environment(ShellState.self) private var shell52    @State private var expanded = false53    @State private var deletion: Int?5455    var body: some View {56        Group {57            if node.children.isEmpty {58                label59            } else {60                DisclosureGroup(isExpanded: $expanded) {61                    ForEach(node.children) { FolderTreeRow(node: $0, depth: depth + 1) }62                } label: { label }63            }64        }65    }6667    private var label: some View {68        Button { shell.open(source: .folder(node.id)) } label: { FolderRow(title: node.title) }69            .buttonStyle(.plain)70            .accessibilityIdentifier("folder-row")71            .contextMenu {72                FolderMenu(folder: node.folder) { Task { deletion = await app.descendantCount(of: node.id) } }73            }74            .dropDestination(for: NoteTransfer.self) { items, _ in75                let ids = items.flatMap(\.ids).filter { $0 != node.id }76                guard !ids.isEmpty else { return false }77                Task { await NoteCommands(app: app, shell: shell).drop(ids, on: .folder(node.id)) }78                return true79            }80            .confirmationDialog("Supprimer ce dossier ?", isPresented: Binding(get: { deletion != nil }, set: { if !$0 { deletion = nil } }), titleVisibility: .visible, presenting: deletion) { _ in81                Button("Mettre à la corbeille", role: .destructive) { Task { await NoteCommands(app: app, shell: shell).trash([node.id]) } }82                Button("Annuler", role: .cancel) {}83            } message: { count in Text(FolderDeletionText.message(count: count)) }84    }85}8687/// Names a new folder or renames an existing one.88struct FolderNameSheet: View {89    enum Mode { case create(parentId: String?, section: String), rename(id: String, current: String) }90    let mode: Mode91    @Environment(AppModel.self) private var app92    @Environment(ShellState.self) private var shell93    @Environment(\.dismiss) private var dismiss94    @State private var name = ""95    @FocusState private var focused: Bool9697    private var trimmed: String { name.trimmingCharacters(in: .whitespacesAndNewlines) }98    private var isRename: Bool { if case .rename = mode { return true } else { return false } }99100    var body: some View {101        NavigationStack {102            Form {103                Section {104                    TextField("Nom du dossier", text: $name)105                        .focused($focused)106                        .submitLabel(.done)107                        .onSubmit(save)108                        .accessibilityIdentifier("folder-name")109                } footer: {110                    if case .create(_, let section) = mode { Text("Le dossier sera créé dans \(NoteSection.title(section)). Il est enregistré sur cet appareil à l’instant.") }111                }112            }113            .navigationTitle(isRename ? "Renommer le dossier" : "Nouveau dossier")114            .navigationBarTitleDisplayMode(.inline)115            .toolbar {116                ToolbarItem(placement: .cancellationAction) { Button("Annuler") { dismiss() } }117                ToolbarItem(placement: .confirmationAction) {118                    Button(isRename ? "Renommer" : "Créer", action: save).disabled(trimmed.isEmpty).accessibilityIdentifier("folder-save")119                }120            }121            .onAppear {122                if case .rename(_, let current) = mode { name = current }123                focused = true124            }125        }126        .presentationDetents([.medium])127    }128129    private func save() {130        guard !trimmed.isEmpty else { return }131        let title = trimmed132        Task {133            let done = await app.attempt {134                switch mode {135                case .create(let parentId, let section): try await app.createFolder(title: title, parentId: parentId, section: section)136                case .rename(let id, _): try await app.rename(id, to: title)137                }138            }139            if done { dismiss() }140        }141    }142}143
Files main
Menu

Theme