Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
}
}
}
39 changes: 39 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,45 @@ data class Game(
val city: String
)

/**
* Clean up?
*/
Copy link
Collaborator

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.

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
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 _currGameFlow = MutableStateFlow<ApiResponse<GameDetailsGame>>(ApiResponse.Loading)
val currGamesFlow = _currGameFlow.asStateFlow()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: I always prefer full length variable names. We have auto complete so they don't slow us down when typing, and they're just more readable. So perhaps change this to currentGameFlow. Or better yet, gameByIdFlow.

/**
* 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,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) {
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!

val temp = GameDetailsGame(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Logically this makes sense but there's a way that you can clean it up with extension functions. Let's create a new file GameByIdQueryMappers that help us map Apollo types to our custom data classes. Then we can add each of these as extension functions. Here's an example:

fun GameByIdQuery.Game.toGameDetails(): GameDetailsGame {
    TODO("Your mapping code here")
}

You can also create one of these for the GameDetailsTeam and GameDetailsBoxScore. Let me know if that makes sense.
Then at the end in the repository your code could be simplified to something like:

val response = apolloClient.query(GameByIdQuery(id)).execute()
val game = response.data?.game?.toGameDetails()
if (game == null) {
  // TODO handle null case and return
}
game?.let {
    _currGameFlow.value = ApiResponse.Success(it)
}

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

}