Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View RBusarow's full-sized avatar
🕶️

Rick Busarow RBusarow

🕶️
View GitHub Profile
@RBusarow
RBusarow / Dispatchers.kt
Created January 11, 2020 23:57
more succinct version of the Dispatchers object from kotlinx.coroutines
object Dispatchers {
val Default: CoroutineDispatcher = createDefaultDispatcher()
val Main: MainCoroutineDispatcher get() = MainDispatcherLoader.dispatcher
val Unconfined: CoroutineDispatcher = kotlinx.coroutines.Unconfined
val IO: CoroutineDispatcher = DefaultScheduler.IO
}
@RBusarow
RBusarow / DaggerCoroutineScopeModule.kt
Created January 11, 2020 22:49
Examples of Dagger2 and Koin modules for providing dispatcher-specific CoroutineScopes.
@Module
object CoroutineScopeModule {
@Provides
fun provideDefaultCoroutineScope(): DefaultCoroutineScope = DefaultCoroutineScope()
@Provides
fun provideIOCoroutineScope(): IOCoroutineScope = IOCoroutineScope()
@Provides
@RBusarow
RBusarow / MutableLiveData2.kt
Created December 28, 2019 21:29
nullability-safe Kotlin version of MutableLiveData
@Suppress("UNCHECKED_CAST")
class MutableLiveData2<T>(value: T) : LiveData<T>(value) {
override fun getValue(): T = super.getValue() as T
public override fun setValue(value: T) = super.setValue(value)
public override fun postValue(value: T) = super.postValue(value)
}
@RBusarow
RBusarow / MutableLiveData.java
Created December 28, 2019 21:28
setValue() from the MutableLiveData source
package androidx.lifecycle;
public class MutableLiveData<T> extends LiveData<T> {
...
@Override
public void setValue(T value) {
super.setValue(value);
}
}
@RBusarow
RBusarow / SomeFile.kt
Created December 28, 2019 21:27
example of setting a LiveData's value to null when the type is non-nullable
val someLiveData = MutableLiveData<String>()
// this compiles!!
someLiveData.value = null
@RBusarow
RBusarow / LiveData.java
Created December 28, 2019 21:25
getValue() function from LiveData source
package androidx.lifecycle;
@Nullable
public T getValue() {
Object data = mData;
if (data != NOT_SET) {
return (T) data;
}
return null;
}
@RBusarow
RBusarow / SomeFile.kt
Created December 28, 2019 21:24
example of getting a null value from a non-nullable LiveData
val someLiveData = MutableLiveData<String>()
val currentValue = someLiveData.value
println(currentValue) // null
@RBusarow
RBusarow / SomeFile.kt
Created December 28, 2019 21:23
example of a type-safe observer from ktx 2.2.0
val someLiveData : LiveData<String> = ...
viewModel.someLiveData.observe(this) { someString: String ->
// the value from this lambda is inferred as non-nullable
stringWhichIsNotNull = message
}
@RBusarow
RBusarow / SomeFile.kt
Created December 28, 2019 21:21
example of a nullable LiveData observer
val someLiveData : LiveData<String> = ...
someLiveData.observe(this, Observer { someString: String? ->
// the value from the callback is always nullable
stringWhichIsNotNull = someString ?: "placeholder message"
}
@RBusarow
RBusarow / SharedFlow.kt
Last active February 17, 2020 05:03
Resettable lazy version of a shared Flow, which allows for multiple observers to consume a single emitter, with an automatic reset after the last consumer stops and an automatic resume when another consumer starts.
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.channels.BroadcastChannel
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
fun <T> Flow<T>.shareIn(
scope: CoroutineScope
): Flow<T> = SharedFlow(
BroadcastManager(