-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: change cache policy during manual refresh (#339)
- Loading branch information
Showing
17 changed files
with
98 additions
and
76 deletions.
There are no files selected for viewing
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
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
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
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
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
32 changes: 14 additions & 18 deletions
32
...ed/data/src/commonMain/kotlin/fr/androidmakers/store/graphql/SpeakersGraphQLRepository.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,33 @@ | ||
package fr.androidmakers.store.graphql | ||
|
||
import com.apollographql.apollo.ApolloClient | ||
import com.apollographql.apollo.exception.DefaultApolloException | ||
import fr.androidmakers.domain.model.Speaker | ||
import fr.androidmakers.domain.repo.SpeakersRepository | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.map | ||
|
||
class SpeakersGraphQLRepository(private val apolloClient: ApolloClient) : SpeakersRepository { | ||
|
||
override fun getSpeakers(): Flow<Result<List<Speaker>>> { | ||
override fun getSpeakers(refresh: Boolean): Flow<Result<List<Speaker>>> { | ||
return apolloClient.query(GetSpeakersQuery()) | ||
.cacheAndNetwork() | ||
.map { it.map { it.speakers.map { it.speakerDetails.toSpeaker() } } } | ||
.cacheAndNetwork(refresh) | ||
.map { dataResult -> | ||
dataResult.map { data -> | ||
data.speakers.map { | ||
it.speakerDetails.toSpeaker() | ||
} | ||
} | ||
} | ||
} | ||
|
||
override fun getSpeaker(id: String): Flow<Result<Speaker>> { | ||
return apolloClient.query(GetSpeakersQuery()) | ||
.cacheAndNetwork() | ||
.map { | ||
it.fold( | ||
onSuccess = { value -> | ||
val speaker = value.speakers.map { it.speakerDetails }.singleOrNull { it.id == id }?.toSpeaker() | ||
if (speaker != null) { | ||
Result.success(speaker) | ||
} else { | ||
Result.failure(DefaultApolloException("Something wrong happened")) | ||
} | ||
}, | ||
onFailure = { exception -> | ||
Result.failure(exception) | ||
} | ||
) | ||
.map { dataResult -> | ||
dataResult.mapCatching { data -> | ||
data.speakers.firstOrNull { it.speakerDetails.id == id }?.speakerDetails?.toSpeaker() | ||
?: error("Speaker not found") | ||
} | ||
} | ||
} | ||
} |
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
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
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
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
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
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
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
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
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
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
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