Skip to content

Instantly share code, notes, and snippets.

Revisions

  1. @adam-hurwitz adam-hurwitz revised this gist Apr 8, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion CustomViewModelFactory.kt
    Original file line number Diff line number Diff line change
    @@ -21,7 +21,7 @@ class SomeViewModel(private val state: SavedStateHandle, private val someString:

    class Fragment: Fragment() {
    // Create VM in activity/fragment with VM factory.
    // Don't forget to add "kotlinOptions { jvmTarget = '1.8' }" to build.gradle (:app)
    // Don't forget to add "kotlinOptions { jvmTarget = '1.8' }" to "android" build.gradle (:app)
    val someViewModel: SomeViewModel by viewModels { SomeViewModelFactory(
    savedStateRegistryOwner = this,
    defaultArgs = Bundle().apply {
  2. @adam-hurwitz adam-hurwitz revised this gist Apr 8, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion CustomViewModelFactory.kt
    Original file line number Diff line number Diff line change
    @@ -21,7 +21,7 @@ class SomeViewModel(private val state: SavedStateHandle, private val someString:

    class Fragment: Fragment() {
    // Create VM in activity/fragment with VM factory.
    // Don't forget to add 'kotlinOptions { jvmTarget = '1.8' } to build.gradle (:app)'
    // Don't forget to add "kotlinOptions { jvmTarget = '1.8' }" to build.gradle (:app)
    val someViewModel: SomeViewModel by viewModels { SomeViewModelFactory(
    savedStateRegistryOwner = this,
    defaultArgs = Bundle().apply {
  3. @adam-hurwitz adam-hurwitz revised this gist Apr 8, 2020. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions CustomViewModelFactory.kt
    Original file line number Diff line number Diff line change
    @@ -21,6 +21,7 @@ class SomeViewModel(private val state: SavedStateHandle, private val someString:

    class Fragment: Fragment() {
    // Create VM in activity/fragment with VM factory.
    // Don't forget to add 'kotlinOptions { jvmTarget = '1.8' } to build.gradle (:app)'
    val someViewModel: SomeViewModel by viewModels { SomeViewModelFactory(
    savedStateRegistryOwner = this,
    defaultArgs = Bundle().apply {
  4. @adam-hurwitz adam-hurwitz revised this gist Feb 27, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion CustomViewModelFactory.kt
    Original file line number Diff line number Diff line change
    @@ -24,7 +24,7 @@ class Fragment: Fragment() {
    val someViewModel: SomeViewModel by viewModels { SomeViewModelFactory(
    savedStateRegistryOwner = this,
    defaultArgs = Bundle().apply {
    putInt("SOME_INT_KEY", 18)
    putInt(SOME_INT_KEY, 18)
    },
    someString = "someString") }
    private var someInt: Int = 0
  5. @adam-hurwitz adam-hurwitz revised this gist Feb 27, 2020. 1 changed file with 5 additions and 6 deletions.
    11 changes: 5 additions & 6 deletions CustomViewModelFactory.kt
    Original file line number Diff line number Diff line change
    @@ -3,13 +3,12 @@ class SomeViewModelFactory(
    private val defaultArgs: Bundle,
    private val someString: String) : AbstractSavedStateViewModelFactory(owner, defaultArgs) {
    override fun <T : ViewModel?> create(key: String, modelClass: Class<T>, state: SavedStateHandle) =
    SomeViewModel(state, defaultArgs.getString("DEFAULT_STRING"), someString) as T
    SomeViewModel(state, someString) as T
    }

    class SomeViewModel(private val state: SavedStateHandle, private val defaultString: String, private val someString: String) : ViewModel() {
    val someInt = state.get<Int>(SOME_INT_KEY).let { someInt ->
    if (someInt == null) 0 else someInt
    }
    class SomeViewModel(private val state: SavedStateHandle, private val someString: String) : ViewModel() {
    // The default value is used from 'defaultArgs' since 'defaultArgs' are saved in the ViewModelFactory.
    val someInt = state.get<Int>(SOME_INT_KEY)

    init {
    //TODO: Use 'defaultString' and 'someString' to init process when VM is created. i.e. Get data request.
    @@ -25,7 +24,7 @@ class Fragment: Fragment() {
    val someViewModel: SomeViewModel by viewModels { SomeViewModelFactory(
    savedStateRegistryOwner = this,
    defaultArgs = Bundle().apply {
    putString("DEFAULT_STRING", "defaultValue")
    putInt("SOME_INT_KEY", 18)
    },
    someString = "someString") }
    private var someInt: Int = 0
  6. @adam-hurwitz adam-hurwitz revised this gist Feb 27, 2020. 1 changed file with 11 additions and 5 deletions.
    16 changes: 11 additions & 5 deletions CustomViewModelFactory.kt
    Original file line number Diff line number Diff line change
    @@ -1,17 +1,18 @@
    class SomeViewModelFactory(
    private val owner: SavedStateRegistryOwner,
    private val someString: String) : AbstractSavedStateViewModelFactory(owner, null) {
    private val defaultArgs: Bundle,
    private val someString: String) : AbstractSavedStateViewModelFactory(owner, defaultArgs) {
    override fun <T : ViewModel?> create(key: String, modelClass: Class<T>, state: SavedStateHandle) =
    SomeViewModel(state, someString) as T
    SomeViewModel(state, defaultArgs.getString("DEFAULT_STRING"), someString) as T
    }

    class SomeViewModel(private val state: SavedStateHandle, private val someString: String) : ViewModel() {
    class SomeViewModel(private val state: SavedStateHandle, private val defaultString: String, private val someString: String) : ViewModel() {
    val someInt = state.get<Int>(SOME_INT_KEY).let { someInt ->
    if (someInt == null) 0 else someInt
    }

    init {
    //TODO: Use 'someString' to init process when VM is created. i.e. Get data request.
    //TODO: Use 'defaultString' and 'someString' to init process when VM is created. i.e. Get data request.
    }

    fun saveSomeInt(someInt: Int) {
    @@ -21,7 +22,12 @@ class SomeViewModel(private val state: SavedStateHandle, private val someString:

    class Fragment: Fragment() {
    // Create VM in activity/fragment with VM factory.
    val someViewModel: SomeViewModel by viewModels { SomeViewModelFactory(this, "someString") }
    val someViewModel: SomeViewModel by viewModels { SomeViewModelFactory(
    savedStateRegistryOwner = this,
    defaultArgs = Bundle().apply {
    putString("DEFAULT_STRING", "defaultValue")
    },
    someString = "someString") }
    private var someInt: Int = 0

    override fun onSaveInstanceState(outState: Bundle) {
  7. @adam-hurwitz adam-hurwitz revised this gist Feb 26, 2020. 1 changed file with 7 additions and 8 deletions.
    15 changes: 7 additions & 8 deletions CustomViewModelFactory.kt
    Original file line number Diff line number Diff line change
    @@ -6,32 +6,31 @@ class SomeViewModelFactory(
    }

    class SomeViewModel(private val state: SavedStateHandle, private val someString: String) : ViewModel() {
    val feedPosition = state.get<Int>(FEED_POSITION_KEY).let { position ->
    if (position == null) 0 else position
    val someInt = state.get<Int>(SOME_INT_KEY).let { someInt ->
    if (someInt == null) 0 else someInt
    }

    init {
    //TODO: Use 'someString' to init process when VM is created. i.e. Get data request.
    }

    fun saveFeedPosition(position: Int) {
    state.set(FEED_POSITION_KEY, position)
    fun saveSomeInt(someInt: Int) {
    state.set(SOME_INT_KEY, position)
    }
    }

    class Fragment: Fragment() {
    // Create VM in activity/fragment with VM factory.
    val someViewModel: SomeViewModel by viewModels { SomeViewModelFactory(this, "someString") }
    private var feedPosition: Int = 0
    private var someInt: Int = 0

    override fun onSaveInstanceState(outState: Bundle) {
    super.onSaveInstanceState(outState)
    someViewModel.saveFeedPosition((contentRecyclerView.layoutManager as LinearLayoutManager)
    .findFirstVisibleItemPosition())
    someViewModel.saveSomeInt(...)
    }

    override fun onViewStateRestored(savedInstanceState: Bundle?) {
    super.onViewStateRestored(savedInstanceState)
    feedPosition = someViewModel.feedPosition
    someInt = someViewModel.someInt
    }
    }
  8. @adam-hurwitz adam-hurwitz revised this gist Feb 8, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion CustomViewModelFactory.kt
    Original file line number Diff line number Diff line change
    @@ -32,6 +32,6 @@ class Fragment: Fragment() {

    override fun onViewStateRestored(savedInstanceState: Bundle?) {
    super.onViewStateRestored(savedInstanceState)
    savedRecyclerPosition = someViewModel.feedPosition
    feedPosition = someViewModel.feedPosition
    }
    }
  9. @adam-hurwitz adam-hurwitz revised this gist Feb 8, 2020. No changes.
  10. @adam-hurwitz adam-hurwitz revised this gist Feb 8, 2020. 1 changed file with 14 additions and 14 deletions.
    28 changes: 14 additions & 14 deletions CustomViewModelFactory.kt
    Original file line number Diff line number Diff line change
    @@ -5,6 +5,20 @@ class SomeViewModelFactory(
    SomeViewModel(state, someString) as T
    }

    class SomeViewModel(private val state: SavedStateHandle, private val someString: String) : ViewModel() {
    val feedPosition = state.get<Int>(FEED_POSITION_KEY).let { position ->
    if (position == null) 0 else position
    }

    init {
    //TODO: Use 'someString' to init process when VM is created. i.e. Get data request.
    }

    fun saveFeedPosition(position: Int) {
    state.set(FEED_POSITION_KEY, position)
    }
    }

    class Fragment: Fragment() {
    // Create VM in activity/fragment with VM factory.
    val someViewModel: SomeViewModel by viewModels { SomeViewModelFactory(this, "someString") }
    @@ -20,18 +34,4 @@ class Fragment: Fragment() {
    super.onViewStateRestored(savedInstanceState)
    savedRecyclerPosition = someViewModel.feedPosition
    }
    }

    class SomeViewModel(private val state: SavedStateHandle, private val someString: String) : ViewModel() {
    val feedPosition = state.get<Int>(FEED_POSITION_KEY).let { position ->
    if (position == null) 0 else position
    }

    init {
    //TODO: Use 'someString' to init process when VM is created. i.e. Get data request.
    }

    fun saveFeedPosition(position: Int) {
    state.set(FEED_POSITION_KEY, position)
    }
    }
  11. @adam-hurwitz adam-hurwitz revised this gist Feb 8, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion CustomViewModelFactory.kt
    Original file line number Diff line number Diff line change
    @@ -12,7 +12,7 @@ class Fragment: Fragment() {

    override fun onSaveInstanceState(outState: Bundle) {
    super.onSaveInstanceState(outState)
    someViewModel.saveSomeInt((contentRecyclerView.layoutManager as LinearLayoutManager)
    someViewModel.saveFeedPosition((contentRecyclerView.layoutManager as LinearLayoutManager)
    .findFirstVisibleItemPosition())
    }

  12. @adam-hurwitz adam-hurwitz revised this gist Feb 8, 2020. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions CustomViewModelFactory.kt
    Original file line number Diff line number Diff line change
    @@ -12,13 +12,13 @@ class Fragment: Fragment() {

    override fun onSaveInstanceState(outState: Bundle) {
    super.onSaveInstanceState(outState)
    feedViewModel.saveSomeInt((contentRecyclerView.layoutManager as LinearLayoutManager)
    someViewModel.saveSomeInt((contentRecyclerView.layoutManager as LinearLayoutManager)
    .findFirstVisibleItemPosition())
    }

    override fun onViewStateRestored(savedInstanceState: Bundle?) {
    super.onViewStateRestored(savedInstanceState)
    savedRecyclerPosition = feedViewModel.feedPosition
    savedRecyclerPosition = someViewModel.feedPosition
    }
    }

  13. @adam-hurwitz adam-hurwitz revised this gist Feb 8, 2020. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions CustomViewModelFactory.kt
    Original file line number Diff line number Diff line change
    @@ -8,6 +8,7 @@ class SomeViewModelFactory(
    class Fragment: Fragment() {
    // Create VM in activity/fragment with VM factory.
    val someViewModel: SomeViewModel by viewModels { SomeViewModelFactory(this, "someString") }
    private var feedPosition: Int = 0

    override fun onSaveInstanceState(outState: Bundle) {
    super.onSaveInstanceState(outState)
  14. @adam-hurwitz adam-hurwitz revised this gist Feb 8, 2020. No changes.
  15. @adam-hurwitz adam-hurwitz revised this gist Feb 8, 2020. 1 changed file with 15 additions and 4 deletions.
    19 changes: 15 additions & 4 deletions CustomViewModelFactory.kt
    Original file line number Diff line number Diff line change
    @@ -8,18 +8,29 @@ class SomeViewModelFactory(
    class Fragment: Fragment() {
    // Create VM in activity/fragment with VM factory.
    val someViewModel: SomeViewModel by viewModels { SomeViewModelFactory(this, "someString") }

    override fun onSaveInstanceState(outState: Bundle) {
    super.onSaveInstanceState(outState)
    feedViewModel.saveSomeInt((contentRecyclerView.layoutManager as LinearLayoutManager)
    .findFirstVisibleItemPosition())
    }

    override fun onViewStateRestored(savedInstanceState: Bundle?) {
    super.onViewStateRestored(savedInstanceState)
    savedRecyclerPosition = feedViewModel.feedPosition
    }
    }

    class SomeViewModel(private val state: SavedStateHandle, private val someString: String) : ViewModel() {
    val someInt = state.get<Int>(SOME_KEY).let { someInt ->
    if (someInt == null) 0 else someInt
    val feedPosition = state.get<Int>(FEED_POSITION_KEY).let { position ->
    if (position == null) 0 else position
    }

    init {
    //TODO: Use 'someString' to init process when VM is created. i.e. Get data request.
    }

    fun saveSomething(someInt: Int) {
    state.set(SOME_KEY, someInt)
    fun saveFeedPosition(position: Int) {
    state.set(FEED_POSITION_KEY, position)
    }
    }
  16. @adam-hurwitz adam-hurwitz revised this gist Feb 8, 2020. 1 changed file with 8 additions and 0 deletions.
    8 changes: 8 additions & 0 deletions CustomViewModelFactory.kt
    Original file line number Diff line number Diff line change
    @@ -11,7 +11,15 @@ class Fragment: Fragment() {
    }

    class SomeViewModel(private val state: SavedStateHandle, private val someString: String) : ViewModel() {
    val someInt = state.get<Int>(SOME_KEY).let { someInt ->
    if (someInt == null) 0 else someInt
    }

    init {
    //TODO: Use 'someString' to init process when VM is created. i.e. Get data request.
    }

    fun saveSomething(someInt: Int) {
    state.set(SOME_KEY, someInt)
    }
    }
  17. @adam-hurwitz adam-hurwitz created this gist Feb 8, 2020.
    17 changes: 17 additions & 0 deletions CustomViewModelFactory.kt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    class SomeViewModelFactory(
    private val owner: SavedStateRegistryOwner,
    private val someString: String) : AbstractSavedStateViewModelFactory(owner, null) {
    override fun <T : ViewModel?> create(key: String, modelClass: Class<T>, state: SavedStateHandle) =
    SomeViewModel(state, someString) as T
    }

    class Fragment: Fragment() {
    // Create VM in activity/fragment with VM factory.
    val someViewModel: SomeViewModel by viewModels { SomeViewModelFactory(this, "someString") }
    }

    class SomeViewModel(private val state: SavedStateHandle, private val someString: String) : ViewModel() {
    init {
    //TODO: Use 'someString' to init process when VM is created. i.e. Get data request.
    }
    }