@@ -2,62 +2,196 @@ package io.horizontalsystems.ethereumkit.sample
22
33import android.arch.lifecycle.MutableLiveData
44import android.arch.lifecycle.ViewModel
5+ import android.util.Log
6+ import android.widget.Toast
57import io.horizontalsystems.ethereumkit.EthereumKit
6- import io.horizontalsystems.ethereumkit.EthereumKit.KitState
7- import io.horizontalsystems.ethereumkit.EthereumKit.NetworkType
8- import io.horizontalsystems.ethereumkit.models.Transaction
8+ import io.horizontalsystems.ethereumkit.EthereumKit.SyncState
9+ import io.horizontalsystems.ethereumkit.sample.core.Erc20Adapter
10+ import io.horizontalsystems.ethereumkit.sample.core.EthereumAdapter
11+ import io.horizontalsystems.ethereumkit.sample.core.TransactionRecord
12+ import io.reactivex.android.schedulers.AndroidSchedulers
13+ import io.reactivex.disposables.CompositeDisposable
14+ import java.math.BigDecimal
915
10- class MainViewModel : ViewModel (), EthereumKit.Listener {
16+ class MainViewModel : ViewModel () {
1117
12- val transactions = MutableLiveData <List <Transaction >>()
13- val balance = MutableLiveData <Double >()
18+ private val infuraKey = " 2a1306f1d12f4c109a4d4fb9be46b02e"
19+ private val etherscanKey = " GKNHXT22ED7PRVCKZATFZQD1YI7FK9AAYE"
20+ private val contractAddress = " 0xF559862f9265756619d5523bBC4bd8422898e97d"
21+ private val contractDecimal = 28
22+ private val testMode = true
23+
24+ private val disposables = CompositeDisposable ()
25+
26+ private var ethereumKit: EthereumKit
27+ private val erc20Adapter: Erc20Adapter
28+ private val ethereumAdapter: EthereumAdapter
29+
30+
31+
32+ val transactions = MutableLiveData <List <TransactionRecord >>()
33+ val balance = MutableLiveData <BigDecimal >()
34+ val fee = MutableLiveData <BigDecimal >()
1435 val lastBlockHeight = MutableLiveData <Int >()
15- val fee = MutableLiveData <Double >()
16- val kitState = MutableLiveData <KitState >()
36+ val etherState = MutableLiveData <SyncState >()
37+ val erc20State = MutableLiveData <SyncState >()
38+
39+ val erc20TokenBalance = MutableLiveData <BigDecimal >()
1740 val sendStatus = SingleLiveEvent <Throwable ?>()
1841
19- private var ethereumKit: EthereumKit
2042
2143 init {
22- val words = listOf (" subway" , " plate" , " brick" , " pattern" , " inform" , " used" , " oblige" , " identify" , " cherry" , " drop" , " flush" , " balance" )
23- ethereumKit = EthereumKit (words, NetworkType .Kovan )
44+ // val words = "subway plate brick pattern inform used oblige identify cherry drop flush balance".split(" ")
45+ val words = " mom year father track attend frown loyal goddess crisp abandon juice roof" .split(" " )
46+
47+ ethereumKit = EthereumKit .ethereumKit(App .instance, words, " unique-wallet-id" , testMode, infuraKey = infuraKey, etherscanKey = etherscanKey)
48+ ethereumAdapter = EthereumAdapter (ethereumKit)
49+ erc20Adapter = Erc20Adapter (ethereumKit, contractAddress, contractDecimal)
50+
51+ ethereumKit.start()
2452
25- ethereumKit.listener = this
2653
27- transactions.value = ethereumKit.transactions
28- balance.value = ethereumKit.balance
2954 fee.value = ethereumKit.fee()
55+ updateBalance()
56+ updateErc20Balance()
57+ updateState()
58+ updateErc20State()
59+ updateLastBlockHeight()
60+
61+ //
62+ // Ethereum
63+ //
64+ ethereumAdapter.transactionSubject.subscribe {
65+ updateTransactions()
66+ }.let {
67+ disposables.add(it)
68+ }
69+
70+ ethereumAdapter.balanceSubject.subscribe {
71+ updateBalance()
72+ }.let {
73+ disposables.add(it)
74+ }
75+
76+ ethereumAdapter.lastBlockHeightSubject.subscribe {
77+ updateLastBlockHeight()
78+ }.let {
79+ disposables.add(it)
80+ }
81+
82+ ethereumAdapter.syncStateUpdateSubject.subscribe {
83+ updateState()
84+ }.let {
85+ disposables.add(it)
86+ }
87+
88+ erc20Adapter.syncStateUpdateSubject.subscribe {
89+ updateErc20State()
90+ }.let {
91+ disposables.add(it)
92+ }
93+
94+ //
95+ // ERC20
96+ //
97+
98+ erc20Adapter.balanceSubject.subscribe {
99+ updateErc20Balance()
100+ }.let {
101+ disposables.add(it)
102+ }
103+
30104 ethereumKit.start()
31105 }
32106
107+ private fun updateLastBlockHeight () {
108+ lastBlockHeight.postValue(ethereumKit.lastBlockHeight)
109+ }
110+
111+ private fun updateState () {
112+ etherState.postValue(ethereumAdapter.syncState)
113+ }
114+
115+ private fun updateErc20State () {
116+ erc20State.postValue(erc20Adapter.syncState)
117+ }
118+
119+ private fun updateBalance () {
120+ balance.postValue(ethereumAdapter.balance)
121+ }
122+
123+ private fun updateErc20Balance () {
124+ erc20TokenBalance.postValue(erc20Adapter.balance)
125+ }
126+
127+ //
128+ // Ethereum
129+ //
130+
33131 fun refresh () {
34- ethereumKit.refresh ()
132+ ethereumKit.start ()
35133 fee.postValue(ethereumKit.fee())
36134 }
37135
38136 fun receiveAddress (): String {
39- return ethereumKit.receiveAddress()
137+ return ethereumKit.receiveAddress
40138 }
41139
42- fun send (address : String , amount : Double ) {
43- ethereumKit.send(address, amount) { error ->
44- sendStatus.value = error
45- }
46- }
140+ fun send (address : String , amount : BigDecimal ) {
141+ ethereumAdapter.sendSingle(address, amount)
142+ .subscribeOn(io.reactivex.schedulers.Schedulers .io())
143+ .observeOn(AndroidSchedulers .mainThread())
144+ .subscribe({
145+ // success
146+ Toast .makeText(App .instance, " Success" , Toast .LENGTH_SHORT ).show()
147+ }, {
148+ Log .e(" MainViewModel" , " send failed ${it.message} " )
149+ sendStatus.value = it
150+ })?.let { disposables.add(it) }
47151
48- override fun transactionsUpdated (inserted : List <Transaction >, updated : List <Transaction >, deleted : List <Int >) {
49- transactions.postValue(ethereumKit.transactions)
50152 }
51153
52- override fun balanceUpdated (balance : Double ) {
53- this .balance.postValue(balance)
154+ //
155+ // ERC20
156+ //
157+
158+ fun sendERC20 (address : String , amount : BigDecimal ) {
159+ erc20Adapter.sendSingle(address, amount)
160+ .subscribeOn(io.reactivex.schedulers.Schedulers .io())
161+ .observeOn(AndroidSchedulers .mainThread())
162+ .subscribe({
163+ // success
164+ Toast .makeText(App .instance, " Success" , Toast .LENGTH_SHORT ).show()
165+ }, {
166+ Log .e(" MainViewModel" , " send failed ${it.message} " )
167+ sendStatus.value = it
168+ })?.let { disposables.add(it) }
54169 }
55170
56- override fun lastBlockHeightUpdated (height : Int ) {
57- this .lastBlockHeight.postValue(height)
171+ fun filterTransactions (ethTx : Boolean ) {
172+ val txMethod = if (ethTx) ethereumAdapter.transactionsSingle() else erc20Adapter.transactionsSingle()
173+
174+ txMethod
175+ .observeOn(AndroidSchedulers .mainThread())
176+ .subscribe { txList: List <TransactionRecord > ->
177+ transactions.value = txList
178+ }.let {
179+ disposables.add(it)
180+ }
58181 }
59182
60- override fun onKitStateUpdate (state : KitState ) {
61- this .kitState.postValue(state)
183+ //
184+ // Private
185+ //
186+
187+ private fun updateTransactions () {
188+ ethereumAdapter.transactionsSingle()
189+ .observeOn(AndroidSchedulers .mainThread())
190+ .subscribe { list: List <TransactionRecord > ->
191+ transactions.value = list
192+ }.let {
193+ disposables.add(it)
194+ }
62195 }
196+
63197}
0 commit comments