Skip to content
This repository has been archived by the owner on Oct 28, 2022. It is now read-only.

Commit

Permalink
Add syncState to provider and kit (#221)
Browse files Browse the repository at this point in the history
- Send updates only on change sync state.
- Update pods
  • Loading branch information
ant013 committed Feb 22, 2019
1 parent e5becc8 commit da46a07
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 12 deletions.
19 changes: 19 additions & 0 deletions HSBitcoinKit/HSBitcoinKit/Core/BitcoinKit.swift
Expand Up @@ -214,6 +214,10 @@ extension BitcoinKit {
return dataProvider.balance
}

public var syncState: BitcoinKit.KitState {
return kitStateProvider.syncState
}

public func transactions(fromHash: String? = nil, limit: Int? = nil) -> Single<[TransactionInfo]> {
return dataProvider.transactions(fromHash: fromHash, limit: limit)
}
Expand Down Expand Up @@ -323,3 +327,18 @@ extension BitcoinKit {
}

}

extension BitcoinKit.KitState {

public static func == (lhs: BitcoinKit.KitState, rhs: BitcoinKit.KitState) -> Bool {
switch (lhs, rhs) {
case (.synced, .synced), (.notSynced, .notSynced):
return true
case (.syncing(progress: let leftProgress), .syncing(progress: let rightProgress)):
return leftProgress == rightProgress
default:
return false
}
}

}
18 changes: 13 additions & 5 deletions HSBitcoinKit/HSBitcoinKit/Core/KitStateProvider.swift
Expand Up @@ -7,20 +7,28 @@ class KitStateProvider: IKitStateProvider {
private var initialBestBlockHeight: Int32 = 0
private var currentBestBlockHeight: Int32 = 0

private(set) var syncState: BitcoinKit.KitState = .notSynced {
didSet {
if !(oldValue == syncState) {
delegate?.handleKitStateUpdate(state: syncState)
}
}
}

}

