Skip to content

Commit

Permalink
[nyoman] add baseViewModel
Browse files Browse the repository at this point in the history
  • Loading branch information
inyomanw committed Apr 1, 2019
1 parent 63ab7fc commit d875642
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 2 deletions.
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
8 changes: 8 additions & 0 deletions corelibrary/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Boolean>()
fun observeLoading(): LiveData<Boolean> = isLoading

protected val isError = MutableLiveData<NetworkError?>()
fun observeError(): LiveData<NetworkError?> = isError

protected val isEmtyData = MutableLiveData<Boolean>()
fun observeEmptyData(): LiveData<Boolean> = isEmtyData

private fun launch(movie: () -> Disposable) {
disposable.add(movie())
}

protected fun <T> Single<T>.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()
}
}
25 changes: 23 additions & 2 deletions corelibrary/src/main/java/com/inyomanw/corelibrary/utils/Ext.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down
3 changes: 3 additions & 0 deletions corelibrary/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
<resources>
<string name="app_name">corelibrary</string>
<string name="allias_day">d</string>
<string name="allias_week">w</string>
<string name="allias_month">m</string>
</resources>

0 comments on commit d875642

Please sign in to comment.