From d875642deaffd711fbc620fef95925f2a2b0fb99 Mon Sep 17 00:00:00 2001 From: I Nyoman Wiryanata Date: Mon, 1 Apr 2019 16:43:32 +0700 Subject: [PATCH] [nyoman] add baseViewModel --- build.gradle | 3 + corelibrary/build.gradle | 8 +++ .../corelibrary/base/BaseViewModel.kt | 62 +++++++++++++++++++ .../com/inyomanw/corelibrary/utils/Ext.kt | 25 +++++++- corelibrary/src/main/res/values/strings.xml | 3 + 5 files changed, 99 insertions(+), 2 deletions(-) create mode 100644 corelibrary/src/main/java/com/inyomanw/corelibrary/base/BaseViewModel.kt diff --git a/build.gradle b/build.gradle index d2851bb..76271e3 100644 --- a/build.gradle +++ b/build.gradle @@ -9,6 +9,9 @@ buildscript { ext.retrofit_version = '2.4.0' ext.okhttp3_version = '3.10.0' ext.dagger_version = '2.16' + ext.rxjava_version = '2.2.3' + ext.rxandroid_version = '2.1.0' + ext.rxbinding_version = '2.0.0' repositories { google() diff --git a/corelibrary/build.gradle b/corelibrary/build.gradle index 21db94a..68c3504 100644 --- a/corelibrary/build.gradle +++ b/corelibrary/build.gradle @@ -62,6 +62,14 @@ dependencies { /* Dagger2 - default dependency */ kapt "com.google.dagger:dagger-compiler:$dagger_version" + + //rx + implementation "io.reactivex.rxjava2:rxandroid:$rxandroid_version" + implementation "io.reactivex.rxjava2:rxjava:$rxjava_version" + implementation "com.jakewharton.rxbinding2:rxbinding:$rxbinding_version" + implementation "com.jakewharton.rxbinding2:rxbinding-support-v4:$rxbinding_version" + implementation "com.jakewharton.rxbinding2:rxbinding-appcompat-v7:$rxbinding_version" + implementation "com.jakewharton.rxbinding2:rxbinding-design:$rxbinding_version" } repositories { mavenCentral() diff --git a/corelibrary/src/main/java/com/inyomanw/corelibrary/base/BaseViewModel.kt b/corelibrary/src/main/java/com/inyomanw/corelibrary/base/BaseViewModel.kt new file mode 100644 index 0000000..36847b9 --- /dev/null +++ b/corelibrary/src/main/java/com/inyomanw/corelibrary/base/BaseViewModel.kt @@ -0,0 +1,62 @@ +package com.inyomanw.corelibrary.base + +import android.arch.lifecycle.LiveData +import android.arch.lifecycle.MutableLiveData +import android.arch.lifecycle.ViewModel +import android.support.annotation.CallSuper +import io.reactivex.Single +import io.reactivex.android.schedulers.AndroidSchedulers +import io.reactivex.disposables.CompositeDisposable +import io.reactivex.disposables.Disposable +import io.reactivex.schedulers.Schedulers + +abstract class BaseViewModel : ViewModel() { + private val disposable = CompositeDisposable() + + private val isLoading = MutableLiveData() + fun observeLoading(): LiveData = isLoading + + protected val isError = MutableLiveData() + fun observeError(): LiveData = isError + + protected val isEmtyData = MutableLiveData() + fun observeEmptyData(): LiveData = isEmtyData + + private fun launch(movie: () -> Disposable) { + disposable.add(movie()) + } + + protected fun Single.onResult(action: (T) -> Unit, error: (NetworkError) -> Unit) { + launch { + this + .doOnSubscribe { + isLoading.postValue(true) + } + .doOnError { + isLoading.postValue(false) + } + .doOnSuccess { + isLoading.postValue(false) + } + .observeOn(AndroidSchedulers.mainThread()) + .subscribeOn(Schedulers.io()) + .unsubscribeOn(Schedulers.io()) + .subscribe( + { result -> + action.invoke(result) + isLoading.postValue(false) + }, + { err -> + error.invoke(NetworkError(err)) + isLoading.postValue(false) + } + ) + } + } + + @CallSuper + override fun onCleared() { + super.onCleared() + disposable.clear() + } +} \ No newline at end of file diff --git a/corelibrary/src/main/java/com/inyomanw/corelibrary/utils/Ext.kt b/corelibrary/src/main/java/com/inyomanw/corelibrary/utils/Ext.kt index 39ad992..f4fbbfe 100644 --- a/corelibrary/src/main/java/com/inyomanw/corelibrary/utils/Ext.kt +++ b/corelibrary/src/main/java/com/inyomanw/corelibrary/utils/Ext.kt @@ -7,12 +7,12 @@ import android.widget.ImageView import android.widget.Toast import com.bumptech.glide.Glide import com.inyomanw.corelibrary.R -import org.joda.time.DateTime -import org.joda.time.DateTimeZone +import org.joda.time.* import org.joda.time.format.DateTimeFormat import java.text.ParseException import java.text.SimpleDateFormat import java.util.* +import kotlin.math.absoluteValue fun ImageView.onLoad(context: Context, url :String){ Glide.with(context) @@ -80,6 +80,27 @@ fun String?.convertDate(inputFormat: String = "yyyy-MM-dd", return "" } +fun Context.calculateTwoDates(firstDate: String = "yyyy-MM-dd"): String { + val date1 = LocalDateTime.parse(firstDate) + val str = DateTimeFormat.forPattern("yyyy-MM-dd") + val secondDate = LocalDate.now().toString(str) + val date2 = LocalDateTime.parse(secondDate) + var value = Days.daysBetween(date2, date1).days + return when { + value >= 7 -> { + value = Weeks.weeksBetween(date2, date1).weeks + value.absoluteValue.toString() + getString(R.string.allias_week) + } + value >= 30 -> { + value = Months.monthsBetween(date2, date1).months + value.absoluteValue.toString() + getString(R.string.allias_month) + } + value == 0 -> 1.toString() + getString(R.string.allias_day) + else -> value.absoluteValue.toString() + getString(R.string.allias_day) + + } +} + fun String.changeDateFormat(oldPattern: String, newPattern: String): String { var res = "" try { diff --git a/corelibrary/src/main/res/values/strings.xml b/corelibrary/src/main/res/values/strings.xml index 28e248c..d05f75f 100644 --- a/corelibrary/src/main/res/values/strings.xml +++ b/corelibrary/src/main/res/values/strings.xml @@ -1,3 +1,6 @@ corelibrary + d + w + m