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.
This commit is contained in:
parent
31add11412
commit
1abbe95083
4 changed files with 73 additions and 106 deletions
|
@ -6,6 +6,11 @@ apply plugin: "androidx.navigation.safeargs.kotlin"
|
||||||
apply plugin: 'io.fabric'
|
apply plugin: 'io.fabric'
|
||||||
apply plugin: 'com.google.gms.google-services'
|
apply plugin: 'com.google.gms.google-services'
|
||||||
|
|
||||||
|
// Needed for Kotlin's @Parcelize annotation
|
||||||
|
androidExtensions {
|
||||||
|
experimental = true
|
||||||
|
}
|
||||||
|
|
||||||
android {
|
android {
|
||||||
compileSdkVersion 28
|
compileSdkVersion 28
|
||||||
defaultConfig {
|
defaultConfig {
|
||||||
|
|
|
@ -1,11 +1,13 @@
|
||||||
package cy.agorise.bitsybitshareswallet.models
|
package cy.agorise.bitsybitshareswallet.models
|
||||||
|
|
||||||
import android.os.Parcel
|
|
||||||
import android.os.Parcelable
|
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]
|
* Model that includes all the options to filter the transactions in the [TransactionsFragment]
|
||||||
*/
|
*/
|
||||||
|
@Parcelize
|
||||||
data class FilterOptions (
|
data class FilterOptions (
|
||||||
var query: String = "",
|
var query: String = "",
|
||||||
var transactionsDirection: Int = 0,
|
var transactionsDirection: Int = 0,
|
||||||
|
@ -18,47 +20,4 @@ data class FilterOptions (
|
||||||
var fromEquivalentValue: Long = 0L,
|
var fromEquivalentValue: Long = 0L,
|
||||||
var toEquivalentValue: Long = 5000L,
|
var toEquivalentValue: Long = 5000L,
|
||||||
var agoriseFees: Boolean = true
|
var agoriseFees: Boolean = true
|
||||||
) : Parcelable {
|
) : 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<FilterOptions> {
|
|
||||||
override fun createFromParcel(parcel: Parcel): FilterOptions {
|
|
||||||
return FilterOptions(parcel)
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun newArray(size: Int): Array<FilterOptions?> {
|
|
||||||
return arrayOfNulls(size)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -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<Class<?>, 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 <T> void setService(Class<T> klass, T thing) {
|
|
||||||
Services.put(klass, thing);
|
|
||||||
}
|
|
||||||
|
|
||||||
public <T> T getService(Class<T> serviceClass) {
|
|
||||||
|
|
||||||
T service = serviceClass.cast(Services.get(serviceClass));
|
|
||||||
if (service == null) {
|
|
||||||
service = createService(serviceClass);
|
|
||||||
setService(serviceClass, service);
|
|
||||||
}
|
|
||||||
return service;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static <S> S createService(Class<S> 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);
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -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<Class<*>, 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 <T> getService(serviceClass: Class<T>): T {
|
||||||
|
|
||||||
|
var service = serviceClass.cast(services[serviceClass])
|
||||||
|
if (service == null) {
|
||||||
|
service = createService(serviceClass)
|
||||||
|
setService(serviceClass, service)
|
||||||
|
}
|
||||||
|
return service
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun <T> setService(klass: Class<T>, thing: T) {
|
||||||
|
services[klass] = thing as Any
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun <S> createService(serviceClass: Class<S>): 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)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in a new issue