-
Notifications
You must be signed in to change notification settings - Fork 0
Networking For GameDetails Page and ViewModel #28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,6 +15,45 @@ data class Game( | |
| val city: String | ||
| ) | ||
|
|
||
| /** | ||
| * Clean up? | ||
| */ | ||
| 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<List<String?>?>?, | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. omg this type is heinous with all the nulls 😭 I think we should ask backend if it's possible to clean this up a little. Please tell them in the backend Slack that many of the fields are nullable, and ask if it is possible to be cleaned up. It might not be though. |
||
| val team: GameDetailsTeam?, | ||
| val boxScore: List<GameDetailsBoxScore>? | ||
| ) | ||
|
|
||
|
|
||
| //Data for HomeScreen game displays | ||
| data class GameCardData( | ||
| val teamLogo: String, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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<List<Game>>>(ApiResponse.Loading) | ||
| val upcomingGamesFlow = _upcomingGamesFlow.asStateFlow() | ||
|
|
||
|
|
||
| private val _currGameFlow = MutableStateFlow<ApiResponse<GameDetailsGame>>(ApiResponse.Loading) | ||
| val currGamesFlow = _currGameFlow.asStateFlow() | ||
|
||
| /** | ||
| * Asynchronously fetches the list of games from the API. Once finished, will send down | ||
| * `upcomingGamesFlow` to be observed. | ||
|
|
@@ -58,4 +62,58 @@ class ScoreRepository @Inject constructor( | |
| _upcomingGamesFlow.value = ApiResponse.Error | ||
| } | ||
| } | ||
|
|
||
| fun getGameById(id: String) = appScope.launch { | ||
| _currGameFlow.value = ApiResponse.Loading | ||
| try { | ||
| val response = apolloClient.query(GameByIdQuery(id)).execute() | ||
| val game = response.data?.game | ||
|
|
||
| if (game != null) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To know if there's an error, I think you should use the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have asked Amy, and I am waiting for her response! |
||
| val temp = GameDetailsGame( | ||
|
||
| id = game.id ?: "", | ||
| city = game.city, | ||
| date = game.date, | ||
| gender = game.gender, | ||
| location = game.location, | ||
| opponentId = game.opponentId, | ||
| result = game.result, | ||
| sport = game.sport, | ||
| state = game.state, | ||
| time = game.time, | ||
| scoreBreakdown = game.scoreBreakdown, | ||
| team = game.team?.let { team -> | ||
| GameDetailsTeam( | ||
| id = team.id, | ||
| color = team.color, | ||
| image = team.image, | ||
| name = team.name | ||
| ) | ||
| }, | ||
| boxScore = game.boxScore?.mapNotNull { boxScore -> | ||
| boxScore?.let { | ||
| GameDetailsBoxScore( | ||
| team = it.team, | ||
| period = it.period, | ||
| time = it.time, | ||
| description = it.description, | ||
| scorer = it.scorer, | ||
| assist = it.assist, | ||
| scoreBy = it.scoreBy, | ||
| corScore = it.corScore, | ||
| oppScore = it.oppScore | ||
| ) | ||
| } | ||
| } | ||
| ) | ||
| _currGameFlow.value = ApiResponse.Success(temp) | ||
| } else { | ||
| _currGameFlow.value = ApiResponse.Error | ||
| } | ||
| } catch (e: Exception) { | ||
| Log.e("ScoreRepository", "Error fetching game with id: ${id}: ", e) | ||
| _currGameFlow.value = ApiResponse.Error | ||
| } | ||
| } | ||
|
|
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't need this comment here anymore, I think it's fine to create custom data classes like this because it creates a layer of abstraction between our Apollo types and the data that our app handles.