Skip to content

Commit

Permalink
fix: updaterDelegate was nil
Browse files Browse the repository at this point in the history
  • Loading branch information
tisfeng committed Jan 26, 2024
1 parent 51910eb commit af80b68
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 50 deletions.
4 changes: 2 additions & 2 deletions Easydict/Feature/Configuration/Configuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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])
}
}
Expand Down
5 changes: 2 additions & 3 deletions Easydict/Feature/Configuration/EZConfiguration.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@
#import <Foundation/Foundation.h>
#import "EZLanguageManager.h"
#import "EZLayoutManager.h"
#import <Sparkle/SPUUpdater.h>

NS_ASSUME_NONNULL_BEGIN

@class SPUStandardUpdaterController;

FOUNDATION_EXPORT NSString *const kHideMainWindowKey;
FOUNDATION_EXPORT NSString *const kLaunchAtStartupKey;
FOUNDATION_EXPORT NSString *const kHideMenuBarIconKey;
Expand Down Expand Up @@ -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;
Expand Down
10 changes: 3 additions & 7 deletions Easydict/Feature/Configuration/EZConfiguration.m
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions Easydict/Feature/StatusItem/EZMenuItemManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#import "EZConfiguration.h"
#import "Easydict-Swift.h"
#import <Sparkle/SPUStandardUpdaterController.h>
#import <Sparkle/SPUUpdater.h>

@interface EZMenuItemManager () <NSMenuDelegate>

Expand Down Expand Up @@ -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 {
Expand Down
3 changes: 1 addition & 2 deletions Easydict/NewApp/EasydictApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import SwiftUI
enum EasydictCmpatibilityEntry {
static func main() {
parseArmguments()
GlobalContext.initializeProperty()
if NewAppManager.shared.enable {
EasydictApp.main()
} else {
Expand All @@ -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<Bool>.hideMenuBarIcon.name)
Expand Down
55 changes: 24 additions & 31 deletions Easydict/NewApp/Utility/GlobalContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
6 changes: 3 additions & 3 deletions Easydict/NewApp/View/MenuItemView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ final class MenuItemStore: ObservableObject {
@Published var canCheckForUpdates = false

init() {
GlobalContext
GlobalContext.shared
.updaterController
.updater
.publisher(for: \.canCheckForUpdates)
Expand All @@ -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 等还是能正常显示的。
Expand Down Expand Up @@ -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)
}

Expand Down

0 comments on commit af80b68

Please sign in to comment.