Change DamusUserDefaults to mirror settings from app container

... to shared container instead of migrating

This commit is a reimplementation of DamusUserDefaults that mirrors
settings from the app to the shared container (instead of migrating
values over).

This new implementation brings the benefit of being backwards compatible
with the user's settings. That is, even if the user upgrades or
downgrades between various versions and changes settings along the way,
the main settings in the app will stay consistent between Damus versions
— that is, changes to the settings would not be lost between
downgrades/upgrades

General settings test
----------------------

PASS

Device: iPhone 15 Pro simulator
iOS: 17.0.1
Damus: This commit
Setup: A device with non-standard settings
Steps:
1. Flash Damus on the device
2. Check any non-default settings that were there before. Ensure that settings remained the same. PASS
3. Change one setting (any setting) to a non-default value
4. Restart Damus
5. Ensure settings change in step 3 persisted on the device

Notification settings test
--------------------------

PASS

Device: iPhone 15 Pro simulator
iOS: 17.0.1
Damus: This commit
Setup:
- Two phones running Damus on different accounts
- Local relay with strfry-push-notify test setup
- Apple push notification test tool

Coverage:
1. Mention notifications
2. DM notifications
3. Reaction notifications
4. Repost notifications

Steps for each notification type:
1. Use the secondary phone to generate a push notification
2. Trigger the push notification (Send push notification from test tool)
3. Ensure that the notification is received on the other device
4. Turn off notifications for that notification type on settings
5. Trigger the same push notification (Resend push notification from test tool)
6. Ensure that the notification is not received on the other device
7. Turn on notifications for that notification type on settings
8. Trigger the same push notification (Resend from test tool)
9. Ensure that notification appears on the device

Result: PASS (notifications are received when enabled and not received when disabled)
Signed-off-by: Daniel D’Aquino <daniel@daquino.me>
Signed-off-by: William Casarin <jb55@jb55.com>
This commit is contained in:
Daniel D’Aquino
2023-12-01 21:26:54 +00:00
committed by William Casarin
parent aab9e97a25
commit 54674104ea
6 changed files with 121 additions and 52 deletions

View File

@@ -16,13 +16,13 @@ func setting_property_key(key: String) -> String {
}
func setting_get_property_value<T>(key: String, scoped_key: String, default_value: T) -> T {
if let loaded = DamusUserDefaults.shared.object(forKey: scoped_key) as? T {
if let loaded = DamusUserDefaults.standard.object(forKey: scoped_key) as? T {
return loaded
} else if let loaded = DamusUserDefaults.shared.object(forKey: key) as? T {
} else if let loaded = DamusUserDefaults.standard.object(forKey: key) as? T {
// If pubkey-scoped setting does not exist but the deprecated non-pubkey-scoped setting does,
// migrate the deprecated setting into the pubkey-scoped one and delete the deprecated one.
DamusUserDefaults.shared.set(loaded, forKey: scoped_key)
DamusUserDefaults.shared.removeObject(forKey: key)
DamusUserDefaults.standard.set(loaded, forKey: scoped_key)
DamusUserDefaults.standard.removeObject(forKey: key)
return loaded
} else {
return default_value
@@ -31,7 +31,7 @@ func setting_get_property_value<T>(key: String, scoped_key: String, default_valu
func setting_set_property_value<T: Equatable>(scoped_key: String, old_value: T, new_value: T) -> T? {
guard old_value != new_value else { return nil }
DamusUserDefaults.shared.set(new_value, forKey: scoped_key)
DamusUserDefaults.standard.set(new_value, forKey: scoped_key)
UserSettingsStore.shared?.objectWillChange.send()
return new_value
}
@@ -65,14 +65,14 @@ func setting_set_property_value<T: Equatable>(scoped_key: String, old_value: T,
init(key: String, default_value: T) {
self.key = pk_setting_key(UserSettingsStore.pubkey ?? .empty, key: key)
if let loaded = DamusUserDefaults.shared.string(forKey: self.key), let val = T.init(from: loaded) {
if let loaded = DamusUserDefaults.standard.string(forKey: self.key), let val = T.init(from: loaded) {
self.value = val
} else if let loaded = DamusUserDefaults.shared.string(forKey: key), let val = T.init(from: loaded) {
} else if let loaded = DamusUserDefaults.standard.string(forKey: key), let val = T.init(from: loaded) {
// If pubkey-scoped setting does not exist but the deprecated non-pubkey-scoped setting does,
// migrate the deprecated setting into the pubkey-scoped one and delete the deprecated one.
self.value = val
DamusUserDefaults.shared.set(val.to_string(), forKey: self.key)
DamusUserDefaults.shared.removeObject(forKey: key)
DamusUserDefaults.standard.set(val.to_string(), forKey: self.key)
DamusUserDefaults.standard.removeObject(forKey: key)
} else {
self.value = default_value
}
@@ -85,7 +85,7 @@ func setting_set_property_value<T: Equatable>(scoped_key: String, old_value: T,
return
}
self.value = newValue
DamusUserDefaults.shared.set(newValue.to_string(), forKey: key)
DamusUserDefaults.standard.set(newValue.to_string(), forKey: key)
UserSettingsStore.shared!.objectWillChange.send()
}
}