From 412c7df2324ebb4a9016fdc2e184423dcabf551d Mon Sep 17 00:00:00 2001 From: Lava <34743145+CanglongCl@users.noreply.github.com> Date: Thu, 25 Jan 2024 11:30:20 -0800 Subject: [PATCH 1/9] introduce global context and migrate updater controller into --- Easydict.xcodeproj/project.pbxproj | 4 ++ Easydict/App/AppDelegate.h | 2 - .../Feature/Configuration/Configuration.swift | 10 +---- .../Feature/Configuration/EZConfiguration.m | 2 +- Easydict/NewApp/EasydictApp.swift | 37 +++------------- Easydict/NewApp/GlobalContext.swift | 44 +++++++++++++++++++ Easydict/NewApp/View/MenuItemView.swift | 20 ++++----- 7 files changed, 65 insertions(+), 54 deletions(-) create mode 100644 Easydict/NewApp/GlobalContext.swift diff --git a/Easydict.xcodeproj/project.pbxproj b/Easydict.xcodeproj/project.pbxproj index 0feacd3fa..57c136e50 100644 --- a/Easydict.xcodeproj/project.pbxproj +++ b/Easydict.xcodeproj/project.pbxproj @@ -277,6 +277,7 @@ EA9943EE2B5353AB00EE7B97 /* WindowTypeExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = EA9943ED2B5353AB00EE7B97 /* WindowTypeExtensions.swift */; }; EA9943F02B5354C400EE7B97 /* ShowWindowPositionExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = EA9943EF2B5354C400EE7B97 /* ShowWindowPositionExtensions.swift */; }; EA9943F22B5358BF00EE7B97 /* LanguageExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = EA9943F12B5358BF00EE7B97 /* LanguageExtensions.swift */; }; + EAE3D3502B62E9DE001EE3E3 /* GlobalContext.swift in Sources */ = {isa = PBXBuildFile; fileRef = EAE3D34F2B62E9DE001EE3E3 /* GlobalContext.swift */; }; EAED41EC2B54AA920005FE0A /* ServiceConfigurationSection.swift in Sources */ = {isa = PBXBuildFile; fileRef = EAED41EB2B54AA920005FE0A /* ServiceConfigurationSection.swift */; }; EAED41EF2B54B1430005FE0A /* ConfigurableService.swift in Sources */ = {isa = PBXBuildFile; fileRef = EAED41EE2B54B1430005FE0A /* ConfigurableService.swift */; }; EAED41F22B54B39D0005FE0A /* OpenAIService+ConfigurableService.swift in Sources */ = {isa = PBXBuildFile; fileRef = EAED41F12B54B39D0005FE0A /* OpenAIService+ConfigurableService.swift */; }; @@ -772,6 +773,7 @@ EA9943ED2B5353AB00EE7B97 /* WindowTypeExtensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WindowTypeExtensions.swift; sourceTree = ""; }; EA9943EF2B5354C400EE7B97 /* ShowWindowPositionExtensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ShowWindowPositionExtensions.swift; sourceTree = ""; }; EA9943F12B5358BF00EE7B97 /* LanguageExtensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LanguageExtensions.swift; sourceTree = ""; }; + EAE3D34F2B62E9DE001EE3E3 /* GlobalContext.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GlobalContext.swift; sourceTree = ""; }; EAED41EB2B54AA920005FE0A /* ServiceConfigurationSection.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ServiceConfigurationSection.swift; sourceTree = ""; }; EAED41EE2B54B1430005FE0A /* ConfigurableService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConfigurableService.swift; sourceTree = ""; }; EAED41F12B54B39D0005FE0A /* OpenAIService+ConfigurableService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "OpenAIService+ConfigurableService.swift"; sourceTree = ""; }; @@ -2068,6 +2070,7 @@ 27FE95262B3DC55F000AD654 /* EasydictApp.swift */, 27FE98042B3DCB09000AD654 /* NewAppManager.swift */, 27FE98062B3DD525000AD654 /* View */, + EAE3D34F2B62E9DE001EE3E3 /* GlobalContext.swift */, ); path = NewApp; sourceTree = ""; @@ -2795,6 +2798,7 @@ 0320C5872B29F35700861B3D /* QueryServiceRecord.swift in Sources */, 03FC699A2B39D13A0035D2DA /* EZOpenAIChatResponse.m in Sources */, 03B022FA29231FA6001C7E63 /* EZServiceTypes.m in Sources */, + EAE3D3502B62E9DE001EE3E3 /* GlobalContext.swift in Sources */, EA9943F02B5354C400EE7B97 /* ShowWindowPositionExtensions.swift in Sources */, 03B0233129231FA6001C7E63 /* MMCrash.m in Sources */, 03B0232629231FA6001C7E63 /* NSAttributedString+MM.m in Sources */, diff --git a/Easydict/App/AppDelegate.h b/Easydict/App/AppDelegate.h index d604f4d1b..4c1bb5030 100644 --- a/Easydict/App/AppDelegate.h +++ b/Easydict/App/AppDelegate.h @@ -11,6 +11,4 @@ @interface AppDelegate : NSObject -@property (weak) IBOutlet SPUStandardUpdaterController *updaterController; - @end diff --git a/Easydict/Feature/Configuration/Configuration.swift b/Easydict/Feature/Configuration/Configuration.swift index f888889c2..b388be637 100644 --- a/Easydict/Feature/Configuration/Configuration.swift +++ b/Easydict/Feature/Configuration/Configuration.swift @@ -30,12 +30,6 @@ let kHideMenuBarIconKey = "EZConfiguration_kHideMenuBarIconKey" } } - var appDelegate = NSApp.delegate as? AppDelegate - - var updater: SPUUpdater? { - appDelegate?.updaterController.updater - } - @DefaultsWrapper(.firstLanguage) var firstLanguage: Language @@ -65,10 +59,10 @@ let kHideMenuBarIconKey = "EZConfiguration_kHideMenuBarIconKey" var automaticallyChecksForUpdates: Bool { get { - updater?.automaticallyChecksForUpdates ?? false + GlobalContext.updaterController.updater.automaticallyDownloadsUpdates } set { - updater?.automaticallyChecksForUpdates = newValue + GlobalContext.updaterController.updater.automaticallyDownloadsUpdates = newValue logSettings(["automatically_checks_for_updates": newValue]) } } diff --git a/Easydict/Feature/Configuration/EZConfiguration.m b/Easydict/Feature/Configuration/EZConfiguration.m index e1b40f69b..3cd01f6ad 100644 --- a/Easydict/Feature/Configuration/EZConfiguration.m +++ b/Easydict/Feature/Configuration/EZConfiguration.m @@ -147,7 +147,7 @@ - (BOOL)automaticallyChecksForUpdates { } - (SPUUpdater *)updater { - return self.appDelegate.updaterController.updater; + return GlobalContext.getUpdaterController.updater; } #pragma mark - setter diff --git a/Easydict/NewApp/EasydictApp.swift b/Easydict/NewApp/EasydictApp.swift index c81ba83f4..ceadd9baf 100644 --- a/Easydict/NewApp/EasydictApp.swift +++ b/Easydict/NewApp/EasydictApp.swift @@ -8,6 +8,7 @@ import Sparkle import SwiftUI +import Defaults @main enum EasydictCmpatibilityEntry { @@ -21,28 +22,12 @@ enum EasydictCmpatibilityEntry { } } -class SPUUpdaterHelper: NSObject, SPUUpdaterDelegate { - func feedURLString(for _: SPUUpdater) -> String? { - var feedURLString = "https://raw.githubusercontent.com/tisfeng/Easydict/main/appcast.xml" - #if DEBUG - feedURLString = "http://localhost:8000/appcast.xml" - #endif - return feedURLString - } -} - -class SPUUserDriverHelper: NSObject, SPUStandardUserDriverDelegate { - var supportsGentleScheduledUpdateReminders: Bool { - true - } -} - struct EasydictApp: App { @NSApplicationDelegateAdaptor - var delegate: AppDelegate + private var delegate: AppDelegate - @AppStorage(kHideMenuBarIconKey) - private var hideMenuBar = false + @Default(.hideMenuBarIcon) + private var hideMenuBar private var menuBarImage: String { #if DEBUG @@ -52,22 +37,10 @@ struct EasydictApp: App { #endif } - let userDriverHelper = SPUUserDriverHelper() - let upadterHelper = SPUUpdaterHelper() - - private let updaterController: SPUStandardUpdaterController - - init() { - // 参考 https://sparkle-project.org/documentation/programmatic-setup/ - // If you want to start the updater manually, pass false to startingUpdater and call .startUpdater() later - // This is where you can also pass an updater delegate if you need one - updaterController = SPUStandardUpdaterController(startingUpdater: true, updaterDelegate: upadterHelper, userDriverDelegate: userDriverHelper) - } - var body: some Scene { if #available(macOS 13, *) { MenuBarExtra(isInserted: $hideMenuBar.toggledValue) { - MenuItemView(updater: updaterController.updater) + MenuItemView() } label: { Label { Text("Easydict") diff --git a/Easydict/NewApp/GlobalContext.swift b/Easydict/NewApp/GlobalContext.swift new file mode 100644 index 000000000..a80dc2baf --- /dev/null +++ b/Easydict/NewApp/GlobalContext.swift @@ -0,0 +1,44 @@ +// +// GlobalContext.swift +// Easydict +// +// Created by 戴藏龙 on 2024/1/25. +// Copyright © 2024 izual. All rights reserved. +// + +import Foundation +import Sparkle + +@objc class GlobalContext: NSObject { + static let updaterController: SPUStandardUpdaterController = { + class SPUUpdaterHelper: NSObject, SPUUpdaterDelegate { + func feedURLString(for _: SPUUpdater) -> String? { + var feedURLString = "https://raw.githubusercontent.com/tisfeng/Easydict/main/appcast.xml" + #if DEBUG + feedURLString = "http://localhost:8000/appcast.xml" + #endif + return feedURLString + } + } + + class SPUUserDriverHelper: NSObject, SPUStandardUserDriverDelegate { + var supportsGentleScheduledUpdateReminders: Bool { + true + } + } + let userDriverHelper = SPUUserDriverHelper() + let upadterHelper = SPUUpdaterHelper() + // 参考 https://sparkle-project.org/documentation/programmatic-setup/ + // If you want to start the updater manually, pass false to startingUpdater and call .startUpdater() later + // This is where you can also pass an updater delegate if you need one + return SPUStandardUpdaterController( + startingUpdater: true, + updaterDelegate: upadterHelper, + userDriverDelegate: userDriverHelper + ) + }() + + @objc class func getUpdaterController() -> SPUStandardUpdaterController { + updaterController + } +} diff --git a/Easydict/NewApp/View/MenuItemView.swift b/Easydict/NewApp/View/MenuItemView.swift index 5f25919b1..f549678ac 100644 --- a/Easydict/NewApp/View/MenuItemView.swift +++ b/Easydict/NewApp/View/MenuItemView.swift @@ -13,21 +13,19 @@ import ZipArchive @available(macOS 13, *) final class MenuItemStore: ObservableObject { @Published var canCheckForUpdates = false - var updater: SPUUpdater - init(updater: SPUUpdater) { - self.updater = updater - self.updater.publisher(for: \.canCheckForUpdates) + + init() { + GlobalContext + .updaterController + .updater + .publisher(for: \.canCheckForUpdates) .assign(to: &$canCheckForUpdates) } } @available(macOS 13, *) struct MenuItemView: View { - @ObservedObject var store: MenuItemStore - - init(updater: SPUUpdater) { - store = MenuItemStore(updater: updater) - } + @ObservedObject private var store: MenuItemStore = .init() var body: some View { // ️.menuBarExtraStyle为 .menu 时某些控件可能会失效 ,只能显示内容(按照菜单项高度、图像以 template 方式渲染)无法交互 ,比如 Stepper、Slider 等,像基本的 Button、Text、Divider、Image 等还是能正常显示的。 @@ -173,7 +171,7 @@ struct MenuItemView: View { private var checkUpdateItem: some View { Button("check_updates") { NSLog("检查更新") - store.updater.checkForUpdates() + GlobalContext.updaterController.updater.checkForUpdates() }.disabled(!store.canCheckForUpdates) } @@ -225,5 +223,5 @@ struct MenuItemView: View { @available(macOS 13, *) #Preview { - MenuItemView(updater: SPUStandardUpdaterController(startingUpdater: true, updaterDelegate: nil, userDriverDelegate: nil).updater) + MenuItemView() } From 5cf9365a9aaaee76257862070e1bf0e322323cea Mon Sep 17 00:00:00 2001 From: Lava <34743145+CanglongCl@users.noreply.github.com> Date: Thu, 25 Jan 2024 16:27:55 -0800 Subject: [PATCH 2/9] refactor: mutually initialize global context --- Easydict.xcodeproj/project.pbxproj | 2 +- Easydict/NewApp/EasydictApp.swift | 3 ++- Easydict/NewApp/{ => Utility}/GlobalContext.swift | 5 +++++ 3 files changed, 8 insertions(+), 2 deletions(-) rename Easydict/NewApp/{ => Utility}/GlobalContext.swift (89%) diff --git a/Easydict.xcodeproj/project.pbxproj b/Easydict.xcodeproj/project.pbxproj index 57c136e50..dd2932c73 100644 --- a/Easydict.xcodeproj/project.pbxproj +++ b/Easydict.xcodeproj/project.pbxproj @@ -2070,7 +2070,6 @@ 27FE95262B3DC55F000AD654 /* EasydictApp.swift */, 27FE98042B3DCB09000AD654 /* NewAppManager.swift */, 27FE98062B3DD525000AD654 /* View */, - EAE3D34F2B62E9DE001EE3E3 /* GlobalContext.swift */, ); path = NewApp; sourceTree = ""; @@ -2233,6 +2232,7 @@ EA9943DD2B534BAE00EE7B97 /* Utility */ = { isa = PBXGroup; children = ( + EAE3D34F2B62E9DE001EE3E3 /* GlobalContext.swift */, EAED41ED2B54B1390005FE0A /* Protocol */, EA9943E62B534D7C00EE7B97 /* Extensions */, ); diff --git a/Easydict/NewApp/EasydictApp.swift b/Easydict/NewApp/EasydictApp.swift index ceadd9baf..fc0a614fa 100644 --- a/Easydict/NewApp/EasydictApp.swift +++ b/Easydict/NewApp/EasydictApp.swift @@ -6,14 +6,15 @@ // Copyright © 2023 izual. All rights reserved. // +import Defaults import Sparkle import SwiftUI -import Defaults @main enum EasydictCmpatibilityEntry { static func main() { parseArmguments() + GlobalContext.initializeProperty() if NewAppManager.shared.enable { EasydictApp.main() } else { diff --git a/Easydict/NewApp/GlobalContext.swift b/Easydict/NewApp/Utility/GlobalContext.swift similarity index 89% rename from Easydict/NewApp/GlobalContext.swift rename to Easydict/NewApp/Utility/GlobalContext.swift index a80dc2baf..8318fcecb 100644 --- a/Easydict/NewApp/GlobalContext.swift +++ b/Easydict/NewApp/Utility/GlobalContext.swift @@ -10,6 +10,11 @@ import Foundation import Sparkle @objc class GlobalContext: NSObject { + /// Initialized all property in global context to assure static properties life circle. + static func initializeProperty() { + let _ = Self.updaterController + } + static let updaterController: SPUStandardUpdaterController = { class SPUUpdaterHelper: NSObject, SPUUpdaterDelegate { func feedURLString(for _: SPUUpdater) -> String? { From a06a42cccc897d1d754d015a752d037d5c9caaf8 Mon Sep 17 00:00:00 2001 From: Lava <34743145+CanglongCl@users.noreply.github.com> Date: Thu, 25 Jan 2024 16:28:09 -0800 Subject: [PATCH 3/9] revert `@Default` in `EasydictApp` --- Easydict/NewApp/EasydictApp.swift | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/Easydict/NewApp/EasydictApp.swift b/Easydict/NewApp/EasydictApp.swift index fc0a614fa..76e34acf7 100644 --- a/Easydict/NewApp/EasydictApp.swift +++ b/Easydict/NewApp/EasydictApp.swift @@ -26,9 +26,11 @@ enum EasydictCmpatibilityEntry { struct EasydictApp: App { @NSApplicationDelegateAdaptor private var delegate: AppDelegate - - @Default(.hideMenuBarIcon) - private var hideMenuBar + + // Use `@Default` will cause a purple warning and continuously call `set` of it. + // I'm not sure why. Just leave `AppStorage` here. + @AppStorage(Defaults.Key.hideMenuBarIcon.name) + private var hideMenuBar = Defaults.Key.hideMenuBarIcon.defaultValue private var menuBarImage: String { #if DEBUG From e6f6bf90186a37cddb25d106217d755d92e04f8c Mon Sep 17 00:00:00 2001 From: tisfeng Date: Fri, 26 Jan 2024 11:17:21 +0800 Subject: [PATCH 4/9] fix: remove updaterController from Main.storyboard --- Easydict/App/AppDelegate.h | 1 - Easydict/App/Base.lproj/Main.storyboard | 14 ++------------ Easydict/Feature/Configuration/EZConfiguration.h | 4 ++++ Easydict/Feature/Configuration/EZConfiguration.m | 6 +++++- Easydict/Feature/StatusItem/EZMenuItemManager.m | 7 +++++++ 5 files changed, 18 insertions(+), 14 deletions(-) diff --git a/Easydict/App/AppDelegate.h b/Easydict/App/AppDelegate.h index 4c1bb5030..2ecaff2c6 100644 --- a/Easydict/App/AppDelegate.h +++ b/Easydict/App/AppDelegate.h @@ -7,7 +7,6 @@ // #import -#import @interface AppDelegate : NSObject diff --git a/Easydict/App/Base.lproj/Main.storyboard b/Easydict/App/Base.lproj/Main.storyboard index 9df59d2cb..52009ade9 100644 --- a/Easydict/App/Base.lproj/Main.storyboard +++ b/Easydict/App/Base.lproj/Main.storyboard @@ -716,18 +716,8 @@ DQ - - - - - + - - - - - - @@ -799,7 +789,7 @@ DQ - + diff --git a/Easydict/Feature/Configuration/EZConfiguration.h b/Easydict/Feature/Configuration/EZConfiguration.h index 310f47031..b31a6e12c 100644 --- a/Easydict/Feature/Configuration/EZConfiguration.h +++ b/Easydict/Feature/Configuration/EZConfiguration.h @@ -12,6 +12,8 @@ NS_ASSUME_NONNULL_BEGIN +@class SPUStandardUpdaterController; + FOUNDATION_EXPORT NSString *const kHideMainWindowKey; FOUNDATION_EXPORT NSString *const kLaunchAtStartupKey; FOUNDATION_EXPORT NSString *const kHideMenuBarIconKey; @@ -84,6 +86,8 @@ typedef NS_ENUM(NSUInteger, EZAppearenceType) { @property (nonatomic, assign) EZAppearenceType appearance; +@property (nonatomic, strong, readonly) SPUStandardUpdaterController *updaterController; + + (instancetype)shared; + (void)destroySharedInstance; diff --git a/Easydict/Feature/Configuration/EZConfiguration.m b/Easydict/Feature/Configuration/EZConfiguration.m index 3cd01f6ad..7b8918484 100644 --- a/Easydict/Feature/Configuration/EZConfiguration.m +++ b/Easydict/Feature/Configuration/EZConfiguration.m @@ -63,7 +63,7 @@ @interface EZConfiguration () @property (nonatomic, strong) AppDelegate *appDelegate; -@property (nonatomic, strong) SPUUpdater *updater; +@property (nonatomic, strong, readwrite) SPUUpdater *updater; @end @@ -150,6 +150,10 @@ - (SPUUpdater *)updater { return GlobalContext.getUpdaterController.updater; } +- (SPUStandardUpdaterController *)updaterController { + return GlobalContext.getUpdaterController; +} + #pragma mark - setter - (void)setFirstLanguage:(EZLanguage)firstLanguage { diff --git a/Easydict/Feature/StatusItem/EZMenuItemManager.m b/Easydict/Feature/StatusItem/EZMenuItemManager.m index 8d9f3b017..861a0e136 100644 --- a/Easydict/Feature/StatusItem/EZMenuItemManager.m +++ b/Easydict/Feature/StatusItem/EZMenuItemManager.m @@ -15,6 +15,7 @@ #import "EZRightClickDetector.h" #import "EZConfiguration.h" #import "Easydict-Swift.h" +#import @interface EZMenuItemManager () @@ -181,6 +182,12 @@ - (IBAction)settingAction:(NSMenuItem *)sender { [EZPreferencesWindowController.shared show]; } +- (IBAction)checkForUpdateItem:(id)sender { + NSLog(@"checkForUpdate"); + + [EZConfiguration.shared.updaterController checkForUpdates:sender]; +} + - (IBAction)feedbackAction:(NSMenuItem *)sender { NSLog(@"反馈问题"); NSString *issueURL = [NSString stringWithFormat:@"%@/issues", EZGithubRepoEasydictURL]; From 51910ebcad0cf22b0b016543d4547768976b8c5c Mon Sep 17 00:00:00 2001 From: tisfeng Date: Fri, 26 Jan 2024 11:20:47 +0800 Subject: [PATCH 5/9] style: fix typo --- Easydict/NewApp/Utility/GlobalContext.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Easydict/NewApp/Utility/GlobalContext.swift b/Easydict/NewApp/Utility/GlobalContext.swift index 8318fcecb..6c2ad3f7f 100644 --- a/Easydict/NewApp/Utility/GlobalContext.swift +++ b/Easydict/NewApp/Utility/GlobalContext.swift @@ -32,13 +32,13 @@ import Sparkle } } let userDriverHelper = SPUUserDriverHelper() - let upadterHelper = SPUUpdaterHelper() + let updaterHelper = SPUUpdaterHelper() // 参考 https://sparkle-project.org/documentation/programmatic-setup/ // If you want to start the updater manually, pass false to startingUpdater and call .startUpdater() later // This is where you can also pass an updater delegate if you need one return SPUStandardUpdaterController( startingUpdater: true, - updaterDelegate: upadterHelper, + updaterDelegate: updaterHelper, userDriverDelegate: userDriverHelper ) }() From af80b68b0a4dee484dce76a0229cb940146cef25 Mon Sep 17 00:00:00 2001 From: tisfeng Date: Fri, 26 Jan 2024 18:16:24 +0800 Subject: [PATCH 6/9] fix: updaterDelegate was nil --- .../Feature/Configuration/Configuration.swift | 4 +- .../Feature/Configuration/EZConfiguration.h | 5 +- .../Feature/Configuration/EZConfiguration.m | 10 +--- .../Feature/StatusItem/EZMenuItemManager.m | 4 +- Easydict/NewApp/EasydictApp.swift | 3 +- Easydict/NewApp/Utility/GlobalContext.swift | 55 ++++++++----------- Easydict/NewApp/View/MenuItemView.swift | 6 +- 7 files changed, 37 insertions(+), 50 deletions(-) diff --git a/Easydict/Feature/Configuration/Configuration.swift b/Easydict/Feature/Configuration/Configuration.swift index b388be637..c78b3f2b1 100644 --- a/Easydict/Feature/Configuration/Configuration.swift +++ b/Easydict/Feature/Configuration/Configuration.swift @@ -59,10 +59,10 @@ let kHideMenuBarIconKey = "EZConfiguration_kHideMenuBarIconKey" var automaticallyChecksForUpdates: Bool { get { - GlobalContext.updaterController.updater.automaticallyDownloadsUpdates + GlobalContext.shared.updaterController.updater.automaticallyDownloadsUpdates } set { - GlobalContext.updaterController.updater.automaticallyDownloadsUpdates = newValue + GlobalContext.shared.updaterController.updater.automaticallyDownloadsUpdates = newValue logSettings(["automatically_checks_for_updates": newValue]) } } diff --git a/Easydict/Feature/Configuration/EZConfiguration.h b/Easydict/Feature/Configuration/EZConfiguration.h index b31a6e12c..266797563 100644 --- a/Easydict/Feature/Configuration/EZConfiguration.h +++ b/Easydict/Feature/Configuration/EZConfiguration.h @@ -9,11 +9,10 @@ #import #import "EZLanguageManager.h" #import "EZLayoutManager.h" +#import NS_ASSUME_NONNULL_BEGIN -@class SPUStandardUpdaterController; - FOUNDATION_EXPORT NSString *const kHideMainWindowKey; FOUNDATION_EXPORT NSString *const kLaunchAtStartupKey; FOUNDATION_EXPORT NSString *const kHideMenuBarIconKey; @@ -86,7 +85,7 @@ typedef NS_ENUM(NSUInteger, EZAppearenceType) { @property (nonatomic, assign) EZAppearenceType appearance; -@property (nonatomic, strong, readonly) SPUStandardUpdaterController *updaterController; +@property (nonatomic, strong, readonly) SPUUpdater *updater; + (instancetype)shared; + (void)destroySharedInstance; diff --git a/Easydict/Feature/Configuration/EZConfiguration.m b/Easydict/Feature/Configuration/EZConfiguration.m index 7b8918484..a3104745a 100644 --- a/Easydict/Feature/Configuration/EZConfiguration.m +++ b/Easydict/Feature/Configuration/EZConfiguration.m @@ -142,16 +142,12 @@ - (BOOL)launchAtStartup { return launchAtStartup; } -- (BOOL)automaticallyChecksForUpdates { - return self.updater.automaticallyChecksForUpdates; -} - - (SPUUpdater *)updater { - return GlobalContext.getUpdaterController.updater; + return GlobalContext.shared.updaterController.updater; } -- (SPUStandardUpdaterController *)updaterController { - return GlobalContext.getUpdaterController; +- (BOOL)automaticallyChecksForUpdates { + return self.updater.automaticallyChecksForUpdates; } #pragma mark - setter diff --git a/Easydict/Feature/StatusItem/EZMenuItemManager.m b/Easydict/Feature/StatusItem/EZMenuItemManager.m index 861a0e136..9f40e3fc9 100644 --- a/Easydict/Feature/StatusItem/EZMenuItemManager.m +++ b/Easydict/Feature/StatusItem/EZMenuItemManager.m @@ -16,6 +16,7 @@ #import "EZConfiguration.h" #import "Easydict-Swift.h" #import +#import @interface EZMenuItemManager () @@ -184,8 +185,7 @@ - (IBAction)settingAction:(NSMenuItem *)sender { - (IBAction)checkForUpdateItem:(id)sender { NSLog(@"checkForUpdate"); - - [EZConfiguration.shared.updaterController checkForUpdates:sender]; + [EZConfiguration.shared.updater checkForUpdates]; } - (IBAction)feedbackAction:(NSMenuItem *)sender { diff --git a/Easydict/NewApp/EasydictApp.swift b/Easydict/NewApp/EasydictApp.swift index 76e34acf7..bb3e1be41 100644 --- a/Easydict/NewApp/EasydictApp.swift +++ b/Easydict/NewApp/EasydictApp.swift @@ -14,7 +14,6 @@ import SwiftUI enum EasydictCmpatibilityEntry { static func main() { parseArmguments() - GlobalContext.initializeProperty() if NewAppManager.shared.enable { EasydictApp.main() } else { @@ -26,7 +25,7 @@ enum EasydictCmpatibilityEntry { struct EasydictApp: App { @NSApplicationDelegateAdaptor private var delegate: AppDelegate - + // Use `@Default` will cause a purple warning and continuously call `set` of it. // I'm not sure why. Just leave `AppStorage` here. @AppStorage(Defaults.Key.hideMenuBarIcon.name) diff --git a/Easydict/NewApp/Utility/GlobalContext.swift b/Easydict/NewApp/Utility/GlobalContext.swift index 6c2ad3f7f..d80b1e29d 100644 --- a/Easydict/NewApp/Utility/GlobalContext.swift +++ b/Easydict/NewApp/Utility/GlobalContext.swift @@ -9,41 +9,34 @@ import Foundation import Sparkle -@objc class GlobalContext: NSObject { - /// Initialized all property in global context to assure static properties life circle. - static func initializeProperty() { - let _ = Self.updaterController +@objcMembers +class GlobalContext: NSObject { + static let shared = GlobalContext() + + let updaterController: SPUStandardUpdaterController + let updaterHelper: SPUUpdaterHelper + let userDriverHelper: SPUUserDriverHelper + + override init() { + updaterHelper = SPUUpdaterHelper() + userDriverHelper = SPUUserDriverHelper() + + updaterController = SPUStandardUpdaterController(startingUpdater: true, updaterDelegate: updaterHelper, userDriverDelegate: userDriverHelper) } - static let updaterController: SPUStandardUpdaterController = { - class SPUUpdaterHelper: NSObject, SPUUpdaterDelegate { - func feedURLString(for _: SPUUpdater) -> String? { - var feedURLString = "https://raw.githubusercontent.com/tisfeng/Easydict/main/appcast.xml" - #if DEBUG - feedURLString = "http://localhost:8000/appcast.xml" - #endif - return feedURLString - } + class SPUUpdaterHelper: NSObject, SPUUpdaterDelegate { + func feedURLString(for _: SPUUpdater) -> String? { + var feedURLString = "https://raw.githubusercontent.com/tisfeng/Easydict/main/appcast.xml" + #if DEBUG + feedURLString = "http://localhost:8000/appcast.xml" + #endif + return feedURLString } + } - class SPUUserDriverHelper: NSObject, SPUStandardUserDriverDelegate { - var supportsGentleScheduledUpdateReminders: Bool { - true - } + class SPUUserDriverHelper: NSObject, SPUStandardUserDriverDelegate { + var supportsGentleScheduledUpdateReminders: Bool { + true } - let userDriverHelper = SPUUserDriverHelper() - let updaterHelper = SPUUpdaterHelper() - // 参考 https://sparkle-project.org/documentation/programmatic-setup/ - // If you want to start the updater manually, pass false to startingUpdater and call .startUpdater() later - // This is where you can also pass an updater delegate if you need one - return SPUStandardUpdaterController( - startingUpdater: true, - updaterDelegate: updaterHelper, - userDriverDelegate: userDriverHelper - ) - }() - - @objc class func getUpdaterController() -> SPUStandardUpdaterController { - updaterController } } diff --git a/Easydict/NewApp/View/MenuItemView.swift b/Easydict/NewApp/View/MenuItemView.swift index f549678ac..65a264c59 100644 --- a/Easydict/NewApp/View/MenuItemView.swift +++ b/Easydict/NewApp/View/MenuItemView.swift @@ -15,7 +15,7 @@ final class MenuItemStore: ObservableObject { @Published var canCheckForUpdates = false init() { - GlobalContext + GlobalContext.shared .updaterController .updater .publisher(for: \.canCheckForUpdates) @@ -25,7 +25,7 @@ final class MenuItemStore: ObservableObject { @available(macOS 13, *) struct MenuItemView: View { - @ObservedObject private var store: MenuItemStore = .init() + @ObservedObject private var store = MenuItemStore() var body: some View { // ️.menuBarExtraStyle为 .menu 时某些控件可能会失效 ,只能显示内容(按照菜单项高度、图像以 template 方式渲染)无法交互 ,比如 Stepper、Slider 等,像基本的 Button、Text、Divider、Image 等还是能正常显示的。 @@ -171,7 +171,7 @@ struct MenuItemView: View { private var checkUpdateItem: some View { Button("check_updates") { NSLog("检查更新") - GlobalContext.updaterController.updater.checkForUpdates() + GlobalContext.shared.updaterController.updater.checkForUpdates() }.disabled(!store.canCheckForUpdates) } From e6803604d76b2c0b867f352e38f68f185c0d7433 Mon Sep 17 00:00:00 2001 From: tisfeng Date: Fri, 26 Jan 2024 21:15:11 +0800 Subject: [PATCH 7/9] perf: set updaterHelper to private --- Easydict/NewApp/Utility/GlobalContext.swift | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Easydict/NewApp/Utility/GlobalContext.swift b/Easydict/NewApp/Utility/GlobalContext.swift index d80b1e29d..557c22ae1 100644 --- a/Easydict/NewApp/Utility/GlobalContext.swift +++ b/Easydict/NewApp/Utility/GlobalContext.swift @@ -14,8 +14,9 @@ class GlobalContext: NSObject { static let shared = GlobalContext() let updaterController: SPUStandardUpdaterController - let updaterHelper: SPUUpdaterHelper - let userDriverHelper: SPUUserDriverHelper + + private let updaterHelper: SPUUpdaterHelper + private let userDriverHelper: SPUUserDriverHelper override init() { updaterHelper = SPUUpdaterHelper() From a9c77a3dbb2746920753d8366d81e07a2cfc28af Mon Sep 17 00:00:00 2001 From: tisfeng Date: Fri, 26 Jan 2024 22:09:41 +0800 Subject: [PATCH 8/9] perf: remove sparkle code from AppDelegate --- Easydict/App/AppDelegate.m | 20 +------------------- 1 file changed, 1 insertion(+), 19 deletions(-) diff --git a/Easydict/App/AppDelegate.m b/Easydict/App/AppDelegate.m index 6e6efd49c..9fbdbd85d 100644 --- a/Easydict/App/AppDelegate.m +++ b/Easydict/App/AppDelegate.m @@ -15,11 +15,9 @@ #import "EZLog.h" #import "EZSchemeParser.h" #import "AppDelegate+EZURLScheme.h" -#import -#import #import "Easydict-Swift.h" -@interface AppDelegate () +@interface AppDelegate () @end @@ -99,20 +97,4 @@ - (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)applica return NO; } -#pragma mark - SUUpdaterDelegate - -- (NSString *)feedURLStringForUpdater:(SPUUpdater *)updater { - NSString *feedURLString = @"https://raw.githubusercontent.com/tisfeng/Easydict/main/appcast.xml"; -#if DEBUG - feedURLString = @"http://localhost:8000/appcast.xml"; -#endif - return feedURLString; -} - -#pragma mark - SPUStandardUserDriverDelegate - -- (BOOL)supportsGentleScheduledUpdateReminders { - return YES; -} - @end From ee70166cf1640dc263abd5cfd40261348f807521 Mon Sep 17 00:00:00 2001 From: tisfeng Date: Fri, 26 Jan 2024 22:12:46 +0800 Subject: [PATCH 9/9] perf: remove unused #import header --- Easydict/App/AppDelegate.m | 5 ----- 1 file changed, 5 deletions(-) diff --git a/Easydict/App/AppDelegate.m b/Easydict/App/AppDelegate.m index 9fbdbd85d..ccec0a435 100644 --- a/Easydict/App/AppDelegate.m +++ b/Easydict/App/AppDelegate.m @@ -7,13 +7,8 @@ // #import "AppDelegate.h" -#import "EZMenuItemManager.h" #import "EZShortcut.h" #import "MMCrash.h" -#import "EZWindowManager.h" -#import "EZLanguageManager.h" -#import "EZLog.h" -#import "EZSchemeParser.h" #import "AppDelegate+EZURLScheme.h" #import "Easydict-Swift.h"