From b6974d1f20dc583a4212444622faca45c046103c Mon Sep 17 00:00:00 2001 From: phlpsong <103433299+phlpsong@users.noreply.github.com> Date: Fri, 26 Jan 2024 09:59:49 +0800 Subject: [PATCH] fix: setting crash after uninstall app in disabled app list (#364) Co-authored-by: Tisfeng --- .../EZDisableAutoSelectTextViewController.m | 14 +++++++++++++- .../View/SettingView/Tabs/DisabledAppTab.swift | 7 ++++++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/Easydict/Feature/PerferenceWindow/DisableAutoSelectTextViewController/EZDisableAutoSelectTextViewController.m b/Easydict/Feature/PerferenceWindow/DisableAutoSelectTextViewController/EZDisableAutoSelectTextViewController.m index 86a5c8e56..8df326346 100644 --- a/Easydict/Feature/PerferenceWindow/DisableAutoSelectTextViewController/EZDisableAutoSelectTextViewController.m +++ b/Easydict/Feature/PerferenceWindow/DisableAutoSelectTextViewController/EZDisableAutoSelectTextViewController.m @@ -57,7 +57,7 @@ - (void)viewDidLoad { } - (void)setup { - self.appModelList = [[EZLocalStorage.shared selectTextTypeAppModelList] mutableCopy]; + [self setupAppModelList]; [self.titleTextField mas_makeConstraints:^(MASConstraintMaker *make) { make.top.left.right.inset(kMargin + 5); // ???: Why is the actual inset is 18? @@ -78,6 +78,18 @@ - (void)setup { }]; } +- (void)setupAppModelList { + self.appModelList = [[NSMutableArray alloc] init]; + NSArray *allAppModelList = [EZLocalStorage.shared selectTextTypeAppModelList]; + NSWorkspace* workspace = [NSWorkspace sharedWorkspace]; + for (EZAppModel *appModel in allAppModelList) { + NSURL *appURL = [workspace URLForApplicationWithBundleIdentifier:appModel.appBundleID]; + if (appURL) { + [self.appModelList addObject:appModel]; + } + } +} + #pragma mark - Getter && Setter diff --git a/Easydict/NewApp/View/SettingView/Tabs/DisabledAppTab.swift b/Easydict/NewApp/View/SettingView/Tabs/DisabledAppTab.swift index d45ba5a44..ece9cc335 100644 --- a/Easydict/NewApp/View/SettingView/Tabs/DisabledAppTab.swift +++ b/Easydict/NewApp/View/SettingView/Tabs/DisabledAppTab.swift @@ -25,7 +25,12 @@ private class DisabledAppViewModel: ObservableObject { } func fetchDisabledApps() { - appModelList = EZLocalStorage.shared().selectTextTypeAppModelList + let allAppModelList = EZLocalStorage.shared().selectTextTypeAppModelList + + appModelList = allAppModelList.compactMap { appModel in + let url = NSWorkspace.shared.urlForApplication(withBundleIdentifier: appModel.appBundleID) + return url == nil ? nil : appModel + } } func saveDisabledApps() {