Skip to content

Commit 5cdee79

Browse files
author
Ebenezer Ackon
committed
keep reactive stream open by returning Singles
1 parent 14d782a commit 5cdee79

File tree

8 files changed

+130
-201
lines changed

8 files changed

+130
-201
lines changed

app/src/main/java/jfyg/etherscan/helloetherescan/MainActivity.kt

Lines changed: 18 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package jfyg.etherscan.helloetherescan
33
import android.os.Bundle
44
import android.support.v7.app.AppCompatActivity
55
import android.util.Log
6+
import io.reactivex.android.schedulers.AndroidSchedulers
7+
import io.reactivex.rxkotlin.subscribeBy
68
import jfyg.account.Account
79
import jfyg.stat.Stat
810
import kotlinx.android.synthetic.main.activity_main.*
@@ -16,41 +18,27 @@ class MainActivity : AppCompatActivity() {
1618
setContentView(R.layout.activity_main)
1719
setSupportActionBar(toolbar)
1820

19-
//************************************************ Used To Test Library
21+
//************************************************ Used To Test Singles returned from etherscanapi Module
2022
val stat = Stat()
2123
val account = Account()
2224

25+
2326
fab.setOnClickListener {
2427

25-
//stat
26-
Log.d(TAG, "The Stat Status is ${stat.getNetworkStatus()}")
27-
Log.d(TAG, "The Stat Message is ${stat.getNetworkMessage()}")
28-
Log.d(TAG, "The current price of Ether in Btc: ${stat.getLastPriceInBtc()}")
29-
Log.d(TAG, "Timestamp price in Btc: ${stat.getBtcTimestamp()}")
30-
Log.d(TAG, "Timestamp price in Ether: ${stat.getEthTimestamp()}")
31-
Log.d(TAG, "The current price of Ether in Usd: ${stat.getLastPriceInUsd()}")
32-
Log.d(TAG, "The total supply of Ether: ${stat.getTotalSupply()}")
33-
Log.d(TAG, "The total supply of Ether in Wei: ${stat.getTotalSupplyInWei()}")
34-
35-
//account
36-
Log.d(TAG, "The Account Status is: ${account.getNetworkStatus()}")
37-
Log.d(TAG, "The Account Message is: ${account.getNetworkMessage()}")
38-
Log.d(TAG, "The Account Balance is: ${account.getBalance("0x82e4499D4b2A669831a3881d61BB24f7b620c61a")}")
39-
Log.d(TAG, "The Account(1) Balance is: ${account.getMultiBalance(threeAddresses())?.get(1)?.balance}")
40-
Log.d(TAG, "The Account Size of Blocks is: ${account.getBlocks("0x82e4499D4b2A669831a3881d61BB24f7b620c61a")?.size}")
41-
Log.d(TAG, "The Account Size of Transactions is: ${account.getTransactions("0x82e4499D4b2A669831a3881d61BB24f7b620c61a")?.size}")
42-
Log.d(TAG, "The Account Size of Internal Transactions is: ${account.getInternalTransactions("0x2c1ba59d6f58433fb1eaee7d20b26ed83bda51a3")?.size}")
43-
44-
//transactions
45-
}
46-
}
28+
//stat test
29+
stat.getLastPriceInBtc()?.observeOn(AndroidSchedulers.mainThread())
30+
?.subscribeBy {
31+
Log.d(TAG, "The current price of Ether in Btc: $it")
32+
}
4733

48-
fun threeAddresses(): ArrayList<String> {
49-
val address = ArrayList<String>()
50-
address.add("0x82e4499D4b2A669831a3881d61BB24f7b620c61a")
51-
address.add("0x63a9975ba31b0b9626b34300f7f627147df1f526")
52-
address.add("0x198ef1ec325a96cc354c7266a038be8b5c558f67")
53-
return address
34+
//account test
35+
account.getTransactions("0x2c1ba59d6f58433fb1eaee7d20b26ed83bda51a3")?.observeOn(AndroidSchedulers.mainThread())
36+
?.subscribeBy {
37+
Log.d(TAG, "The Account Size of Transactions is: ${it.size}")
38+
}
39+
40+
//transaction test
41+
}
5442
}
5543

