Skip to content

Instantly share code, notes, and snippets.

@lukaspili
Created May 30, 2017
Embed
What would you like to do?
override fun onAttach(view: HomeView) {
view.events()
.flatMap(this::processEvent)
.subscribe(view::render)
.toDisposeBag()
}
private fun processEvent(event: HomeViewEvent): Observable<HomeViewModel> = when (event) {
is HomeViewEvent.MapReady -> Observable.empty()
is HomeViewEvent.MapRefresh -> loadStations(event.bounds)
else -> illegalAccess()
}
private fun loadStations(bounds: LatLngBounds): Observable<HomeViewModel> {
return stationRepository.find(bounds)
.map {
viewModel.copy(
stations = it.map { HomeViewModel.Station(it, false) }
)
}
}
fun render(viewModel: HomeViewModel) {
}
fun events(): Observable<out HomeViewEvent> = Observable.merge(mapReady(), mapEvents())
private fun mapReady() = rxMapView.ready(home_view_mapview)
.doOnSuccess { map = it }
.map { HomeViewEvent.MapReady() }
.toObservable()
private fun mapEvents() = rxMapView.events
.map { event ->
when (event) {
RxMapView.Event.CameraIdle -> HomeViewEvent.MapRefresh(map.projection.visibleRegion.latLngBounds)
else -> throw IllegalAccessException()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment