Skip to content

Commit

Permalink
Merge pull request #521 from mysteriumnetwork/bugfix/update_balance_a…
Browse files Browse the repository at this point in the history
…fter_topup

Update account balance after top-up
  • Loading branch information
IrynaTsymbaliukGeniusee authored Feb 9, 2022
2 parents 47bb05d + 7e163ae commit 02becf0
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import org.koin.core.component.KoinComponent
import org.koin.core.component.inject
import updated.mysterium.vpn.model.payment.PaymentStatus
import updated.mysterium.vpn.network.provider.usecase.UseCaseProvider
import updated.mysterium.vpn.ui.balance.BalanceViewModel

class PaymentStatusService : Service(), KoinComponent {

Expand All @@ -22,6 +23,7 @@ class PaymentStatusService : Service(), KoinComponent {
private val useCaseProvider: UseCaseProvider by inject()
private val notificationManager: AppNotificationManager by inject()
private val paymentUseCase = useCaseProvider.payment()
private val balanceViewModel: BalanceViewModel by inject()

override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
bindPaymentCallback()
Expand All @@ -48,6 +50,7 @@ class PaymentStatusService : Service(), KoinComponent {
) {
if (paymentStatus == PaymentStatus.STATUS_PAID) {
notificationManager.showSuccessPaymentNotification(amount, currency)
balanceViewModel.forceBalanceUpdate()
} else {
notificationManager.showFailedPaymentNotification()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,20 +55,18 @@ class BalanceViewModel(useCaseProvider: UseCaseProvider) : ViewModel() {
}
}

fun getCurrentBalance() = liveDataResult {
if (balanceRequest == null) {
val handler = CoroutineExceptionHandler { _, exception ->
Log.e(TAG, exception.localizedMessage ?: exception.toString())
}
viewModelScope.launch(Dispatchers.IO + handler) {
fun forceBalanceUpdate() {
val handler = CoroutineExceptionHandler { _, exception ->
Log.e(TAG, exception.localizedMessage ?: exception.toString())
}
viewModelScope.launch(Dispatchers.IO + handler) {
if (balanceRequest == null) {
initBalanceRequest()
}
balanceRequest?.let {
_balanceLiveData.postValue(balanceUseCase.forceBalanceUpdate(it).balance)
}
}
var balance = 0.0
balanceRequest?.let {
balance = balanceUseCase.getBalance(it)
}
balance
}

private suspend fun startDeferredNode(coreService: CompletableDeferred<MysteriumCoreService>) {
Expand Down

0 comments on commit 02becf0

Please sign in to comment.