Skip to content

Instantly share code, notes, and snippets.

View antonicg's full-sized avatar

Antoni Castejón antonicg

View GitHub Profile
flow { emit("whatever") }
// In an Android ViewModel you will use the viewModelScope:
viewModelScope.launch { someFlow.collect { /* do whatever */ } }
// In a Fragment we have the lifecycleScope :
lifecycleScope.launch { someFlow.collect { /* do whatever */ } }
// Or with the launchIn method:
someFlow.launchIn(viewModelScope)
fun getFlow() = flow { emit(1) }
.flowOn(Dispatchers.IO)
// and the subscription...
runBlocking {
getFlow().collect {}
}
fun getObservable() = Observable.just(1)
.subscribeOn(Schedulers.io())
// and in a different file...
getObservable().subscribe()
val disposable = someObservable.subscribe()
disposable.dispose()
// or...
val compositeDisposable = CompositeDisposable()
compositeDisposable.add(disposable)
compositeDisposable.dispose()
flowReturningAnError
.catch { emit(defaultValue) }
flow {
var x = 0
do {
delay(1000)
emit(x)
x++
} while (x < 1000)
}
package com.antonicastejon.cryptodata.viewmodels
import android.arch.core.executor.testing.InstantTaskExecutorRule
import android.arch.lifecycle.Observer
import com.antonicastejon.cryptodata.common.limitCryptoListSizeArrayEmptyCryptoViewModel
import com.antonicastejon.cryptodata.common.mock
import com.antonicastejon.cryptodata.common.whenever
import com.antonicastejon.cryptodata.domain.CryptoListUseCases
import com.antonicastejon.cryptodata.domain.CryptoViewModel
import com.antonicastejon.cryptodata.presentation.main.crypto_list.*
fun limitCryptoListSizeArrayEmptyCryptoViewModel(): List<CryptoViewModel> =
ArrayList<CryptoViewModel>(Collections.nCopies(LIMIT_CRYPTO_LIST, CryptoViewModel()))
fun cryptoPOJOmodel() =
Crypto("1", "name", "sy", 10, "100", "0.1", "100", "1000", "0", "500", "5", "10", "-10", "")
fun cryptoViewModelFrom(crypto:Crypto) =
CryptoViewModel(crypto.id, crypto.name, crypto.symbol, crypto.rank, crypto.priceUsd.toFloat(), crypto.priceBtc.toFloat(), crypto.percentChange24h.toFloat())
package com.antonicastejon.cryptodata.usecases
import android.arch.core.executor.testing.InstantTaskExecutorRule
import com.antonicastejon.cryptodata.common.cryptoPOJOmodel
import com.antonicastejon.cryptodata.common.cryptoViewModelFrom
import com.antonicastejon.cryptodata.common.mock
import com.antonicastejon.cryptodata.common.whenever
import com.antonicastejon.cryptodata.domain.CryptoListInteractor
import com.antonicastejon.cryptodata.model.CoinMarketCapRepository
import io.reactivex.Single