From b8b099b45fde0ccbeee172903e717669b0ed33aa Mon Sep 17 00:00:00 2001 From: fartem Date: Sat, 22 Aug 2020 13:20:01 +0300 Subject: [PATCH] 2020-08-22 Version 2.4.0: Added RxJava integration for currencies APIs 1. Closed: - Closed #9. --- .travis.yml | 4 +- app/build.gradle | 27 +++++-- .../debug/DebugCurrenciesListTest.kt | 2 +- .../runner/AndroidJacocoTestRunner.java | 3 +- .../support/ElapsedTimeIdlingResource.java | 1 + .../components/BaseThemeActivity.kt | 5 +- .../components/splash/SplashActivity.kt | 3 +- .../model/api/currency/CurrencyApi.kt | 8 +- .../model/api/currency/CurrencyListing.kt | 2 +- .../model/api/currency/CurrencyPrice.kt | 35 +-------- .../currency/CurrencyPricePercentChangeDay.kt | 29 +------ .../currency/coinmarketcap/CmcCurrencyApi.kt | 78 +++++-------------- .../coinmarketcap/CmcCurrencyListing.kt | 2 +- .../cache/CmcCurrencyMapDbInstanceProvider.kt | 4 +- .../CurrencyResponseDeserializer.kt | 3 - .../retrofit/CmcCurrencyService.kt | 6 +- .../impl/currency/debug/DebugCurrency.kt | 2 +- .../impl/currency/debug/DebugCurrencyApi.kt | 38 ++++----- .../list/CurrenciesListPresenterImpl.kt | 62 +++++++-------- .../view/list/recycler/CurrencyHolder.kt | 22 +++--- app/src/main/res/layout/activity_splash.xml | 4 +- app/src/main/res/layout/item_currency.xml | 2 +- .../api/CurrencyPricePercentChangeDayTest.kt | 2 +- .../model/api/CurrencyPriceTest.kt | 2 +- .../coinmarketcap/CmcCurrencyListingTest.kt | 2 +- build.gradle | 2 +- 26 files changed, 130 insertions(+), 220 deletions(-) diff --git a/.travis.yml b/.travis.yml index 36ae712..877815b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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 @@ -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: diff --git a/app/build.gradle b/app/build.gradle index a1062a6..e3d1b9b 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -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', "\"\"" @@ -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 { @@ -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' diff --git a/app/src/androidTest/java/com/smlnskgmail/jaman/cryptotracker/debug/DebugCurrenciesListTest.kt b/app/src/androidTest/java/com/smlnskgmail/jaman/cryptotracker/debug/DebugCurrenciesListTest.kt index f448c1f..55a4a67 100644 --- a/app/src/androidTest/java/com/smlnskgmail/jaman/cryptotracker/debug/DebugCurrenciesListTest.kt +++ b/app/src/androidTest/java/com/smlnskgmail/jaman/cryptotracker/debug/DebugCurrenciesListTest.kt @@ -35,7 +35,7 @@ class DebugCurrenciesListTest { ) } - delay(3_000) + delay(5_000) onView(withId(R.id.currencies_list)).check( RecyclerViewCountAssertion(10) ) diff --git a/app/src/androidTest/java/com/smlnskgmail/jaman/cryptotracker/runner/AndroidJacocoTestRunner.java b/app/src/androidTest/java/com/smlnskgmail/jaman/cryptotracker/runner/AndroidJacocoTestRunner.java index 86ec6ea..9acce98 100644 --- a/app/src/androidTest/java/com/smlnskgmail/jaman/cryptotracker/runner/AndroidJacocoTestRunner.java +++ b/app/src/androidTest/java/com/smlnskgmail/jaman/cryptotracker/runner/AndroidJacocoTestRunner.java @@ -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 @@ -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"); diff --git a/app/src/androidTest/java/com/smlnskgmail/jaman/cryptotracker/support/ElapsedTimeIdlingResource.java b/app/src/androidTest/java/com/smlnskgmail/jaman/cryptotracker/support/ElapsedTimeIdlingResource.java index b90de3a..d03d9f6 100644 --- a/app/src/androidTest/java/com/smlnskgmail/jaman/cryptotracker/support/ElapsedTimeIdlingResource.java +++ b/app/src/androidTest/java/com/smlnskgmail/jaman/cryptotracker/support/ElapsedTimeIdlingResource.java @@ -6,6 +6,7 @@ public class ElapsedTimeIdlingResource implements IdlingResource { private final long startTime; private final long waitingTime; + private ResourceCallback resourceCallback; public ElapsedTimeIdlingResource(long waitingTime) { diff --git a/app/src/main/java/com/smlnskgmail/jaman/cryptotracker/components/BaseThemeActivity.kt b/app/src/main/java/com/smlnskgmail/jaman/cryptotracker/components/BaseThemeActivity.kt index 52d55ed..143a185 100644 --- a/app/src/main/java/com/smlnskgmail/jaman/cryptotracker/components/BaseThemeActivity.kt +++ b/app/src/main/java/com/smlnskgmail/jaman/cryptotracker/components/BaseThemeActivity.kt @@ -7,7 +7,7 @@ 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 @@ -15,8 +15,9 @@ abstract class BaseThemeActivity : AppCompatActivity() { PreferencesManager.theme( this ).themeResId + }.let { + setTheme(it) } - setTheme(theme) super.onCreate(savedInstanceState) setContentView(layoutResId()) } diff --git a/app/src/main/java/com/smlnskgmail/jaman/cryptotracker/components/splash/SplashActivity.kt b/app/src/main/java/com/smlnskgmail/jaman/cryptotracker/components/splash/SplashActivity.kt index 3f49b07..8ac77f2 100644 --- a/app/src/main/java/com/smlnskgmail/jaman/cryptotracker/components/splash/SplashActivity.kt +++ b/app/src/main/java/com/smlnskgmail/jaman/cryptotracker/components/splash/SplashActivity.kt @@ -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 @@ -30,7 +31,7 @@ class SplashActivity : BaseThemeActivity() { } private fun showLogo() { - Handler().postDelayed( + Handler(Looper.getMainLooper()).postDelayed( { val appIntent = Intent( this, diff --git a/app/src/main/java/com/smlnskgmail/jaman/cryptotracker/model/api/currency/CurrencyApi.kt b/app/src/main/java/com/smlnskgmail/jaman/cryptotracker/model/api/currency/CurrencyApi.kt index 6b8bbb9..c789b98 100644 --- a/app/src/main/java/com/smlnskgmail/jaman/cryptotracker/model/api/currency/CurrencyApi.kt +++ b/app/src/main/java/com/smlnskgmail/jaman/cryptotracker/model/api/currency/CurrencyApi.kt @@ -3,15 +3,15 @@ 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 @@ -19,7 +19,7 @@ interface CurrencyApi { } - interface CurrencyListingLoadResult { + interface CurrencyListingLoadTarget { fun loaded( currencyListing: CurrencyListing? diff --git a/app/src/main/java/com/smlnskgmail/jaman/cryptotracker/model/api/currency/CurrencyListing.kt b/app/src/main/java/com/smlnskgmail/jaman/cryptotracker/model/api/currency/CurrencyListing.kt index 5c3bf5e..5856694 100644 --- a/app/src/main/java/com/smlnskgmail/jaman/cryptotracker/model/api/currency/CurrencyListing.kt +++ b/app/src/main/java/com/smlnskgmail/jaman/cryptotracker/model/api/currency/CurrencyListing.kt @@ -3,6 +3,6 @@ package com.smlnskgmail.jaman.cryptotracker.model.api.currency interface CurrencyListing { fun currentPrice(): CurrencyPrice - fun changeDay(): CurrencyPricePercentChangeDay + fun changeHour(): CurrencyPricePercentChangeDay } diff --git a/app/src/main/java/com/smlnskgmail/jaman/cryptotracker/model/api/currency/CurrencyPrice.kt b/app/src/main/java/com/smlnskgmail/jaman/cryptotracker/model/api/currency/CurrencyPrice.kt index 8187c1e..8068e3b 100644 --- a/app/src/main/java/com/smlnskgmail/jaman/cryptotracker/model/api/currency/CurrencyPrice.kt +++ b/app/src/main/java/com/smlnskgmail/jaman/cryptotracker/model/api/currency/CurrencyPrice.kt @@ -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) } } diff --git a/app/src/main/java/com/smlnskgmail/jaman/cryptotracker/model/api/currency/CurrencyPricePercentChangeDay.kt b/app/src/main/java/com/smlnskgmail/jaman/cryptotracker/model/api/currency/CurrencyPricePercentChangeDay.kt index aece908..f419f2e 100644 --- a/app/src/main/java/com/smlnskgmail/jaman/cryptotracker/model/api/currency/CurrencyPricePercentChangeDay.kt +++ b/app/src/main/java/com/smlnskgmail/jaman/cryptotracker/model/api/currency/CurrencyPricePercentChangeDay.kt @@ -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() - } - - } diff --git a/app/src/main/java/com/smlnskgmail/jaman/cryptotracker/model/impl/currency/coinmarketcap/CmcCurrencyApi.kt b/app/src/main/java/com/smlnskgmail/jaman/cryptotracker/model/impl/currency/coinmarketcap/CmcCurrencyApi.kt index 87d08ab..ec2c1a2 100644 --- a/app/src/main/java/com/smlnskgmail/jaman/cryptotracker/model/impl/currency/coinmarketcap/CmcCurrencyApi.kt +++ b/app/src/main/java/com/smlnskgmail/jaman/cryptotracker/model/impl/currency/coinmarketcap/CmcCurrencyApi.kt @@ -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 @@ -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() @@ -38,6 +35,7 @@ class CmcCurrencyApi : init { retrofit = Retrofit.Builder() .baseUrl("https://pro-api.coinmarketcap.com/") + .addCallAdapterFactory(RxJava3CallAdapterFactory.create()) .addConverterFactory( GsonConverterFactory.create(gson) ) @@ -48,66 +46,26 @@ class CmcCurrencyApi : } override fun currencies( - currenciesLoadResult: CurrencyApi.CurrenciesLoadResult + currenciesLoadTarget: CurrencyApi.CurrenciesLoadTarget ) { currencyService.currencies( - CurrencyType.supportedSymbols().joinToString(",") - ).enqueue( - object : Callback { - override fun onFailure( - call: Call, - throwable: Throwable - ) { - currenciesLoadResult.loaded( - emptyList() - ) - } - - override fun onResponse( - call: Call, - response: Response - ) { - 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 { - override fun onFailure( - call: Call, - throwable: Throwable - ) { - currencyListingLoadResult.loaded( - null - ) - } - - @SuppressLint("SetTextI18n") - override fun onResponse( - call: Call, - response: Response - ) { - currencyListingLoadResult.loaded( - response.body()!!.currencyListing - ) - } - } - ) + currencyService.listing((currency as CmcCurrency).id) + .subscribeOn(Schedulers.io()) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe { currencyListingLoadTarget.loaded(it.currencyListing) } } } diff --git a/app/src/main/java/com/smlnskgmail/jaman/cryptotracker/model/impl/currency/coinmarketcap/CmcCurrencyListing.kt b/app/src/main/java/com/smlnskgmail/jaman/cryptotracker/model/impl/currency/coinmarketcap/CmcCurrencyListing.kt index 8221f47..cbf97c1 100644 --- a/app/src/main/java/com/smlnskgmail/jaman/cryptotracker/model/impl/currency/coinmarketcap/CmcCurrencyListing.kt +++ b/app/src/main/java/com/smlnskgmail/jaman/cryptotracker/model/impl/currency/coinmarketcap/CmcCurrencyListing.kt @@ -20,7 +20,7 @@ data class CmcCurrencyListing( return currentPrice } - override fun changeDay(): CurrencyPricePercentChangeDay { + override fun changeHour(): CurrencyPricePercentChangeDay { return changeHourPrice } diff --git a/app/src/main/java/com/smlnskgmail/jaman/cryptotracker/model/impl/currency/coinmarketcap/cache/CmcCurrencyMapDbInstanceProvider.kt b/app/src/main/java/com/smlnskgmail/jaman/cryptotracker/model/impl/currency/coinmarketcap/cache/CmcCurrencyMapDbInstanceProvider.kt index 4cc96ef..b3cf20b 100644 --- a/app/src/main/java/com/smlnskgmail/jaman/cryptotracker/model/impl/currency/coinmarketcap/cache/CmcCurrencyMapDbInstanceProvider.kt +++ b/app/src/main/java/com/smlnskgmail/jaman/cryptotracker/model/impl/currency/coinmarketcap/cache/CmcCurrencyMapDbInstanceProvider.kt @@ -25,10 +25,10 @@ class CmcCurrencyMapDbInstanceProvider : CurrencyInstanceProvider { writer.writeUTF(currency.firstHistoricalDate()) writer.writeUTF(currency.lastHistoricalDate()) writer.writeFloat( - currency.currencyListing().currentPrice().value() + currency.currencyListing().currentPrice().price ) writer.writeFloat( - currency.currencyListing().changeDay().value() + currency.currencyListing().changeHour().value ) writer.writeUTF( currency.currencyType().toString() diff --git a/app/src/main/java/com/smlnskgmail/jaman/cryptotracker/model/impl/currency/coinmarketcap/deserializers/CurrencyResponseDeserializer.kt b/app/src/main/java/com/smlnskgmail/jaman/cryptotracker/model/impl/currency/coinmarketcap/deserializers/CurrencyResponseDeserializer.kt index 045057f..2e54857 100644 --- a/app/src/main/java/com/smlnskgmail/jaman/cryptotracker/model/impl/currency/coinmarketcap/deserializers/CurrencyResponseDeserializer.kt +++ b/app/src/main/java/com/smlnskgmail/jaman/cryptotracker/model/impl/currency/coinmarketcap/deserializers/CurrencyResponseDeserializer.kt @@ -22,11 +22,8 @@ class CurrencyResponseDeserializer : JsonDeserializer { context: JsonDeserializationContext? ): CurrencyResponse { val gson = Gson() - val currencyTypeToken = object : TypeToken() {}.type - val currencies = arrayListOf() - val dataJson = json!!.asJsonObject.get("data") for (data in dataJson.asJsonObject.entrySet()) { val currency = gson.fromJson( diff --git a/app/src/main/java/com/smlnskgmail/jaman/cryptotracker/model/impl/currency/coinmarketcap/retrofit/CmcCurrencyService.kt b/app/src/main/java/com/smlnskgmail/jaman/cryptotracker/model/impl/currency/coinmarketcap/retrofit/CmcCurrencyService.kt index 8993eb9..6959d2b 100644 --- a/app/src/main/java/com/smlnskgmail/jaman/cryptotracker/model/impl/currency/coinmarketcap/retrofit/CmcCurrencyService.kt +++ b/app/src/main/java/com/smlnskgmail/jaman/cryptotracker/model/impl/currency/coinmarketcap/retrofit/CmcCurrencyService.kt @@ -2,7 +2,7 @@ package com.smlnskgmail.jaman.cryptotracker.model.impl.currency.coinmarketcap.re 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 io.reactivex.rxjava3.core.Observable import retrofit2.http.GET import retrofit2.http.Headers import retrofit2.http.Query @@ -16,7 +16,7 @@ interface CmcCurrencyService { ) fun currencies( @Query("symbol") currencies: String - ): Call + ): Observable @GET("v1/cryptocurrency/quotes/latest") @Headers( @@ -25,6 +25,6 @@ interface CmcCurrencyService { ) fun listing( @Query("id") currencyId: Int - ): Call + ): Observable } diff --git a/app/src/main/java/com/smlnskgmail/jaman/cryptotracker/model/impl/currency/debug/DebugCurrency.kt b/app/src/main/java/com/smlnskgmail/jaman/cryptotracker/model/impl/currency/debug/DebugCurrency.kt index af76c38..9e73880 100644 --- a/app/src/main/java/com/smlnskgmail/jaman/cryptotracker/model/impl/currency/debug/DebugCurrency.kt +++ b/app/src/main/java/com/smlnskgmail/jaman/cryptotracker/model/impl/currency/debug/DebugCurrency.kt @@ -51,7 +51,7 @@ class DebugCurrency( ) } - override fun changeDay(): CurrencyPricePercentChangeDay { + override fun changeHour(): CurrencyPricePercentChangeDay { return CurrencyPricePercentChangeDay( 0.19f ) diff --git a/app/src/main/java/com/smlnskgmail/jaman/cryptotracker/model/impl/currency/debug/DebugCurrencyApi.kt b/app/src/main/java/com/smlnskgmail/jaman/cryptotracker/model/impl/currency/debug/DebugCurrencyApi.kt index 07c4cf4..f8afcca 100644 --- a/app/src/main/java/com/smlnskgmail/jaman/cryptotracker/model/impl/currency/debug/DebugCurrencyApi.kt +++ b/app/src/main/java/com/smlnskgmail/jaman/cryptotracker/model/impl/currency/debug/DebugCurrencyApi.kt @@ -1,10 +1,12 @@ package com.smlnskgmail.jaman.cryptotracker.model.impl.currency.debug -import android.annotation.SuppressLint -import android.os.AsyncTask import com.smlnskgmail.jaman.cryptotracker.model.api.currency.Currency import com.smlnskgmail.jaman.cryptotracker.model.api.currency.CurrencyApi import com.smlnskgmail.jaman.cryptotracker.model.api.currency.CurrencyType +import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers +import io.reactivex.rxjava3.core.Observable +import io.reactivex.rxjava3.schedulers.Schedulers +import java.util.concurrent.TimeUnit @Suppress( "unused", @@ -15,7 +17,7 @@ class DebugCurrencyApi : private val date = "2019-03-05T18:05:05.000Z" - private val currencies = setOf( + private val currencies = listOf( currencyFor( "Bitcoin", CurrencyType.BTC @@ -73,36 +75,22 @@ class DebugCurrencyApi : } override fun currencies( - currenciesLoadResult: CurrencyApi.CurrenciesLoadResult + currenciesLoadTarget: CurrencyApi.CurrenciesLoadTarget ) { - CurrenciesLoader(currenciesLoadResult).execute() + Observable + .timer(5, TimeUnit.SECONDS) + .subscribeOn(Schedulers.io()) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe { currenciesLoadTarget.loaded(currencies) } } override fun currencyListing( currency: Currency, - currencyListingLoadResult: CurrencyApi.CurrencyListingLoadResult + currencyListingLoadTarget: CurrencyApi.CurrencyListingLoadTarget ) { - currencyListingLoadResult.loaded( + currencyListingLoadTarget.loaded( currency.currencyListing() ) } - @SuppressLint("StaticFieldLeak") - private inner class CurrenciesLoader( - private val currenciesLoadResult: CurrencyApi.CurrenciesLoadResult - ) : AsyncTask, List>() { - - override fun doInBackground( - vararg params: Void? - ): List { - Thread.sleep(1_500) - return currencies.toList() - } - - override fun onPostExecute(result: List?) { - currenciesLoadResult.loaded(result!!) - } - - } - } diff --git a/app/src/main/java/com/smlnskgmail/jaman/cryptotracker/presenter/list/CurrenciesListPresenterImpl.kt b/app/src/main/java/com/smlnskgmail/jaman/cryptotracker/presenter/list/CurrenciesListPresenterImpl.kt index 3c8a579..dc9169c 100644 --- a/app/src/main/java/com/smlnskgmail/jaman/cryptotracker/presenter/list/CurrenciesListPresenterImpl.kt +++ b/app/src/main/java/com/smlnskgmail/jaman/cryptotracker/presenter/list/CurrenciesListPresenterImpl.kt @@ -12,8 +12,6 @@ class CurrenciesListPresenterImpl : CurrenciesListPresenter { private lateinit var currencyApi: CurrencyApi private lateinit var currencyCache: CurrencyCache - private var firstStart = true - override fun init( currenciesListView: CurrenciesListView, currencyApi: CurrencyApi, @@ -22,26 +20,26 @@ class CurrenciesListPresenterImpl : CurrenciesListPresenter { this.currenciesListView = currenciesListView this.currencyApi = currencyApi this.currencyCache = currencyCache - loadCurrencies() } private fun loadCurrencies() { val currencies = currencyCache.getCurrencies() - firstStart = currencies.isEmpty() - if (!firstStart) { - currenciesListView.showCurrencies( - currencies - ) - currenciesListView.showSeekLoader() - } else { - currenciesListView.showFullScreenLoader() - } - loadCurrenciesWithAction { - if (firstStart) { - currenciesListView.hideFullScreenLoader() + currencies.isEmpty().let { containsCurrencies -> + if (containsCurrencies) { + currenciesListView.showFullScreenLoader() } else { - currenciesListView.hideSeekLoader() + currenciesListView.showCurrencies( + currencies + ) + currenciesListView.showSeekLoader() + } + loadCurrenciesWithAction { + if (containsCurrencies) { + currenciesListView.hideFullScreenLoader() + } else { + currenciesListView.hideSeekLoader() + } } } } @@ -49,21 +47,23 @@ class CurrenciesListPresenterImpl : CurrenciesListPresenter { private fun loadCurrenciesWithAction( action: () -> Unit ) { - currencyApi.currencies(object : CurrencyApi.CurrenciesLoadResult { + currencyApi.currencies(object : CurrencyApi.CurrenciesLoadTarget { override fun loaded(currencies: List) { - if (currencies.isNotEmpty()) { - currenciesListView.showCurrencies( - currencies - ) - currencyCache.clear() - currencyCache.putCurrencies( - currencies - ) - } else { - currenciesListView.showCurrencies( - emptyList() - ) - currenciesListView.showLoadError() + currencies.isNotEmpty().let { containsCurrencies -> + if (containsCurrencies) { + currenciesListView.showCurrencies( + currencies + ) + currencyCache.clear() + currencyCache.putCurrencies( + currencies + ) + } else { + currenciesListView.showCurrencies( + emptyList() + ) + currenciesListView.showLoadError() + } } action() } @@ -87,7 +87,7 @@ class CurrenciesListPresenterImpl : CurrenciesListPresenter { ) { currencyApi.currencyListing( currency, - object : CurrencyApi.CurrencyListingLoadResult { + object : CurrencyApi.CurrencyListingLoadTarget { override fun loaded(currencyListing: CurrencyListing?) { if (currencyListing != null) { currency.updateCurrencyListing( diff --git a/app/src/main/java/com/smlnskgmail/jaman/cryptotracker/view/list/recycler/CurrencyHolder.kt b/app/src/main/java/com/smlnskgmail/jaman/cryptotracker/view/list/recycler/CurrencyHolder.kt index 2c8dcf6..256fdc8 100644 --- a/app/src/main/java/com/smlnskgmail/jaman/cryptotracker/view/list/recycler/CurrencyHolder.kt +++ b/app/src/main/java/com/smlnskgmail/jaman/cryptotracker/view/list/recycler/CurrencyHolder.kt @@ -61,23 +61,23 @@ class CurrencyHolder( currencyListing: CurrencyListing ) { val price = currencyListing.currentPrice() - val changeHour = currencyListing.changeDay() + val changeHour = currencyListing.changeHour() itemView.currency_price.text = price.toString() itemView.currency_price_at_last_hour.text = changeHour.toString() - val changeHourColorResId = when { - changeHour.value() > 0 -> R.color.colorCurrencyPriceUp - changeHour.value() < 0 -> R.color.colorCurrencyPriceDown + when { + changeHour.value > 0 -> R.color.colorCurrencyPriceUp + changeHour.value < 0 -> R.color.colorCurrencyPriceDown else -> R.color.colorCurrencyPriceStable - } - - itemView.currency_price_at_last_hour.setTextColor( - ContextCompat.getColor( - itemView.context, - changeHourColorResId + }.let { color -> + itemView.currency_price_at_last_hour.setTextColor( + ContextCompat.getColor( + itemView.context, + color + ) ) - ) + } } private fun clearItem() { diff --git a/app/src/main/res/layout/activity_splash.xml b/app/src/main/res/layout/activity_splash.xml index 2e85334..687e6f9 100644 --- a/app/src/main/res/layout/activity_splash.xml +++ b/app/src/main/res/layout/activity_splash.xml @@ -11,13 +11,13 @@ android:id="@+id/logo" android:layout_width="@dimen/image_size_splash" android:layout_height="@dimen/image_size_splash" + android:src="@drawable/ic_bitcoin" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.40" - android:src="@drawable/ic_bitcoin" - android:tint="@color/colorAccent" + app:tint="@color/colorAccent" tools:ignore="ContentDescription" />