From 4148141e43f24fd00a3a5e57ef9fbf97a0aab37b Mon Sep 17 00:00:00 2001 From: ArtemHryhorovGeniusee Date: Tue, 30 Nov 2021 23:56:52 +0200 Subject: [PATCH 1/3] Added balance check after import --- .../create/account/CreateAccountActivity.kt | 61 ++++++++++++++----- .../create/account/CreateAccountViewModel.kt | 9 +++ 2 files changed, 54 insertions(+), 16 deletions(-) diff --git a/android/app/src/main/java/updated/mysterium/vpn/ui/create/account/CreateAccountActivity.kt b/android/app/src/main/java/updated/mysterium/vpn/ui/create/account/CreateAccountActivity.kt index 09770c687..62d411c8e 100644 --- a/android/app/src/main/java/updated/mysterium/vpn/ui/create/account/CreateAccountActivity.kt +++ b/android/app/src/main/java/updated/mysterium/vpn/ui/create/account/CreateAccountActivity.kt @@ -32,8 +32,8 @@ import java.io.InputStreamReader class CreateAccountActivity : BaseActivity() { private companion object { - const val MIME_TYPE_JSON = "application/json" - const val MIME_TYPE_BINARY_FILE = "application/octet-stream" + const val MIME_TYPE_JSON = "application/json" + const val MIME_TYPE_BINARY_FILE = "application/octet-stream" const val KEY_REQUEST_CODE = 0 const val TAG = "CreateAccountActivity" } @@ -129,31 +129,56 @@ class CreateAccountActivity : BaseActivity() { viewModel.getIdentity().observe(this) { it.onSuccess { identity -> if (identity.registered) { - navigateToHome() + navigateToConnectionOrHome(isBackTransition = false) + finish() } else { - checkFreeRegistration(identity) + checkUpdatedBalance(identity) + } + } + it.onFailure { error -> + val errorMessage = error.localizedMessage ?: error.toString() + Log.e(TAG, errorMessage) + detailedErrorPopUp(errorMessage) { + checkRegistrationStatus() } } + } + } + private fun checkUpdatedBalance(identity: IdentityModel) { + viewModel.forceBalanceUpdate(identity).observe(this) { + it.onSuccess { balanceResponse -> + if (balanceResponse.balance > 0) { + //registerAccount(identity) + } else { + checkFreeRegistration(identity) + } + } it.onFailure { error -> - Log.e(TAG, error.localizedMessage ?: error.toString()) - navigateToTopUp() + val errorMessage = error.localizedMessage ?: error.toString() + Log.e(TAG, errorMessage) + detailedErrorPopUp(errorMessage) { + checkUpdatedBalance(identity) + } } } } - private fun checkFreeRegistration(identityModel: IdentityModel) { + private fun checkFreeRegistration(identity: IdentityModel) { viewModel.isFreeRegistrationAvailable().observe(this) { - it.onSuccess { isAvailable -> - if (isAvailable) { - registerAccount(identityModel) + it.onSuccess { freeRegistration -> + if (freeRegistration) { + //registerAccount(identity) } else { navigateToTopUp() } } - - it.onFailure { - navigateToTopUp() + it.onFailure { error -> + val errorMessage = error.localizedMessage ?: error.toString() + Log.e(TAG, errorMessage) + detailedErrorPopUp(errorMessage) { + checkFreeRegistration(identity) + } } } } @@ -225,7 +250,9 @@ class CreateAccountActivity : BaseActivity() { } passwordEditText.onFocusChangeListener = View.OnFocusChangeListener { _, hasFocus -> if (hasFocus) { - bindingPasswordPopUp.passwordEditText.hint = getString(R.string.pop_up_password_account_hint) + bindingPasswordPopUp.passwordEditText.hint = getString( + R.string.pop_up_password_account_hint + ) clearErrorState(bindingPasswordPopUp) } } @@ -238,14 +265,16 @@ class CreateAccountActivity : BaseActivity() { } showPasswordImageView.setOnClickListener { val oldPosition = position - bindingPasswordPopUp.passwordEditText.transformationMethod = HideReturnsTransformationMethod.getInstance() + bindingPasswordPopUp.passwordEditText.transformationMethod = + HideReturnsTransformationMethod.getInstance() bindingPasswordPopUp.showPasswordImageView.visibility = View.INVISIBLE bindingPasswordPopUp.hidePasswordImageView.visibility = View.VISIBLE bindingPasswordPopUp.passwordEditText.setSelection(oldPosition) } hidePasswordImageView.setOnClickListener { val oldPosition = position - bindingPasswordPopUp.passwordEditText.transformationMethod = PasswordTransformationMethod.getInstance() + bindingPasswordPopUp.passwordEditText.transformationMethod = + PasswordTransformationMethod.getInstance() bindingPasswordPopUp.showPasswordImageView.visibility = View.VISIBLE bindingPasswordPopUp.hidePasswordImageView.visibility = View.INVISIBLE bindingPasswordPopUp.passwordEditText.setSelection(oldPosition) diff --git a/android/app/src/main/java/updated/mysterium/vpn/ui/create/account/CreateAccountViewModel.kt b/android/app/src/main/java/updated/mysterium/vpn/ui/create/account/CreateAccountViewModel.kt index 307ae7915..ba63eaaf0 100644 --- a/android/app/src/main/java/updated/mysterium/vpn/ui/create/account/CreateAccountViewModel.kt +++ b/android/app/src/main/java/updated/mysterium/vpn/ui/create/account/CreateAccountViewModel.kt @@ -8,6 +8,7 @@ import io.intercom.android.sdk.Intercom import io.intercom.android.sdk.UserAttributes import kotlinx.coroutines.CoroutineExceptionHandler import kotlinx.coroutines.launch +import mysterium.GetBalanceRequest import mysterium.RegisterIdentityRequest import updated.mysterium.vpn.common.extensions.liveDataResult import updated.mysterium.vpn.model.wallet.IdentityModel @@ -31,6 +32,7 @@ class CreateAccountViewModel(useCaseProvider: UseCaseProvider) : ViewModel() { private val privateKeyUseCase = useCaseProvider.privateKey() private val connectionUseCase = useCaseProvider.connection() private val loginUseCase = useCaseProvider.login() + private val balanceUseCase = useCaseProvider.balance() fun importAccount(privateKey: String, passphrase: String) = liveDataResult { privateKeyUseCase.importIdentity(privateKey, passphrase) @@ -81,6 +83,13 @@ class CreateAccountViewModel(useCaseProvider: UseCaseProvider) : ViewModel() { connectionUseCase.registrationFees() } + fun forceBalanceUpdate(identity: IdentityModel) = liveDataResult { + val balanceRequest = GetBalanceRequest().apply { + identityAddress = identity.address + } + balanceUseCase.forceBalanceUpdate(balanceRequest) + } + fun accountFlowShown() = loginUseCase.accountFlowShown() private fun registerIntercomClient(address: String) { From f1ce4e8074713af9dd6a408c47e5c39b7ac312ab Mon Sep 17 00:00:00 2001 From: ArtemHryhorovGeniusee Date: Wed, 1 Dec 2021 18:29:52 +0200 Subject: [PATCH 2/3] Separate logic of registration in VM --- .../java/updated/mysterium/vpn/di/Modules.kt | 4 + .../vpn/notification/PushReceiver.kt | 4 +- .../vpn/ui/base/RegistrationViewModel.kt | 91 ++++++++++++++ .../create/account/CreateAccountActivity.kt | 100 +++++----------- .../create/account/CreateAccountViewModel.kt | 26 ---- .../ui/prepare/top/up/PrepareTopUpActivity.kt | 46 ++++---- .../vpn/ui/private/key/PrivateKeyActivity.kt | 13 +- .../mysterium/vpn/ui/splash/SplashActivity.kt | 111 +++--------------- .../vpn/ui/splash/SplashViewModel.kt | 28 ----- .../res/layout/activity_create_account.xml | 15 +++ 10 files changed, 191 insertions(+), 247 deletions(-) create mode 100644 android/app/src/main/java/updated/mysterium/vpn/ui/base/RegistrationViewModel.kt diff --git a/android/app/src/main/java/updated/mysterium/vpn/di/Modules.kt b/android/app/src/main/java/updated/mysterium/vpn/di/Modules.kt index af77afb05..5ab460262 100644 --- a/android/app/src/main/java/updated/mysterium/vpn/di/Modules.kt +++ b/android/app/src/main/java/updated/mysterium/vpn/di/Modules.kt @@ -18,6 +18,7 @@ import updated.mysterium.vpn.notification.AppNotificationManager import updated.mysterium.vpn.ui.balance.BalanceViewModel import updated.mysterium.vpn.ui.base.AllNodesViewModel import updated.mysterium.vpn.ui.base.BaseViewModel +import updated.mysterium.vpn.ui.base.RegistrationViewModel import updated.mysterium.vpn.ui.connection.ConnectionViewModel import updated.mysterium.vpn.ui.create.account.CreateAccountViewModel import updated.mysterium.vpn.ui.favourites.FavouritesViewModel @@ -154,6 +155,9 @@ object Modules { viewModel { MenuViewModel(get()) } + viewModel { + RegistrationViewModel(get()) + } } private fun provideDatabase(context: Context) = Room.databaseBuilder( diff --git a/android/app/src/main/java/updated/mysterium/vpn/notification/PushReceiver.kt b/android/app/src/main/java/updated/mysterium/vpn/notification/PushReceiver.kt index 3992d9331..8e1e99c9c 100644 --- a/android/app/src/main/java/updated/mysterium/vpn/notification/PushReceiver.kt +++ b/android/app/src/main/java/updated/mysterium/vpn/notification/PushReceiver.kt @@ -120,9 +120,7 @@ class PushReceiver : BroadcastReceiver(), KoinComponent { Intent(context, HomeSelectionActivity::class.java) } loginUseCase.isAccountCreated() -> { - Intent(context, PrepareTopUpActivity::class.java).apply { - putExtra(PrepareTopUpActivity.IS_NEW_USER_KEY, loginUseCase.isNewUser()) - } + Intent(context, PrepareTopUpActivity::class.java) } termsUseCase.isTermsAccepted() -> { Intent(context, CreateAccountActivity::class.java) diff --git a/android/app/src/main/java/updated/mysterium/vpn/ui/base/RegistrationViewModel.kt b/android/app/src/main/java/updated/mysterium/vpn/ui/base/RegistrationViewModel.kt new file mode 100644 index 000000000..5561c06e9 --- /dev/null +++ b/android/app/src/main/java/updated/mysterium/vpn/ui/base/RegistrationViewModel.kt @@ -0,0 +1,91 @@ +package updated.mysterium.vpn.ui.base + +import android.util.Log +import androidx.lifecycle.LiveData +import androidx.lifecycle.MutableLiveData +import androidx.lifecycle.ViewModel +import androidx.lifecycle.viewModelScope +import kotlinx.coroutines.CoroutineExceptionHandler +import kotlinx.coroutines.launch +import mysterium.GetBalanceRequest +import mysterium.RegisterIdentityRequest +import updated.mysterium.vpn.model.wallet.Identity +import updated.mysterium.vpn.model.wallet.IdentityModel +import updated.mysterium.vpn.network.provider.usecase.UseCaseProvider + +class RegistrationViewModel(useCaseProvider: UseCaseProvider) : ViewModel() { + + private companion object { + const val TAG = "RegistrationViewModel" + } + + val accountRegistrationResult: LiveData + get() = _accountRegistrationResult + + val accountRegistrationError: LiveData + get() = _accountRegistrationError + + private val _accountRegistrationResult = MutableLiveData() + private val _accountRegistrationError = MutableLiveData() + + private val connectionUseCase = useCaseProvider.connection() + private val loginUseCase = useCaseProvider.login() + private val balanceUseCase = useCaseProvider.balance() + + fun tryRegisterAccount(identity: Identity? = null) { + val handler = CoroutineExceptionHandler { _, exception -> + Log.e(TAG, exception.localizedMessage ?: exception.toString()) + _accountRegistrationError.postValue(exception) + } + viewModelScope.launch(handler) { + val identityModel = if (identity == null) { + IdentityModel(connectionUseCase.getIdentity()) + } else { + IdentityModel(identity) + } + + checkRegistrationStatus(identityModel) + } + } + + private suspend fun checkRegistrationStatus(identity: IdentityModel) { + if (identity.registered) { + _accountRegistrationResult.postValue(true) + } else { + checkFreeRegistration(identity) + } + } + + private suspend fun checkFreeRegistration(identity: IdentityModel) { + val isRegistrationAvailable = loginUseCase.isFreeRegistrationAvailable(identity.address) + if (isRegistrationAvailable) { + registerAccount(identity) + } else { + checkUpdatedBalance(identity) + } + } + + private suspend fun checkUpdatedBalance(identity: IdentityModel) { + val balanceRequest = GetBalanceRequest().apply { + identityAddress = identity.address + } + val balance = balanceUseCase.forceBalanceUpdate(balanceRequest).balance + if (balance > 0) { + registerAccount(identity) + } else { + _accountRegistrationResult.postValue(false) + } + } + + private suspend fun registerAccount(identity: IdentityModel) { + val req = RegisterIdentityRequest().apply { + identityAddress = identity.address + token?.let { + this.token = it + } + } + connectionUseCase.registerIdentity(req) + connectionUseCase.registrationFees() + _accountRegistrationResult.postValue(true) + } +} diff --git a/android/app/src/main/java/updated/mysterium/vpn/ui/create/account/CreateAccountActivity.kt b/android/app/src/main/java/updated/mysterium/vpn/ui/create/account/CreateAccountActivity.kt index 62d411c8e..e84a029dc 100644 --- a/android/app/src/main/java/updated/mysterium/vpn/ui/create/account/CreateAccountActivity.kt +++ b/android/app/src/main/java/updated/mysterium/vpn/ui/create/account/CreateAccountActivity.kt @@ -20,9 +20,9 @@ import org.koin.android.ext.android.inject import updated.mysterium.vpn.App import updated.mysterium.vpn.common.extensions.hideKeyboard import updated.mysterium.vpn.common.extensions.setSelectionChangedListener -import updated.mysterium.vpn.model.wallet.IdentityModel import updated.mysterium.vpn.ui.balance.BalanceViewModel import updated.mysterium.vpn.ui.base.BaseActivity +import updated.mysterium.vpn.ui.base.RegistrationViewModel import updated.mysterium.vpn.ui.home.selection.HomeSelectionActivity import updated.mysterium.vpn.ui.prepare.top.up.PrepareTopUpActivity import updated.mysterium.vpn.ui.private.key.PrivateKeyActivity @@ -40,6 +40,7 @@ class CreateAccountActivity : BaseActivity() { private val viewModel: CreateAccountViewModel by inject() private val balanceViewModel: BalanceViewModel by inject() + private val registrationViewModel: RegistrationViewModel by inject() private var privateKeyJson: String? = null private lateinit var binding: ActivityCreateAccountBinding private lateinit var bindingPasswordPopUp: PopUpAccountPasswordBinding @@ -83,6 +84,22 @@ class CreateAccountActivity : BaseActivity() { binding.importAccountFrame.isClickable = true showRegistrationErrorPopUp() }) + + registrationViewModel.accountRegistrationResult.observe(this) { isRegistered -> + binding.proposalsLoader.visibility = View.INVISIBLE + if (isRegistered) { + navigateToConnectionOrHome(isBackTransition = false) + finish() + } else { + navigateToTopUp() + } + } + registrationViewModel.accountRegistrationError.observe(this) { + binding.proposalsLoader.visibility = View.INVISIBLE + detailedErrorPopUp(it.localizedMessage ?: it.toString()) { + registrationViewModel.tryRegisterAccount() + } + } } private fun uploadKey() { @@ -117,82 +134,24 @@ class CreateAccountActivity : BaseActivity() { } private fun applyNewIdentity(newIdentityAddress: String) { - viewModel.applyNewIdentity(newIdentityAddress).observe(this, { - val deferredMysteriumCoreService = App.getInstance(this).deferredMysteriumCoreService - balanceViewModel.initDeferredNode(deferredMysteriumCoreService) - viewModel.accountCreated(false) - checkRegistrationStatus() - }) - } + binding.proposalsLoader.visibility = View.VISIBLE - private fun checkRegistrationStatus() { - viewModel.getIdentity().observe(this) { + viewModel.applyNewIdentity(newIdentityAddress).observe(this, { it.onSuccess { identity -> - if (identity.registered) { - navigateToConnectionOrHome(isBackTransition = false) - finish() - } else { - checkUpdatedBalance(identity) - } - } - it.onFailure { error -> - val errorMessage = error.localizedMessage ?: error.toString() - Log.e(TAG, errorMessage) - detailedErrorPopUp(errorMessage) { - checkRegistrationStatus() - } + val deferredMysteriumCoreService = + App.getInstance(this).deferredMysteriumCoreService + balanceViewModel.initDeferredNode(deferredMysteriumCoreService) + viewModel.accountCreated(false) + registrationViewModel.tryRegisterAccount(identity) } - } - } - private fun checkUpdatedBalance(identity: IdentityModel) { - viewModel.forceBalanceUpdate(identity).observe(this) { - it.onSuccess { balanceResponse -> - if (balanceResponse.balance > 0) { - //registerAccount(identity) - } else { - checkFreeRegistration(identity) - } - } it.onFailure { error -> - val errorMessage = error.localizedMessage ?: error.toString() - Log.e(TAG, errorMessage) - detailedErrorPopUp(errorMessage) { - checkUpdatedBalance(identity) + binding.proposalsLoader.visibility = View.GONE + detailedErrorPopUp(error.localizedMessage ?: error.toString()) { + applyNewIdentity(newIdentityAddress) } } - } - } - - private fun checkFreeRegistration(identity: IdentityModel) { - viewModel.isFreeRegistrationAvailable().observe(this) { - it.onSuccess { freeRegistration -> - if (freeRegistration) { - //registerAccount(identity) - } else { - navigateToTopUp() - } - } - it.onFailure { error -> - val errorMessage = error.localizedMessage ?: error.toString() - Log.e(TAG, errorMessage) - detailedErrorPopUp(errorMessage) { - checkFreeRegistration(identity) - } - } - } - } - - private fun registerAccount(identityModel: IdentityModel) { - viewModel.registerAccount(identityModel).observe(this) { - it.onSuccess { - navigateToHome() - } - - it.onFailure { - showRegistrationErrorPopUp() - } - } + }) } private fun showPasswordWrongState() { @@ -308,7 +267,6 @@ class CreateAccountActivity : BaseActivity() { private fun navigateToTopUp() { val intent = Intent(this, PrepareTopUpActivity::class.java).apply { flags = Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK - putExtra(PrepareTopUpActivity.IS_NEW_USER_KEY, false) } startActivity(intent) } diff --git a/android/app/src/main/java/updated/mysterium/vpn/ui/create/account/CreateAccountViewModel.kt b/android/app/src/main/java/updated/mysterium/vpn/ui/create/account/CreateAccountViewModel.kt index ba63eaaf0..9c659ce80 100644 --- a/android/app/src/main/java/updated/mysterium/vpn/ui/create/account/CreateAccountViewModel.kt +++ b/android/app/src/main/java/updated/mysterium/vpn/ui/create/account/CreateAccountViewModel.kt @@ -8,8 +8,6 @@ import io.intercom.android.sdk.Intercom import io.intercom.android.sdk.UserAttributes import kotlinx.coroutines.CoroutineExceptionHandler import kotlinx.coroutines.launch -import mysterium.GetBalanceRequest -import mysterium.RegisterIdentityRequest import updated.mysterium.vpn.common.extensions.liveDataResult import updated.mysterium.vpn.model.wallet.IdentityModel import updated.mysterium.vpn.model.wallet.IdentityRegistrationStatus @@ -32,7 +30,6 @@ class CreateAccountViewModel(useCaseProvider: UseCaseProvider) : ViewModel() { private val privateKeyUseCase = useCaseProvider.privateKey() private val connectionUseCase = useCaseProvider.connection() private val loginUseCase = useCaseProvider.login() - private val balanceUseCase = useCaseProvider.balance() fun importAccount(privateKey: String, passphrase: String) = liveDataResult { privateKeyUseCase.importIdentity(privateKey, passphrase) @@ -62,34 +59,11 @@ class CreateAccountViewModel(useCaseProvider: UseCaseProvider) : ViewModel() { loginUseCase.userCreateOrImportAccount(isNewUser) } - fun isFreeRegistrationAvailable() = liveDataResult { - val address = connectionUseCase.getIdentityAddress() - loginUseCase.isFreeRegistrationAvailable(address) - } - fun getIdentity() = liveDataResult { val identity = connectionUseCase.getIdentity() IdentityModel(identity) } - fun registerAccount(identity: IdentityModel) = liveDataResult { - val req = RegisterIdentityRequest().apply { - identityAddress = identity.address - token?.let { - this.token = it - } - } - connectionUseCase.registerIdentity(req) - connectionUseCase.registrationFees() - } - - fun forceBalanceUpdate(identity: IdentityModel) = liveDataResult { - val balanceRequest = GetBalanceRequest().apply { - identityAddress = identity.address - } - balanceUseCase.forceBalanceUpdate(balanceRequest) - } - fun accountFlowShown() = loginUseCase.accountFlowShown() private fun registerIntercomClient(address: String) { diff --git a/android/app/src/main/java/updated/mysterium/vpn/ui/prepare/top/up/PrepareTopUpActivity.kt b/android/app/src/main/java/updated/mysterium/vpn/ui/prepare/top/up/PrepareTopUpActivity.kt index 9e330b4d9..ce66d4d20 100644 --- a/android/app/src/main/java/updated/mysterium/vpn/ui/prepare/top/up/PrepareTopUpActivity.kt +++ b/android/app/src/main/java/updated/mysterium/vpn/ui/prepare/top/up/PrepareTopUpActivity.kt @@ -5,7 +5,6 @@ import android.os.Bundle import android.util.Log import android.view.View import android.widget.EditText -import android.widget.Toast import androidx.core.content.ContextCompat import androidx.core.widget.addTextChangedListener import network.mysterium.vpn.R @@ -13,39 +12,42 @@ import network.mysterium.vpn.databinding.ActivityPrepareTopUpBinding import network.mysterium.vpn.databinding.PopUpReferralCodeBinding import network.mysterium.vpn.databinding.PopUpRetryRegistrationBinding import org.koin.android.ext.android.inject -import updated.mysterium.vpn.analytics.AnalyticWrapper import updated.mysterium.vpn.model.pushy.PushyTopic import updated.mysterium.vpn.ui.base.BaseActivity +import updated.mysterium.vpn.ui.base.RegistrationViewModel import updated.mysterium.vpn.ui.home.selection.HomeSelectionActivity import updated.mysterium.vpn.ui.top.up.amount.TopUpAmountActivity -import updated.mysterium.vpn.ui.top.up.crypto.TopUpCryptoActivity class PrepareTopUpActivity : BaseActivity() { companion object { - const val IS_NEW_USER_KEY = "IS_NEW_USER" private const val TAG = "PrepareTopUpActivity" } private lateinit var binding: ActivityPrepareTopUpBinding private val viewModel: PrepareTopUpViewModel by inject() + private val registrationViewModel: RegistrationViewModel by inject() private var isReferralTokenUsed = false override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) binding = ActivityPrepareTopUpBinding.inflate(layoutInflater) - checkUserRegistrationStatus() setContentView(binding.root) + registrationViewModel.tryRegisterAccount() + subscribeViewModel() bindsAction() } - private fun checkUserRegistrationStatus() { - intent.extras?.let { - val isNewUser = it.getBoolean(IS_NEW_USER_KEY, true) - if (isNewUser) { - binding.referralProgram.visibility = View.VISIBLE - } else { - binding.referralProgram.visibility = View.GONE + private fun subscribeViewModel() { + registrationViewModel.accountRegistrationResult.observe(this) { isRegistered -> + if (isRegistered) { + navigateToConnectionOrHome(isBackTransition = false) + finish() + } + } + registrationViewModel.accountRegistrationError.observe(this) { + detailedErrorPopUp(it.localizedMessage ?: it.toString()) { + registrationViewModel.tryRegisterAccount() } } } @@ -101,16 +103,18 @@ class PrepareTopUpActivity : BaseActivity() { }) } } - bindingPopUp.registrationTokenEditText.onFocusChangeListener = View.OnFocusChangeListener { _, hasFocus -> - if (hasFocus) { - bindingPopUp.registrationTokenEditText.background = ContextCompat.getDrawable( - this, R.drawable.shape_password_field - ) - bindingPopUp.registrationTokenEditText.text?.clear() - bindingPopUp.registrationTokenEditText.hint = getString(R.string.pop_up_referral_hint) - bindingPopUp.errorText.visibility = View.INVISIBLE + bindingPopUp.registrationTokenEditText.onFocusChangeListener = + View.OnFocusChangeListener { _, hasFocus -> + if (hasFocus) { + bindingPopUp.registrationTokenEditText.background = ContextCompat.getDrawable( + this, R.drawable.shape_password_field + ) + bindingPopUp.registrationTokenEditText.text?.clear() + bindingPopUp.registrationTokenEditText.hint = + getString(R.string.pop_up_referral_hint) + bindingPopUp.errorText.visibility = View.INVISIBLE + } } - } dialog.show() } diff --git a/android/app/src/main/java/updated/mysterium/vpn/ui/private/key/PrivateKeyActivity.kt b/android/app/src/main/java/updated/mysterium/vpn/ui/private/key/PrivateKeyActivity.kt index 3e8646361..6744b740a 100644 --- a/android/app/src/main/java/updated/mysterium/vpn/ui/private/key/PrivateKeyActivity.kt +++ b/android/app/src/main/java/updated/mysterium/vpn/ui/private/key/PrivateKeyActivity.kt @@ -65,7 +65,10 @@ class PrivateKeyActivity : BaseActivity(), ActivityCompat.OnRequestPermissionsRe ) { ActivityCompat.requestPermissions( this, - arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE), + arrayOf( + Manifest.permission.READ_EXTERNAL_STORAGE, + Manifest.permission.WRITE_EXTERNAL_STORAGE + ), PERMISSION_REQUEST_EXT_STORAGE ) } else { @@ -141,9 +144,10 @@ class PrivateKeyActivity : BaseActivity(), ActivityCompat.OnRequestPermissionsRe dialog.dismiss() exportIdentity(passphrase) } - bindingPopUp.cancelButton.onFocusChangeListener = View.OnFocusChangeListener { _, hasFocus -> - dialog.dismiss() - } + bindingPopUp.cancelButton.onFocusChangeListener = + View.OnFocusChangeListener { _, hasFocus -> + dialog.dismiss() + } dialog.show() } @@ -151,7 +155,6 @@ class PrivateKeyActivity : BaseActivity(), ActivityCompat.OnRequestPermissionsRe viewModel.accountCreated() val intent = Intent(this, PrepareTopUpActivity::class.java).apply { flags = Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK - putExtra(PrepareTopUpActivity.IS_NEW_USER_KEY, true) } startActivity(intent) } diff --git a/android/app/src/main/java/updated/mysterium/vpn/ui/splash/SplashActivity.kt b/android/app/src/main/java/updated/mysterium/vpn/ui/splash/SplashActivity.kt index f7d688c85..e38f8b088 100644 --- a/android/app/src/main/java/updated/mysterium/vpn/ui/splash/SplashActivity.kt +++ b/android/app/src/main/java/updated/mysterium/vpn/ui/splash/SplashActivity.kt @@ -5,7 +5,6 @@ import android.app.Activity import android.content.Intent import android.net.VpnService import android.os.Bundle -import android.util.Log import android.view.Gravity import android.view.View import android.widget.Toast @@ -15,7 +14,6 @@ import androidx.lifecycle.lifecycleScope import kotlinx.coroutines.flow.collect import network.mysterium.vpn.R import network.mysterium.vpn.databinding.ActivitySplashBinding -import network.mysterium.vpn.databinding.PopUpRetryRegistrationBinding import org.koin.android.ext.android.inject import updated.mysterium.vpn.App import updated.mysterium.vpn.analytics.AnalyticEvent @@ -27,6 +25,7 @@ import updated.mysterium.vpn.model.pushy.PushyTopic import updated.mysterium.vpn.ui.balance.BalanceViewModel import updated.mysterium.vpn.ui.base.AllNodesViewModel import updated.mysterium.vpn.ui.base.BaseActivity +import updated.mysterium.vpn.ui.base.RegistrationViewModel import updated.mysterium.vpn.ui.create.account.CreateAccountActivity import updated.mysterium.vpn.ui.onboarding.OnboardingActivity import updated.mysterium.vpn.ui.prepare.top.up.PrepareTopUpActivity @@ -35,15 +34,12 @@ import updated.mysterium.vpn.ui.wallet.ExchangeRateViewModel class SplashActivity : BaseActivity() { - private companion object { - const val TAG = "SplashActivity" - } - private lateinit var binding: ActivitySplashBinding private val balanceViewModel: BalanceViewModel by inject() private val viewModel: SplashViewModel by inject() private val allNodesViewModel: AllNodesViewModel by inject() private val exchangeRateViewModel: ExchangeRateViewModel by inject() + private val registrationViewModel: RegistrationViewModel by inject() private val analytic: MysteriumAnalytic by inject() private var isVpnPermissionGranted = false private var isLoadingStarted = false @@ -100,10 +96,24 @@ class SplashActivity : BaseActivity() { wifiNetworkErrorPopUp() }) + registrationViewModel.accountRegistrationResult.observe(this) { isRegistered -> + if (isRegistered) { + navigateToConnectionOrHome(isBackTransition = false) + finish() + } else { + navigateForward() + } + } + registrationViewModel.accountRegistrationError.observe(this) { + detailedErrorPopUp(it.localizedMessage ?: it.toString()) { + registrationViewModel.tryRegisterAccount() + } + } + lifecycleScope.launchWhenStarted { analytic.eventTracked.collect { event -> if (event == AnalyticEvent.STARTUP.eventName) { - navigateForward() + registrationViewModel.tryRegisterAccount() } } } @@ -149,7 +159,7 @@ class SplashActivity : BaseActivity() { navigateToTerms() } viewModel.isTopUpFlowShown() -> { - checkRegistrationStatus() + navigateToConnectionOrHome(isBackTransition = false) } viewModel.isAccountCreated() -> { navigateToTopUp() @@ -160,90 +170,6 @@ class SplashActivity : BaseActivity() { } } - private fun checkRegistrationStatus() { - viewModel.getIdentity().observe(this) { - it.onSuccess { identity -> - if (identity.registered) { - navigateToConnectionOrHome(isBackTransition = false) - finish() - } else { - checkUpdatedBalance() - } - } - it.onFailure { error -> - val errorMessage = error.localizedMessage ?: error.toString() - Log.e(TAG, errorMessage) - detailedErrorPopUp(errorMessage) { - checkRegistrationStatus() - } - } - } - } - - private fun checkUpdatedBalance() { - viewModel.forceBalanceUpdate().observe(this) { - it.onSuccess { balanceResponse -> - if (balanceResponse.balance > 0) { - registerAccount() - } else { - checkFreeRegistration() - } - } - it.onFailure { error -> - val errorMessage = error.localizedMessage ?: error.toString() - Log.e(TAG, errorMessage) - detailedErrorPopUp(errorMessage) { - checkUpdatedBalance() - } - } - } - } - - private fun checkFreeRegistration() { - viewModel.checkFreeRegistration().observe(this) { - it.onSuccess { freeRegistration -> - if (freeRegistration) { - registerAccount() - } else { - navigateToTopUp() - } - } - it.onFailure { error -> - val errorMessage = error.localizedMessage ?: error.toString() - Log.e(TAG, errorMessage) - detailedErrorPopUp(errorMessage) { - checkFreeRegistration() - } - } - } - } - - private fun registerAccount() { - viewModel.registerAccount().observe(this) { - it.onSuccess { - navigateToConnectionOrHome(isBackTransition = false) - } - it.onFailure { error -> - Log.e(TAG, error.localizedMessage ?: error.toString()) - showRegistrationErrorPopUp() - } - } - } - - private fun showRegistrationErrorPopUp() { - val bindingPopUp = PopUpRetryRegistrationBinding.inflate(layoutInflater) - val dialog = createPopUp(bindingPopUp.root, true) - bindingPopUp.tryAgainButton.setOnClickListener { - dialog.dismiss() - viewModel.registerAccount() - } - bindingPopUp.cancelButton.setOnClickListener { - dialog.dismiss() - finish() - } - dialog.show() - } - private fun ensureVpnServicePermission() { val vpnServiceIntent = VpnService.prepare(this) if (vpnServiceIntent == null) { @@ -300,7 +226,6 @@ class SplashActivity : BaseActivity() { private fun navigateToTopUp() { val intent = Intent(this, PrepareTopUpActivity::class.java).apply { - putExtra(PrepareTopUpActivity.IS_NEW_USER_KEY, viewModel.isNewUser()) flags = Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK } startActivity(intent) diff --git a/android/app/src/main/java/updated/mysterium/vpn/ui/splash/SplashViewModel.kt b/android/app/src/main/java/updated/mysterium/vpn/ui/splash/SplashViewModel.kt index cbbf3f25d..6bcc4e010 100644 --- a/android/app/src/main/java/updated/mysterium/vpn/ui/splash/SplashViewModel.kt +++ b/android/app/src/main/java/updated/mysterium/vpn/ui/splash/SplashViewModel.kt @@ -9,8 +9,6 @@ import kotlinx.coroutines.CompletableDeferred import kotlinx.coroutines.CoroutineExceptionHandler import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch -import mysterium.GetBalanceRequest -import mysterium.RegisterIdentityRequest import updated.mysterium.vpn.common.extensions.liveDataResult import updated.mysterium.vpn.common.livedata.SingleLiveEvent import updated.mysterium.vpn.core.DeferredNode @@ -131,30 +129,4 @@ class SplashViewModel(useCaseProvider: UseCaseProvider) : ViewModel() { fun getIdentity() = liveDataResult { IdentityModel(connectionUseCase.getIdentity()) } - - fun checkFreeRegistration() = liveDataResult { - val address = connectionUseCase.getIdentity().address - loginUseCase.isFreeRegistrationAvailable(address) - } - - fun registerAccount() = liveDataResult { - val identity = connectionUseCase.getIdentity() - val identityModel = IdentityModel(identity) - val req = RegisterIdentityRequest().apply { - identityAddress = identityModel.address - token?.let { - this.token = it - } - } - connectionUseCase.registerIdentity(req) - connectionUseCase.registrationFees() - } - - fun forceBalanceUpdate() = liveDataResult { - val address = connectionUseCase.getIdentityAddress() - val balanceRequest = GetBalanceRequest().apply { - identityAddress = address - } - balanceUseCase.forceBalanceUpdate(balanceRequest) - } } diff --git a/android/app/src/main/res/layout/activity_create_account.xml b/android/app/src/main/res/layout/activity_create_account.xml index dc7e42a20..ca4df0c75 100644 --- a/android/app/src/main/res/layout/activity_create_account.xml +++ b/android/app/src/main/res/layout/activity_create_account.xml @@ -5,6 +5,21 @@ android:layout_width="match_parent" android:layout_height="match_parent"> + + Date: Wed, 1 Dec 2021 18:32:41 +0200 Subject: [PATCH 3/3] Rename loader on xml --- .../vpn/ui/create/account/CreateAccountActivity.kt | 8 ++++---- .../app/src/main/res/layout/activity_create_account.xml | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/android/app/src/main/java/updated/mysterium/vpn/ui/create/account/CreateAccountActivity.kt b/android/app/src/main/java/updated/mysterium/vpn/ui/create/account/CreateAccountActivity.kt index e84a029dc..ed0eeb9c7 100644 --- a/android/app/src/main/java/updated/mysterium/vpn/ui/create/account/CreateAccountActivity.kt +++ b/android/app/src/main/java/updated/mysterium/vpn/ui/create/account/CreateAccountActivity.kt @@ -86,7 +86,7 @@ class CreateAccountActivity : BaseActivity() { }) registrationViewModel.accountRegistrationResult.observe(this) { isRegistered -> - binding.proposalsLoader.visibility = View.INVISIBLE + binding.loader.visibility = View.INVISIBLE if (isRegistered) { navigateToConnectionOrHome(isBackTransition = false) finish() @@ -95,7 +95,7 @@ class CreateAccountActivity : BaseActivity() { } } registrationViewModel.accountRegistrationError.observe(this) { - binding.proposalsLoader.visibility = View.INVISIBLE + binding.loader.visibility = View.INVISIBLE detailedErrorPopUp(it.localizedMessage ?: it.toString()) { registrationViewModel.tryRegisterAccount() } @@ -134,7 +134,7 @@ class CreateAccountActivity : BaseActivity() { } private fun applyNewIdentity(newIdentityAddress: String) { - binding.proposalsLoader.visibility = View.VISIBLE + binding.loader.visibility = View.VISIBLE viewModel.applyNewIdentity(newIdentityAddress).observe(this, { it.onSuccess { identity -> @@ -146,7 +146,7 @@ class CreateAccountActivity : BaseActivity() { } it.onFailure { error -> - binding.proposalsLoader.visibility = View.GONE + binding.loader.visibility = View.GONE detailedErrorPopUp(error.localizedMessage ?: error.toString()) { applyNewIdentity(newIdentityAddress) } diff --git a/android/app/src/main/res/layout/activity_create_account.xml b/android/app/src/main/res/layout/activity_create_account.xml index ca4df0c75..6895c6067 100644 --- a/android/app/src/main/res/layout/activity_create_account.xml +++ b/android/app/src/main/res/layout/activity_create_account.xml @@ -6,7 +6,7 @@ android:layout_height="match_parent">