Skip to content

Instantly share code, notes, and snippets.

interface ViewModelLiveDataExtension {
operator fun <T> LiveData<T>.invoke(value: T) {
if (this is ViewModelLiveData) {
val viewModelLiveData = this
viewModelLiveData.setValue(value)
}
}
fun <T> ViewModel.liveData(value: T? = null) = lazy {
ViewModelLiveData<T>().apply { value?.let { setValue(value) } } as LiveData<T>
abstract class BaseViewModel: ViewModel() {
protected operator fun <T> LiveData<T>.invoke(value: T) {
if (this is ViewModelLiveData) {
val viewModelLiveData = this
viewModelLiveData.setValue(value)
}
}
protected fun <T> liveData(value: T) = lazy {
ViewModelLiveData(value) as LiveData<T>
fun onRestResult(response:Response){
if (response.data != null){
showItensOnScreen(response.data)
}else{
showError()
}
}
fun showItensOnScreen(model:RestModel){
livedataName.value = model.name
@paulocns
paulocns / kdb_exemple.kt
Last active February 15, 2020 04:59
Kdb Exemple
queryViewModelArc.searchEnabled.bind(searchButton::setEnabled)
queryViewModelArc.showLoading.bind { loadinLayout.present = it }
queryViewModelArc.queryValue.twoWayBind(queryEditText.bindableText)
/**
* (C) Copyright 2018 Paulo Vitor Sato Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
public suspend fun <T : Any> Call<T>.await(): T {
return suspendCancellableCoroutine { continuation ->
enqueue(object : Callback<T> {
override fun onResponse(call: Call<T>?, response: Response<T?>) {
if (response.isSuccessful) {
val body = response.body()
if (body == null) {
continuation.resumeWithException(
NullPointerException("Response body is null: $response")
)
class SearchShows @Inject
constructor(private val showRepository: ShowRepository) : UseCase() {
var query: String? = null
override fun buildUseCaseObservable(): Single<List<ShowResponse>> {
return showRepository.searchShow(query).flatMapPublisher { Flowable.fromIterable(it) }
.flatMapSingle({ showInfo:ShowInfo ->
showRepository.showRating(showInfo.show.ids.trakt)
.subscribeOn(Schedulers.io())
.map { rating ->
class SearchShows @Inject
constructor(private val showRepository: ShowRepository) : UseCase() {
var id: String? = null
override suspend fun executeOnBackground(): Single<Show> {
val singleDetail = showRepository.showDetail(id).subscribeOn(Schedulers.io())
val singleBanner = showRepository.showBanner(id).subscribeOn(Schedulers.io())
return Single.zip(singleDetail, singleBanner, BiFunction { detail, banner -> Show(detail, banner})
}
}
class SearchShows @Inject
constructor(private val showRepository: ShowRepository, private val resourceRepository: ResourceRepository) : UseCase() {
private var query: String? = null
fun setQuery(query: String) {
this.query = query
}
override fun buildUseCaseObservable(): Single<String> {
return showRepository.searchShow(query).map { showInfos ->
class SearchShows @Inject
constructor(private val showRepository: ShowRepository) :
UseCase<List<ShowResponse>>() {
var query: String? = null
override suspend fun executeOnBackground(): List<ShowResponse> {
query?.let { query ->
return showRepository.searchShow(query).map {
background {