-
Notifications
You must be signed in to change notification settings - Fork 0
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
Networking For GameDetails Page and ViewModel #28
base: main
Are you sure you want to change the base?
Changes from all commits
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 |
---|---|---|
@@ -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 | ||
) | ||
} | ||
|
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 _currentGameFlow = MutableStateFlow<ApiResponse<GameDetailsGame>>(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) { | ||
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 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! |
||
_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 | ||
} | ||
} | ||
|
||
} |
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.
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.