Skip to content

Commit

Permalink
Trigger onStateUpdate only when state was changed (#193)
Browse files Browse the repository at this point in the history
  • Loading branch information
abdrasulov committed Feb 26, 2019
1 parent 4a40c1f commit 1b2af8f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class BitcoinKit(seed: ByteArray, networkType: NetworkType, walletId: String? =
// DataProvider getters
val balance get() = dataProvider.balance
val lastBlockInfo get() = dataProvider.lastBlockInfo
val syncState get() = kitStateProvider.syncState

private val peerGroup: PeerGroup
private val initialSyncer: InitialSyncer
Expand All @@ -55,6 +56,7 @@ class BitcoinKit(seed: ByteArray, networkType: NetworkType, walletId: String? =
private val transactionCreator: TransactionCreator
private val transactionBuilder: TransactionBuilder
private val dataProvider: DataProvider
private val kitStateProvider: KitStateProvider
private val unspentOutputProvider: UnspentOutputProvider
private val realmFactory = RealmFactory("bitcoinkit-${networkType.name}-$walletId")

Expand All @@ -76,8 +78,8 @@ class BitcoinKit(seed: ByteArray, networkType: NetworkType, walletId: String? =
dataProvider = DataProvider(realmFactory, this, unspentOutputProvider)
addressConverter = AddressConverter(network)
addressManager = AddressManager(realmFactory, hdWallet, addressConverter)
kitStateProvider = KitStateProvider(this)

val kitStateProvider = KitStateProvider(this)
val peerHostManager = PeerHostManager(network, realmFactory)
val transactionLinker = TransactionLinker()
val transactionExtractor = TransactionExtractor(addressConverter)
Expand Down Expand Up @@ -237,6 +239,17 @@ class BitcoinKit(seed: ByteArray, networkType: NetworkType, walletId: String? =
object Synced : KitState()
object NotSynced : KitState()
class Syncing(val progress: Double) : KitState()

override fun equals(other: Any?) = when {
this is Synced && other is Synced -> true
this is NotSynced && other is NotSynced -> true
this is Syncing && other is Syncing -> this.progress == other.progress
else -> false
}

override fun hashCode(): Int {
return javaClass.hashCode()
}
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.horizontalsystems.bitcoinkit.core

import io.horizontalsystems.bitcoinkit.BitcoinKit
import io.horizontalsystems.bitcoinkit.BitcoinKit.KitState

interface ISyncStateListener {
Expand All @@ -19,19 +20,27 @@ class KitStateProvider(private val listener: Listener) : ISyncStateListener {
private var initialBestBlockHeight = 0
private var currentBestBlockHeight = 0

var syncState: BitcoinKit.KitState = KitState.NotSynced
private set(value) {
if (value != field) {
field = value
listener.onKitStateUpdate(field)
}
}

//
// SyncStateListener implementations
//
override fun onSyncStart() {
listener.onKitStateUpdate(KitState.Syncing(0.0))
syncState = KitState.Syncing(0.0)
}

override fun onSyncStop() {
listener.onKitStateUpdate(KitState.NotSynced)
syncState = KitState.NotSynced
}

override fun onSyncFinish() {
listener.onKitStateUpdate(KitState.Synced)
syncState = KitState.Synced
}

override fun onInitialBestBlockHeightUpdate(height: Int) {
Expand All @@ -51,9 +60,9 @@ class KitStateProvider(private val listener: Listener) : ISyncStateListener {
}

if (progress >= 1) {
listener.onKitStateUpdate(KitState.Synced)
syncState = KitState.Synced
} else {
listener.onKitStateUpdate(KitState.Syncing(progress))
syncState = KitState.Syncing(progress)
}

}
Expand Down

0 comments on commit 1b2af8f

Please # to comment.