Encapsulate launchesOnLogin AppPreference from Mac

This commit is contained in:
Davide De Rosa 2022-07-18 07:54:01 +02:00
parent b26e334881
commit f8c6b6580d
6 changed files with 23 additions and 17 deletions

View File

@ -27,7 +27,7 @@ import Foundation
import PassepartoutLibrary
enum AppPreference: String, KeyStoreDomainLocation {
case launchesOnLogin // placeholder, hardcoded in PassepartoutMac
case launchesOnLogin
case isShowingFavorites

View File

@ -24,7 +24,7 @@
//
import Foundation
import UIKit
import SwiftUI
class DefaultLightUtils: LightUtils {
private let app: UIApplication
@ -33,6 +33,8 @@ class DefaultLightUtils: LightUtils {
app = .shared
}
@AppStorage(AppPreference.launchesOnLogin.key) var launchesOnLogin = false
func requestScene() {
guard app.connectedScenes.isEmpty else {
return

View File

@ -27,5 +27,7 @@ import Foundation
@objc
public protocol LightUtils {
var launchesOnLogin: Bool { get set }
func requestScene()
}

View File

@ -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<AnyCancellable> = []
init(title: String) {
init(_ title: String, utils: LightUtils) {
self.title = title
self.utils = utils
}
@objc func toggleLaunchesOnLogin() {

View File

@ -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
}
}

View File

@ -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 {