Repository Public
1// SP Notes iOS2// File: Features/Organize/MoveNoteSheet.swift3// Description: Sheet to move notes or folders to a section or into a folder of the tree (with a search field). Destinations4// that would put a folder inside itself are disabled. Also the folder picker reused by the search filters.5//6// Author: Simon-Pierre Boucher7// Contact: contact@spboucher.ai8import SPNotesCore9import SwiftUI1011/// A place a page can be moved to.12enum MoveTarget: Hashable {13 case section(String)14 case folder(String)15}1617/// Pure rules of the move sheet (tested).18enum MoveRules {19 /// Whether the pages `moving` may go to `target`.20 static func isValid(_ target: MoveTarget, moving ids: [String], folders: [PageSummary]) -> Bool {21 switch target {22 case .section: return true23 case .folder(let id): return !FolderTree.invalidTargets(moving: ids, folders: folders).contains(id)24 }25 }2627 /// Whether `target` is where the page already is (nothing to do).28 static func isCurrent(_ target: MoveTarget, of page: PageSummary) -> Bool {29 switch target {30 case .section(let section): return page.parentId == nil && page.section == section31 case .folder(let id): return page.parentId == id32 }33 }3435 /// Folders whose name or path matches the search text (case and accents ignored).36 static func matches(_ query: String, folders: [PageSummary]) -> [PageSummary] {37 let needle = query.trimmingCharacters(in: .whitespacesAndNewlines)38 guard !needle.isEmpty else { return folders }39 return folders.filter { NoteTitle.display($0.title).range(of: needle, options: [.caseInsensitive, .diacriticInsensitive]) != nil }40 }41}4243struct MoveNoteSheet: View {44 let noteIds: [String]4546 @Environment(AppModel.self) private var app47 /// Present when the sheet is shown from the shell (it may also be shown from the editor, without one).48 @Environment(ShellState.self) private var shell: ShellState?49 @Environment(\.dismiss) private var dismiss50 @State private var moving: [PageSummary] = []5152 var body: some View {53 NavigationStack {54 FolderPickerList(moving: noteIds, current: moving.count == 1 ? moving.first : nil) { target, name in55 Task {56 let done = await app.attempt {57 for id in noteIds {58 switch target {59 case .section(let section): try await app.move(id, toParent: nil, section: section)60 case .folder(let folder): try await app.move(id, toParent: folder)61 }62 }63 }64 if done {65 Haptics.success()66 dismiss()67 shell?.show(String(localized: "Déplacé : \(name)"))68 }69 }70 }71 .navigationTitle(noteIds.count > 1 ? "Déplacer \(noteIds.count) éléments" : "Déplacer")72 .navigationBarTitleDisplayMode(.inline)73 .toolbar { ToolbarItem(placement: .cancellationAction) { Button("Annuler") { dismiss() } } }74 .task {75 var pages: [PageSummary] = []76 for id in noteIds { if let page = try? await app.store.summary(id: id) { pages.append(page) } }77 moving = pages78 }79 }80 }81}8283/// The sections and the folder tree as a list to pick from, with a search field.84struct FolderPickerList: View {85 /// Pages being moved (their own subtrees are disabled). Empty when only picking a folder.86 var moving: [String] = []87 var current: PageSummary?88 var includeSections = true89 let onPick: (MoveTarget, String) -> Void9091 @Environment(AppModel.self) private var app92 @State private var folders: [PageSummary] = []93 @State private var query = ""9495 private var invalid: Set<String> { FolderTree.invalidTargets(moving: moving, folders: folders) }9697 var body: some View {98 let tree = FolderTree.build(folders)99 let invalid = invalid100 List {101 if query.trimmingCharacters(in: .whitespaces).isEmpty {102 ForEach(NoteSection.editable, id: \.self) { section in103 let rows = FolderTree.flatten(tree[section] ?? [])104 if includeSections || !rows.isEmpty {105 Section(NoteSection.title(section)) {106 if includeSections {107 row(.section(section), title: String(localized: "À la racine de \(NoteSection.title(section))"), symbol: NoteSection.symbol(section), depth: 0, disabled: false)108 }109 ForEach(rows, id: \.node.id) { item in110 row(.folder(item.node.id), title: item.node.title, symbol: "folder", depth: item.depth + (includeSections ? 1 : 0), disabled: invalid.contains(item.node.id))111 }112 }113 }114 }115 } else {116 let found = MoveRules.matches(query, folders: folders)117 if found.isEmpty {118 ContentUnavailableView.search(text: query)119 } else {120 ForEach(found) { folder in121 row(.folder(folder.id), title: FolderTree.path(of: folder.id, in: folders), symbol: "folder", depth: 0, disabled: invalid.contains(folder.id), subtitle: NoteSection.title(folder.section))122 }123 }124 }125 }126 .listStyle(.insetGrouped)127 .searchable(text: $query, placement: .navigationBarDrawer(displayMode: .always), prompt: "Chercher un dossier")128 .overlay {129 if folders.isEmpty, !includeSections { EmptyStateView(symbol: "folder", title: "Aucun dossier") }130 }131 .task(id: app.feed.structure) { folders = (try? await app.store.folderTree()) ?? [] }132 }133134 private func row(_ target: MoveTarget, title: String, symbol: String, depth: Int, disabled: Bool, subtitle: String? = nil) -> some View {135 let isCurrent = current.map { MoveRules.isCurrent(target, of: $0) } ?? false136 return Button { onPick(target, title) } label: {137 HStack(spacing: 12) {138 Image(systemName: symbol).foregroundStyle(disabled ? Color.spMuted : Color.accentColor).frame(width: 26)139 VStack(alignment: .leading, spacing: 2) {140 Text(title).foregroundStyle(disabled ? Color.spMuted : Color.spText)141 if let subtitle { Text(subtitle).font(.footnote).foregroundStyle(Color.spTextSecondary) }142 if disabled { Text("Impossible : c’est le dossier déplacé ou l’un de ses sous-dossiers").font(.footnote).foregroundStyle(Color.spMuted) }143 }144 Spacer(minLength: 4)145 if isCurrent { Label("Emplacement actuel", systemImage: "checkmark").labelStyle(.iconOnly).foregroundStyle(Color.accentColor) }146 }147 .padding(.leading, CGFloat(min(depth, 6)) * 18)148 .frame(minHeight: 36)149 .contentShape(Rectangle())150 }151 .buttonStyle(.plain)152 .disabled(disabled || isCurrent)153 .accessibilityIdentifier("move-target")154 }155}156