This file contains hidden or 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
/** | |
* A generic class that holds a value with its loading status. | |
* @param <T> | |
</T> */ | |
data class Resource<out T>(val status: Status, val data: T?, val message: String?) { | |
companion object { | |
fun <T> success(data: T?): Resource<T> { | |
return Resource(SUCCESS, data, null) | |
} |
This file contains hidden or 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
class SimpleViewModel : ViewModel() { | |
var data: LiveData<String> = MutableLiveData() | |
var loading: LiveData<Boolean> = MutableLiveData() | |
var error: LiveData<Throwable> = MutableLiveData() | |
} |
This file contains hidden or 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
class StateLiveData<T> : MutableLiveData<T>() { | |
val state = EventLiveData<State>() | |
} |
This file contains hidden or 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
fun refreshSavedBillers() { | |
payBillManager.getAll() | |
.compose(defaultOptions()) | |
.autoDisposable() | |
.subscribe({ | |
displaySavedBillers(it) | |
}) { Timber.d(it) } | |
} |
This file contains hidden or 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
class EventLiveData<T> : MutableLiveData<T>() { | |
private var isRead: AtomicBoolean = AtomicBoolean(false) | |
/** | |
* ensure the event is non-null and can only been seen once | |
*/ | |
fun observeEvent(owner: LifecycleOwner, observer: Observer<T>) { | |
super.observe(owner, Observer { | |
if (it != null && isRead.compareAndSet(false, true)) { |
This file contains hidden or 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
class StateLiveData<T> : MutableLiveData<T>() { | |
val state = EventLiveData<State>() | |
init { | |
clearState() | |
} | |
fun post(observable: Observable<T>, successOnFirstValue: Boolean = true): Disposable { | |
postLoading() |
This file contains hidden or 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
open class BaseViewModel : ViewModel() { | |
private val disposableList = mutableListOf<Disposable>() | |
// integration with rx | |
fun <T> Observable<T>.subscribe(stateLiveData: StateLiveData<T>, successOnFirstValue: Boolean = true) = stateLiveData.post(this, successOnFirstValue).also { disposableList.add(it) } | |
override fun onCleared() { | |
disposableList.filter { !it.isDisposed }.forEach { it.dispose() } | |
disposableList.clear() |
This file contains hidden or 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
abstract class BaseActivity : AppCompatActivity() { | |
fun <T> LiveData<T>.observe(observer: (T?) -> Unit) = observe(this@BaseActivity, Observer { observer(it) }) | |
fun <T> LiveData<T>.observeNonNull(observer: (T) -> Unit) = observe { if (it != null) observer(it) } | |
fun <T> EventLiveData<T>.observeEvent(observer: (T) -> Unit) = observeEvent(this@BaseActivity, Observer { if (it != null) observer(it) }) | |
inline fun <reified T : BaseViewModel> getViewModel(): T = ViewModelProviders.of(this)[T::class.java] |
This file contains hidden or 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
class PayBillViewModel : BaseViewModel() { | |
var manager: PayBillManager = PayBillManager() | |
var bills: StateLiveData<List<SavedBill>> = StateLiveData() | |
fun loadBills() { | |
manager.getAll() | |
.subscribe(bills) | |
} |
This file contains hidden or 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
class PayBillActivity : BaseActivity() { | |
lateinit var payBillViewModel : PayBillViewModel | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_pay_bill) | |
payBillViewModel = getViewModel() | |
payBillViewModel.loadBills() |