From ad3063cc30b2590649e008eb664cd4ad2784c6d8 Mon Sep 17 00:00:00 2001 From: Kyle Date: Mon, 6 Nov 2023 23:16:16 +0800 Subject: [PATCH] [Feature] Add remove all button --- Localizable.xcstrings | 12 ++- .../View/SettingView/FolderSettingTab.swift | 82 +++++++++++-------- Shared/ViewModel/FolderItemStore.swift | 14 ++++ 3 files changed, 75 insertions(+), 33 deletions(-) diff --git a/Localizable.xcstrings b/Localizable.xcstrings index 1d31b40..d13daf2 100644 --- a/Localizable.xcstrings +++ b/Localizable.xcstrings @@ -111,7 +111,7 @@ } } }, - "Add Folder(s)" : { + "Add Folders" : { "localizations" : { "zh-Hans" : { "stringUnit" : { @@ -554,6 +554,16 @@ } } }, + "Remove All" : { + "localizations" : { + "zh-Hans" : { + "stringUnit" : { + "state" : "translated", + "value" : "删除全部" + } + } + } + }, "Right-click is the same as control-click" : { "localizations" : { "zh-Hans" : { diff --git a/MenuHelper/View/SettingView/FolderSettingTab.swift b/MenuHelper/View/SettingView/FolderSettingTab.swift index 355a46f..1be3175 100644 --- a/MenuHelper/View/SettingView/FolderSettingTab.swift +++ b/MenuHelper/View/SettingView/FolderSettingTab.swift @@ -31,18 +31,26 @@ struct FolderSettingTab: View { } content: { VStack(alignment: .leading) { HStack { - Text("User Seleted Directories") + VStack(alignment: .leading) { + Text("User Seleted Directories") + ( + Text("Directories where you have permission for *application menu items* to open apps and *new file action menu* to create file") + + Text(verbatim: "\n") + + Text("Recommended folder is \("/Users/\(NSUserName())") (current user's 🏠 directory)") + ) + .foregroundColor(.secondary) + .font(.caption) + } Spacer() - Button { - channel.send(name: "ChoosePermissionFolder", data: nil) - } label: { Label("Add Folder(s)", systemImage: "folder.badge.plus") } - } - VStack(alignment: .leading) { - Text("Directories where you have permission for *application menu items* to open apps and *new file action menu* to create file") - Text("Recommended folder is \("/Users/\(NSUserName())") (current user's 🏠 directory)") + VStack { + Button { + channel.send(name: "ChoosePermissionFolder", data: nil) + } label: { Label("Add Folders", systemImage: "folder.badge.plus") } + Button { + store.deleteAllBookmarkItems() + } label: { Label("Remove All", systemImage: "folder.badge.minus") } + } } - .foregroundColor(.secondary) - .font(.caption) List { ForEach(store.bookmarkItems) { item in HStack { @@ -75,30 +83,40 @@ struct FolderSettingTab: View { } content: { VStack(alignment: .leading) { HStack { - Text("Finder Sync Directories") + VStack(alignment: .leading) { + Text("Finder Sync Directories") + ( + Text("Toolbar item menu will show in every directory") + + Text(verbatim: "\n") + + Text("But *contextual menu* will only show in Finder Sync directories") + ) + .foregroundColor(.secondary) + .font(.caption) + } Spacer() - Button { - let panel = NSOpenPanel() - panel.allowsMultipleSelection = true - panel.allowedContentTypes = [.folder] - panel.canChooseDirectories = true - if let pw = getpwuid(getuid()), let home = pw.pointee.pw_dir { - let path = FileManager.default.string(withFileSystemRepresentation: home, length: strlen(home)) - panel.directoryURL = URL(fileURLWithPath: path) - } else { - panel.directoryURL = URL(fileURLWithPath: "/Users") - } - if panel.runModal() == .OK { - store.appendItems(panel.urls.map { SyncFolderItem($0) }) - } - } label: { Label("Add Folder(s)", systemImage: "folder.badge.plus") } - } - VStack(alignment: .leading) { - Text("Toolbar item menu will show in every directory") - Text("But *contextual menu* will only show in Finder Sync directories") + VStack { + Button { + let panel = NSOpenPanel() + panel.allowsMultipleSelection = true + panel.allowedContentTypes = [.folder] + panel.canChooseDirectories = true + if let pw = getpwuid(getuid()), let home = pw.pointee.pw_dir { + let path = FileManager.default.string(withFileSystemRepresentation: home, length: strlen(home)) + panel.directoryURL = URL(fileURLWithPath: path) + } else { + panel.directoryURL = URL(fileURLWithPath: "/Users") + } + if panel.runModal() == .OK { + store.appendItems(panel.urls.map { SyncFolderItem($0) }) + } + } label: { Label("Add Folders", systemImage: "folder.badge.plus") } + Button { + store.deleteAllSyncItems() + } label: { Label("Remove All", systemImage: "folder.badge.minus") } + } } - .foregroundColor(.secondary) - .font(.caption) + + List { ForEach(store.syncItems) { item in HStack { diff --git a/Shared/ViewModel/FolderItemStore.swift b/Shared/ViewModel/FolderItemStore.swift index 4379cf2..77b7041 100644 --- a/Shared/ViewModel/FolderItemStore.swift +++ b/Shared/ViewModel/FolderItemStore.swift @@ -82,6 +82,20 @@ class FolderItemStore: ObservableObject { try? save() } + @MainActor func deleteAllBookmarkItems() { + withAnimation { + bookmarkItems.removeAll() + } + try? save() + } + + @MainActor func deleteAllSyncItems() { + withAnimation { + syncItems.removeAll() + } + try? save() + } + // MARK: - UserDefaults @MainActor private func load() throws {