Skip to content

Instantly share code, notes, and snippets.

View kakai248's full-sized avatar

Ricardo Carrapiço kakai248

View GitHub Profile
@MainThread
inline fun <reified VM : ViewModel> ComponentActivity.viewModel(
noinline provider: (SavedStateHandle) -> VM
) = createLazyViewModel(
viewModelClass = VM::class,
savedStateRegistryOwnerProducer = { this },
viewModelStoreOwnerProducer = { this },
viewModelProvider = provider
)
@kakai248
kakai248 / Example.kt
Created February 24, 2020 18:00
Example on how to to inject SavedStateHandle into ViewModel's
// ViewModel factory
@MainThread
inline fun <reified VM : ViewModel> ComponentActivity.viewModel(
noinline provider: (SavedStateHandle) -> VM
) = createLazyViewModel(
viewModelClass = VM::class,
savedStateRegistryOwnerProducer = { this },
viewModelStoreOwnerProducer = { this },
viewModelProvider = provider
public class ViewModelInjectionFactory<VM extends ViewModel> implements ViewModelProvider.Factory {
private Lazy<VM> viewModel;
@Inject
public ViewModelInjectionFactory(Lazy<VM> viewModel) {
this.viewModel = viewModel;
}
@Override
class UploadFilesUseCase(
private val schedulerProvider: SchedulerProvider,
private val httpManager: HttpManager
) {
private var operation: Completable? = null
fun uploadFiles(): Completable = synchronized(this) {
operation
?: (doUploadFiles()
inline fun Modifier.conditional(
condition: Boolean,
ifTrue: Modifier.() -> Modifier,
ifFalse: Modifier.() -> Modifier = { this },
): Modifier = if (condition) {
then(ifTrue(this))
} else {
then(ifFalse(this))
}