Skip to content
This repository has been archived by the owner on Aug 15, 2021. It is now read-only.

Commit

Permalink
2020-08-22 Version 2.4.0: Added RxJava integration for currencies APIs
Browse files Browse the repository at this point in the history
1. Closed:
- Closed #9.
  • Loading branch information
fartem committed Aug 22, 2020
1 parent b781c04 commit b8b099b
Show file tree
Hide file tree
Showing 26 changed files with 130 additions and 220 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ android:
components:
- tools
- platform-tools
- build-tools-29.0.2
- build-tools-29.0.3
- android-29
- android-22
- extra-android-m2repository
Expand All @@ -19,7 +19,7 @@ jdk:
- oraclejdk8

before_install:
- yes | sdkmanager "build-tools;29.0.2"
- yes | sdkmanager "build-tools;29.0.3"
- chmod +x gradlew

script:
Expand Down
27 changes: 21 additions & 6 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ apply from: 'jacoco.gradle'

android {
compileSdkVersion 29
buildToolsVersion '29.0.2'
buildToolsVersion '29.0.3'
defaultConfig {
applicationId 'com.smlnskgmail.jaman.cryptotracker'
minSdkVersion 21
targetSdkVersion 29
versionCode 1
versionName '2.3.1'
versionName '2.4.0'
testInstrumentationRunner 'com.smlnskgmail.jaman.cryptotracker.runner.AndroidJacocoTestRunner'

buildConfigField 'String', 'API', "\"\""
Expand All @@ -24,6 +24,16 @@ android {
testCoverageEnabled = true
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = "1.8"
}
lintOptions {
abortOnError false
}

flavorDimensions 'build'
productFlavors {
Expand Down Expand Up @@ -62,18 +72,23 @@ android {

dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.core:core-ktx:1.2.0'
implementation 'com.google.android.material:material:1.1.0'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.core:core-ktx:1.3.1'
implementation 'com.google.android.material:material:1.2.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'androidx.recyclerview:recyclerview:1.1.0'
implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.0.0'
implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.1.0'
implementation "androidx.preference:preference:1.1.1"

// Dagger 2
implementation 'com.google.dagger:dagger:2.24'
kapt 'com.google.dagger:dagger-compiler:2.24'

// RxJava
implementation 'io.reactivex.rxjava3:rxjava:3.0.0'
implementation 'io.reactivex.rxjava3:rxandroid:3.0.0'
implementation 'com.github.akarnokd:rxjava3-retrofit-adapter:3.0.0'

// MapDB
// noinspection GradleDependency
implementation 'org.mapdb:mapdb:2.0-beta13'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class DebugCurrenciesListTest {
)
}

delay(3_000)
delay(5_000)
onView(withId(R.id.currencies_list)).check(
RecyclerViewCountAssertion(10)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import java.lang.reflect.Method;
import java.nio.charset.StandardCharsets;

@SuppressWarnings("unused")
@SuppressWarnings({"unused", "RedundantSuppression"})
public class AndroidJacocoTestRunner extends AndroidJUnitRunner {

@Override
Expand All @@ -18,6 +18,7 @@ public void finish(
Bundle results
) {
try {
@SuppressWarnings("rawtypes")
Class rt = Class.forName("org.jacoco.agent.rt.RT");
@SuppressWarnings("unchecked")
Method getAgent = rt.getMethod("getAgent");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ public class ElapsedTimeIdlingResource implements IdlingResource {

private final long startTime;
private final long waitingTime;

private ResourceCallback resourceCallback;

public ElapsedTimeIdlingResource(long waitingTime) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,17 @@ import com.smlnskgmail.jaman.cryptotracker.components.preferences.PreferencesMan
abstract class BaseThemeActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
val theme = if (isFullScreen()) {
if (isFullScreen()) {
PreferencesManager.theme(
this
).fullScreenThemeResId
} else {
PreferencesManager.theme(
this
).themeResId
}.let {
setTheme(it)
}
setTheme(theme)
super.onCreate(savedInstanceState)
setContentView(layoutResId())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.smlnskgmail.jaman.cryptotracker.components.splash
import android.content.Intent
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import com.smlnskgmail.jaman.cryptotracker.BuildConfig
import com.smlnskgmail.jaman.cryptotracker.MainActivity
import com.smlnskgmail.jaman.cryptotracker.R
Expand Down Expand Up @@ -30,7 +31,7 @@ class SplashActivity : BaseThemeActivity() {
}

private fun showLogo() {
Handler().postDelayed(
Handler(Looper.getMainLooper()).postDelayed(
{
val appIntent = Intent(
this,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,23 @@ package com.smlnskgmail.jaman.cryptotracker.model.api.currency
interface CurrencyApi {

fun currencies(
currenciesLoadResult: CurrenciesLoadResult
currenciesLoadTarget: CurrenciesLoadTarget
)

fun currencyListing(
currency: Currency,
currencyListingLoadResult: CurrencyListingLoadResult
currencyListingLoadTarget: CurrencyListingLoadTarget
)

interface CurrenciesLoadResult {
interface CurrenciesLoadTarget {

fun loaded(
currencies: List<Currency>
)

}

interface CurrencyListingLoadResult {
interface CurrencyListingLoadTarget {

fun loaded(
currencyListing: CurrencyListing?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ package com.smlnskgmail.jaman.cryptotracker.model.api.currency
interface CurrencyListing {

fun currentPrice(): CurrencyPrice
fun changeDay(): CurrencyPricePercentChangeDay
fun changeHour(): CurrencyPricePercentChangeDay

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,14 @@ package com.smlnskgmail.jaman.cryptotracker.model.api.currency
import java.text.NumberFormat
import java.util.*

class CurrencyPrice(
private val price: Float
data class CurrencyPrice(
val price: Float
) {

fun value(): Float {
return price
}

override fun toString(): String {
val numberFormat = NumberFormat.getCurrencyInstance(
return NumberFormat.getCurrencyInstance(
Locale.US
)
return numberFormat.format(
price
)
}

override fun equals(other: Any?): Boolean {
if (this === other) {
return true
}
if (javaClass != other?.javaClass) {
return false
}

other as CurrencyPrice

if (price != other.price) {
return false
}
return true
}

override fun hashCode(): Int {
return price.hashCode()
).format(price)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,12 @@ package com.smlnskgmail.jaman.cryptotracker.model.api.currency

import java.text.DecimalFormat

class CurrencyPricePercentChangeDay(
private val value: Float
data class CurrencyPricePercentChangeDay(
val value: Float
) {

fun value(): Float {
return value
}

override fun toString(): String {
return "${DecimalFormat("0.##").format(value)}%"
}

override fun equals(other: Any?): Boolean {
if (this === other) {
return true
}
if (javaClass != other?.javaClass) {
return false
}

other as CurrencyPricePercentChangeDay

if (value != other.value) {
return false
}
return true
}

override fun hashCode(): Int {
return value.hashCode()
}


}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.smlnskgmail.jaman.cryptotracker.model.impl.currency.coinmarketcap

import android.annotation.SuppressLint
import com.google.gson.GsonBuilder
import com.smlnskgmail.jaman.cryptotracker.model.api.currency.Currency
import com.smlnskgmail.jaman.cryptotracker.model.api.currency.CurrencyApi
Expand All @@ -10,17 +9,15 @@ import com.smlnskgmail.jaman.cryptotracker.model.impl.currency.coinmarketcap.des
import com.smlnskgmail.jaman.cryptotracker.model.impl.currency.coinmarketcap.retrofit.CmcCurrencyService
import com.smlnskgmail.jaman.cryptotracker.model.impl.currency.coinmarketcap.retrofit.responses.CurrencyListingResponse
import com.smlnskgmail.jaman.cryptotracker.model.impl.currency.coinmarketcap.retrofit.responses.CurrencyResponse
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import hu.akarnokd.rxjava3.retrofit.RxJava3CallAdapterFactory
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
import io.reactivex.rxjava3.schedulers.Schedulers
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory

class CmcCurrencyApi :
CurrencyApi {
class CmcCurrencyApi : CurrencyApi {

private var retrofit: Retrofit

private var currencyService: CmcCurrencyService

private val gson = GsonBuilder()
Expand All @@ -38,6 +35,7 @@ class CmcCurrencyApi :
init {
retrofit = Retrofit.Builder()
.baseUrl("https://pro-api.coinmarketcap.com/")
.addCallAdapterFactory(RxJava3CallAdapterFactory.create())
.addConverterFactory(
GsonConverterFactory.create(gson)
)
Expand All @@ -48,66 +46,26 @@ class CmcCurrencyApi :
}

override fun currencies(
currenciesLoadResult: CurrencyApi.CurrenciesLoadResult
currenciesLoadTarget: CurrencyApi.CurrenciesLoadTarget
) {
currencyService.currencies(
CurrencyType.supportedSymbols().joinToString(",")
).enqueue(
object : Callback<CurrencyResponse> {
override fun onFailure(
call: Call<CurrencyResponse>,
throwable: Throwable
) {
currenciesLoadResult.loaded(
emptyList()
)
}

override fun onResponse(
call: Call<CurrencyResponse>,
response: Response<CurrencyResponse>
) {
val body = response.body()
if (body == null) {
currenciesLoadResult.loaded(
emptyList()
)
} else {
currenciesLoadResult.loaded(
body.currencies
)
}
}
}
)
CurrencyType.supportedSymbols().joinToString(","))
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
{ currenciesLoadTarget.loaded(it.currencies) },
{ currenciesLoadTarget.loaded(emptyList()) }
)
}

override fun currencyListing(
currency: Currency,
currencyListingLoadResult: CurrencyApi.CurrencyListingLoadResult
currencyListingLoadTarget: CurrencyApi.CurrencyListingLoadTarget
) {
currencyService.listing((currency as CmcCurrency).id).enqueue(
object : Callback<CurrencyListingResponse> {
override fun onFailure(
call: Call<CurrencyListingResponse>,
throwable: Throwable
) {
currencyListingLoadResult.loaded(
null
)
}

@SuppressLint("SetTextI18n")
override fun onResponse(
call: Call<CurrencyListingResponse>,
response: Response<CurrencyListingResponse>
) {
currencyListingLoadResult.loaded(
response.body()!!.currencyListing
)
}
}
)
currencyService.listing((currency as CmcCurrency).id)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe { currencyListingLoadTarget.loaded(it.currencyListing) }
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ data class CmcCurrencyListing(
return currentPrice
}

override fun changeDay(): CurrencyPricePercentChangeDay {
override fun changeHour(): CurrencyPricePercentChangeDay {
return changeHourPrice
}

Expand Down
Loading

0 comments on commit b8b099b

Please sign in to comment.