From 4ff44c0bc3bcdde5fc8ebd094b96a5c7ce6029ff Mon Sep 17 00:00:00 2001 From: Jerry Zhang Date: Sun, 28 Jan 2024 00:08:29 -0800 Subject: [PATCH 1/2] Migrate to Defaults for Privacy & About tabs (#360) * refractor: migrate from AppStorage to Defaults in Privacy and About tabs * refractor: use binding to set autoChecksForUpdates * fix: unable to set updater * fix: automaticallyDownloadsUpdates in GlobalContext is wrong * perf: improve code * perf: add updater in Configuration for easy outside use --------- Co-authored-by: Tisfeng Co-authored-by: Lava <34743145+CanglongCl@users.noreply.github.com> --- .../Feature/Configuration/Configuration.swift | 6 +++-- Easydict/NewApp/View/MenuItemView.swift | 6 ++--- .../View/SettingView/Tabs/AboutTab.swift | 23 ++++++++++++++++--- .../View/SettingView/Tabs/PrivacyTab.swift | 8 +++---- 4 files changed, 29 insertions(+), 14 deletions(-) diff --git a/Easydict/Feature/Configuration/Configuration.swift b/Easydict/Feature/Configuration/Configuration.swift index c78b3f2b1..7f7c6dc53 100644 --- a/Easydict/Feature/Configuration/Configuration.swift +++ b/Easydict/Feature/Configuration/Configuration.swift @@ -57,12 +57,14 @@ let kHideMenuBarIconKey = "EZConfiguration_kHideMenuBarIconKey" @DefaultsWrapper(.launchAtStartup) var launchAtStartup: Bool + let updater = GlobalContext.shared.updaterController.updater + var automaticallyChecksForUpdates: Bool { get { - GlobalContext.shared.updaterController.updater.automaticallyDownloadsUpdates + updater.automaticallyChecksForUpdates } set { - GlobalContext.shared.updaterController.updater.automaticallyDownloadsUpdates = newValue + updater.automaticallyChecksForUpdates = newValue logSettings(["automatically_checks_for_updates": newValue]) } } diff --git a/Easydict/NewApp/View/MenuItemView.swift b/Easydict/NewApp/View/MenuItemView.swift index 65a264c59..7b25db502 100644 --- a/Easydict/NewApp/View/MenuItemView.swift +++ b/Easydict/NewApp/View/MenuItemView.swift @@ -15,9 +15,7 @@ final class MenuItemStore: ObservableObject { @Published var canCheckForUpdates = false init() { - GlobalContext.shared - .updaterController - .updater + Configuration.shared.updater .publisher(for: \.canCheckForUpdates) .assign(to: &$canCheckForUpdates) } @@ -171,7 +169,7 @@ struct MenuItemView: View { private var checkUpdateItem: some View { Button("check_updates") { NSLog("检查更新") - GlobalContext.shared.updaterController.updater.checkForUpdates() + Configuration.shared.updater.checkForUpdates() }.disabled(!store.canCheckForUpdates) } diff --git a/Easydict/NewApp/View/SettingView/Tabs/AboutTab.swift b/Easydict/NewApp/View/SettingView/Tabs/AboutTab.swift index aa6f6a4e4..28e79d37f 100644 --- a/Easydict/NewApp/View/SettingView/Tabs/AboutTab.swift +++ b/Easydict/NewApp/View/SettingView/Tabs/AboutTab.swift @@ -6,6 +6,7 @@ // Copyright © 2023 izual. All rights reserved. // +import Defaults import SwiftUI @available(macOS 13, *) @@ -20,7 +21,7 @@ struct AboutTab: View { .font(.system(size: 26, weight: .semibold)) Text("current_version") + Text(verbatim: " \(version)") .font(.system(size: 14)) - Toggle("auto_check_update", isOn: $autoChecksForUpdates) + Toggle("auto_check_update", isOn: $checkUpdaterViewModel.autoChecksForUpdates) Text(verbatim: "(") + Text("lastest_version") + Text(verbatim: " \(lastestVersion ?? version))") HStack { @@ -45,8 +46,8 @@ struct AboutTab: View { } } - @AppStorage("EZConfiguration_kAutomaticallyChecksForUpdatesKey") - private var autoChecksForUpdates = false + @StateObject private var checkUpdaterViewModel = CheckUpdaterViewModel() + @State private var lastestVersion: String? private var appName: String { @@ -56,6 +57,22 @@ struct AboutTab: View { private var version: String { Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String ?? "" } + + class CheckUpdaterViewModel: ObservableObject { + private let updater = Configuration.shared.updater + + @Published var autoChecksForUpdates = true { + didSet { + updater.automaticallyChecksForUpdates = autoChecksForUpdates + } + } + + init() { + updater + .publisher(for: \.automaticallyChecksForUpdates) + .assign(to: &$autoChecksForUpdates) + } + } } @available(macOS 13, *) diff --git a/Easydict/NewApp/View/SettingView/Tabs/PrivacyTab.swift b/Easydict/NewApp/View/SettingView/Tabs/PrivacyTab.swift index 1cb7714e0..b51403174 100644 --- a/Easydict/NewApp/View/SettingView/Tabs/PrivacyTab.swift +++ b/Easydict/NewApp/View/SettingView/Tabs/PrivacyTab.swift @@ -6,6 +6,7 @@ // Copyright © 2023 izual. All rights reserved. // +import Defaults import SwiftUI @available(macOS 13, *) @@ -33,11 +34,8 @@ struct PrivacyTab: View { .formStyle(.grouped) } - @AppStorage("EZConfiguration_kAllowCrashLogKey") - private var allowCollectCrashLog = true - - @AppStorage("EZConfiguration_kAllowAnalyticsKey") - private var allowCollectAnalytics = true + @Default(.allowCrashLog) private var allowCollectCrashLog + @Default(.allowAnalytics) private var allowCollectAnalytics } @available(macOS 13, *) From 896d04d41a7f7aaa9055acf6c53a13d1f7a5ff24 Mon Sep 17 00:00:00 2001 From: tisfeng Date: Sun, 28 Jan 2024 11:28:45 +0800 Subject: [PATCH 2/2] docs: update sponsor list --- README.md | 1 + README_EN.md | 1 + 2 files changed, 2 insertions(+) diff --git a/README.md b/README.md index dfb27f0b8..3e2f06b69 100644 --- a/README.md +++ b/README.md @@ -824,6 +824,7 @@ Easydict 作为一个免费开源的非盈利项目,目前主要是作者个 | 2024-01-15 | ㅤ | 20 | 感谢开源,感谢有你:) | | 2024-01-16 | ㅤ sd | 5 | 大佬牛逼🐂🍺 | | 2024-01-23 | ㅤ | 5 | | +| 2024-01-28 | ㅤ | 7 | |

diff --git a/README_EN.md b/README_EN.md index 369d47ec0..eeffe8858 100644 --- a/README_EN.md +++ b/README_EN.md @@ -821,6 +821,7 @@ If you don't want your username to be displayed in the list, please choose anony | 2024-01-15 | ㅤ | 20 | 感谢开源,感谢有你:) | | 2024-01-16 | ㅤ sd | 5 | 大佬牛逼🐂🍺 | | 2024-01-23 | ㅤ | 5 | | +| 2024-01-28 | ㅤ | 7 | |