From 1abbe95083fa9eb0a1beae0619e0075585c40565 Mon Sep 17 00:00:00 2001 From: Severiano Jaramillo Date: Thu, 22 Aug 2019 11:41:01 -0500 Subject: [PATCH] Add Kotlin's Parcelize to FilterOptions. - Enabled Kotlin's Parcelize annotation and used it to easily make FilterOptions Parcelable. - Converted the ServiceGenerator class to Kotlin. This is a helper class that generates instances of Retrofit Services. --- app/build.gradle | 5 ++ .../models/FilterOptions.kt | 49 ++------------ .../network/ServiceGenerator.java | 61 ------------------ .../network/ServiceGenerator.kt | 64 +++++++++++++++++++ 4 files changed, 73 insertions(+), 106 deletions(-) delete mode 100644 app/src/main/java/cy/agorise/bitsybitshareswallet/network/ServiceGenerator.java create mode 100644 app/src/main/java/cy/agorise/bitsybitshareswallet/network/ServiceGenerator.kt diff --git a/app/build.gradle b/app/build.gradle index fa0616c..9285707 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -6,6 +6,11 @@ apply plugin: "androidx.navigation.safeargs.kotlin" apply plugin: 'io.fabric' apply plugin: 'com.google.gms.google-services' +// Needed for Kotlin's @Parcelize annotation +androidExtensions { + experimental = true +} + android { compileSdkVersion 28 defaultConfig { diff --git a/app/src/main/java/cy/agorise/bitsybitshareswallet/models/FilterOptions.kt b/app/src/main/java/cy/agorise/bitsybitshareswallet/models/FilterOptions.kt index 5218de1..0e77359 100644 --- a/app/src/main/java/cy/agorise/bitsybitshareswallet/models/FilterOptions.kt +++ b/app/src/main/java/cy/agorise/bitsybitshareswallet/models/FilterOptions.kt @@ -1,11 +1,13 @@ package cy.agorise.bitsybitshareswallet.models -import android.os.Parcel import android.os.Parcelable +import kotlinx.android.parcel.Parcelize +import cy.agorise.bitsybitshareswallet.fragments.TransactionsFragment /** * Model that includes all the options to filter the transactions in the [TransactionsFragment] */ +@Parcelize data class FilterOptions ( var query: String = "", var transactionsDirection: Int = 0, @@ -18,47 +20,4 @@ data class FilterOptions ( var fromEquivalentValue: Long = 0L, var toEquivalentValue: Long = 5000L, var agoriseFees: Boolean = true -) : Parcelable { - constructor(parcel: Parcel) : this( - parcel.readString(), - parcel.readInt(), - parcel.readByte() != 0.toByte(), - parcel.readLong(), - parcel.readLong(), - parcel.readByte() != 0.toByte(), - parcel.readString(), - parcel.readByte() != 0.toByte(), - parcel.readLong(), - parcel.readLong(), - parcel.readByte() != 0.toByte() - ) - - override fun writeToParcel(parcel: Parcel, flags: Int) { - parcel.writeString(query) - parcel.writeInt(transactionsDirection) - parcel.writeByte(if (dateRangeAll) 1 else 0) - parcel.writeLong(startDate) - parcel.writeLong(endDate) - parcel.writeByte(if (assetAll) 1 else 0) - parcel.writeString(asset) - parcel.writeByte(if (equivalentValueAll) 1 else 0) - parcel.writeLong(fromEquivalentValue) - parcel.writeLong(toEquivalentValue) - parcel.writeByte(if (agoriseFees) 1 else 0) - } - - override fun describeContents(): Int { - return 0 - } - - companion object CREATOR : Parcelable.Creator { - override fun createFromParcel(parcel: Parcel): FilterOptions { - return FilterOptions(parcel) - } - - override fun newArray(size: Int): Array { - return arrayOfNulls(size) - } - } - -} \ No newline at end of file +) : Parcelable \ No newline at end of file diff --git a/app/src/main/java/cy/agorise/bitsybitshareswallet/network/ServiceGenerator.java b/app/src/main/java/cy/agorise/bitsybitshareswallet/network/ServiceGenerator.java deleted file mode 100644 index c5738a9..0000000 --- a/app/src/main/java/cy/agorise/bitsybitshareswallet/network/ServiceGenerator.java +++ /dev/null @@ -1,61 +0,0 @@ -package cy.agorise.bitsybitshareswallet.network; - -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; -import com.jakewharton.retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory; -import cy.agorise.bitsybitshareswallet.models.coingecko.MarketData; -import cy.agorise.bitsybitshareswallet.models.coingecko.MarketDataDeserializer; -import okhttp3.OkHttpClient; -import okhttp3.logging.HttpLoggingInterceptor; -import retrofit2.Retrofit; -import retrofit2.converter.gson.GsonConverterFactory; - -import java.util.HashMap; -import java.util.concurrent.TimeUnit; - -public class ServiceGenerator{ - private static OkHttpClient.Builder httpClient; - private static Retrofit.Builder builder; - - private static HashMap, Object> Services = new HashMap<>(); - - private ServiceGenerator(String apiBaseUrl, Gson gson) { - HttpLoggingInterceptor logging = new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY); - httpClient = new OkHttpClient.Builder().addInterceptor(logging); - builder = new Retrofit.Builder() - .baseUrl(apiBaseUrl) - .addConverterFactory(GsonConverterFactory.create(gson)) - .addCallAdapterFactory(RxJava2CallAdapterFactory.create()); - } - - public ServiceGenerator(String apiBaseUrl){ - this(apiBaseUrl, new Gson()); - } - - private static void setService(Class klass, T thing) { - Services.put(klass, thing); - } - - public T getService(Class serviceClass) { - - T service = serviceClass.cast(Services.get(serviceClass)); - if (service == null) { - service = createService(serviceClass); - setService(serviceClass, service); - } - return service; - } - - private static S createService(Class serviceClass) { - httpClient.readTimeout(15, TimeUnit.SECONDS); - httpClient.connectTimeout(15, TimeUnit.SECONDS); - OkHttpClient client = httpClient.build(); - if(serviceClass == CoingeckoService.class){ - // The MarketData class needs a custom de-serializer - Gson gson = new GsonBuilder().registerTypeAdapter(MarketData.class, new MarketDataDeserializer()).create(); - builder.addConverterFactory(GsonConverterFactory.create(gson)); - } - Retrofit retrofit = builder.client(client).build(); - return retrofit.create(serviceClass); - } -} diff --git a/app/src/main/java/cy/agorise/bitsybitshareswallet/network/ServiceGenerator.kt b/app/src/main/java/cy/agorise/bitsybitshareswallet/network/ServiceGenerator.kt new file mode 100644 index 0000000..ce4deb3 --- /dev/null +++ b/app/src/main/java/cy/agorise/bitsybitshareswallet/network/ServiceGenerator.kt @@ -0,0 +1,64 @@ +package cy.agorise.bitsybitshareswallet.network + +import com.google.gson.Gson +import com.google.gson.GsonBuilder +import com.jakewharton.retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory +import cy.agorise.bitsybitshareswallet.models.coingecko.MarketData +import cy.agorise.bitsybitshareswallet.models.coingecko.MarketDataDeserializer +import okhttp3.OkHttpClient +import okhttp3.logging.HttpLoggingInterceptor +import retrofit2.Retrofit +import retrofit2.converter.gson.GsonConverterFactory + +import java.util.HashMap +import java.util.concurrent.TimeUnit + +class ServiceGenerator private constructor(apiBaseUrl: String, gson: Gson) { + + private var httpClient: OkHttpClient.Builder + private var builder: Retrofit.Builder + + private val services = HashMap, Any>() + + init { + val logging = HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY) + httpClient = OkHttpClient.Builder().addInterceptor(logging) + builder = Retrofit.Builder() + .baseUrl(apiBaseUrl) + .addConverterFactory(GsonConverterFactory.create(gson)) + .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) + } + + constructor(apiBaseUrl: String) : this(apiBaseUrl, Gson()) + + fun getService(serviceClass: Class): T { + + var service = serviceClass.cast(services[serviceClass]) + if (service == null) { + service = createService(serviceClass) + setService(serviceClass, service) + } + return service + } + + private fun setService(klass: Class, thing: T) { + services[klass] = thing as Any + } + + private fun createService(serviceClass: Class): S { + httpClient.readTimeout(15, TimeUnit.SECONDS) + httpClient.connectTimeout(15, TimeUnit.SECONDS) + val client = httpClient.build() + if (serviceClass == CoingeckoService::class.java) { + // The MarketData class needs a custom de-serializer + val gson = GsonBuilder().registerTypeAdapter( + MarketData::class.java, + MarketDataDeserializer() + ).create() + builder.addConverterFactory(GsonConverterFactory.create(gson)) + } + val retrofit = builder.client(client).build() + return retrofit.create(serviceClass) + } + +}