Skip to content

Commit

Permalink
fix(sync): Sync everything to local storage to access it from the ext…
Browse files Browse the repository at this point in the history
…ension
  • Loading branch information
f-person committed Apr 9, 2023
1 parent 99a20eb commit 85e11b6
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 19 deletions.
2 changes: 2 additions & 0 deletions Armenian/ArmenianCalloutProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ class ArmenianCalloutActionProvider: BaseCalloutActionProvider {
.character("Ըւ")
]
case "։":
NSLog("colon chars: \(SharedDefaults.colonCalloutCharacters); comma chars: \(SharedDefaults.commaCalloutCharacters)")

return SharedDefaults.colonCalloutCharacters.map { .character(String($0)) }
case ",":
return SharedDefaults.commaCalloutCharacters.map { .character(String($0)) }
Expand Down
14 changes: 14 additions & 0 deletions Armenian/KeyboardViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ class KeyboardViewController: KeyboardInputViewController {

keyboardContext.setLocale(.armenian)

NSLog("audio: \(SharedDefaults.enableAudioFeedback); haptic: \(SharedDefaults.enableHapticFeedback); sync: \(SharedDefaults.enableSync)")

do {
calloutActionProvider = try ArmenianCalloutActionProvider()
} catch {
Expand All @@ -34,7 +36,19 @@ class KeyboardViewController: KeyboardInputViewController {
keyboardFeedbackHandler = StandardKeyboardFeedbackHandler(settings: keyboardFeedbackSettings)

keyboardActionHandler = ArmenianActionHandler(inputViewController: self)

NotificationCenter.default.addObserver(
self,
selector: #selector(updateLocalUserDefaults(_:)),
name: NSUbiquitousKeyValueStore.didChangeExternallyNotification,
object: nil
)

super.viewDidLoad()
}

@objc func updateLocalUserDefaults(_ notification: Notification) {
SharedDefaults.syncPreferencesToLocal()
}
}

51 changes: 37 additions & 14 deletions SharedDefaults/SharedDefaults.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public struct SharedDefaults {
public static var enableSync: Bool {
set {
if newValue {
syncAllPreferencesToCloud()
syncPreferencesToCloud()
}
iCloudStore?.set(newValue, forKey: enableSyncKey)
}
Expand All @@ -47,17 +47,17 @@ public struct SharedDefaults {
set { set(newValue, forKey: enableHapticFeedbackKey) }
get { get(enableHapticFeedbackKey, defaultValue: defaultEnableHapticFeedback) }
}

public static var enableAudioFeedback: Bool {
set { set(newValue, forKey: enableAudioFeedbackKey) }
get { get(enableAudioFeedbackKey, defaultValue: defaultEnableAudioFeedback) }
}

public static var colonCalloutCharacters: String {
set { set(newValue, forKey: colonCalloutCharactersKey) }
get { get(colonCalloutCharactersKey, defaultValue: defaultColonCalloutCharacters) }
}

public static var commaCalloutCharacters: String {
set { set(newValue, forKey: commaCalloutCharactersKey) }
get { get(commaCalloutCharactersKey, defaultValue: defaultCommaCalloutCharacters) }
Expand All @@ -71,31 +71,54 @@ public struct SharedDefaults {
}

private static func set<T>(_ value: T, forKey key: String) {
if let store = iCloudStore {
store.set(value, forKey: key)
} else {
localStore?.set(value, forKey: key)
localStore?.set(value, forKey: key)
if enableSync {
iCloudStore?.set(value, forKey: key)
}
}

private static func get<T>(_ key: String, defaultValue: T) -> T {
if let store = iCloudStore {
return store.object(forKey: key) as? T ?? defaultValue
if let localValue = localStore?.object(forKey: key) as? T {
return localValue
} else {
return localStore?.object(forKey: key) as? T ?? defaultValue
return defaultValue
}
}

private static func syncAllPreferencesToCloud() {
public static func syncPreferencesToCloud() {
guard let iCloudStore = iCloudStore else {
NSLog("Error: iCloud store is not available")
return
}

iCloudStore.set(enableHapticFeedback, forKey: enableHapticFeedbackKey)
iCloudStore.set(enableAudioFeedback, forKey: enableAudioFeedbackKey)
iCloudStore.set(colonCalloutCharacters, forKey: colonCalloutCharactersKey)
iCloudStore.set(commaCalloutCharacters, forKey: commaCalloutCharactersKey)
}

public static func stopSyncingWithCloud() {
iCloudStore?.removeObject(forKey: enableHapticFeedbackKey)
iCloudStore?.removeObject(forKey: enableAudioFeedbackKey)
iCloudStore?.removeObject(forKey: colonCalloutCharactersKey)
iCloudStore?.removeObject(forKey: commaCalloutCharactersKey)
}

public static func syncPreferencesToLocal() {
guard let iCloudStore = iCloudStore else {
NSLog("Error: iCloud store is not available")
return
}

let hapticFeedback = iCloudStore.object(forKey: enableHapticFeedbackKey) as? Bool ?? defaultEnableHapticFeedback
let audioFeedback = iCloudStore.object(forKey: enableAudioFeedbackKey) as? Bool ?? defaultEnableAudioFeedback
let colonCallouts = iCloudStore.object(forKey: colonCalloutCharactersKey) as? String ?? defaultColonCalloutCharacters
let commaCallouts = iCloudStore.object(forKey: commaCalloutCharactersKey) as? String ?? defaultCommaCalloutCharacters

localStore?.set(hapticFeedback, forKey: enableHapticFeedbackKey)
localStore?.set(audioFeedback, forKey: enableAudioFeedbackKey)
localStore?.set(colonCallouts, forKey: colonCalloutCharactersKey)
localStore?.set(commaCallouts, forKey: commaCalloutCharactersKey)
}
}

10 changes: 6 additions & 4 deletions hayatar.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,7 @@
ASSETCATALOG_COMPILER_INCLUDE_ALL_APPICON_ASSETS = NO;
CODE_SIGN_ENTITLEMENTS = hayatar/hayatar.entitlements;
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 16;
CURRENT_PROJECT_VERSION = 17;
DEVELOPMENT_ASSET_PATHS = "\"hayatar/Preview Content\"";
DEVELOPMENT_TEAM = 82MV322Q6H;
ENABLE_PREVIEWS = YES;
Expand Down Expand Up @@ -576,7 +576,7 @@
ASSETCATALOG_COMPILER_INCLUDE_ALL_APPICON_ASSETS = NO;
CODE_SIGN_ENTITLEMENTS = hayatar/hayatar.entitlements;
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 16;
CURRENT_PROJECT_VERSION = 17;
DEVELOPMENT_ASSET_PATHS = "\"hayatar/Preview Content\"";
DEVELOPMENT_TEAM = 82MV322Q6H;
ENABLE_PREVIEWS = YES;
Expand Down Expand Up @@ -610,7 +610,7 @@
buildSettings = {
CODE_SIGN_ENTITLEMENTS = Armenian/Armenian.entitlements;
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 16;
CURRENT_PROJECT_VERSION = 17;
DEVELOPMENT_TEAM = 82MV322Q6H;
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_FILE = Armenian/Info.plist;
Expand All @@ -636,7 +636,7 @@
buildSettings = {
CODE_SIGN_ENTITLEMENTS = Armenian/Armenian.entitlements;
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 16;
CURRENT_PROJECT_VERSION = 17;
DEVELOPMENT_TEAM = 82MV322Q6H;
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_FILE = Armenian/Info.plist;
Expand All @@ -660,6 +660,7 @@
02485AB229DFABCE00244E8F /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
APPLICATION_EXTENSION_API_ONLY = YES;
CLANG_ENABLE_MODULES = YES;
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 1;
Expand Down Expand Up @@ -696,6 +697,7 @@
02485AB329DFABCE00244E8F /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
APPLICATION_EXTENSION_API_ONLY = YES;
CLANG_ENABLE_MODULES = YES;
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 1;
Expand Down
13 changes: 12 additions & 1 deletion hayatar/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ struct ContentView: View {
@State private var showSyncConfirmationAlert = false

var body: some View {
NavigationStack {
NSLog("enableHapticFeedback: \(enableHapticFeedback), enableAudioFeedback: \(enableAudioFeedback), commaCalloutCharacters: \(commaCalloutCharacters), colonCalloutCharacters: \(colonCalloutCharacters), enableSync: \(enableSync)")


return NavigationStack {
Form {
Section("Feedback") {
Toggle(isOn: Binding(
Expand Down Expand Up @@ -47,7 +50,15 @@ struct ContentView: View {
))
}

// SyncSettingsView(enableSync: $enableSync)
SyncSettingsView(enableSync: $enableSync)
.onChange(of: enableSync) { newValue in
if newValue {
SharedDefaults.syncPreferencesToCloud()
} else {
SharedDefaults.stopSyncingWithCloud()
}
}

Section {
ResetSettingsButton(onReset: {
Expand Down
2 changes: 2 additions & 0 deletions hayatar/hayatar.entitlements
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
<dict>
<key>com.apple.developer.icloud-container-identifiers</key>
<array/>
<key>com.apple.developer.icloud-services</key>
<array/>
<key>com.apple.developer.ubiquity-kvstore-identifier</key>
<string>$(TeamIdentifierPrefix)$(CFBundleIdentifier)</string>
<key>com.apple.security.application-groups</key>
Expand Down

0 comments on commit 85e11b6

Please sign in to comment.