Disallow multiple sync accounts
This commit is contained in:
@@ -8,6 +8,7 @@ import android.text.Editable
|
||||
import android.text.TextWatcher
|
||||
import android.view.View
|
||||
import android.widget.Button
|
||||
import android.widget.Toast
|
||||
import androidx.activity.OnBackPressedCallback
|
||||
import androidx.activity.viewModels
|
||||
import androidx.core.graphics.Insets
|
||||
@@ -54,6 +55,9 @@ class SyncAuthActivity : BaseActivity<ActivitySyncAuthBinding>(), View.OnClickLi
|
||||
viewModel.onTokenObtained.observe(this, ::onTokenReceived)
|
||||
viewModel.onError.observe(this, ::onError)
|
||||
viewModel.isLoading.observe(this, ::onLoadingStateChanged)
|
||||
viewModel.onAccountAlreadyExists.observe(this) {
|
||||
onAccountAlreadyExists()
|
||||
}
|
||||
|
||||
supportFragmentManager.setFragmentResultListener(SyncHostDialogFragment.REQUEST_KEY, this, this)
|
||||
pageBackCallback.update()
|
||||
@@ -151,6 +155,16 @@ class SyncAuthActivity : BaseActivity<ActivitySyncAuthBinding>(), View.OnClickLi
|
||||
finish()
|
||||
}
|
||||
|
||||
private fun onAccountAlreadyExists() {
|
||||
Toast.makeText(this, R.string.account_already_exists, Toast.LENGTH_SHORT)
|
||||
.show()
|
||||
accountAuthenticatorResponse?.onError(
|
||||
AccountManager.ERROR_CODE_UNSUPPORTED_OPERATION,
|
||||
getString(R.string.account_already_exists),
|
||||
)
|
||||
super.finishAfterTransition()
|
||||
}
|
||||
|
||||
private class EmailTextWatcher(
|
||||
private val button: Button,
|
||||
) : TextWatcher {
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package org.koitharu.kotatsu.sync.ui
|
||||
|
||||
import android.accounts.AccountManager
|
||||
import android.content.Context
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||
@@ -19,11 +20,22 @@ class SyncAuthViewModel @Inject constructor(
|
||||
private val api: SyncAuthApi,
|
||||
) : BaseViewModel() {
|
||||
|
||||
val onAccountAlreadyExists = SingleLiveEvent<Unit>()
|
||||
val onTokenObtained = SingleLiveEvent<SyncAuthResult>()
|
||||
val host = MutableLiveData("")
|
||||
|
||||
private val defaultHost = context.getString(R.string.sync_host_default)
|
||||
|
||||
init {
|
||||
launchJob(Dispatchers.Default) {
|
||||
val am = AccountManager.get(context)
|
||||
val accounts = am.getAccountsByType(context.getString(R.string.account_type_sync))
|
||||
if (accounts.isNotEmpty()) {
|
||||
onAccountAlreadyExists.emitCall(Unit)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun obtainToken(email: String, password: String) {
|
||||
val hostValue = host.value.ifNullOrEmpty { defaultHost }
|
||||
launchLoadingJob(Dispatchers.Default) {
|
||||
|
||||
Reference in New Issue
Block a user