我想使用raywenderlich approach来清理viewmodel类。
因此,他们在此博客中写道,我们有一个不适合用户的活页夹组件。但是我想使用此类作为viewmodel的帮助类。
我在这里创建一个我称为ViewModel的Binder类的类,我的视图将使用该类与ViewModel类建立联系。
现在,我的Binder类正在帮助我清理ViewModel类,例如,
已编辑:
所以我的View类将是:
class UserDetailView : AppCompatActivity(), UiCallbacks {
lateinit var screenCallbacks: ScreenCallbacks;
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
screenCallbacks = UserDetailBinder(this).bindLayout(R.layout.activity_learn_binding)
screenCallbacks.loadData()
}
override fun onUserNameChanged(userName: String) {
}
}
我的Binder类将是:
class UserDetailBinder(private val userDetailView: UserDetailView) : ScreenCallbacks, ViewModelController {
private val userDetailObservers = UserDetailViewState(userDetailView)
init {
userDetailObservers.observe(userDetailView)
}
fun bindLayout(layout: Int): ScreenCallbacks {
binding<com.bold.job.databinding.ActivityLearnBindingBinding, OwnViewModel>(userDetailView, layout).let {
var viewModel = viewModel(userDetailView, OwnViewModel::class.java)
it.viewModel = viewModel
}
return this
}
override fun loadData() {
}
}