0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

KotlinとClean ArchitectureとViewModelと

0
Last updated at Posted at 2021-08-06

KotlinでViewModelとPresenterとControllerをどうすれば楽にView層で使えるのか考察してみました。
ここでは架空のフレームワークを使っています。適宜利用するフレームワークに脳内変換してください。

Presenter->View

Clean Architectureでよくみる図のように、ViewはPresenterから発行される値をViewModelで監視します。

class ViewModel {
    internal val mutableHogeFlow = MutableStateFlow("")
    val hogeFlow: StateFlow<String>
        get() = mutableHogeFlow
}

class Presenter(private val viewModel: ViewModel): UsecaseOutputPort {
    override fun handle(hoge: String) {
        viewModel.mutableHogeFlow.value = hoge
    }
}
class View(private val viewModel): CoroutineScope {
    val hoge = viewModel.hogeFlow.value
    
    init() {
        launch {
            viewModel.hogeFlow.collect { hoge = it }
        }
    }
}

ViewModelではFlowを使ってView側に変更を通知できるようにします。

View->Controller

本題です。

入力用のViewModelを用意します。
ViewModelにon〇〇()のようなControllerの操作をするメソッドを持たせるのは責務違反なので、MVVMのようにViewModelに全てまとめるのではなく、Presenterで使うViewModelとControllerで使うViewModelは分けておくべきです。また、Controllerを操作する際は直接View側でViewModelの値をControllerで渡すべきです。

class ViewModel {
    var hoge = ""
}

class Controller {
    fun doSomething(hoge: String)
}

class View(val controller: Controller) {
    val viewModel = ViewModel()

    val button = Button(
        onClick = { controller.doSomething(viewModel.hoge) }
    )
    val text = Text(
        onChange = { viewModel.hoge = it }
    )
}

まとめ

PresenterとController用のViewModelは分けましょう。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?