-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
99 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
corelibrary/src/main/java/com/inyomanw/corelibrary/base/BaseViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |