This article was inspired by this original post and this reply. Here I’m going to tell my observations about the mentioned problem.
We can reformulate the problem as “make value getter visible in all places and value setter visible only inside ViewModel
”. Let’s start with that statement and try to use Kotlin’s visibility structure to solve it.
Making value getter visible in all places is easy — it will just be public
(by default in Kotlin). In order to make value setter visible only inside ViewModel
, we need to make it private
and place inside ViewModel
. But there are many ViewModel
. So we can create class BaseViewModel
, place the value setter to that class and make it protected
. Here is where interesting things start.
#kotlin