From aca4bf1ec2cf93627b0007731c6ce85a415c9d69 Mon Sep 17 00:00:00 2001 From: anlicheng <244108715@qq.com> Date: Mon, 23 Mar 2026 16:05:46 +0800 Subject: [PATCH] fix --- punchnet/Core/LaunchManager.swift | 38 +++++++++++++++++++ punchnet/Networking/SDLAPIClient+App.swift | 12 ++---- .../Views/Settings/SettingsSystemView.swift | 31 ++++++++++++--- punchnet/Views/Update/AppUpdateManager.swift | 1 + punchnet/Views/Update/AppUpdateView.swift | 2 +- punchnet/punchnetApp.swift | 3 +- 6 files changed, 71 insertions(+), 16 deletions(-) create mode 100644 punchnet/Core/LaunchManager.swift diff --git a/punchnet/Core/LaunchManager.swift b/punchnet/Core/LaunchManager.swift new file mode 100644 index 0000000..5815f38 --- /dev/null +++ b/punchnet/Core/LaunchManager.swift @@ -0,0 +1,38 @@ +// +// LaunchManager.swift +// punchnet +// +// Created by 安礼成 on 2026/3/23. +// +import ServiceManagement +import Observation + +@Observable +class LaunchManager { + // 获取当前主 App 的服务实例 + private let service = SMAppService.mainApp + + // 检查当前是否已开启自启动 + var launchAtLogin: Bool + + init() { + self.launchAtLogin = (service.status == .enabled) + } + + func toggleLaunchAtLogin(enabled: Bool) throws { + if enabled { + try service.register() + } else { + try service.unregister() + } + + // 3. 重点:操作完成后,手动更新存储属性以触发 View 刷新 + self.launchAtLogin = (service.status == .enabled) + } + + // 4. 提供一个手动同步方法(用于应对用户在系统设置中修改的情况) + func refreshLaunchStatus() { + self.launchAtLogin = (service.status == .enabled) + } + +} diff --git a/punchnet/Networking/SDLAPIClient+App.swift b/punchnet/Networking/SDLAPIClient+App.swift index bee992b..a0ec1d6 100644 --- a/punchnet/Networking/SDLAPIClient+App.swift +++ b/punchnet/Networking/SDLAPIClient+App.swift @@ -29,22 +29,18 @@ extension SDLAPIClient { let hasUpdate: Bool let latestVersion: String let latestBuild: Int - // 强制升级的url地址 - let forceUpdateUrl: String? + let forceUpdate: Bool + let downloadUrl: String let releaseNotes: String let minSupportedVersion: String let publishTime: Int - // 是否强制升级 - var forceUpdate: Bool { - return forceUpdateUrl != nil - } - enum CodingKeys: String, CodingKey { case hasUpdate = "has_update" case latestVersion = "latest_version" case latestBuild = "latest_build" - case forceUpdateUrl = "force_update" + case forceUpdate = "force_update" + case downloadUrl = "download_url" case releaseNotes = "release_notes" case minSupportedVersion = "min_supported_version" case publishTime = "publish_time" diff --git a/punchnet/Views/Settings/SettingsSystemView.swift b/punchnet/Views/Settings/SettingsSystemView.swift index f7363a8..e7f299f 100644 --- a/punchnet/Views/Settings/SettingsSystemView.swift +++ b/punchnet/Views/Settings/SettingsSystemView.swift @@ -7,11 +7,14 @@ import SwiftUI struct SettingsSystemView: View { + // 启动管理 + @State private var launchManager = LaunchManager() + // 为每个设置项提供独立的状态 - @State private var launchAtLogin: Bool = false - @State private var autoConnect: Bool = false + @AppStorage("autoConnect") private var autoConnect: Bool = false + @AppStorage("autoUpdate") private var autoUpdate: Bool = true + @State private var showMainUI: Bool = true - @State private var autoUpdate: Bool = true var body: some View { ScrollView(.vertical, showsIndicators: false) { @@ -21,13 +24,26 @@ struct SettingsSystemView: View { systemSectionHeader(title: "启动与运行", icon: "power.circle.fill") VStack(spacing: 0) { - ToggleRow(icon: "macwindow.badge.plus", title: "开机时自动启动", isOn: $launchAtLogin) + ToggleRow(icon: "macwindow.badge.plus", title: "开机时自动启动", isOn: Binding( + get: { + launchManager.launchAtLogin + }, + set: { newValue in + do { + try launchManager.toggleLaunchAtLogin(enabled: newValue) + } catch let err { + NSLog("toggle get error: \(err)") + } + } + )) - Divider().padding(.leading, 48) // 为图标留出间距 + Divider() + .padding(.leading, 48) // 为图标留出间距 ToggleRow(icon: "bolt.horizontal.icloud.fill", title: "应用启动后自动连接", isOn: $autoConnect) - Divider().padding(.leading, 48) + Divider() + .padding(.leading, 48) ToggleRow(icon: "macwindow", title: "启动时显示主界面", isOn: $showMainUI) } @@ -56,6 +72,9 @@ struct SettingsSystemView: View { .padding(32) .frame(maxWidth: 600, alignment: .leading) } + .onAppear { + + } } // 辅助头部 diff --git a/punchnet/Views/Update/AppUpdateManager.swift b/punchnet/Views/Update/AppUpdateManager.swift index a512d16..64fe2c0 100644 --- a/punchnet/Views/Update/AppUpdateManager.swift +++ b/punchnet/Views/Update/AppUpdateManager.swift @@ -38,6 +38,7 @@ class AppUpdateManager { } catch { print("Update check failed: \(error)") } + return false } } diff --git a/punchnet/Views/Update/AppUpdateView.swift b/punchnet/Views/Update/AppUpdateView.swift index c72a271..cbedd23 100644 --- a/punchnet/Views/Update/AppUpdateView.swift +++ b/punchnet/Views/Update/AppUpdateView.swift @@ -55,7 +55,7 @@ struct AppUpdateView: View { } Button { - if let forceUpdateUrl = info.forceUpdateUrl, let url = URL(string: forceUpdateUrl) { + if let url = URL(string: info.downloadUrl) { NSWorkspace.shared.open(url) } } label: { diff --git a/punchnet/punchnetApp.swift b/punchnet/punchnetApp.swift index 2435231..dfae0fb 100644 --- a/punchnet/punchnetApp.swift +++ b/punchnet/punchnetApp.swift @@ -45,7 +45,8 @@ struct punchnetApp: App { var body: some Scene { WindowGroup(id: "main") { - RootView() + // RootView() + SettingsView() .navigationTitle("") .environment(self.appContext) .environment(self.userContext)