Skip to content

Commit

Permalink
refactor: use get* and observer* instead of properties in CoinJoinSer…
Browse files Browse the repository at this point in the history
…vice
  • Loading branch information
HashEngineering committed Dec 5, 2023
1 parent 2dbbcb9 commit f81b7ed
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1025,7 +1025,7 @@ void initViewModel() {
blockchainStateDao.load().observe(this, (blockchainState) -> handleBlockchainStateNotification(blockchainState, mixingStatus));
registerCrowdNodeConfirmedAddressFilter();

FlowExtKt.observe(coinJoinService.getMixingState(), this, (mixingStatus, continuation) -> {
FlowExtKt.observe(coinJoinService.observeMixingState(), this, (mixingStatus, continuation) -> {
handleBlockchainStateNotification(blockchainState, mixingStatus);
return null;
});
Expand Down
28 changes: 13 additions & 15 deletions wallet/src/de/schildbach/wallet/service/CoinJoinService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ enum class CoinJoinMode {
* Monitor the status of the CoinJoin Mixing Service
*/
interface CoinJoinService {
val mixingStatus: MixingStatus
val mixingState: Flow<MixingStatus>
val mixingProgress: Flow<Double>
suspend fun getMixingState(): MixingStatus
fun observeMixingState(): Flow<MixingStatus>
fun observeMixingProgress(): Flow<Double>
}

enum class MixingStatus {
Expand Down Expand Up @@ -122,11 +122,11 @@ class CoinJoinMixingService @Inject constructor(
private var sessionCompleteListeners: ArrayList<SessionCompleteListener> = arrayListOf()

var mode: CoinJoinMode = CoinJoinMode.NONE
override var mixingStatus: MixingStatus = MixingStatus.NOT_STARTED
private set
val _mixingState = MutableStateFlow(MixingStatus.NOT_STARTED)
override val mixingState: Flow<MixingStatus>
get() = _mixingState
private val _mixingState = MutableStateFlow(MixingStatus.NOT_STARTED)
private val _progressFlow = MutableStateFlow(0.00)

override suspend fun getMixingState(): MixingStatus = _mixingState.value
override fun observeMixingState(): Flow<MixingStatus> = _mixingState

private val coroutineScope = CoroutineScope(
Executors.newFixedThreadPool(2, ContextPropagatingThreadFactory("coinjoin-pool")).asCoroutineDispatcher()
Expand All @@ -146,9 +146,7 @@ class CoinJoinMixingService @Inject constructor(
private val updateMixingStateMutex = Mutex(locked = false)
private var exception: Throwable? = null

override val mixingProgress: Flow<Double>
get() = _progressFlow
private val _progressFlow = MutableStateFlow(0.00)
override fun observeMixingProgress(): Flow<Double> = _progressFlow

init {
blockchainStateProvider.observeNetworkStatus()
Expand Down Expand Up @@ -236,8 +234,8 @@ class CoinJoinMixingService @Inject constructor(
"coinjoin-updateState: $mode, $hasAnonymizableBalance, $networkStatus, $isSynced, ${blockChain != null}"
)
this.networkStatus = networkStatus
this.mixingStatus = mixingStatus
_mixingState.value = mixingStatus
// this.getMixingState = getMixingState
// _mixingState.value = getMixingState
this.hasAnonymizableBalance = hasAnonymizableBalance
this.isSynced = isSynced
this.mode = mode
Expand Down Expand Up @@ -268,8 +266,8 @@ class CoinJoinMixingService @Inject constructor(
) {
updateMixingStateMutex.lock()
try {
val previousMixingStatus = this.mixingStatus
this.mixingStatus = mixingStatus
val previousMixingStatus = _mixingState.value
_mixingState.value = mixingStatus
log.info("coinjoin-updateMixingState: $previousMixingStatus -> $mixingStatus")

when {
Expand Down
11 changes: 8 additions & 3 deletions wallet/src/de/schildbach/wallet/ui/SettingsViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ import de.schildbach.wallet.service.CoinJoinMode
import de.schildbach.wallet.service.CoinJoinService
import de.schildbach.wallet.service.MixingStatus
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.launch
import org.bitcoinj.utils.MonetaryFormat
import org.bitcoinj.wallet.Wallet
import org.bitcoinj.wallet.WalletEx
import org.dash.wallet.common.WalletDataProvider
Expand All @@ -29,8 +30,12 @@ class SettingsViewModel @Inject constructor(
val coinJoinMixingMode: Flow<CoinJoinMode>
get() = coinJoinConfig.observeMode()

val coinJoinMixingStatus: MixingStatus
get() = coinJoinService.mixingStatus
var coinJoinMixingStatus: MixingStatus = MixingStatus.NOT_STARTED
init {
coinJoinService.observeMixingState()
.onEach { coinJoinMixingStatus = it }
.launchIn(viewModelScope)
}

var decimalFormat: DecimalFormat = DecimalFormat("0.000")
val walletBalance: String
Expand Down
6 changes: 2 additions & 4 deletions wallet/src/de/schildbach/wallet/ui/main/MainViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,6 @@ import org.dash.wallet.common.util.head
import org.dash.wallet.common.util.toBigDecimal
import org.dash.wallet.integrations.crowdnode.transactions.FullCrowdNodeSignUpTxSet
import org.slf4j.LoggerFactory
import java.math.MathContext
import java.math.RoundingMode
import java.text.DecimalFormat
import java.util.Currency
import java.util.Locale
Expand Down Expand Up @@ -210,9 +208,9 @@ class MainViewModel @Inject constructor(
val coinJoinMode: Flow<CoinJoinMode>
get() = coinJoinConfig.observeMode()
val mixingState: Flow<MixingStatus>
get() = coinJoinService.mixingState
get() = coinJoinService.observeMixingState()
val mixingProgress: Flow<Double>
get() = coinJoinService.mixingProgress
get() = coinJoinService.observeMixingProgress()

var decimalFormat: DecimalFormat = DecimalFormat("0.000")
val walletBalance: String
Expand Down

0 comments on commit f81b7ed

Please sign in to comment.