19. How to use
class MyViewModelFactory
:ViewModelProvider.Factory(useCase: MyUseCase) {
fun <T: ViewModel> create(aClass: Class<T>): T {
return MyViewModel(useCase) as T
}
}
64. Solves the one big android
question once and forever
who is the view?
65. ViewModel in data binding
<TextView
…
android:id="@+id/all_shows_item_title"
android:text="@{viewModel.title}" />
<data>
<variable name="viewModel"
type="com.vmn.playplex.main.allshows.AllShowsViewModel"/>
</data>
66. ViewModel in data binding
<android.support.v7.widget.CardView
…
android:onClick="@{() -> viewModel.onClicked()}">
<data>
<variable name="viewModel"
type="com.vmn.playplex.main.allshows.AllShowsViewModel"/>
</data>
67. How to use
class AllShowsViewModel: ViewModelObservable() {
var title : CharSequence = ""
68. How to use
class AllShowsViewModel: ViewModelObservable() {
@Bindable
var title : CharSequence = ""
private set(value) {
if (field != value) {
field = value
notifyPropertyChanged(BR.title)
}
}
69. How to use
class AllShowsViewModel: ViewModelObservable() {
@Bindable
var title by bindable<CharSequence>("")
private set
Custom property delegate
70. How to use
class AllShowsFragment : Fragment () {
@Inject
lateinit var showsViewModel: AllShowsViewModel
71. How to use
class AllShowsFragment : Fragment () {
@Inject
lateinit var showsViewModel: AllShowsViewModel
override fun onCreateView(…):View? =
72. How to use
class AllShowsFragment : Fragment () {
@Inject
lateinit var showsViewModel: AllShowsViewModel
override fun onCreateView(…):View? =
FragmentShowsBinding.inflate(
inflater, container, false).apply {
viewModel = showsViewModel
}).root
73. How to use
class AllShowsFragment : Fragment () {
@Inject
lateinit var showsViewModel: AllShowsViewModel
override fun onCreateView(…):View? =
FragmentShowsBinding.inflate(
inflater, container, false).apply {
viewModel = showsViewModel
}).root
fragment_shows.xml
74. How to use
class MyViewModel()
:ViewModelObservable() {
Coming soon
Jose Alcérreca, Google
https://medium.com/@dpreussler/add-the-new-viewmodel-to-your-mvvm-36bfea86b159
76. .. show a toast
class SeriesViewModel : Viewmodel() {
…
@Bindable
var error = ObservableField<String>()
77. .. show a toast
viewModel.error.addOnPropertyChangedCallback(
object : OnPropertyChangedCallback() {
override fun onPropertyChanged(…) {
showToast(viewModel.error.get()
}
})
78. .. show a toast (alternative)
<FrameLayout
app:showError="@{viewModel.error}">
@BindingAdapter("showError")
fun ViewGroup.onErrorAppeared(error: String?){
errorString?.let {
showToast(context, error))
}
}
79. Data binding full picture
XML
Activity ViewModel(un)bind
bind
Life cycle
aware class (un)bind
80. WAYS TO OBSERVE DATA from VM?
•Data binding Observable
from xml or code, might need unregister
•RxJava Observable
from code, needs unregister
•LiveData Observable,
from code, no unregister, life cycle aware
87. all problems solved?
ViewModels provide a convenient way to
retain data across configuration changes
but they are not persisted if the application
is killed by the operating system
https://developer.android.com/topic/libraries/architecture/viewmodel.html#viewm
odel_vs_savedinstancestate
100. lets tweak it
class MyModelFactory(val bundle: Bundle?)
:ViewModelProvider.Factory() {
…
fun <T: ViewModel> create(aClass: Class<T>): T {
return MyViewModel().apply {
readFrom(bundle)
} as T
}
...
101. lets tweak it
override onSaveInstanceState(bundle: Bundle){
super.onSaveInstanceState(bundle);
viewModel.writeTo(bundle);
}