Skip to content

Commit

Permalink
fix: refactor disabled app tab
Browse files Browse the repository at this point in the history
  • Loading branch information
phlpsong committed Jan 18, 2024
1 parent 00c211e commit 90c026d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 22 deletions.
8 changes: 4 additions & 4 deletions Easydict.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */; };
Expand Down Expand Up @@ -709,7 +709,7 @@
0A2A05A52B59757100EEA142 /* Bundle+AppInfo.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Bundle+AppInfo.swift"; sourceTree = "<group>"; };
0A2BA95F2B49A989002872A4 /* Binding+DidSet.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Binding+DidSet.swift"; sourceTree = "<group>"; };
0A2BA9632B4A3CCD002872A4 /* Notification+Name.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Notification+Name.swift"; sourceTree = "<group>"; };
0A8685C72B552A590022534F /* DisabledTab.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DisabledTab.swift; sourceTree = "<group>"; };
0A8685C72B552A590022534F /* DisabledAppTab.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DisabledAppTab.swift; sourceTree = "<group>"; };
0AC11B212B4D16A500F07198 /* WindowAccessor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WindowAccessor.swift; sourceTree = "<group>"; };
0AC11B232B4E46B300F07198 /* TapHandlerView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TapHandlerView.swift; sourceTree = "<group>"; };
17BCAEF32B0DFF9000A7D372 /* EZNiuTransTranslateResponse.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = EZNiuTransTranslateResponse.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -2090,7 +2090,7 @@
EAED41EA2B54A4900005FE0A /* ServiceConfiguration */,
278540332B3DE04F004E9488 /* GeneralTab.swift */,
0A057D6C2B499A000025C51D /* ServiceTab.swift */,
0A8685C72B552A590022534F /* DisabledTab.swift */,
0A8685C72B552A590022534F /* DisabledAppTab.swift */,
276742042B3DC230002A2C75 /* PrivacyTab.swift */,
276742052B3DC230002A2C75 /* AboutTab.swift */,
);
Expand Down Expand Up @@ -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 */,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// DisabledTab.swift
// DisabledAppTab.swift
// Easydict
//
// Created by phlpsong on 2024/1/15.
Expand Down Expand Up @@ -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 ?? ""
Expand All @@ -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 {
Expand All @@ -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
Expand All @@ -93,7 +95,7 @@ struct DisabledAppTab: View {
}
.clipShape(RoundedRectangle(cornerRadius: 8))
.padding(.bottom)
.padding(.horizontal, 40)
.padding(.horizontal, 35)
.onTapGesture {
disabledAppViewModel.selectedAppModels = []
}
Expand All @@ -106,8 +108,9 @@ struct DisabledAppTab: View {

appListViewWithToolbar
}
.frame(maxWidth: 500)
.environmentObject(disabledAppViewModel)
.task {
.onAppear {
disabledAppViewModel.fetchDisabledApps()
}
}
Expand Down Expand Up @@ -187,6 +190,7 @@ struct BlockAppItemView: View {
}
.frame(maxWidth: .infinity)
.contentShape(Rectangle())
.listRowBackground(listRowBgColor)
.padding(.vertical, 4)
.padding(.leading, 6)
.task {
Expand All @@ -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))
Expand Down

0 comments on commit 90c026d

Please sign in to comment.