Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 3219833

Browse files
authoredMar 4, 2018
Merge pull request #6 from EbenezerGH/apikey_singleton
add apikey singleton with small error handling
2 parents 357f94e + bb389a2 commit 3219833

File tree

5 files changed

+49
-26
lines changed

5 files changed

+49
-26
lines changed
 

‎app/src/main/AndroidManifest.xml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3-
package="jfyg.etherscan.helloetherescan">
3+
package="jfyg.etherscan.helloetherescan">
44

55
<application
6+
android:name=".HelloEtherscanApplication"
67
android:allowBackup="true"
78
android:icon="@mipmap/ic_launcher"
89
android:label="@string/app_name"
@@ -14,13 +15,13 @@
1415
android:label="@string/app_name"
1516
android:theme="@style/AppTheme.NoActionBar">
1617
<intent-filter>
17-
<action android:name="android.intent.action.MAIN" />
18+
<action android:name="android.intent.action.MAIN"/>
1819

19-
<category android:name="android.intent.category.LAUNCHER" />
20+
<category android:name="android.intent.category.LAUNCHER"/>
2021
</intent-filter>
2122
</activity>
2223
</application>
2324

24-
<uses-permission android:name="android.permission.INTERNET" />
25+
<uses-permission android:name="android.permission.INTERNET"/>
2526

2627
</manifest>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package jfyg.etherscan.helloetherescan
2+
3+
import android.util.Log
4+
5+
class ApiKey private constructor() {
6+
private object Holder {
7+
val TakeOff = ApiKey()
8+
}
9+
10+
companion object {
11+
val takeOff: ApiKey by lazy { Holder.TakeOff }
12+
}
13+
14+
private var apiKey: String? = null
15+
16+
fun callApiKey(): String? {
17+
if (apiKey == null) {
18+
Log.w("", "No api key seems to be added. No record of your queries will be kept in your " +
19+
"https://etherscan.io/ console")
20+
}
21+
return ApiKey.takeOff.apiKey
22+
}
23+
24+
fun setApiKey(apiKey: String) {
25+
this.apiKey = apiKey
26+
}
27+
28+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package jfyg.etherscan.helloetherescan
2+
3+
import android.app.Application
4+
5+
class HelloEtherscanApplication : Application() {
6+
override fun onCreate() {
7+
super.onCreate()
8+
ApiKey.takeOff.setApiKey("1I7CRNU2QIU253UBPFVB5UV2C2PBDURAIYZ")
9+
}
10+
}

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

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ package jfyg.etherscan.helloetherescan
33
import android.os.Bundle
44
import android.support.v7.app.AppCompatActivity
55
import android.util.Log
6-
import android.view.Menu
7-
import android.view.MenuItem
86
import io.reactivex.android.schedulers.AndroidSchedulers
97
import io.reactivex.schedulers.Schedulers
108
import jfyg.etherscan.helloetherescan.ethereum.EthereumStat
@@ -13,6 +11,7 @@ import jfyg.etherscan.helloetherescan.network.responses.MainResponse
1311
import jfyg.etherscan.helloetherescan.network.RestClient
1412

1513
import kotlinx.android.synthetic.main.activity_main.*
14+
1615
//TODO: clean up and write tests
1716
class MainActivity : AppCompatActivity() {
1817
private val TAG = javaClass.name!!
@@ -24,21 +23,22 @@ class MainActivity : AppCompatActivity() {
2423
setContentView(R.layout.activity_main)
2524
setSupportActionBar(toolbar)
2625

26+
//~~~ For Testing
27+
/*
2728
var testing = EthereumStat()
28-
2929
fab.setOnClickListener {
3030
//testing.getTotalSupply()
3131
//testing.getTotalSupplyInWei()
3232
testing.getLastPriceInUsd()
3333
testing.getLastPriceInBtc()
3434
//queryStats("stats", "ethPrice")
3535
}
36+
*/
3637
}
3738

3839
fun queryStats(module: String, action: String) {
39-
//pass apikey that will save in preferences or singleton or something.. or application class
4040
RestClient().getQuery()
41-
.getEtherStats(module, action, "1I7CRNU2QIU253UBPFVB5UV2C2PBDURAIYZ")
41+
.getEtherStats(module, action, ApiKey.takeOff.callApiKey())
4242
.observeOn(AndroidSchedulers.mainThread())
4343
.subscribeOn(Schedulers.io())
4444
.subscribe(this::handleResponse, this::handleError)
@@ -57,20 +57,4 @@ class MainActivity : AppCompatActivity() {
5757
Log.d(TAG, "The error " + error.message)
5858
}
5959

60-
override fun onCreateOptionsMenu(menu: Menu): Boolean {
61-
// Inflate the menu; this adds items to the action bar if it is present.
62-
menuInflater.inflate(R.menu.menu_main, menu)
63-
return true
64-
}
65-
66-
override fun onOptionsItemSelected(item: MenuItem): Boolean {
67-
// Handle action bar item clicks here. The action bar will
68-
// automatically handle clicks on the Home/Up button, so long
69-
// as you specify a parent activity in AndroidManifest.xml.
70-
return when (item.itemId) {
71-
R.id.action_settings -> true
72-
else -> super.onOptionsItemSelected(item)
73-
}
74-
}
75-
7660
}

‎app/src/main/java/jfyg/etherscan/helloetherescan/network/NetworkService.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@ interface NetworkService {
1313
@GET("api")
1414
fun getEtherStats(@Query("module") module: String,
1515
@Query("action") action: String,
16-
@Query("apikey") apikey: String): Single<MainResponse>
16+
@Query("apikey") apikey: String?): Single<MainResponse>
1717
}

0 commit comments

Comments
 (0)
Please sign in to comment.