Skip to content

Commit

Permalink
Merge pull request #477 from mysteriumnetwork/bugfix/Balance-check-on…
Browse files Browse the repository at this point in the history
…-import

Balance check on import
  • Loading branch information
ArtemHryhorovGeniusee authored Dec 1, 2021
2 parents a4aec77 + 0f428b2 commit 03700ee
Show file tree
Hide file tree
Showing 10 changed files with 200 additions and 218 deletions.
4 changes: 4 additions & 0 deletions android/app/src/main/java/updated/mysterium/vpn/di/Modules.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -154,6 +155,9 @@ object Modules {
viewModel {
MenuViewModel(get())
}
viewModel {
RegistrationViewModel(get())
}
}

private fun provideDatabase(context: Context) = Room.databaseBuilder(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Boolean>
get() = _accountRegistrationResult

val accountRegistrationError: LiveData<Throwable>
get() = _accountRegistrationError

private val _accountRegistrationResult = MutableLiveData<Boolean>()
private val _accountRegistrationError = MutableLiveData<Throwable>()

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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -32,14 +32,15 @@ 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"
}

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
Expand Down Expand Up @@ -83,6 +84,22 @@ class CreateAccountActivity : BaseActivity() {
binding.importAccountFrame.isClickable = true
showRegistrationErrorPopUp()
})

registrationViewModel.accountRegistrationResult.observe(this) { isRegistered ->
binding.loader.visibility = View.INVISIBLE
if (isRegistered) {
navigateToConnectionOrHome(isBackTransition = false)
finish()
} else {
navigateToTopUp()
}
}
registrationViewModel.accountRegistrationError.observe(this) {
binding.loader.visibility = View.INVISIBLE
detailedErrorPopUp(it.localizedMessage ?: it.toString()) {
registrationViewModel.tryRegisterAccount()
}
}
}

private fun uploadKey() {
Expand Down Expand Up @@ -117,57 +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.loader.visibility = View.VISIBLE

private fun checkRegistrationStatus() {
viewModel.getIdentity().observe(this) {
viewModel.applyNewIdentity(newIdentityAddress).observe(this, {
it.onSuccess { identity ->
if (identity.registered) {
navigateToHome()
} else {
checkFreeRegistration(identity)
}
val deferredMysteriumCoreService =
App.getInstance(this).deferredMysteriumCoreService
balanceViewModel.initDeferredNode(deferredMysteriumCoreService)
viewModel.accountCreated(false)
registrationViewModel.tryRegisterAccount(identity)
}

it.onFailure { error ->
Log.e(TAG, error.localizedMessage ?: error.toString())
navigateToTopUp()
}
}
}

private fun checkFreeRegistration(identityModel: IdentityModel) {
viewModel.isFreeRegistrationAvailable().observe(this) {
it.onSuccess { isAvailable ->
if (isAvailable) {
registerAccount(identityModel)
} else {
navigateToTopUp()
binding.loader.visibility = View.GONE
detailedErrorPopUp(error.localizedMessage ?: error.toString()) {
applyNewIdentity(newIdentityAddress)
}
}

it.onFailure {
navigateToTopUp()
}
}
}

private fun registerAccount(identityModel: IdentityModel) {
viewModel.registerAccount(identityModel).observe(this) {
it.onSuccess {
navigateToHome()
}

it.onFailure {
showRegistrationErrorPopUp()
}
}
})
}

private fun showPasswordWrongState() {
Expand Down Expand Up @@ -225,7 +209,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)
}
}
Expand All @@ -238,14 +224,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)
Expand Down Expand Up @@ -279,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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import io.intercom.android.sdk.Intercom
import io.intercom.android.sdk.UserAttributes
import kotlinx.coroutines.CoroutineExceptionHandler
import kotlinx.coroutines.launch
import mysterium.RegisterIdentityRequest
import updated.mysterium.vpn.common.extensions.liveDataResult
import updated.mysterium.vpn.model.wallet.IdentityModel
import updated.mysterium.vpn.model.wallet.IdentityRegistrationStatus
Expand Down Expand Up @@ -60,27 +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 accountFlowShown() = loginUseCase.accountFlowShown()

private fun registerIntercomClient(address: String) {
Expand Down
Loading

0 comments on commit 03700ee

Please sign in to comment.