chore: simplify check if account was already registered

This commit is contained in:
Manuel Jenny 2021-03-16 15:07:00 +01:00
parent bd2bfa9724
commit 4fd187e697
No known key found for this signature in database
GPG Key ID: 1C80FE62B2BEAA18

View File

@ -204,9 +204,6 @@ class CloudConnectionListPresenter @Inject constructor( //
prepareForSavingPCloudCloud(PCloudCloud.aCopyOf(pCloudSkeleton).withUsername(username).build()) prepareForSavingPCloudCloud(PCloudCloud.aCopyOf(pCloudSkeleton).withUsername(username).build())
} }
}) })
Log.d("pCloud", "Account access granted, authData:\n$authData")
} }
AuthorizationResult.ACCESS_DENIED -> //TODO: Add proper handling for denied grants. AuthorizationResult.ACCESS_DENIED -> //TODO: Add proper handling for denied grants.
Log.d("pCloud", "Account access denied") Log.d("pCloud", "Account access denied")
@ -226,20 +223,14 @@ class CloudConnectionListPresenter @Inject constructor( //
.withCloudType(CloudTypeModel.valueOf(selectedCloudType.get())) // .withCloudType(CloudTypeModel.valueOf(selectedCloudType.get())) //
.run(object : DefaultResultHandler<List<Cloud>>() { .run(object : DefaultResultHandler<List<Cloud>>() {
override fun onSuccess(clouds: List<Cloud>) { override fun onSuccess(clouds: List<Cloud>) {
// here check if a cloud with the same mail adress already exists, clouds.firstOrNull {
// if so update (in case of the token changed) else create a new one
val existingPCloudCloud: PCloudCloud? = clouds.firstOrNull {
(it as PCloudCloud).username() == cloud.username() (it as PCloudCloud).username() == cloud.username()
} as PCloudCloud? }?.let { it as PCloudCloud
saveCloud(PCloudCloud.aCopyOf(it) //
if (existingPCloudCloud != null && existingPCloudCloud.accessToken() != cloud.accessToken()) {
saveCloud(PCloudCloud.aCopyOf(existingPCloudCloud) //
.withUrl(cloud.url()) .withUrl(cloud.url())
.withAccessToken(cloud.accessToken()) .withAccessToken(it.accessToken())
.build()) .build())
} else if (existingPCloudCloud == null) { } ?: saveCloud(cloud)
saveCloud(cloud);
}
} }
}) })
} }