88import SwiftUI
99
1010struct SyncSettingsView : View {
11+ @Bindable private var settingsManager = AppSettingsManager . shared
1112 @Bindable private var syncCoordinator = SyncCoordinator . shared
12- @State private var syncSettings : SyncSettings = AppSettingsStorage . shared. loadSync ( )
1313
1414 private let licenseManager = LicenseManager . shared
1515
1616 var body : some View {
1717 Form {
1818 Section ( " iCloud Sync " ) {
19- Toggle ( " iCloud Sync: " , isOn: $syncSettings. enabled)
20- . onChange ( of: syncSettings. enabled) { _, newValue in
21- persistSettings ( )
19+ Toggle ( " iCloud Sync: " , isOn: $settingsManager. sync. enabled)
20+ . onChange ( of: settingsManager. sync. enabled) { _, newValue in
2221 updatePasswordSyncFlag ( )
2322 if newValue {
2423 syncCoordinator. enableSync ( )
@@ -32,7 +31,7 @@ struct SyncSettingsView: View {
3231 . foregroundStyle ( . secondary)
3332 }
3433
35- if syncSettings . enabled {
34+ if settingsManager . sync . enabled {
3635 statusSection
3736
3837 syncCategoriesSection
@@ -106,20 +105,17 @@ struct SyncSettingsView: View {
106105
107106 private var syncCategoriesSection : some View {
108107 Section ( " Sync Categories " ) {
109- Toggle ( " Connections: " , isOn: $syncSettings. syncConnections)
110- . onChange ( of: syncSettings. syncConnections) { _, newValue in
111- persistSettings ( )
112- if !newValue, syncSettings. syncPasswords {
113- syncSettings. syncPasswords = false
114- persistSettings ( )
108+ Toggle ( " Connections: " , isOn: $settingsManager. sync. syncConnections)
109+ . onChange ( of: settingsManager. sync. syncConnections) { _, newValue in
110+ if !newValue, settingsManager. sync. syncPasswords {
111+ settingsManager. sync. syncPasswords = false
115112 onPasswordSyncChanged ( false )
116113 }
117114 }
118115
119- if syncSettings. syncConnections {
120- Toggle ( " Passwords: " , isOn: $syncSettings. syncPasswords)
121- . onChange ( of: syncSettings. syncPasswords) { _, newValue in
122- persistSettings ( )
116+ if settingsManager. sync. syncConnections {
117+ Toggle ( " Passwords: " , isOn: $settingsManager. sync. syncPasswords)
118+ . onChange ( of: settingsManager. sync. syncPasswords) { _, newValue in
123119 onPasswordSyncChanged ( newValue)
124120 }
125121 . padding ( . leading, 20 )
@@ -130,14 +126,11 @@ struct SyncSettingsView: View {
130126 . padding ( . leading, 20 )
131127 }
132128
133- Toggle ( " Groups & Tags: " , isOn: $syncSettings. syncGroupsAndTags)
134- . onChange ( of: syncSettings. syncGroupsAndTags) { _, _ in persistSettings ( ) }
129+ Toggle ( " Groups & Tags: " , isOn: $settingsManager. sync. syncGroupsAndTags)
135130
136- Toggle ( " SSH Profiles: " , isOn: $syncSettings. syncSSHProfiles)
137- . onChange ( of: syncSettings. syncSSHProfiles) { _, _ in persistSettings ( ) }
131+ Toggle ( " SSH Profiles: " , isOn: $settingsManager. sync. syncSSHProfiles)
138132
139- Toggle ( " Settings: " , isOn: $syncSettings. syncSettings)
140- . onChange ( of: syncSettings. syncSettings) { _, _ in persistSettings ( ) }
133+ Toggle ( " Settings: " , isOn: $settingsManager. sync. syncSettings)
141134 }
142135 }
143136
@@ -167,20 +160,17 @@ struct SyncSettingsView: View {
167160
168161 // MARK: - Helpers
169162
170- private func persistSettings( ) {
171- AppSettingsStorage . shared. saveSync ( syncSettings)
172- }
173-
174163 private func onPasswordSyncChanged( _ enabled: Bool ) {
175- let effective = syncSettings . enabled && syncSettings . syncConnections && enabled
164+ let effective = settingsManager . sync . enabled && settingsManager . sync . syncConnections && enabled
176165 Task . detached {
177166 KeychainHelper . shared. migratePasswordSyncState ( synchronizable: effective)
178167 UserDefaults . standard. set ( effective, forKey: KeychainHelper . passwordSyncEnabledKey)
179168 }
180169 }
181170
182171 private func updatePasswordSyncFlag( ) {
183- let effective = syncSettings. enabled && syncSettings. syncConnections && syncSettings. syncPasswords
172+ let sync = settingsManager. sync
173+ let effective = sync. enabled && sync. syncConnections && sync. syncPasswords
184174 let current = UserDefaults . standard. bool ( forKey: KeychainHelper . passwordSyncEnabledKey)
185175 guard effective != current else { return }
186176 Task . detached {
@@ -190,10 +180,7 @@ struct SyncSettingsView: View {
190180 }
191181
192182 private func openLicenseSettings( ) {
193- NSApp . sendAction ( Selector ( ( " showSettingsWindow: " ) ) , to: nil , from: nil )
194- DispatchQueue . main. asyncAfter ( deadline: . now( ) + 0.1 ) {
195- UserDefaults . standard. set ( SettingsTab . license. rawValue, forKey: " selectedSettingsTab " )
196- }
183+ UserDefaults . standard. set ( SettingsTab . license. rawValue, forKey: " selectedSettingsTab " )
197184 }
198185}
199186
0 commit comments