-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from etiennelenhart/delegate-patch
Delegate patch
- Loading branch information
Showing
3 changed files
with
50 additions
and
3 deletions.
There are no files selected for viewing
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
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
47 changes: 47 additions & 0 deletions
47
eiffel/src/main/java/com/etiennelenhart/eiffel/viewmodel/delegate/SharedFactoryVieModel.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package com.etiennelenhart.eiffel.viewmodel.delegate | ||
|
||
import android.arch.lifecycle.ViewModel | ||
import android.arch.lifecycle.ViewModelProvider | ||
import android.arch.lifecycle.ViewModelProviders | ||
import android.support.v4.app.Fragment | ||
import kotlin.properties.ReadOnlyProperty | ||
import kotlin.reflect.KProperty | ||
|
||
/** | ||
* Property delegate to lazily provide a shared [ViewModel] instantiated by a [ViewModelProvider.Factory] | ||
* from the [ViewModelProvider] of this [Fragment]s Activity. | ||
* | ||
* May be used in a [Fragment] like this: | ||
* ``` | ||
* val viewModel by SharedFactoryViewModel(SampleViewModel::class.java, sampleFactory) | ||
* ``` | ||
* | ||
* @param[T] Type of the provided view model. | ||
* @param[viewModelClass] Java class of the provided view model. | ||
* @param[factory] Block to lazily get the factory to instantiate the view model. | ||
*/ | ||
class SharedFactoryViewModel<out T : ViewModel>(private val viewModelClass: Class<T>, | ||
private val factory: () -> ViewModelProvider.Factory) : ReadOnlyProperty<Fragment, T> { | ||
|
||
private var value: T? = null | ||
|
||
override fun getValue(thisRef: Fragment, property: KProperty<*>): T { | ||
if (value == null) value = ViewModelProviders.of(thisRef.activity!!).get(viewModelClass) | ||
return value!! | ||
} | ||
} | ||
|
||
/** | ||
* Convenience inline function to use [SharedFactoryViewModel] delegate with type parameter instead of Java class. | ||
* | ||
* May be used in a [Fragment] like this: | ||
* ``` | ||
* val viewModel by sharedViewModel<SampleViewModel>(sampleFactory) | ||
* ``` | ||
* | ||
* @param[T] Type of the provided view model. | ||
* @param[factory] Block to lazily get the factory to instantiate the view model. | ||
*/ | ||
inline fun <reified T : ViewModel> Fragment.sharedViewModel(noinline factory: () -> ViewModelProvider.Factory): SharedFactoryViewModel<T> { | ||
return SharedFactoryViewModel(T::class.java, factory) | ||
} |