Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Refactor] Introduce GlobalContext and migrate the updater controller into it #365

Merged
merged 10 commits into from
Jan 27, 2024
4 changes: 4 additions & 0 deletions Easydict.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */; };
Expand Down Expand Up @@ -772,6 +773,7 @@
EA9943ED2B5353AB00EE7B97 /* WindowTypeExtensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WindowTypeExtensions.swift; sourceTree = "<group>"; };
EA9943EF2B5354C400EE7B97 /* ShowWindowPositionExtensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ShowWindowPositionExtensions.swift; sourceTree = "<group>"; };
EA9943F12B5358BF00EE7B97 /* LanguageExtensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LanguageExtensions.swift; sourceTree = "<group>"; };
EAE3D34F2B62E9DE001EE3E3 /* GlobalContext.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GlobalContext.swift; sourceTree = "<group>"; };
EAED41EB2B54AA920005FE0A /* ServiceConfigurationSection.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ServiceConfigurationSection.swift; sourceTree = "<group>"; };
EAED41EE2B54B1430005FE0A /* ConfigurableService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConfigurableService.swift; sourceTree = "<group>"; };
EAED41F12B54B39D0005FE0A /* OpenAIService+ConfigurableService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "OpenAIService+ConfigurableService.swift"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -2230,6 +2232,7 @@
EA9943DD2B534BAE00EE7B97 /* Utility */ = {
isa = PBXGroup;
children = (
EAE3D34F2B62E9DE001EE3E3 /* GlobalContext.swift */,
EAED41ED2B54B1390005FE0A /* Protocol */,
EA9943E62B534D7C00EE7B97 /* Extensions */,
);
Expand Down Expand Up @@ -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 */,
Expand Down
2 changes: 0 additions & 2 deletions Easydict/App/AppDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,4 @@

@interface AppDelegate : NSObject <NSApplicationDelegate>

@property (weak) IBOutlet SPUStandardUpdaterController *updaterController;
tisfeng marked this conversation as resolved.
Show resolved Hide resolved

@end
10 changes: 2 additions & 8 deletions Easydict/Feature/Configuration/Configuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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])
}
}
Expand Down
2 changes: 1 addition & 1 deletion Easydict/Feature/Configuration/EZConfiguration.m
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ - (BOOL)automaticallyChecksForUpdates {
}

- (SPUUpdater *)updater {
return self.appDelegate.updaterController.updater;
return GlobalContext.getUpdaterController.updater;
}

#pragma mark - setter
Expand Down
42 changes: 9 additions & 33 deletions Easydict/NewApp/EasydictApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
// Copyright © 2023 izual. All rights reserved.
//

import Defaults
import Sparkle
import SwiftUI

@main
enum EasydictCmpatibilityEntry {
static func main() {
parseArmguments()
GlobalContext.initializeProperty()
if NewAppManager.shared.enable {
EasydictApp.main()
} else {
Expand All @@ -21,28 +23,14 @@ 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

@AppStorage(kHideMenuBarIconKey)
private var hideMenuBar = false
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<Bool>.hideMenuBarIcon.name)
private var hideMenuBar = Defaults.Key<Bool>.hideMenuBarIcon.defaultValue

private var menuBarImage: String {
#if DEBUG
Expand All @@ -52,22 +40,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")
Expand Down
49 changes: 49 additions & 0 deletions Easydict/NewApp/Utility/GlobalContext.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//
// GlobalContext.swift
// Easydict
//
// Created by 戴藏龙 on 2024/1/25.
// Copyright © 2024 izual. All rights reserved.
//

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? {
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
tisfeng marked this conversation as resolved.
Show resolved Hide resolved
}
}
let userDriverHelper = SPUUserDriverHelper()
let upadterHelper = SPUUpdaterHelper()
tisfeng marked this conversation as resolved.
Show resolved Hide resolved
// 参考 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
}
}
20 changes: 9 additions & 11 deletions Easydict/NewApp/View/MenuItemView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 等还是能正常显示的。
Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -225,5 +223,5 @@ struct MenuItemView: View {

@available(macOS 13, *)
#Preview {
MenuItemView(updater: SPUStandardUpdaterController(startingUpdater: true, updaterDelegate: nil, userDriverDelegate: nil).updater)
MenuItemView()
}