Last active
March 27, 2019 02:41
-
-
Save ttymsd/847b7b83fdab26b9f17142d98c936cc7 to your computer and use it in GitHub Desktop.
アーキテクチャの試し書き
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class User | |
class Program | |
class MergedState(u: User, p: Program) | |
interface UserRepository { | |
suspend fun getUser(): User | |
} | |
interface ProgramRepository { | |
suspend fun getPrograms(): List<Program> | |
} | |
fun <X, Y, Z> zip(source1: LiveData<X>, source2: LiveData<Y>, merge: (X, Y) -> Z): LiveData<Z> { | |
return MediatorLiveData<Z>().apply { | |
addSource(source1) { s1 -> | |
val s2 = source2.value | |
if (s1 != null && s2 != null) postValue(merge(s1, s2)) | |
} | |
addSource(source2) { s2 -> | |
val s1 = source1.value | |
if (s1 != null && s2 != null) postValue(merge(s1, s2)) | |
} | |
} | |
} | |
class UserStore { | |
lateinit var userRepository: UserRepository | |
private val _user = MutableLiveData<User>() | |
val user: LiveData<User> | |
get() = _user | |
suspend fun getUser() { | |
_user.postValue(userRepository.getUser()) | |
} | |
} | |
class ProgramsStore { | |
lateinit var programRepository: ProgramRepository | |
private val _programs = MutableLiveData<List<Program>>() | |
val program: LiveData<List<Program>> | |
get() = _programs | |
suspend fun getPrograms() { | |
_programs.postValue(programRepository.getPrograms()) | |
} | |
} | |
class ActivityScopeViewModel : BaseViewModel() { | |
lateinit var userStore: UserStore | |
fun getUser() { | |
launchWithLoading { | |
userStore.getUser() | |
} | |
} | |
} | |
class FragmentScopeViewModel : BaseViewModel() { | |
lateinit var userStore: UserStore | |
lateinit var programsStore: ProgramsStore | |
val fragmentState: LiveData<List<MergedState>> | |
get() = zip(userStore.user, programsStore.program) { u, p -> | |
p.map { MergedState(u, it) } | |
} | |
fun getPrograms() { | |
launchWithLoading { | |
programsStore.getPrograms() | |
} | |
} | |
} | |
class DemoActivity : Activity() { | |
lateinit var activityScopeViewModel: ActivityScopeViewModel | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
activityScopeViewModel.getUser() | |
} | |
} | |
class DemoFragment : BaseFragment() { | |
lateinit var fragmentScopeViewModel: FragmentScopeViewModel | |
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | |
super.onViewCreated(view, savedInstanceState) | |
fragmentScopeViewModel.fragmentState.observe(this, Observer { | |
// bindingにわたすなり, RecyclerViewの構築を行うなり | |
}) | |
fragmentScopeViewModel.getPrograms() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
mobxのexample https://github.com/gothinkster/react-mobx-realworld-example-app
ViewModelから状態を切り出し、またRepositoryに状態を持たせないために、間にStoreをおいた形
ViewModelは複数の状態から画面に必要な状態を計算できるようになった
Repositoryは従来のCRUDにのみ関心を持つ