Skip to content
This repository has been archived by the owner on Jan 13, 2022. It is now read-only.

Latest commit

 

History

History
38 lines (28 loc) · 1.03 KB

File metadata and controls

38 lines (28 loc) · 1.03 KB

AndroidX ViewModel Saved State extensions for Katana

The extensions declared in this artifact provide support for injection of ViewModel with Saved State module. Also see katana-androidx-viewmodel.

Create a ViewModel binding inside a module:

data class MyViewModel(
    val state: SavedStateHandle,
    val someDependency: SomeDependency
) : ViewModel() {

    var someValue: String?
        get() = state.get("SOME_VALUE")
        set(value) {
            state.set("SOME_VALUE", value)
        }
}

Module {
    
    viewModelSavedState { state -> MyViewModel(state, get()) }
}

Inject ViewModel in your Activity or Fragment:

class MyFragment : Fragment(),
                   KatanaTrait {
                   
    override val component = Component(...)
    
    private val viewModel by viewModelSavedState<MyViewModel, MyFragment>() 
}