diff --git a/Passepartout/App/Context/AppPreference.swift b/Passepartout/App/Context/AppPreference.swift index 6414d7bc..e343053d 100644 --- a/Passepartout/App/Context/AppPreference.swift +++ b/Passepartout/App/Context/AppPreference.swift @@ -27,7 +27,7 @@ import Foundation import PassepartoutLibrary enum AppPreference: String, KeyStoreDomainLocation { - case launchesOnLogin // placeholder, hardcoded in PassepartoutMac + case launchesOnLogin case isShowingFavorites diff --git a/Passepartout/App/Mac/Models/DefaultLightUtils.swift b/Passepartout/App/Mac/Models/DefaultLightUtils.swift index 80c6528c..5d4aca1b 100644 --- a/Passepartout/App/Mac/Models/DefaultLightUtils.swift +++ b/Passepartout/App/Mac/Models/DefaultLightUtils.swift @@ -24,7 +24,7 @@ // import Foundation -import UIKit +import SwiftUI class DefaultLightUtils: LightUtils { private let app: UIApplication @@ -32,6 +32,8 @@ class DefaultLightUtils: LightUtils { init() { app = .shared } + + @AppStorage(AppPreference.launchesOnLogin.key) var launchesOnLogin = false func requestScene() { guard app.connectedScenes.isEmpty else { diff --git a/Passepartout/AppShared/Mac/Models/LightUtils.swift b/Passepartout/AppShared/Mac/Models/LightUtils.swift index 5f7e75cc..5c994d90 100644 --- a/Passepartout/AppShared/Mac/Models/LightUtils.swift +++ b/Passepartout/AppShared/Mac/Models/LightUtils.swift @@ -27,5 +27,7 @@ import Foundation @objc public protocol LightUtils { + var launchesOnLogin: Bool { get set } + func requestScene() } diff --git a/Passepartout/Mac/Menu/LaunchOnLoginItem+ViewModel.swift b/Passepartout/Mac/Menu/LaunchOnLoginItem+ViewModel.swift index 578d6390..e3bc1609 100644 --- a/Passepartout/Mac/Menu/LaunchOnLoginItem+ViewModel.swift +++ b/Passepartout/Mac/Menu/LaunchOnLoginItem+ViewModel.swift @@ -29,38 +29,37 @@ import ServiceManagement extension LaunchOnLoginItem { class ViewModel: ObservableObject { - - // XXX: hardcoded from AppPreference - private let launchesOnLoginKey = "Passepartout.App.launchesOnLogin" - let title: String - var launchesOnLogin: Bool { + let utils: LightUtils + + var persistentlyLaunchesOnLogin: Bool { get { - persistentlyLaunchesOnLogin + launchesOnLogin } set { guard SMLoginItemSetEnabled(Constants.Mac.appLauncherId as CFString, newValue) else { return } - persistentlyLaunchesOnLogin = newValue + launchesOnLogin = newValue objectWillChange.send() } } - private var persistentlyLaunchesOnLogin: Bool { + private var launchesOnLogin: Bool { get { - UserDefaults.standard.bool(forKey: launchesOnLoginKey) + utils.launchesOnLogin } set { - UserDefaults.standard.set(newValue, forKey: launchesOnLoginKey) + utils.launchesOnLogin = newValue } } private var subscriptions: Set = [] - init(title: String) { + init(_ title: String, utils: LightUtils) { self.title = title + self.utils = utils } @objc func toggleLaunchesOnLogin() { diff --git a/Passepartout/Mac/Menu/LaunchOnLoginItem.swift b/Passepartout/Mac/Menu/LaunchOnLoginItem.swift index aa3993a7..afa7368f 100644 --- a/Passepartout/Mac/Menu/LaunchOnLoginItem.swift +++ b/Passepartout/Mac/Menu/LaunchOnLoginItem.swift @@ -29,8 +29,8 @@ import AppKit struct LaunchOnLoginItem: Item { private let viewModel: ViewModel - init(_ title: String) { - viewModel = ViewModel(title: title) + init(_ title: String, utils: LightUtils) { + viewModel = ViewModel(title, utils: utils) } func asMenuItem(withParent parent: NSMenu) -> NSMenuItem { @@ -50,6 +50,6 @@ struct LaunchOnLoginItem: Item { } private var state: NSControl.StateValue { - viewModel.launchesOnLogin ? .on : .off + viewModel.persistentlyLaunchesOnLogin ? .on : .off } } diff --git a/Passepartout/Mac/Menu/PassepartoutMenu.swift b/Passepartout/Mac/Menu/PassepartoutMenu.swift index e2374448..b8aeebd5 100644 --- a/Passepartout/Mac/Menu/PassepartoutMenu.swift +++ b/Passepartout/Mac/Menu/PassepartoutMenu.swift @@ -60,7 +60,10 @@ class PassepartoutMenu { VisibilityItem(utils: macMenuDelegate.utils) { $0 ? L10n.Global.Strings.hide : L10n.Global.Strings.show }, - LaunchOnLoginItem(L10n.Preferences.Items.LaunchesOnLogin.caption), + LaunchOnLoginItem( + L10n.Preferences.Items.LaunchesOnLogin.caption, + utils: macMenuDelegate.utils + ), ] as [ItemGroup]) if profileManager.hasProfiles {