56-
}
44+
}
Lines changed: 42 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package jfyg.account
22

3+
import io.reactivex.Single
34
import jfyg.model.Balances
45
import jfyg.model.Blocks
56
import jfyg.model.Transactions
@@ -10,38 +11,49 @@ import jfyg.utils.QueryUtils
1011
class Account : AccountContract {
1112

1213
private val query = QueryMediator()
13-
private val genericNetworkQuery = query.accountBalance("account", "balance",
14-
"0x82e4499D4b2A669831a3881d61BB24f7b620c61a", "latest")
14+
private val genericNetworkQuery = query.accountBalance("account",
15+
"balance",
16+
"0x82e4499D4b2A669831a3881d61BB24f7b620c61a",
17+
"latest")
1518

1619
private val queryUtil = QueryUtils()
1720

18-
override fun getBalance(account: String?): Double? =
19-
query.accountBalance("account", "balance",
20-
account, "latest").let { query.fetchAccountBalance()?.result?.toDouble() }
21-
22-
23-
override fun getMultiBalance(accounts: ArrayList<String>?): ArrayList<Balances>? =
24-
query.accountMultiBalance("account", "balancemulti",
25-
queryUtil.retrieveAccounts(accounts), "latest").let { query.fetchAccountMultiBalance()?.result }
26-
27-
//todo #29
28-
override fun getBlocks(account: String?): ArrayList<Blocks>? =
29-
query.accountBlock("account", "getminedblocks",
30-
account, "blocks").let { query.fetchAccountBlock()?.result }
31-
32-
//todo #29
33-
override fun getTransactions(account: String?): ArrayList<Transactions>? =
34-
query.accountTransactions("account", "txlist",
35-
account, "0", "99999999",
36-
"asc").let { query.fetchAccountTransaction()?.result }
37-
38-
override fun getInternalTransactions(account: String?): ArrayList<TransactionsInternal>? =
39-
query.accountInternalTransactions("account", "txlistinternal",
40-
account, "0", "99999999",
41-
"asc").let { query.fetchAccountInternalTransaction()?.result }
42-
43-
override fun getNetworkStatus(): String? = genericNetworkQuery.let { query.fetchAccountBalance()?.status }
44-
45-
override fun getNetworkMessage(): String? = genericNetworkQuery.let { query.fetchAccountBalance()?.message }
21+
override fun getBalance(account: String?): Single<Double>? =
22+
query.accountBalance("account",
23+
"balance",
24+
account,
25+
"latest")?.map { it.result?.toDouble() }
26+
27+
override fun getMultiBalance(accounts: ArrayList<String>?): Single<ArrayList<Balances>>? =
28+
query.accountMultiBalance("account",
29+
"balancemulti",
30+
queryUtil.retrieveAccounts(accounts),
31+
"latest")?.map { it.result }
32+
33+
override fun getBlocks(account: String?): Single<ArrayList<Blocks>>? =
34+
query.accountBlock("account",
35+
"getminedblocks",
36+
account,
37+
"blocks")?.map { it.result }
38+
39+
override fun getTransactions(account: String?): Single<ArrayList<Transactions>>? =
40+
query.accountTransactions("account",
41+
"txlist",
42+
account,
43+
"0",
44+
"99999999",
45+
"asc")?.map { it.result }
46+
47+
override fun getInternalTransactions(account: String?): Single<ArrayList<TransactionsInternal>>? =
48+
query.accountInternalTransactions("account",
49+
"txlistinternal",
50+
account,
51+
"0",
52+
"99999999",
53+
"asc")?.map { it.result }
54+
55+
override fun getNetworkStatus(): Single<String>? = genericNetworkQuery?.map { it.status }
56+
57+
override fun getNetworkMessage(): Single<String>? = genericNetworkQuery?.map { it.message }
4658

4759
}
Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package jfyg.account
22

