|
| 1 | +package dev.johnoreilly.common.screens |
| 2 | + |
| 3 | +import androidx.compose.runtime.Composable |
| 4 | +import androidx.compose.runtime.collectAsState |
| 5 | +import androidx.compose.runtime.getValue |
| 6 | +import com.slack.circuit.runtime.CircuitContext |
| 7 | +import com.slack.circuit.runtime.CircuitUiEvent |
| 8 | +import com.slack.circuit.runtime.CircuitUiState |
| 9 | +import com.slack.circuit.runtime.Navigator |
| 10 | +import com.slack.circuit.runtime.presenter.Presenter |
| 11 | +import com.slack.circuit.runtime.screen.Screen |
| 12 | +import dev.johnoreilly.common.getCountryName |
| 13 | +import dev.johnoreilly.common.model.Network |
| 14 | +import dev.johnoreilly.common.remote.Station |
| 15 | +import dev.johnoreilly.common.repository.CityBikesRepository |
| 16 | +import dev.johnoreilly.common.viewmodel.Country |
| 17 | + |
| 18 | + |
| 19 | +@Target(AnnotationTarget.CLASS) |
| 20 | +@Retention(AnnotationRetention.BINARY) |
| 21 | +annotation class Parcelize |
| 22 | + |
| 23 | + |
| 24 | +@Parcelize |
| 25 | +data object CountryListScreen : Screen { |
| 26 | + data class State( |
| 27 | + val countryList: List<Country>, |
| 28 | + val eventSink: (Event) -> Unit |
| 29 | + ) : CircuitUiState |
| 30 | + |
| 31 | + sealed class Event : CircuitUiEvent { |
| 32 | + data class CountryClicked(val countryCode: String) : Event() |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +@Parcelize |
| 37 | +data class NetworkListScreen(val countryCode: String) : Screen { |
| 38 | + data class State( |
| 39 | + val countryCode: String, |
| 40 | + val countryName: String, |
| 41 | + val networkList: List<Network>, |
| 42 | + val eventSink: (Event) -> Unit |
| 43 | + ) : CircuitUiState |
| 44 | + |
| 45 | + sealed class Event : CircuitUiEvent { |
| 46 | + data class NetworkClicked(val networkId: String) : Event() |
| 47 | + data object BackClicked : Event() |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +@Parcelize |
| 52 | +data class StationListScreen(val networkId: String) : Screen { |
| 53 | + data class State( |
| 54 | + val networkId: String, |
| 55 | + val stationList: List<Station>, |
| 56 | + val eventSink: (Event) -> Unit |
| 57 | + ) : CircuitUiState |
| 58 | + |
| 59 | + sealed class Event : CircuitUiEvent { |
| 60 | + data object BackClicked : Event() |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | + |
| 65 | +// Presenters (TODO: where should we put these?) |
| 66 | + |
| 67 | +class CountryListPresenter( |
| 68 | + private val navigator: Navigator, |
| 69 | + private val cityBikesRepository: CityBikesRepository |
| 70 | +) : Presenter<CountryListScreen.State> { |
| 71 | + @Composable |
| 72 | + override fun present(): CountryListScreen.State { |
| 73 | + val groupedNetworkList by cityBikesRepository.groupedNetworkList.collectAsState() |
| 74 | + val countryCodeList = groupedNetworkList.keys.toList() |
| 75 | + val countryList = countryCodeList.map { countryCode -> Country(countryCode, getCountryName(countryCode)) } |
| 76 | + .sortedBy { it.displayName } |
| 77 | + return CountryListScreen.State(countryList) { event -> |
| 78 | + when (event) { |
| 79 | + is CountryListScreen.Event.CountryClicked -> navigator.goTo(NetworkListScreen(event.countryCode)) |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + class Factory(private val repository: CityBikesRepository) : Presenter.Factory { |
| 85 | + override fun create(screen: Screen, navigator: Navigator, context: CircuitContext): Presenter<*>? { |
| 86 | + return when (screen) { |
| 87 | + CountryListScreen -> return CountryListPresenter(navigator, repository) |
| 88 | + else -> null |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | + |
| 95 | +class NetworkListPresenter( |
| 96 | + private val screen: NetworkListScreen, |
| 97 | + private val navigator: Navigator, |
| 98 | + private val cityBikesRepository: CityBikesRepository |
| 99 | +) : Presenter<NetworkListScreen.State> { |
| 100 | + @Composable |
| 101 | + override fun present(): NetworkListScreen.State { |
| 102 | + val groupedNetworkList by cityBikesRepository.groupedNetworkList.collectAsState() |
| 103 | + val countryList = groupedNetworkList[screen.countryCode]?.sortedBy { it.city } ?: emptyList() |
| 104 | + val oountryName = getCountryName(screen.countryCode) |
| 105 | + return NetworkListScreen.State(screen.countryCode, oountryName, countryList) { event -> |
| 106 | + when (event) { |
| 107 | + is NetworkListScreen.Event.NetworkClicked -> navigator.goTo(StationListScreen(event.networkId)) |
| 108 | + NetworkListScreen.Event.BackClicked -> navigator.pop() |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + class Factory(private val repository: CityBikesRepository) : Presenter.Factory { |
| 114 | + override fun create(screen: Screen, navigator: Navigator, context: CircuitContext): Presenter<*>? { |
| 115 | + return when (screen) { |
| 116 | + is NetworkListScreen -> return NetworkListPresenter(screen, navigator, repository) |
| 117 | + else -> null |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | + |
| 124 | +class StationListPresenter( |
| 125 | + private val screen: StationListScreen, |
| 126 | + private val navigator: Navigator, |
| 127 | + private val cityBikesRepository: CityBikesRepository |
| 128 | +) : Presenter<StationListScreen.State> { |
| 129 | + @Composable |
| 130 | + override fun present(): StationListScreen.State { |
| 131 | + val stationList by cityBikesRepository.pollNetworkUpdates(screen.networkId).collectAsState(emptyList()) |
| 132 | + return StationListScreen.State(screen.networkId, stationList) { event -> |
| 133 | + when (event) { |
| 134 | + StationListScreen.Event.BackClicked -> navigator.pop() |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + class Factory(private val repository: CityBikesRepository) : Presenter.Factory { |
| 140 | + override fun create(screen: Screen, navigator: Navigator, context: CircuitContext): Presenter<*>? { |
| 141 | + return when (screen) { |
| 142 | + is StationListScreen -> return StationListPresenter(screen, navigator, repository) |
| 143 | + else -> null |
| 144 | + } |
| 145 | + } |
| 146 | + } |
| 147 | +} |
| 148 | + |
0 commit comments