Skip to content

Commit

Permalink
Replace rx observable with coroutine flow
Browse files Browse the repository at this point in the history
  • Loading branch information
abdrasulov committed Sep 15, 2023
1 parent 67400fa commit 09b29de
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ import io.horizontalsystems.marketkit.models.FullCoin
import io.horizontalsystems.marketkit.models.Token
import io.horizontalsystems.marketkit.models.TokenType
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.subjects.PublishSubject
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.update

class ManageWalletsService(
private val walletManager: IWalletManager,
Expand All @@ -28,18 +30,15 @@ class ManageWalletsService(
private val account: Account?
) : Clearable {

val itemsObservable = PublishSubject.create<List<Item>>()
var items: List<Item> = listOf()
private set(value) {
field = value
itemsObservable.onNext(value)
}
private val _itemsFlow = MutableStateFlow<List<Item>>(listOf())
val itemsFlow
get() = _itemsFlow.asStateFlow()

val accountType: AccountType?
get() = account?.type

private var fullCoins = listOf<FullCoin>()
private var sortedItems = listOf<Item>()
private var items = listOf<Item>()

private val disposables = CompositeDisposable()

Expand Down Expand Up @@ -94,7 +93,7 @@ class ManageWalletsService(
}
}

sortedItems = fullCoins
items = fullCoins
.map { getItemsForFullCoin(it) }
.flatten()
.sortedWith(comparator)
Expand Down Expand Up @@ -139,7 +138,7 @@ class ManageWalletsService(
}

private fun syncState() {
items = sortedItems
_itemsFlow.update { items }
}

private fun handleUpdated(wallets: List<Wallet>) {
Expand All @@ -155,7 +154,7 @@ class ManageWalletsService(
}

private fun updateSortedItems(token: Token, enable: Boolean) {
sortedItems = sortedItems.map { item ->
items = items.map { item ->
if (item.token == token) {
item.copy(enabled = enable)
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ package io.horizontalsystems.bankwallet.modules.managewallets

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import io.horizontalsystems.bankwallet.core.Clearable
import io.horizontalsystems.bankwallet.core.badge
import io.horizontalsystems.bankwallet.core.iconPlaceholder
import io.horizontalsystems.bankwallet.core.imageUrl
import io.horizontalsystems.bankwallet.core.subscribeIO
import io.horizontalsystems.bankwallet.modules.market.ImageSource
import io.horizontalsystems.bankwallet.modules.restoreaccount.restoreblockchains.CoinViewItem
import io.horizontalsystems.marketkit.models.Token
import io.reactivex.disposables.CompositeDisposable
import kotlinx.coroutines.launch

class ManageWalletsViewModel(
private val service: ManageWalletsService,
Expand All @@ -19,14 +19,12 @@ class ManageWalletsViewModel(

val viewItemsLiveData = MutableLiveData<List<CoinViewItem<Token>>>()

private var disposables = CompositeDisposable()

init {
service.itemsObservable
.subscribeIO { sync(it) }
.let { disposables.add(it) }

sync(service.items)
viewModelScope.launch {
service.itemsFlow.collect {
sync(it)
}
}
}

private fun sync(items: List<ManageWalletsService.Item>) {
Expand Down Expand Up @@ -63,7 +61,6 @@ class ManageWalletsViewModel(

override fun onCleared() {
clearables.forEach(Clearable::clear)
disposables.clear()
}

data class BirthdayHeightViewItem(
Expand Down

0 comments on commit 09b29de

Please sign in to comment.