From 90c026d769cc5a42ee34aa20b1dfcd87ca9b4abb Mon Sep 17 00:00:00 2001 From: phlpsong Date: Thu, 18 Jan 2024 23:27:22 +0800 Subject: [PATCH] fix: refactor disabled app tab --- Easydict.xcodeproj/project.pbxproj | 8 ++-- ...DisabledTab.swift => DisabledAppTab.swift} | 40 ++++++++++--------- 2 files changed, 26 insertions(+), 22 deletions(-) rename Easydict/NewApp/View/SettingView/Tabs/{DisabledTab.swift => DisabledAppTab.swift} (87%) diff --git a/Easydict.xcodeproj/project.pbxproj b/Easydict.xcodeproj/project.pbxproj index 41e4ce22e..c1c23e80f 100644 --- a/Easydict.xcodeproj/project.pbxproj +++ b/Easydict.xcodeproj/project.pbxproj @@ -232,7 +232,7 @@ 0A2A05A62B59757100EEA142 /* Bundle+AppInfo.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0A2A05A52B59757100EEA142 /* Bundle+AppInfo.swift */; }; 0A2BA9602B49A989002872A4 /* Binding+DidSet.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0A2BA95F2B49A989002872A4 /* Binding+DidSet.swift */; }; 0A2BA9642B4A3CCD002872A4 /* Notification+Name.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0A2BA9632B4A3CCD002872A4 /* Notification+Name.swift */; }; - 0A8685C82B552A590022534F /* DisabledTab.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0A8685C72B552A590022534F /* DisabledTab.swift */; }; + 0A8685C82B552A590022534F /* DisabledAppTab.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0A8685C72B552A590022534F /* DisabledAppTab.swift */; }; 0AC11B222B4D16A500F07198 /* WindowAccessor.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0AC11B212B4D16A500F07198 /* WindowAccessor.swift */; }; 0AC11B242B4E46B300F07198 /* TapHandlerView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0AC11B232B4E46B300F07198 /* TapHandlerView.swift */; }; 17BCAEF72B0DFF9000A7D372 /* EZNiuTransTranslateResponse.m in Sources */ = {isa = PBXBuildFile; fileRef = 17BCAEF52B0DFF9000A7D372 /* EZNiuTransTranslateResponse.m */; }; @@ -709,7 +709,7 @@ 0A2A05A52B59757100EEA142 /* Bundle+AppInfo.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Bundle+AppInfo.swift"; sourceTree = ""; }; 0A2BA95F2B49A989002872A4 /* Binding+DidSet.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Binding+DidSet.swift"; sourceTree = ""; }; 0A2BA9632B4A3CCD002872A4 /* Notification+Name.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Notification+Name.swift"; sourceTree = ""; }; - 0A8685C72B552A590022534F /* DisabledTab.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DisabledTab.swift; sourceTree = ""; }; + 0A8685C72B552A590022534F /* DisabledAppTab.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DisabledAppTab.swift; sourceTree = ""; }; 0AC11B212B4D16A500F07198 /* WindowAccessor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WindowAccessor.swift; sourceTree = ""; }; 0AC11B232B4E46B300F07198 /* TapHandlerView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TapHandlerView.swift; sourceTree = ""; }; 17BCAEF32B0DFF9000A7D372 /* EZNiuTransTranslateResponse.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = EZNiuTransTranslateResponse.h; sourceTree = ""; }; @@ -2090,7 +2090,7 @@ EAED41EA2B54A4900005FE0A /* ServiceConfiguration */, 278540332B3DE04F004E9488 /* GeneralTab.swift */, 0A057D6C2B499A000025C51D /* ServiceTab.swift */, - 0A8685C72B552A590022534F /* DisabledTab.swift */, + 0A8685C72B552A590022534F /* DisabledAppTab.swift */, 276742042B3DC230002A2C75 /* PrivacyTab.swift */, 276742052B3DC230002A2C75 /* AboutTab.swift */, ); @@ -2707,7 +2707,7 @@ 17BCAEF82B0DFF9000A7D372 /* EZNiuTransTranslate.m in Sources */, 039F5506294B6E29004AB940 /* EZSettingViewController.m in Sources */, 03BD281E29481C0400F5891A /* EZAudioPlayer.m in Sources */, - 0A8685C82B552A590022534F /* DisabledTab.swift in Sources */, + 0A8685C82B552A590022534F /* DisabledAppTab.swift in Sources */, 03E02A2629250D1D00A10260 /* EZEventMonitor.m in Sources */, 03B0233429231FA6001C7E63 /* MMConsoleLogFormatter.m in Sources */, 037852B9295D49F900D0E2CF /* EZTableRowView.m in Sources */, diff --git a/Easydict/NewApp/View/SettingView/Tabs/DisabledTab.swift b/Easydict/NewApp/View/SettingView/Tabs/DisabledAppTab.swift similarity index 87% rename from Easydict/NewApp/View/SettingView/Tabs/DisabledTab.swift rename to Easydict/NewApp/View/SettingView/Tabs/DisabledAppTab.swift index 12e5403e8..3ba038389 100644 --- a/Easydict/NewApp/View/SettingView/Tabs/DisabledTab.swift +++ b/Easydict/NewApp/View/SettingView/Tabs/DisabledAppTab.swift @@ -1,5 +1,5 @@ // -// DisabledTab.swift +// DisabledAppTab.swift // Easydict // // Created by phlpsong on 2024/1/15. @@ -30,14 +30,23 @@ class DisabledAppViewModel: ObservableObject { selectedAppModels = [] } - func newAppSelected(for url: URL) { - guard let newSelectApp = newDisabledApp(from: url) else { return } - guard !appModelList.contains(newSelectApp) else { return } - appModelList.append(newSelectApp) + func newAppURLsSelected(from urls: [URL]) { + urls.forEach { url in + let gotAccess = url.startAccessingSecurityScopedResource() + if !gotAccess { return } + appendNewDisabledApp(for: url) + url.stopAccessingSecurityScopedResource() + } + } + + func appendNewDisabledApp(for url: URL) { + guard let selectAppModel = disabledAppModel(from: url) else { return } + guard !appModelList.contains(selectAppModel) else { return } + appModelList.append(selectAppModel) saveDisabledApps() } - func newDisabledApp(from url: URL) -> EZAppModel? { + func disabledAppModel(from url: URL) -> EZAppModel? { let appModel = EZAppModel() guard let bundle = Bundle(url: url) else { return nil } appModel.appBundleID = bundle.bundleIdentifier ?? "" @@ -48,8 +57,6 @@ class DisabledAppViewModel: ObservableObject { @available(macOS 13.0, *) struct DisabledAppTab: View { - @Environment(\.colorScheme) private var colorScheme - @ObservedObject var disabledAppViewModel = DisabledAppViewModel() var listToolbar: some View { @@ -61,18 +68,13 @@ struct DisabledAppTab: View { ) { result in switch result { case let .success(urls): - urls.forEach { url in - let gotAccess = url.startAccessingSecurityScopedResource() - if !gotAccess { return } - disabledAppViewModel.newAppSelected(for: url) - url.stopAccessingSecurityScopedResource() - } + disabledAppViewModel.newAppURLsSelected(from: urls) case let .failure(error): print("fileImporter error: \(error)") } } } - + var appListView: some View { List(selection: $disabledAppViewModel.selectedAppModels) { ForEach(disabledAppViewModel.appModelList, id: \.self) { app in @@ -93,7 +95,7 @@ struct DisabledAppTab: View { } .clipShape(RoundedRectangle(cornerRadius: 8)) .padding(.bottom) - .padding(.horizontal, 40) + .padding(.horizontal, 35) .onTapGesture { disabledAppViewModel.selectedAppModels = [] } @@ -106,8 +108,9 @@ struct DisabledAppTab: View { appListViewWithToolbar } + .frame(maxWidth: 500) .environmentObject(disabledAppViewModel) - .task { + .onAppear { disabledAppViewModel.fetchDisabledApps() } } @@ -187,6 +190,7 @@ struct BlockAppItemView: View { } .frame(maxWidth: .infinity) .contentShape(Rectangle()) + .listRowBackground(listRowBgColor) .padding(.vertical, 4) .padding(.leading, 6) .task { @@ -212,7 +216,7 @@ class AppFetcher: ObservableObject { let workspace = NSWorkspace.shared let appURL = workspace.urlForApplication(withBundleIdentifier: appBundleId) guard let appURL else { return } - + let appPath = NSWorkspace.shared.urlForApplication(withBundleIdentifier: appBundleId) guard let appPath else { return } appIcon = workspace.icon(forFile: appPath.path(percentEncoded: false))