需要思想吗?我可以正确清洁ViewModel类吗?

时间:2019-05-24 10:08:38

标签: android mvvm clean-architecture

我想使用raywenderlich approach来清理viewmodel类。

因此,他们在此博客中写道,我们有一个不适合用户的活页夹组件。但是我想使用此类作为viewmodel的帮助类。

enter image description here

我在这里创建一个我称为ViewModel的Binder类的类,我的视图将使用该类与ViewModel类建立联系。

enter image description here

现在,我的Binder类正在帮助我清理ViewModel类,例如, enter image description here

已编辑:

所以我的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() {

    }
}

0 个答案:

没有答案