Skip to content

Commit

Permalink
Implement fee-rates status info feature.(#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
nk-the-crazy committed Nov 8, 2019
1 parent 50688d0 commit 044f6fd
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ import io.horizontalsystems.feeratekit.model.FeeRate

class MainActivity : AppCompatActivity() {

lateinit var viewModel: MainViewModel
private lateinit var viewModel: MainViewModel

lateinit var ratesTextView: TextView
lateinit var ratesRefresh: Button
private lateinit var ratesRefreshBtn: Button
private lateinit var statusInfoBtn: Button

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand All @@ -22,25 +23,41 @@ class MainActivity : AppCompatActivity() {

viewModel = ViewModelProviders.of(this).get(MainViewModel::class.java)
ratesTextView = findViewById(R.id.feeRates)
ratesRefresh = findViewById(R.id.refreshButton)
ratesRefreshBtn = findViewById(R.id.refreshButton)
statusInfoBtn = findViewById(R.id.statusInfoButton)


viewModel.feeRate.observe(this, Observer { rate->
viewModel.feeRateData.observe(this, Observer { rate ->
ratesTextView.text = "${ratesTextView.text}\n\n${getText(rate)}"
})

ratesRefresh.setOnClickListener {
ratesRefreshBtn.setOnClickListener {

ratesTextView.text = "Getting values ... "
viewModel.refresh()
}

statusInfoBtn.setOnClickListener {

ratesTextView.text = "Getting values ... "
viewModel.getStatusInfo()
}
}

private fun getText(rate: FeeRate): String {
private fun getText(data: Any): String {

if (data::class == FeeRate::class) {

return "Coin: ${rate.coin.code}\n" +
"Low: rate=${rate.lowPriority}, duration=${rate.lowPriorityDuration / 60} minutes\n" +
"Medium: rate=${rate.mediumPriority}, duration=${rate.mediumPriorityDuration / 60} minutes\n" +
"High: rate=${rate.highPriority}, duration=${rate.highPriorityDuration / 60} minutes"
val rate = data as FeeRate

return "Coin: ${rate.coin.code}\n" +
"Low: rate=${rate.lowPriority}, duration=${rate.lowPriorityDuration / 60} minutes\n" +
"Medium: rate=${rate.mediumPriority}, duration=${rate.mediumPriorityDuration / 60} minutes\n" +
"High: rate=${rate.highPriority}, duration=${rate.highPriorityDuration / 60} minutes"

} else {

return data.toString()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ import io.reactivex.disposables.CompositeDisposable


class MainViewModel : ViewModel(), FeeRateKit.Listener {
val feeRate = MutableLiveData<FeeRate>()

val feeRateData = MutableLiveData<Any>()
var compositeDisposable = CompositeDisposable()

val feeRateKit: FeeRateKit = FeeRateKit(
Expand All @@ -26,28 +25,41 @@ class MainViewModel : ViewModel(), FeeRateKit.Listener {
this
)

fun getStatusInfo() {

feeRateKit.statusInfo()?.let {
compositeDisposable.add(
it.onErrorReturn { t ->
mutableMapOf(Pair("No data:", ""))
}
.subscribeOn(Schedulers.io())
.subscribe() { t ->
feeRateData.postValue(t)
})
}
}

fun refresh() {

compositeDisposable.add(feeRateKit.getRate("BTC")
.onErrorReturn {
t ->
FeeRate(Coin.BITCOIN,0,0,0,0,0,0,0)
.onErrorReturn { t ->
FeeRate(Coin.BITCOIN, 0, 0, 0, 0, 0, 0, 0)
}
.subscribeOn(Schedulers.io())
.subscribe() {
t -> feeRate.postValue(t)
.subscribe() { t ->
feeRateData.postValue(t)
})

compositeDisposable.add(feeRateKit.getRate("ETH")
.subscribeOn(Schedulers.io())
.subscribe() {
t -> feeRate.postValue(t)
.subscribe() { t ->
feeRateData.postValue(t)
})

compositeDisposable.add(feeRateKit.getRate("BCH")
.subscribeOn(Schedulers.io())
.subscribe() {
t -> feeRate.postValue(t)
.subscribe() { t ->
feeRateData.postValue(t)
})
}

Expand Down
33 changes: 24 additions & 9 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,31 @@
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"/>

<Button
android:text="Refresh"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/refreshButton"
android:layout_marginTop="32dp"
app:layout_constraintTop_toBottomOf="@+id/feeRates"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="8dp" app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="8dp"/>
android:id="@+id/refreshButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="32dp"
android:layout_marginEnd="8dp"
android:text="Get rates"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/feeRates" />

<Button
android:id="@+id/statusInfoButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="124dp"
android:layout_marginEnd="8dp"
android:text="Get Status Info"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/feeRates" />


</androidx.constraintlayout.widget.ConstraintLayout>
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ import io.horizontalsystems.feeratekit.model.FeeRate
import io.horizontalsystems.feeratekit.providers.FeeRateProviderManager
import io.horizontalsystems.feeratekit.storage.InMemoryStorage
import io.reactivex.Single
import io.reactivex.functions.Function4

class FeeRateKit(
providerConfig: FeeProviderConfig,
private val context: Context,
var listener: Listener? = null) {
providerConfig: FeeProviderConfig,
private val context: Context,
var listener: Listener? = null
) {

interface Listener {
fun onRefresh(rate: FeeRate)
Expand Down Expand Up @@ -52,6 +54,46 @@ class FeeRateKit(
listener?.onRefresh(rate)
}

@Suppress("UNCHECKED_CAST")
private fun getStatusData(coin: Coin): Single<Any> {

return (getRate(coin) as Single<Any>).onErrorResumeNext {
Single.just(Pair(coin.name, "Error:${it.localizedMessage}"))
}
}

fun statusInfo(): Single<Map<String, Any>>? {

return Single.zip(
getStatusData(Coin.BITCOIN),
getStatusData(Coin.ETHEREUM),
getStatusData(Coin.BITCOIN_CASH),
getStatusData(Coin.DASH),
Function4<Any, Any, Any, Any, Array<Any>> { btcRate, ethRate, bchRate, dashRate ->
arrayOf(btcRate, ethRate, bchRate, dashRate)
})
.map { rates ->

val statusInfo = LinkedHashMap<String, Any>()

for (rate in rates) {
if (rate::class == FeeRate::class) {
(rate as FeeRate).let {
statusInfo.put(
it.coin.name,
mapOf(
Pair("HighPriority", it.highPriority),
Pair("MediumPriority", it.mediumPriority),
Pair("LowPriority", it.lowPriority)
)
)
}
} else statusInfo.plusAssign((rate as Pair<String,String>))
}
statusInfo
}
}

private fun getRate(coin: Coin): Single<FeeRate> {
return providerManager.getFeeRateProvider(coin).getFeeRates()
}
Expand Down

0 comments on commit 044f6fd

Please # to comment.