@@ -47,7 +47,11 @@ class EateryRepository @Inject constructor(private val networkApi: NetworkApi) {
4747 */
4848 val homeEateryFlow = _homeEateryFlow .asStateFlow()
4949
50- private var lastEateryId: Int? = null
50+ /* *
51+ * A map from eatery ids to the states representing their API loading calls.
52+ */
53+ private val eateryApiCache: MutableStateFlow <Map <Int , EateryApiResponse <Eatery >>> =
54+ MutableStateFlow (mapOf<Int , EateryApiResponse <Eatery >>().withDefault { EateryApiResponse .Error })
5155
5256 init {
5357 // Start loading backend as soon as the app initializes.
@@ -64,22 +68,23 @@ class EateryRepository @Inject constructor(private val networkApi: NetworkApi) {
6468 */
6569 private fun pingAllEateries () {
6670 _eateryFlow .value = EateryApiResponse .Pending
71+ eateryApiCache.update { map -> map.mapValues { EateryApiResponse .Pending } }
6772 CoroutineScope (Dispatchers .IO ).launch {
6873 try {
6974 val eateries = getAllEateries()
7075 _eateryFlow .value = EateryApiResponse .Success (eateries)
76+ eateryApiCache.update { map ->
77+ eateries.filter { it.id != null }
78+ .associate { it.id!! to EateryApiResponse .Success (it) }
79+ .withDefault { EateryApiResponse .Error }
80+ }
7181 } catch (_: Exception ) {
7282 _eateryFlow .value = EateryApiResponse .Error
83+ eateryApiCache.update { map -> map.mapValues { EateryApiResponse .Error } }
7384 }
7485 }
7586 }
7687
77- fun pingLastEatery () {
78- if (lastEateryId != null ) {
79- pingEatery(lastEateryId!! )
80- }
81- }
82-
8388 /* *
8489 * Makes a new call to backend for all the abridged home eatery data.
8590 */
@@ -95,12 +100,6 @@ class EateryRepository @Inject constructor(private val networkApi: NetworkApi) {
95100 }
96101 }
97102
98- /* *
99- * A map from eatery ids to the states representing their API loading calls.
100- */
101- private val eateryApiCache: MutableStateFlow <MutableMap <Int , EateryApiResponse <Eatery >>> =
102- MutableStateFlow (mutableMapOf ())
103-
104103 /* *
105104 * Makes a new call to backend for the specified eatery. After calling,
106105 * `eateryApiCache[eateryId]` is guaranteed to contain a state actively loading that eatery's
@@ -122,7 +121,7 @@ class EateryRepository @Inject constructor(private val networkApi: NetworkApi) {
122121
123122 private fun updateCache (eateryId : Int , response : EateryApiResponse <Eatery >) {
124123 eateryApiCache.update {
125- (it + (eateryId to response)).toMutableMap()
124+ (it + (eateryId to response)).withDefault { EateryApiResponse . Error }
126125 }
127126 }
128127
@@ -131,10 +130,9 @@ class EateryRepository @Inject constructor(private val networkApi: NetworkApi) {
131130 * If ALL eateries are already loaded, then this simply instantly returns that.
132131 */
133132 fun getEateryFlow (eateryId : Int ): Flow <EateryApiResponse <Eatery >> {
134- lastEateryId = eateryId
135133 if (! eateryApiCache.value.contains(eateryId)) {
136134 pingEatery(eateryId)
137135 }
138- return eateryApiCache.map { it[ eateryId] !! }
136+ return eateryApiCache.map { it.getValue( eateryId) }
139137 }
140138}
0 commit comments