Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ERC 20 Token support. Coin manager sync (#141)
- Add syncer for update erc20 coins from ipfs
- Loading branch information
Showing
16 changed files
with
249 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import RxSwift | ||
|
||
class TokenSyncer: ITokenSyncer { | ||
private let disposeBag = DisposeBag() | ||
|
||
private let tokenNetworkManager: ITokenNetworkManager | ||
private let storage: ICoinStorage | ||
private let async: Bool | ||
|
||
init(tokenNetworkManager: ITokenNetworkManager, storage: ICoinStorage, async: Bool = true) { | ||
self.tokenNetworkManager = tokenNetworkManager | ||
self.storage = storage | ||
self.async = async | ||
} | ||
|
||
func sync() { | ||
var observable = Observable.zip(tokenNetworkManager.getTokens(), storage.enabledCoinsObservable().take(1), storage.allCoinsObservable().take(1)) | ||
|
||
if async { | ||
observable = observable.subscribeOn(ConcurrentDispatchQueueScheduler(qos: .background)).observeOn(MainScheduler.instance) | ||
} | ||
|
||
observable.subscribe(onNext: { [weak self] (newCoins, enabledCoins, allCoins) in | ||
self?.update(newCoins: newCoins, enabledCoins: enabledCoins, allCoins: allCoins) | ||
}) | ||
.disposed(by: disposeBag) | ||
} | ||
|
||
private func update(newCoins: [Coin], enabledCoins: [Coin], allCoins: [Coin]) { | ||
let inserted = newCoins.filter { !allCoins.contains($0) } | ||
let deleted = allCoins.filter { !(enabledCoins.contains($0) || newCoins.contains($0)) } | ||
|
||
guard !inserted.isEmpty || !deleted.isEmpty else { | ||
return | ||
} | ||
|
||
storage.update(inserted: inserted, deleted: deleted) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.