Last active
March 26, 2019 09:22
-
-
Save ttymsd/3fb10c32d8b95657e234f7bb4e05e742 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)) | |
} | |
} | |
} | |
sealed class Action { | |
class UserAction(val user: User) : Action() | |
class ProgramsAction(val programs: List<Program>) : Action() | |
} | |
interface Dispatcher { | |
fun dispatch(action: Action) | |
} | |
class UserStore { | |
private val _user = MutableLiveData<User>() | |
val user: LiveData<User> | |
get() = _user | |
fun on(action: Action.UserAction) { | |
_user.postValue(action.user) | |
} | |
} | |
class ProgramsStore { | |
private val _programs = MutableLiveData<List<Program>>() | |
val program: LiveData<List<Program>> | |
get() = _programs | |
fun on(action: Action.ProgramsAction) { | |
_programs.postValue(action.programs) | |
} | |
} | |
class UserActionCreator { | |
lateinit var userRepository: UserRepository | |
fun getUserAction(): Action { | |
return Action.UserAction(userRepository.getUser()) | |
} | |
} | |
class ProgramsActionCreator : BaseActionCreator() { | |
lateinit var dispatcher: Dispatcher | |
lateinit var programsRepository: ProgramRepository | |
fun getProgramsAction(): Action { | |
return Action.ProgramsAction(programsRepository.getPrograms()) | |
} | |
} | |
class ActivityScopeViewModel : BaseViewModel() { | |
lateinit var dispatcher: Dispatcher | |
lateinit var actionCreator: UserActionCreator | |
fun getUser() { | |
launchWithLoading { | |
dispatcher.dispatch(actionCreator.getUserAction()) | |
} | |
} | |
} | |
class FragmentScopeViewModel : BaseViewModel() { | |
lateinit var dispatcher: Dispatcher | |
lateinit var userStore: UserStore | |
lateinit var programsStore: ProgramsStore | |
lateinit var actionCreator: ProgramsActionCreator | |
val fragmentState: LiveData<List<MergedState>> | |
get() = zip(userStore.user, programsStore.program) { u, p -> | |
p.map { MergedState(u, it) } | |
} | |
fun getPrograms() { | |
launchWithLoading { | |
dispatcher.dispatch(actionCreator.getProgramsAction()) | |
} | |
} | |
} | |
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() | |
} | |
} |
Author
ttymsd
commented
Mar 26, 2019
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment