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

Conversation

EmilJiang
Copy link
Contributor

I have added dataclasses for getting the information from the backed. I also added the flow and query to the repository.

I tested this with the dubugger. I basically just hardcoded some .getGameById and then looked at the flow through the debugger.

@EmilJiang EmilJiang requested a review from zachseidner1 March 13, 2025 15:54
Copy link
Collaborator

@zachseidner1 zachseidner1 left a comment

Choose a reason for hiding this comment

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

Emil, this is great work for your first networking PR. The logic makes sense and is easy to follow. This is a small PR too, so easier for me to review. I think the code inside the try catch could be a bit cleaner. Generally try catch blocks should be as short as possible. So if you clean it up with extension functions, this should be good!

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.

Comment on lines 32 to 33
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.

Comment on lines 18 to 20
/**
* 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.

Comment on lines 68 to 73
try {
val response = apolloClient.query(GameByIdQuery(id)).execute()
val game = response.data?.game

if (game != null) {
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)
}

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants