class MyActiviy : LifecycleActivity() {
class MyActiviy : LifecycleActivity() {
private val myViewModelFactory = MyViewModelFactory()
private lateinit var myViewModel: MyViewModel
class MyActiviy : LifecycleActivity() {
private val myViewModelFactory = MyViewModelFactory()
private lateinit var myViewModel: MyViewModel
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
myViewModel = ViewModelProviders.of(this, myViewModelFactory)
.get(MyViewModel::class.java)
class MyActiviy : LifecycleActivity() {
private val myViewModelFactory = MyViewModelFactory()
private lateinit var myViewModel: MyViewModel
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
myViewModel = ViewModelProviders.of(this, myViewModelFactory)
.get(MyViewModel::class.java)
myViewModel.myLiveData.observe(this, Observer {
/* Update View */
})
}
}
class MyFragment : LifecycleFragment() {
private val myViewModelFactory = MyViewModelFactory()
private lateinit var myViewModel: MyViewModel
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
myViewModel = ViewModelProviders.of(this, myViewModelFactory)
.get(MyViewModel::class.java)
myViewModel.myLiveData.observe(this, Observer {
/* Update View */
})
}
}
class MyViewModelFactory : ViewModelProvider.Factory {
override fun <T : ViewModel> create(modelClass: Class<T>): T {
if (modelClass.isAssignableFrom(MyViewModel::class.java)) {
return MyViewModel() as T
}
throw IllegalArgumentException("Unknown MyViewModel class")
}
}
class MyViewModel : ViewModel() {
private val dataSubscription = CompositeDisposable()
class MyViewModel : ViewModel() {
private val dataSubscription = CompositeDisposable()
private var myViewState = MyViewState()
val myLiveData = MutableLiveData<MyViewState>()
class MyViewModel : ViewModel() {
private val dataSubscription = CompositeDisposable()
private var myViewState = MyViewState()
val myLiveData = MutableLiveData<MyViewState>()
init {
getData()
}
class MyViewModel : ViewModel() {
private val dataSubscription = CompositeDisposable()
private var myViewState = MyViewState()
val myLiveData = MutableLiveData<MyViewState>()
init {
getData()
}
fun getData() {
dataSubscriptions.add(myDataSource.getData())
.subscribeOn(Schedulers.computation())
.observeOn(Schedulers.computation())
class MyViewModel : ViewModel() {
private val dataSubscription = CompositeDisposable()
private var myViewState = MyViewState()
val myLiveData = MutableLiveData<MyViewState>()
init {
getData()
}
fun getData() {
dataSubscriptions.add(myDataSource.getData())
.subscribeOn(Schedulers.computation())
.observeOn(Schedulers.computation())
.subscribe({ data ->
myViewState = myViewState.copy(myData = data)
myLiveData.postValue(myViewState)
})
}
class MyViewModel : ViewModel() {
private val dataSubscription = CompositeDisposable()
private var myViewState = MyViewState()
val myLiveData = MutableLiveData<MyViewState>()
init {
getData()
}
fun getData() {
dataSubscriptions.add(myDataSource.getData())
.subscribeOn(Schedulers.computation())
.observeOn(Schedulers.computation())
.subscribe({ data ->
myViewState = myViewState.copy(myData = data)
myLiveData.postValue(myViewState)
})
}
override fun onCleared() {
dataSubscription.dispose()
super.onCleared()
}
}