Skip to content
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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions app/src/main/graphql/GameById.graphql
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
}
}
}
37 changes: 37 additions & 0 deletions app/src/main/java/com/cornellappdev/score/model/Game.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<List<String?>?>?,
Copy link
Collaborator

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.

val team: GameDetailsTeam?,
val boxScore: List<GameDetailsBoxScore>?
)



//Data for HomeScreen game displays
data class GameCardData(
val teamLogo: String,
Expand Down
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
Expand Up @@ -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
Expand All @@ -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.
Expand Down Expand Up @@ -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) {
Copy link
Collaborator

Choose a reason for hiding this comment

The 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 toResult function that I provided Amy. This will also make us more consistent. Ask her about this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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
}
}

}