extension KitStateProvider: ISyncStateListener {

func syncStarted() {
delegate?.handleKitStateUpdate(state: BitcoinKit.KitState.syncing(progress: 0))
syncState = .syncing(progress: 0)
}

func syncStopped() {
delegate?.handleKitStateUpdate(state: BitcoinKit.KitState.notSynced)
syncState = .notSynced
}

func syncFinished() {
delegate?.handleKitStateUpdate(state: BitcoinKit.KitState.synced)
syncState = .synced
}

func initialBestBlockHeightUpdated(height: Int32) {
Expand All @@ -44,9 +52,9 @@ extension KitStateProvider: ISyncStateListener {
}

if progress >= 1 {
delegate?.handleKitStateUpdate(state: BitcoinKit.KitState.synced)
syncState = .synced
} else {
delegate?.handleKitStateUpdate(state: BitcoinKit.KitState.syncing(progress: progress))
syncState = .syncing(progress: progress)
}
}

Expand Down
1 change: 1 addition & 0 deletions HSBitcoinKit/HSBitcoinKit/Core/Protocols.swift
Expand Up @@ -353,6 +353,7 @@ protocol IBlockSyncer: class {
}

protocol IKitStateProvider: class {
var syncState: BitcoinKit.KitState { get }
var delegate: IKitStateProviderDelegate? { get set }
}

Expand Down
Expand Up @@ -26,13 +26,32 @@ class KitStateProviderTest: XCTestCase {
syncer = nil
}

func testStartState() {
XCTAssertEqual(syncer.syncState, .notSynced)
}

func testSetState() {
syncer.syncStarted()

XCTAssertEqual(syncer.syncState, BitcoinKit.KitState.syncing(progress: 0))
}


func testIgnoreSameStateChange() {
syncer.syncStarted()
syncer.syncStarted()

verify(mockDelegate, times(1)).handleKitStateUpdate(state: equal(to: BitcoinKit.KitState.syncing(progress: 0)))
}

func testSyncStarted() {
syncer.syncStarted()

verify(mockDelegate).handleKitStateUpdate(state: equal(to: BitcoinKit.KitState.syncing(progress: 0)))
}

func testSyncStopped() {
syncer.syncStarted()
syncer.syncStopped()

verify(mockDelegate).handleKitStateUpdate(state: equal(to: BitcoinKit.KitState.notSynced))
Expand Down Expand Up @@ -83,7 +102,7 @@ class KitStateProviderTest: XCTestCase {
syncer.currentBestBlockHeightUpdated(height: 102, maxBlockHeight: 200)
syncer.currentBestBlockHeightUpdated(height: 101, maxBlockHeight: 200)

verify(mockDelegate, times(2)).handleKitStateUpdate(state: equal(to: BitcoinKit.KitState.syncing(progress: 0.02)))
verify(mockDelegate).handleKitStateUpdate(state: equal(to: BitcoinKit.KitState.syncing(progress: 0.02)))
verify(mockDelegate, never()).handleKitStateUpdate(state: equal(to: BitcoinKit.KitState.syncing(progress: 0.01)))
}

Expand Down
1 change: 1 addition & 0 deletions HSBitcoinKitDemo/HSBitcoinKitDemo/BalanceController.swift
Expand Up @@ -40,6 +40,7 @@ class BalanceController: UIViewController {
let bitcoinKit = Manager.shared.bitcoinKit!

update(balance: bitcoinKit.balance)
update(state: bitcoinKit.syncState)

if let info = bitcoinKit.lastBlockInfo {
update(lastBlockInfo: info)
Expand Down
12 changes: 6 additions & 6 deletions Podfile.lock
@@ -1,5 +1,5 @@
PODS:
- Alamofire (4.8.0)
- Alamofire (4.8.1)
- BigInt (3.1.0):
- SipHash (~> 1.2)
- Cuckoo (0.12.0)
Expand All @@ -12,11 +12,11 @@ PODS:
- Realm/Headers (3.11.2)
- RealmSwift (3.11.2):
- Realm (= 3.11.2)
- RxAtomic (4.4.0)
- RxAtomic (4.4.1)
- RxRealm (0.7.6):
- RealmSwift (~> 3.0)
- RxSwift (~> 4.0)
- RxSwift (4.4.0):
- RxSwift (4.4.1):
- RxAtomic (~> 4.4)
- SipHash (1.2.2)

Expand Down Expand Up @@ -47,17 +47,17 @@ SPEC REPOS:
- SipHash

SPEC CHECKSUMS:
Alamofire: 3ec537f71edc9804815215393ae2b1a8ea33a844
Alamofire: 16ce2c353fb72865124ddae8a57c5942388f4f11
BigInt: 76b5dfdfa3e2e478d4ffdf161aeede5502e2742f
Cuckoo: d30b6c149e52ffbcfe6b4a9543e4627d08947274
HSCryptoKit: 8679d53083d1136fc128bbf9b040976110ce1491
HSHDWalletKit: f75cd8186f00bae4f6aa76548a0b1f24c13dff72
ObjectMapper: b612bf8c8e99c4dc0bb6013a51f7c27966ed5da9
Realm: 864477d028db77f7c5a0cba64a4892ad53db128a
RealmSwift: 12b8262fc08020b47de4fd88eb3d5c9425b34ec7
RxAtomic: eacf60db868c96bfd63320e28619fe29c179656f
RxAtomic: f8d6adc1ccb87a767811269e4875887bc74dbf19
RxRealm: 5379eddd74f8d617ca7681d1f8d144af25b432b0
RxSwift: 5976ecd04fc2fefd648827c23de5e11157faa973
RxSwift: 92fcf68dfef21f3e2ab1965363d9e7b3d787597e
SipHash: fad90a4683e420c52ef28063063dbbce248ea6d4

PODFILE CHECKSUM: a41413d4ca23e8cea594d2342564cd08f3de19fb
Expand Down

0 comments on commit da46a07

Please sign in to comment.