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
object Dispatchers { | |
val Default: CoroutineDispatcher = createDefaultDispatcher() | |
val Main: MainCoroutineDispatcher get() = MainDispatcherLoader.dispatcher | |
val Unconfined: CoroutineDispatcher = kotlinx.coroutines.Unconfined | |
val IO: CoroutineDispatcher = DefaultScheduler.IO | |
} |
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
@Module | |
object CoroutineScopeModule { | |
@Provides | |
fun provideDefaultCoroutineScope(): DefaultCoroutineScope = DefaultCoroutineScope() | |
@Provides | |
fun provideIOCoroutineScope(): IOCoroutineScope = IOCoroutineScope() | |
@Provides |
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
@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) | |
} |
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
package androidx.lifecycle; | |
public class MutableLiveData<T> extends LiveData<T> { | |
... | |
@Override | |
public void setValue(T value) { | |
super.setValue(value); | |
} | |
} |
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
val someLiveData = MutableLiveData<String>() | |
// this compiles!! | |
someLiveData.value = null |
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
package androidx.lifecycle; | |
@Nullable | |
public T getValue() { | |
Object data = mData; | |
if (data != NOT_SET) { | |
return (T) data; | |
} | |
return null; | |
} |
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
val someLiveData = MutableLiveData<String>() | |
val currentValue = someLiveData.value | |
println(currentValue) // null |
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
val someLiveData : LiveData<String> = ... | |
viewModel.someLiveData.observe(this) { someString: String -> | |
// the value from this lambda is inferred as non-nullable | |
stringWhichIsNotNull = message | |
} |
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
val someLiveData : LiveData<String> = ... | |
someLiveData.observe(this, Observer { someString: String? -> | |
// the value from the callback is always nullable | |
stringWhichIsNotNull = someString ?: "placeholder message" | |
} |