From 7568ccdbb1cd42680752df7afa9a8a4f71bc7a2d Mon Sep 17 00:00:00 2001 From: Andy Scherzinger Date: Sun, 17 Jul 2022 16:23:34 +0200 Subject: [PATCH] split account matcher code for better readability Signed-off-by: Andy Scherzinger --- .../com/nextcloud/talk/utils/AccountUtils.kt | 57 ++++++++++--------- 1 file changed, 30 insertions(+), 27 deletions(-) diff --git a/app/src/main/java/com/nextcloud/talk/utils/AccountUtils.kt b/app/src/main/java/com/nextcloud/talk/utils/AccountUtils.kt index 31a7e9e09..3cc5fde3f 100644 --- a/app/src/main/java/com/nextcloud/talk/utils/AccountUtils.kt +++ b/app/src/main/java/com/nextcloud/talk/utils/AccountUtils.kt @@ -45,42 +45,18 @@ object AccountUtils { return findAccountsForUsers(LegacyUserEntityMapper.toModel(userEntitiesList)) } - fun findAccountsForUsers(userEntitiesList: List): List { + fun findAccountsForUsers(users: List): List { val context = NextcloudTalkApplication.sharedApplication!!.applicationContext val accMgr = AccountManager.get(context) val accounts = accMgr.getAccountsByType(context.getString(R.string.nc_import_account_type)) val accountsAvailable = ArrayList() - var importAccount: ImportAccount - var internalUserEntity: User var accountFound: Boolean for (account in accounts) { accountFound = false - for (i in userEntitiesList.indices) { - internalUserEntity = userEntitiesList[i] - importAccount = getInformationFromAccount(account) - if (importAccount.token != null) { - if (UriUtils.hasHttpProtocollPrefixed(importAccount.baseUrl)) { - if ( - internalUserEntity.username == importAccount.username && - internalUserEntity.baseUrl == importAccount.baseUrl - ) { - accountFound = true - break - } - } else { - if (internalUserEntity.username == importAccount.username && - ( - internalUserEntity.baseUrl == "http://" + importAccount.baseUrl || - internalUserEntity.baseUrl == "https://" + importAccount.baseUrl - ) - ) { - accountFound = true - break - } - } - } else { + for (user in users) { + if (matchAccounts(getInformationFromAccount(account), user)) { accountFound = true break } @@ -94,6 +70,33 @@ object AccountUtils { return accountsAvailable } + private fun matchAccounts(importAccount: ImportAccount, user: User): Boolean { + var accountFound = false + if (importAccount.token != null) { + if (UriUtils.hasHttpProtocollPrefixed(importAccount.baseUrl)) { + if ( + user.username == importAccount.username && + user.baseUrl == importAccount.baseUrl + ) { + accountFound = true + } + } else { + if (user.username == importAccount.username && + ( + user.baseUrl == "http://" + importAccount.baseUrl || + user.baseUrl == "https://" + importAccount.baseUrl + ) + ) { + accountFound = true + } + } + } else { + accountFound = true + } + + return accountFound + } + fun getAppNameBasedOnPackage(packageName: String): String { val context = NextcloudTalkApplication.sharedApplication!!.applicationContext val packageManager = context.packageManager