-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #477 from mysteriumnetwork/bugfix/Balance-check-on…
…-import Balance check on import
- Loading branch information
Showing
10 changed files
with
200 additions
and
218 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
android/app/src/main/java/updated/mysterium/vpn/ui/base/RegistrationViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.