diff options
Diffstat (limited to 'Sora/Data/Settings')
| -rw-r--r-- | Sora/Data/Settings/SettingsManager.swift | 26 |
1 files changed, 24 insertions, 2 deletions
diff --git a/Sora/Data/Settings/SettingsManager.swift b/Sora/Data/Settings/SettingsManager.swift index c824cff..39dbfee 100644 --- a/Sora/Data/Settings/SettingsManager.swift +++ b/Sora/Data/Settings/SettingsManager.swift @@ -201,12 +201,34 @@ class SettingsManager: ObservableObject { // swiftlint:disable:this type_body_l } } + private func mergedCredentialValues<Value>( + extract: (BooruProviderCredentials) -> Value, + isDefault: (Value) -> Bool + ) -> [BooruProvider: Value] { + providerCredentials.reduce(into: [BooruProvider: Value]()) { dictionary, credentials in + let key = credentials.provider + let value = extract(credentials) + + if dictionary[key] == nil { + dictionary[key] = value + } else if isDefault(dictionary[key]!) && !isDefault(value) { + dictionary[key] = value + } + } + } + var providerAPIKeys: [BooruProvider: String] { - Dictionary(uniqueKeysWithValues: providerCredentials.map { ($0.provider, $0.apiKey) }) + mergedCredentialValues( + extract: { $0.apiKey }, + isDefault: { $0.isEmpty } + ) } var providerUserIDs: [BooruProvider: Int] { - Dictionary(uniqueKeysWithValues: providerCredentials.map { ($0.provider, $0.userID) }) + mergedCredentialValues( + extract: { $0.userID }, + isDefault: { $0 == 0 } + ) } // MARK: - Initialisation |