Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save hasanmohdkhan/08230354e8475812720e27022bfe6474 to your computer and use it in GitHub Desktop.

Select an option

Save hasanmohdkhan/08230354e8475812720e27022bfe6474 to your computer and use it in GitHub Desktop.

Revisions

  1. @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
    @@ -11,5 +11,6 @@ class SomeViewModel(private val someString: String) : ViewModel() {

    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("someString") }
    }
  2. @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
    @@ -1,4 +1,4 @@
    // Override ViewModelProvider.NewInstanceFactory to create ViewModel (VM)
    // Override ViewModelProvider.NewInstanceFactory to create the ViewModel (VM).
    class SomeViewModelFactory(private val someString: String): ViewModelProvider.NewInstanceFactory() {
    override fun <T : ViewModel?> create(modelClass: Class<T>): T = SomeViewModel(someString) as T
    }
  3. @adam-hurwitz adam-hurwitz revised this gist Feb 8, 2020. No changes.
  4. @adam-hurwitz adam-hurwitz revised this gist Feb 8, 2020. 1 changed file with 5 additions and 5 deletions.
    10 changes: 5 additions & 5 deletions CustomViewModelFactory.kt
    Original file line number Diff line number Diff line change
    @@ -3,13 +3,13 @@ class SomeViewModelFactory(private val someString: String): ViewModelProvider.Ne
    override fun <T : ViewModel?> create(modelClass: Class<T>): T = SomeViewModel(someString) as T
    }

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

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

    class Fragment: Fragment() {
    // Create VM in activity/fragment with VM factory.
    val someViewModel: SomeViewModel by viewModels { SomeViewModelFactory("someString") }
    }
  5. @adam-hurwitz adam-hurwitz revised this gist Feb 8, 2020. No changes.
  6. @adam-hurwitz adam-hurwitz revised this gist Feb 8, 2020. 1 changed file with 8 additions and 2 deletions.
    10 changes: 8 additions & 2 deletions CustomViewModelFactory.kt
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,15 @@
    // Override ViewModelProvider.NewInstanceFactory to create ViewModel (VM)
    class CustomViewModelFactory(private val someString: String): ViewModelProvider.NewInstanceFactory() {
    class SomeViewModelFactory(private val someString: String): ViewModelProvider.NewInstanceFactory() {
    override fun <T : ViewModel?> create(modelClass: Class<T>): T = SomeViewModel(someString) as T
    }

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

    class SomeViewModel(private val someString: String) : ViewModel() {
    init {
    //TODO: Use 'someString' to init process when VM is created. i.e. Get data request.
    }
    }
  7. @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
    @@ -5,5 +5,5 @@ class CustomViewModelFactory(private val someString: String): ViewModelProvider.

    class Fragment: Fragment() {
    // Create VM in activity/fragment with VM factory.
    val feedViewModel: FeedViewModel by viewModels { CustomViewModelFactory("someString") }
    val contentViewModel: ContentViewModel by viewModels { CustomViewModelFactory("someString") }
    }
  8. @adam-hurwitz adam-hurwitz revised this gist Feb 8, 2020. 1 changed file with 4 additions and 2 deletions.
    6 changes: 4 additions & 2 deletions CustomViewModelFactory.kt
    Original file line number Diff line number Diff line change
    @@ -3,5 +3,7 @@ class CustomViewModelFactory(private val someString: String): ViewModelProvider.
    override fun <T : ViewModel?> create(modelClass: Class<T>): T = SomeViewModel(someString) as T
    }

    // Create VM in activity/fragment with VM factory.
    val feedViewModel: FeedViewModel by viewModels { CustomViewModelFactory("someString") }
    class Fragment: Fragment() {
    // Create VM in activity/fragment with VM factory.
    val feedViewModel: FeedViewModel by viewModels { CustomViewModelFactory("someString") }
    }
  9. @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
    @@ -3,5 +3,5 @@ class CustomViewModelFactory(private val someString: String): ViewModelProvider.
    override fun <T : ViewModel?> create(modelClass: Class<T>): T = SomeViewModel(someString) as T
    }

    // Create ViewModel in Activity/Fragment with VM factory.
    // Create VM in activity/fragment with VM factory.
    val feedViewModel: FeedViewModel by viewModels { CustomViewModelFactory("someString") }
  10. @adam-hurwitz adam-hurwitz created this gist Feb 8, 2020.
    7 changes: 7 additions & 0 deletions CustomViewModelFactory.kt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    // Override ViewModelProvider.NewInstanceFactory to create ViewModel (VM)
    class CustomViewModelFactory(private val someString: String): ViewModelProvider.NewInstanceFactory() {
    override fun <T : ViewModel?> create(modelClass: Class<T>): T = SomeViewModel(someString) as T
    }

    // Create ViewModel in Activity/Fragment with VM factory.
    val feedViewModel: FeedViewModel by viewModels { CustomViewModelFactory("someString") }