diff --git a/apps/ios/Snowbound/Lists.swift b/apps/ios/Snowbound/Lists.swift index f4a5e756d4f0faddc3426f960fad2a7ab3a5ce56..2f4cb108bf87df092486a3e76553e7a1ebb20820 100644 --- a/apps/ios/Snowbound/Lists.swift +++ b/apps/ios/Snowbound/Lists.swift @@ -388,6 +388,11 @@ final class NotebooksViewController: UICollectionViewController, UIDocumentPicke } })) } + if notebook.hasRecycleBin { + actions.append(UIAction(title: "Delete", image: UIImage(systemName: "trash"), attributes: .destructive) { [weak self] _ in + self?.confirmRecycling(paths, of: notebook) + }) + } } let action = UIBarButtonItem(title: "Actions", image: UIImage(systemName: "ellipsis.circle"), menu: UIMenu(children: actions)) action.isEnabled = !actions.isEmpty @@ -398,7 +403,10 @@ final class NotebooksViewController: UICollectionViewController, UIDocumentPicke private func delete(_ notebooks: [Notebook]) { let alert = UIAlertController( title: notebooks.count == 1 ? "Delete Notebook?" : "Delete \(notebooks.count) Notebooks?", - message: "Notebooks that can’t be moved to Recently Deleted will be deleted permanently.", preferredStyle: .alert) + message: notebooks.count == 1 + ? "It moves to Recently Deleted in Files. If it can’t, it’s deleted permanently." + : "They move to Recently Deleted in Files. Any that can’t are deleted permanently.", + preferredStyle: .alert) alert.addAction(UIAlertAction(title: "Cancel", style: .cancel)) alert.addAction(UIAlertAction(title: "Delete", style: .destructive) { [weak self] _ in guard let self, let release = Arranging.reserve() else { return } @@ -533,7 +541,7 @@ final class NotebooksViewController: UICollectionViewController, UIDocumentPicke } return [UIMenu(options: .displayInline, children: [details, now])] } ?? [] - let actions = [ + var actions = [ UIAction(title: "New Section", image: UIImage(systemName: "plus.rectangle.portrait")) { [weak self] _ in self?.newSection(in: notebook) }, @@ -542,6 +550,12 @@ final class NotebooksViewController: UICollectionViewController, UIDocumentPicke (view.window?.windowScene?.delegate as? SceneDelegate)?.showTags(of: notebook, from: self) }, ] + if notebook.hasRecycleBin { + actions.append( + UIAction(title: "Notebook Recycle Bin", image: UIImage(systemName: "trash")) { [weak self] _ in + RecycleBinViewController.present(notebook, in: self?.view.window) + }) + } // A notebook On My iPhone is its folder there, handled as Files handles a folder. guard let folder = folder(of: notebook) else { let image = UIImage(systemName: "xmark.circle") @@ -722,6 +736,16 @@ final class NotebooksViewController: UICollectionViewController, UIDocumentPicke private func remove(_ folder: URL, of notebook: Notebook) { if (try? FileManager.default.trashItem(at: folder, resultingItemURL: nil)) != nil { + if let window = view.window { + SyncToast.show( + "“\(notebook.name)” Deleted", "Restore it from Recently Deleted in Files", symbol: "trash", + hint: "Opens Files", in: window + ) { + if let files = URL(string: "shareddocuments://" + folder.deletingLastPathComponent().path) { + UIApplication.shared.open(files) + } + } + } return rescan() } let name = folder.deletingPathExtension().lastPathComponent @@ -747,14 +771,65 @@ final class NotebooksViewController: UICollectionViewController, UIDocumentPicke ) -> UIContextMenuConfiguration? { guard !isEditing, indexPaths.count == 1, let item = dataSource.itemIdentifier(for: indexPaths[0]) else { return nil } guard case .notebook(let id) = item, let notebook = notebook(id) else { - let moves = arrangeMenu(item) - return moves.isEmpty ? nil : UIContextMenuConfiguration(actionProvider: { _ in UIMenu(children: moves) }) + let actions = arrangeMenu(item) + deleteAction(item) + return actions.isEmpty ? nil : UIContextMenuConfiguration(actionProvider: { _ in UIMenu(children: actions) }) } return UIContextMenuConfiguration(actionProvider: { [weak self] _ in UIMenu(children: (self?.menu(for: notebook).children ?? []) + (self?.arrangeMenu(item) ?? [])) }) } + /// Delete for a section or group. + private func deleteAction(_ item: Item) -> [UIMenuElement] { + switch item { + case .section(let id, let path), .group(let id, let path): + guard let notebook = notebook(id), notebook.hasRecycleBin else { return [] } + return [ + UIAction(title: "Delete", image: UIImage(systemName: "trash"), attributes: .destructive) { [weak self] _ in + self?.confirmRecycling([path], of: notebook) + } + ] + default: return [] + } + } + + /// Moves the sections and groups at `paths` to the notebook's recycle bin once confirmed. + private func confirmRecycling(_ paths: [String], of notebook: Notebook) { + let path = paths[0] + let tab = notebook.tabs.first { $0.path == path } + let title = paths.count > 1 + ? "Delete \(paths.count) Items?" : "Delete “\(tab?.name ?? path.split(separator: "/").last.map(String.init) ?? path)”?" + let message = + paths.count > 1 ? "They move to this notebook’s Recycle Bin." + : tab == nil ? "Its sections move to this notebook’s Recycle Bin." : "It moves to this notebook’s Recycle Bin." + let alert = UIAlertController(title: title, message: message, preferredStyle: .alert) + alert.addAction(UIAlertAction(title: "Cancel", style: .cancel)) + alert.addAction(UIAlertAction(title: "Delete", style: .destructive) { [weak self] _ in + self?.setEditing(false, animated: true) + self?.recycle(paths[...], of: notebook) + }) + present(alert, animated: true) + } + + private func recycle(_ paths: ArraySlice, of notebook: Notebook) { + guard let path = paths.first, let library = notebook.handle, let release = Arranging.reserve() else { return } + for scene in UIApplication.shared.connectedScenes { + _ = (scene.delegate as? SceneDelegate)?.release(path, of: notebook) + } + let pointer = Int(bitPattern: library) + background({ sb_library_delete(OpaquePointer(bitPattern: pointer), path) }) { [weak self] deleted in + notebook.reload { + release() + NotificationCenter.default.post(name: Notebook.listed, object: notebook) + guard deleted else { + self?.alert("Can’t Delete", "Check that the notebook can be reached, then try again.") + return + } + self?.recycle(paths.dropFirst(), of: notebook) + } + } + } + override func collectionView(_ collectionView: UICollectionView, shouldSelectItemAt indexPath: IndexPath) -> Bool { guard let item = dataSource.itemIdentifier(for: indexPath) else { return false } if isEditing { diff --git a/apps/ios/Snowbound/Notebooks.swift b/apps/ios/Snowbound/Notebooks.swift index c52ccb8a9ee9744ddc79b18a783b38956ed27e3f..f8377b990545fec9ec2bd22f8641d6f785140f00 100644 --- a/apps/ios/Snowbound/Notebooks.swift +++ b/apps/ios/Snowbound/Notebooks.swift @@ -762,8 +762,11 @@ final class Section { return id } + /// Deletes pages, conflict pages for good and the rest to the recycle bin, which a toast + /// then offers. func delete(_ ids: [String], done: @escaping () -> Void = {}) { guard let release = Arranging.reserve() else { return } + let recycled = notebook.hasRecycleBin ? ids.filter { row(of: $0)?.version == nil }.count : 0 let (date, time) = titleDate() let pointer = Int(bitPattern: handle) background({ @@ -774,10 +777,17 @@ final class Section { }) { [self] deleted in refreshRows { release() - if deleted { done() } - else { - let window = UIApplication.shared.connectedScenes.lazy.compactMap { ($0 as? UIWindowScene)?.keyWindow }.first + let window = UIApplication.shared.connectedScenes.lazy.compactMap { ($0 as? UIWindowScene)?.keyWindow }.first + guard deleted else { window?.rootViewController?.alert("Can’t Delete", "Some pages couldn’t be deleted. Try again.") + return + } + done() + if recycled > 0, let window { + let title = recycled == 1 ? "Page Moved to Recycle Bin" : "\(recycled) Pages Moved to Recycle Bin" + SyncToast.show(title, "Tap to restore", symbol: "trash", hint: "Opens the Recycle Bin", in: window) { + [notebook = self.notebook] in RecycleBinViewController.present(notebook, in: window) + } } } } diff --git a/apps/ios/Snowbound/Recycle.swift b/apps/ios/Snowbound/Recycle.swift new file mode 100644 index 0000000000000000000000000000000000000000..6f181dc134b4a89e9e507f3a27fe1f9cf09080cd --- /dev/null +++ b/apps/ios/Snowbound/Recycle.swift @@ -0,0 +1,158 @@ +import UIKit + +extension Notebook { + /// Whether the notebook keeps a recycle bin: a folder does, a lone section doesn't. + var hasRecycleBin: Bool { tabs.contains { !$0.path.isEmpty && !$0.path.hasPrefix("/") } } +} + +/// OneNote's Notebook Recycle Bin: the pages and sections deleted from a notebook, each +/// restored to a section or folder the reader picks. +final class RecycleBinViewController: UITableViewController { + private static let deletedPages = "OneNote_RecycleBin/OneNote_DeletedPages.one" + + private let notebook: Notebook + /// Deleted Pages, open while the bin shows. + private var deleted: Section? + private var sections: [Tab] = [] + + /// Shows `notebook`'s bin as a sheet over the controller on top of `window`. + static func present(_ notebook: Notebook, in window: UIWindow?) { + var top = window?.rootViewController + while let presented = top?.presentedViewController { top = presented } + let navigation = UINavigationController(rootViewController: RecycleBinViewController(notebook: notebook)) + navigation.sheetPresentationController?.detents = [.medium(), .large()] + navigation.sheetPresentationController?.prefersGrabberVisible = true + top?.present(navigation, animated: true) + } + + private init(notebook: Notebook) { + self.notebook = notebook + super.init(style: .insetGrouped) + title = "Recycle Bin" + } + + required init?(coder: NSCoder) { fatalError() } + + override func viewDidLoad() { + super.viewDidLoad() + tableView.register(UITableViewCell.self, forCellReuseIdentifier: "binned") + navigationItem.rightBarButtonItem = UIBarButtonItem( + systemItem: .done, primaryAction: UIAction { [weak self] _ in self?.dismiss(animated: true) }) + NotificationCenter.default.addObserver(forName: Section.changed, object: nil, queue: .main) { [weak self] note in + if let self, note.object as AnyObject? === deleted { show() } + } + NotificationCenter.default.addObserver(forName: Notebook.listed, object: notebook, queue: .main) { [weak self] _ in + self?.reload() + } + var loading = UIContentUnavailableConfiguration.loading() + loading.text = "Opening…" + contentUnavailableConfiguration = loading + reload() + } + + private var pages: [Row] { deleted?.rows ?? [] } + + private func reload() { + guard let library = notebook.handle else { return } + let pointer = Int(bitPattern: library) + background({ decode([Tab].self, sb_library_recycle_bin(OpaquePointer(bitPattern: pointer))) }) { [weak self] tabs in + guard let self else { return } + guard let tabs else { + var failed = UIContentUnavailableConfiguration.empty() + failed.text = "Can’t Open Recycle Bin" + failed.secondaryText = "Check that the notebook can be reached, then try again." + contentUnavailableConfiguration = failed + return + } + sections = tabs.filter { $0.path != Self.deletedPages } + guard deleted == nil, let tab = tabs.first(where: { $0.path == Self.deletedPages }) else { return show() } + Section.open(tab, of: notebook) { [weak self] section, _ in + self?.deleted = section + self?.show() + } + } + } + + private func show() { + tableView.reloadData() + var empty = UIContentUnavailableConfiguration.empty() + empty.image = UIImage(systemName: "trash") + empty.text = "No Deleted Pages or Sections" + empty.secondaryText = "Pages and sections you delete appear here." + contentUnavailableConfiguration = pages.isEmpty && sections.isEmpty ? empty : nil + } + + /// The table's groups: Deleted Pages, then Deleted Sections, each where it has rows. + private var groups: [Int] { [0, 1].filter { $0 == 0 ? !pages.isEmpty : !sections.isEmpty } } + + override func numberOfSections(in tableView: UITableView) -> Int { groups.count } + + override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { + groups[section] == 0 ? "Deleted Pages" : "Deleted Sections" + } + + override func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? { + section == groups.count - 1 ? "Content here is deleted after 60 days." : nil + } + + override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { + groups[section] == 0 ? pages.count : sections.count + } + + override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { + let cell = tableView.dequeueReusableCell(withIdentifier: "binned", for: indexPath) + var content = cell.defaultContentConfiguration() + if groups[indexPath.section] == 0 { + let page = pages[indexPath.row] + content.text = page.title.isEmpty ? "Untitled Page" : page.title + content.image = UIImage(systemName: "doc.text") + } else { + let tab = sections[indexPath.row] + content.text = tab.name + content.image = UIImage(systemName: "rectangle.portrait.fill") + content.imageProperties.tintColor = tab.uiColor + } + cell.contentConfiguration = content + cell.selectionStyle = .none + var restore = UIButton.Configuration.plain() + restore.title = "Restore" + let button = UIButton(configuration: restore) + button.menu = menu(at: indexPath) + button.showsMenuAsPrimaryAction = true + button.sizeToFit() + cell.accessoryView = button + return cell + } + + override func tableView( + _ tableView: UITableView, contextMenuConfigurationForRowAt indexPath: IndexPath, point: CGPoint + ) -> UIContextMenuConfiguration? { + let menu = menu(at: indexPath) + return UIContextMenuConfiguration(actionProvider: { _ in menu }) + } + + /// Restore To: the notebook's sections for a page, its folders for a section, as the + /// desktop's Restore To offers them. + private func menu(at indexPath: IndexPath) -> UIMenu { + let place = { (path: String) in path.split(separator: "/").joined(separator: " › ") } + guard groups[indexPath.section] == 1 else { + let page = pages[indexPath.row].id + let choices = notebook.sections(besides: Self.deletedPages).map { tab in + UIAction(title: place(tab.group.isEmpty ? tab.name : tab.group + "/" + tab.name)) { [notebook] _ in + Arranging.move(page: page, of: notebook, from: Self.deletedPages, to: tab.path) + } + } + return UIMenu(title: "Restore To", children: choices) + } + let path = sections[indexPath.row].path + let file = path.split(separator: "/").last.map(String.init) ?? path + let choices = ([""] + notebook.groups).map { folder in + // A section of the same file name there would refuse it. + let taken = notebook.entries(in: folder).contains { $0.split(separator: "/").last.map(String.init) == file } + return UIAction(title: folder.isEmpty ? notebook.name : place(folder), attributes: taken ? .disabled : []) { + [notebook] _ in Arranging.place(path, of: notebook, in: folder) + } + } + return UIMenu(title: "Restore To", children: choices) + } +} diff --git a/apps/ios/Snowbound/Sync.swift b/apps/ios/Snowbound/Sync.swift index 03d21f507a25b46c40dc941260706e703e2f2933..d15ed9441f6a378f93bcabdc3abf44711b2cc31c 100644 --- a/apps/ios/Snowbound/Sync.swift +++ b/apps/ios/Snowbound/Sync.swift @@ -136,7 +136,10 @@ enum Sync { /// A passing note at the top of `window` that opens `details` when tapped, for a change the /// reader would otherwise miss. enum SyncToast { - static func show(_ title: String, _ detail: String, in window: UIWindow, details: @escaping () -> Void) { + static func show( + _ title: String, _ detail: String, symbol: String = "exclamationmark.icloud", hint: String = "Shows sync status", + in window: UIWindow, details: @escaping () -> Void + ) { var configuration: UIButton.Configuration if #available(iOS 26, *) { configuration = .glass() @@ -146,12 +149,12 @@ enum SyncToast { } configuration.title = title configuration.subtitle = detail - configuration.image = UIImage(systemName: "exclamationmark.icloud") + configuration.image = UIImage(systemName: symbol) configuration.imagePadding = 10 configuration.titleLineBreakMode = .byTruncatingMiddle configuration.contentInsets = NSDirectionalEdgeInsets(top: 10, leading: 16, bottom: 10, trailing: 20) let toast = UIButton(configuration: configuration) - toast.accessibilityHint = "Shows sync status" + toast.accessibilityHint = hint toast.translatesAutoresizingMaskIntoConstraints = false toast.alpha = 0 let dismiss = { [weak toast] in