Skip to content

Commit

Permalink
[trello.com/c/siM0qcTZ] use shared userDefaults
Browse files Browse the repository at this point in the history
  • Loading branch information
StanislavDevIOS committed Sep 25, 2024
1 parent 158ce01 commit ede5f09
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 2 deletions.
4 changes: 4 additions & 0 deletions Adamant/Debug.entitlements
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
<string>$(TeamIdentifierPrefix)$(CFBundleIdentifier)</string>
<key>com.apple.security.app-sandbox</key>
<true/>
<key>com.apple.security.application-groups</key>
<array>
<string>group.adamant.adamant-messenger</string>
</array>
<key>com.apple.security.device.camera</key>
<true/>
<key>com.apple.security.network.client</key>
Expand Down
4 changes: 4 additions & 0 deletions Adamant/Release.entitlements
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
<string>$(TeamIdentifierPrefix)$(CFBundleIdentifier)</string>
<key>com.apple.security.app-sandbox</key>
<true/>
<key>com.apple.security.application-groups</key>
<array>
<string>group.adamant.adamant-messenger</string>
</array>
<key>com.apple.security.device.camera</key>
<true/>
<key>com.apple.security.network.client</key>
Expand Down
17 changes: 15 additions & 2 deletions CommonKit/Sources/CommonKit/Services/KeychainStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ public final class KeychainStore: SecuredStore {
private let oldKeychainService = "im.adamant"
private let migrationKey = "migrated"
private let migrationValue = "2"
private lazy var userDefaults = UserDefaults(suiteName: sharedGroup)

public init(secureStorage: SecureStorageProtocol) {
self.secureStorage = secureStorage

migrateUserDefaultsIfNeeded()
clearIfNeeded()
configure()
migrateIfNeeded()
Expand Down Expand Up @@ -90,11 +92,13 @@ private extension KeychainStore {
}

func clearIfNeeded() {
let isFirstRun = !UserDefaults.standard.bool(forKey: firstRun)
guard let userDefaults = userDefaults else { return }

let isFirstRun = !userDefaults.bool(forKey: firstRun)

guard isFirstRun else { return }

UserDefaults.standard.set(true, forKey: firstRun)
userDefaults.set(true, forKey: firstRun)

purgeStore()
}
Expand Down Expand Up @@ -211,6 +215,15 @@ private extension KeychainStore {
setValue(value, for: key)
}
}

func migrateUserDefaultsIfNeeded() {
let migrated = KeychainStore.keychain[migrationKey]
guard migrated != migrationValue else { return }

let value = UserDefaults.standard.bool(forKey: firstRun)
userDefaults?.set(value, forKey: firstRun)
}
}

private let firstRun = "app.firstRun"
private let sharedGroup = "group.adamant.adamant-messenger"
4 changes: 4 additions & 0 deletions MessageNotificationContentExtension/Debug.entitlements
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
<dict>
<key>com.apple.security.app-sandbox</key>
<true/>
<key>com.apple.security.application-groups</key>
<array>
<string>group.adamant.adamant-messenger</string>
</array>
<key>com.apple.security.network.client</key>
<true/>
<key>keychain-access-groups</key>
Expand Down
4 changes: 4 additions & 0 deletions MessageNotificationContentExtension/Release.entitlements
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
<dict>
<key>com.apple.security.app-sandbox</key>
<true/>
<key>com.apple.security.application-groups</key>
<array>
<string>group.adamant.adamant-messenger</string>
</array>
<key>com.apple.security.network.client</key>
<true/>
<key>keychain-access-groups</key>
Expand Down
4 changes: 4 additions & 0 deletions NotificationServiceExtension/Debug.entitlements
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
<dict>
<key>com.apple.security.app-sandbox</key>
<true/>
<key>com.apple.security.application-groups</key>
<array>
<string>group.adamant.adamant-messenger</string>
</array>
<key>com.apple.security.network.client</key>
<true/>
<key>keychain-access-groups</key>
Expand Down
4 changes: 4 additions & 0 deletions NotificationServiceExtension/Release.entitlements
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
<dict>
<key>com.apple.security.app-sandbox</key>
<true/>
<key>com.apple.security.application-groups</key>
<array>
<string>group.adamant.adamant-messenger</string>
</array>
<key>com.apple.security.network.client</key>
<true/>
<key>keychain-access-groups</key>
Expand Down
4 changes: 4 additions & 0 deletions TransferNotificationContentExtension/Debug.entitlements
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
<dict>
<key>com.apple.security.app-sandbox</key>
<true/>
<key>com.apple.security.application-groups</key>
<array>
<string>group.adamant.adamant-messenger</string>
</array>
<key>com.apple.security.network.client</key>
<true/>
<key>keychain-access-groups</key>
Expand Down
4 changes: 4 additions & 0 deletions TransferNotificationContentExtension/Release.entitlements
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
<dict>
<key>com.apple.security.app-sandbox</key>
<true/>
<key>com.apple.security.application-groups</key>
<array>
<string>group.adamant.adamant-messenger</string>
</array>
<key>com.apple.security.network.client</key>
<true/>
<key>keychain-access-groups</key>
Expand Down

0 comments on commit ede5f09

Please sign in to comment.