Skip to content

Instantly share code, notes, and snippets.

@lukaspili
Created May 30, 2017 14:23
Show Gist options
  • Save lukaspili/36781cb9c5f0c06112313dbc053a6033 to your computer and use it in GitHub Desktop.
Save lukaspili/36781cb9c5f0c06112313dbc053a6033 to your computer and use it in GitHub Desktop.
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