3+
import io.reactivex.Single
34
import jfyg.model.Balances
45
import jfyg.model.Blocks
56
import jfyg.model.Transactions
@@ -10,36 +11,36 @@ internal interface AccountContract {
1011
/**
1112
* Return network status
1213
*/
13-
fun getNetworkStatus(): String?
14+
fun getNetworkStatus(): Single<String>?
1415

1516
/**
1617
* Return network message
1718
*/
18-
fun getNetworkMessage(): String?
19+
fun getNetworkMessage(): Single<String>?
1920

2021
/**
2122
* Return account balance
2223
*/
23-
fun getBalance(account: String?): Double?
24+
fun getBalance(account: String?): Single<Double>?
2425

2526
/**
2627
* Return balances of multiple accounts separated by commas
2728
*/
28-
fun getMultiBalance(accounts: ArrayList<String>?): ArrayList<Balances>?
29+
fun getMultiBalance(accounts: ArrayList<String>?): Single<ArrayList<Balances>>?
2930

3031
/**
3132
* Get list of blocks mined by address
3233
*/
33-
fun getBlocks(account: String?): ArrayList<Blocks>?
34+
fun getBlocks(account: String?): Single<ArrayList<Blocks>>?
3435

3536
/**
3637
* Get a list of 'Normal' Transactions By Address
3738
*/
38-
fun getTransactions(account: String?): ArrayList<Transactions>?
39+
fun getTransactions(account: String?): Single<ArrayList<Transactions>>?
3940

4041
/**
4142
* Get a list of 'Internal' Transactions by Address
4243
*/
43-
fun getInternalTransactions(account: String?): ArrayList<TransactionsInternal>?
44+
fun getInternalTransactions(account: String?): Single<ArrayList<TransactionsInternal>>?
4445

4546
}
Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package jfyg.queries
22

3-
import io.reactivex.disposables.Disposable
3+
import io.reactivex.Single
44
import jfyg.response.account.AccountBalanceResponse
55
import jfyg.response.account.AccountBlockResponse
66
import jfyg.response.account.AccountInternalTransactionResponse
@@ -15,23 +15,23 @@ internal interface AccountQueries {
1515
fun accountBalance(module: String?,
1616
action: String?,
1717
address: String?,
18-
tag: String?): Disposable?
18+
tag: String?): Single<AccountBalanceResponse>?
1919

2020
/**
2121
* Get Ether Balance for multiple Addresses in a single call
2222
*/
2323
fun accountMultiBalance(module: String?,
2424
action: String?,
2525
address: String?,
26-
tag: String?): Disposable?
26+
tag: String?): Single<AccountMultiBalanceResponse>?
2727

2828
/**
2929
* Get list of blocks mined by address
3030
*/
3131
fun accountBlock(module: String?,
3232
action: String?,
3333
address: String?,
34-
blocktype: String?): Disposable?
34+
blocktype: String?): Single<AccountBlockResponse>?
3535

3636
/**
3737
* Get a list of 'Normal' transactions by address
@@ -41,7 +41,7 @@ internal interface AccountQueries {
4141
address: String?,
4242
startblock: String?,
4343
endblock: String?,
44-
sort: String?): Disposable?
44+
sort: String?): Single<AccountTransactionResponse>?
4545

4646
/**
4747
* Get a list of 'Internal' transactions by address
@@ -51,16 +51,6 @@ internal interface AccountQueries {
5151
address: String?,
5252
startblock: String?,
5353
endblock: String?,
54-
sort: String?): Disposable?
55-
56-
fun handleResponse(response: AccountBalanceResponse)
57-
58-
fun handleResponse(response: AccountMultiBalanceResponse)
59-
60-
fun handleResponse(response: AccountBlockResponse)
61-
62-
fun handleResponse(response: AccountTransactionResponse)
63-
64-
fun handleResponse(response: AccountInternalTransactionResponse)
54+
sort: String?): Single<AccountInternalTransactionResponse>?
6555

6656
}

0 commit comments

Comments
 (0)