diff --git a/app/src/main/graphql/GameById.graphql b/app/src/main/graphql/GameById.graphql new file mode 100644 index 0000000..aced4b7 --- /dev/null +++ b/app/src/main/graphql/GameById.graphql @@ -0,0 +1,32 @@ +query GameById($id: String!) { + game(id: $id){ + id + city + date + gender + location + opponentId + result + sport + state + time + scoreBreakdown + team { + id + color + image + name + } + boxScore { + team + period + time + description + scorer + assist + scoreBy + corScore + oppScore + } + } +} \ No newline at end of file diff --git a/app/src/main/java/com/cornellappdev/score/model/Game.kt b/app/src/main/java/com/cornellappdev/score/model/Game.kt index 7a71627..5ec9399 100644 --- a/app/src/main/java/com/cornellappdev/score/model/Game.kt +++ b/app/src/main/java/com/cornellappdev/score/model/Game.kt @@ -15,6 +15,43 @@ data class Game( val city: String ) +data class GameDetailsTeam( + val id: String?, + val color: String, + val image: String?, + val name: String +) + +data class GameDetailsBoxScore( + val team: String?, + val period: String?, + val time: String?, + val description: String?, + val scorer: String?, + val assist: String?, + val scoreBy: String?, + val corScore: Int?, + val oppScore: Int? +) + +data class GameDetailsGame( + val id: String?, + val city: String, + val date: String, + val gender: String, + val location: String?, + val opponentId: String, + val result: String?, + val sport: String, + val state: String, + val time: String?, + val scoreBreakdown: List?>?, + val team: GameDetailsTeam?, + val boxScore: List? +) + + + //Data for HomeScreen game displays data class GameCardData( val teamLogo: String, diff --git a/app/src/main/java/com/cornellappdev/score/model/GameByIdQueryMappers.kt b/app/src/main/java/com/cornellappdev/score/model/GameByIdQueryMappers.kt new file mode 100644 index 0000000..fe4213e --- /dev/null +++ b/app/src/main/java/com/cornellappdev/score/model/GameByIdQueryMappers.kt @@ -0,0 +1,44 @@ +package com.cornellappdev.score.model + +import com.example.score.GameByIdQuery + +fun GameByIdQuery.Game.toGameDetails(): GameDetailsGame { + return GameDetailsGame( + id = this.id ?: "", + city = this.city, + date = this.date, + gender = this.gender, + location = this.location, + opponentId = this.opponentId, + result = this.result, + sport = this.sport, + state = this.state, + time = this.time, + scoreBreakdown = this.scoreBreakdown, + team = this.team?.toGameDetailsTeam(), + boxScore = this.boxScore?.mapNotNull { it?.toGameDetailsBoxScore() } + ) +} +fun GameByIdQuery.Team.toGameDetailsTeam(): GameDetailsTeam { + return GameDetailsTeam( + id = this.id, + color = this.color, + image = this.image, + name = this.name + ) +} + +fun GameByIdQuery.BoxScore.toGameDetailsBoxScore(): GameDetailsBoxScore { + return GameDetailsBoxScore( + team = this.team, + period = this.period, + time = this.time, + description = this.description, + scorer = this.scorer, + assist = this.assist, + scoreBy = this.scoreBy, + corScore = this.corScore, + oppScore = this.oppScore + ) +} + diff --git a/app/src/main/java/com/cornellappdev/score/model/ScoreRepository.kt b/app/src/main/java/com/cornellappdev/score/model/ScoreRepository.kt index 073c0e4..dd9aba9 100644 --- a/app/src/main/java/com/cornellappdev/score/model/ScoreRepository.kt +++ b/app/src/main/java/com/cornellappdev/score/model/ScoreRepository.kt @@ -2,10 +2,13 @@ package com.cornellappdev.score.model import android.util.Log import com.apollographql.apollo.ApolloClient +import com.example.score.GameByIdQuery import com.example.score.GamesQuery import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.asStateFlow +import kotlinx.coroutines.flow.map import kotlinx.coroutines.launch import javax.inject.Inject import javax.inject.Singleton @@ -26,7 +29,8 @@ class ScoreRepository @Inject constructor( MutableStateFlow>>(ApiResponse.Loading) val upcomingGamesFlow = _upcomingGamesFlow.asStateFlow() - + private val _currentGameFlow = MutableStateFlow>(ApiResponse.Loading) + val currentGamesFlow = _currentGameFlow.asStateFlow() /** * Asynchronously fetches the list of games from the API. Once finished, will send down * `upcomingGamesFlow` to be observed. @@ -58,4 +62,26 @@ class ScoreRepository @Inject constructor( _upcomingGamesFlow.value = ApiResponse.Error } } + + /** + * Asynchronously fetches game details for a particular game. Once finished, will send down + * `currentGamesFlow` to be observed. + */ + fun getGameById(id: String) = appScope.launch { + _currentGameFlow.value = ApiResponse.Loading + try { + val response = apolloClient.query(GameByIdQuery(id)).execute() + val game = response.data?.game + + if (game != null) { + _currentGameFlow.value = ApiResponse.Success(game.toGameDetails()) + } else { + _currentGameFlow.value = ApiResponse.Error + } + } catch (e: Exception) { + Log.e("ScoreRepository", "Error fetching game with id: ${id}: ", e) + _currentGameFlow.value = ApiResponse.Error + } + } + } \ No newline at end of file