From 0708ffcec8d5c3caa28e104173423cfcf6a7ca3f Mon Sep 17 00:00:00 2001 From: Maurice Parker Date: Wed, 23 Sep 2020 20:36:30 -0500 Subject: [PATCH] Don't allow duplicate accounts on launch. Issue #2448 --- Frameworks/Account/AccountManager.swift | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/Frameworks/Account/AccountManager.swift b/Frameworks/Account/AccountManager.swift index fd357b8cc..b0c8dca0b 100644 --- a/Frameworks/Account/AccountManager.swift +++ b/Frameworks/Account/AccountManager.swift @@ -165,6 +165,15 @@ public final class AccountManager: UnreadCountProvider { NotificationCenter.default.post(name: .UserDidDeleteAccount, object: self, userInfo: userInfo) } + public func duplicateServiceAccount(type: AccountType, username: String?) -> Bool { + for account in accounts { + if account.type == type && username == account.username { + return true + } + } + return false + } + public func existingAccount(with accountID: String) -> Account? { return accountsDictionary[accountID] } @@ -350,16 +359,24 @@ private extension AccountManager { print("Error reading Accounts folder: \(error)") return } + + filenames = filenames?.sorted() filenames?.forEach { (oneFilename) in guard oneFilename != defaultAccountFolderName else { return } if let oneAccount = loadAccount(oneFilename) { - accountsDictionary[oneAccount.accountID] = oneAccount + if !duplicateServiceAccount(oneAccount) { + accountsDictionary[oneAccount.accountID] = oneAccount + } } } } + + func duplicateServiceAccount(_ account: Account) -> Bool { + return duplicateServiceAccount(type: account.type, username: account.username) + } func sortByName(_ accounts: [Account]) -> [Account] { // LocalAccount is first.