Skip to content
Merged
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
11 changes: 1 addition & 10 deletions app/src/main/java/com/cornellappdev/score/model/Game.kt
Original file line number Diff line number Diff line change
Expand Up @@ -277,13 +277,4 @@ fun List<GameDetailsBoxScore>.toScoreEvents(teamLogo: String): List<ScoreEvent>
description = boxScore.description
)
}
}

val validSports = setOf(
"Baseball", "Basketball", "Field Hockey",
"Football", "Ice Hockey", "Lacrosse", "Soccer"
)

fun isValidSport(sportName: String): Boolean {
return sportName in validSports
}
}
58 changes: 30 additions & 28 deletions app/src/main/java/com/cornellappdev/score/model/ScoreRepository.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import javax.inject.Inject
import javax.inject.Singleton
import com.cornellappdev.score.util.isValidSport

/**
* This is a singleton responsible for fetching and caching all data for Score.
Expand Down Expand Up @@ -45,34 +46,35 @@ class ScoreRepository @Inject constructor(
val games = result.getOrNull()

val gamesList: List<Game> =
games?.games?.mapNotNull { game ->
/**
* The final scores in the past game cards are obtained by parsing a String
* result from the GameQuery, which is oftentimes in the format
* Result, CornellScore-OpponentScore (e.g. "W, 2-1"). Not all of the strings
* are in this format (e.g. 4th of 6, 1498 points for women's Swimming and
* Diving), but in this case, the cornellScore and otherScore parameters of
* the game and associated card should be null, and as of right now,
* null-scored games are filtered out.
*/
val scores = game?.result?.split(",")?.getOrNull(1)?.split("-")
val cornellScore = scores?.getOrNull(0)?.toNumberOrNull()
val otherScore = scores?.getOrNull(1)?.toNumberOrNull()
game?.team?.image?.let {
Game(
id = game.id ?: "", // Should never be null
teamLogo = it,
teamName = game.team.name,
teamColor = parseColor(game.team.color).copy(alpha = 0.4f * 255),
gender = if (game.gender == "Mens") "Men's" else "Women's",
sport = game.sport,
date = game.date,
city = game.city,
cornellScore = cornellScore,
otherScore = otherScore
)
}
} ?: emptyList()
games?.games?.filter { game -> isValidSport(game?.sport ?: "") }
?.mapNotNull { game ->
Comment on lines +49 to +50
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nit: filterNotNull and then map is a bit cleaner here so you don't have to handle null in the predicate.

/**
* The final scores in the past game cards are obtained by parsing a String
* result from the GameQuery, which is oftentimes in the format
* Result, CornellScore-OpponentScore (e.g. "W, 2-1"). Not all of the strings
* are in this format (e.g. 4th of 6, 1498 points for women's Swimming and
* Diving), but in this case, the cornellScore and otherScore parameters of
* the game and associated card should be null, and as of right now,
* null-scored games are filtered out.
*/
val scores = game?.result?.split(",")?.getOrNull(1)?.split("-")
val cornellScore = scores?.getOrNull(0)?.toNumberOrNull()
val otherScore = scores?.getOrNull(1)?.toNumberOrNull()
game?.team?.image?.let {
Game(
id = game.id ?: "", // Should never be null
teamLogo = it,
teamName = game.team.name,
teamColor = parseColor(game.team.color).copy(alpha = 0.4f * 255),
gender = if (game.gender == "Mens") "Men's" else "Women's",
sport = game.sport,
date = game.date,
city = game.city,
cornellScore = cornellScore,
otherScore = otherScore
)
}
} ?: emptyList()
_upcomingGamesFlow.value = ApiResponse.Success(gamesList)
} else {
_upcomingGamesFlow.value = ApiResponse.Error
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/java/com/cornellappdev/score/model/Sport.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.cornellappdev.score.model

import androidx.annotation.DrawableRes
import com.cornellappdev.score.util.isValidSport
import com.cornellappdev.score.R

enum class Sport(
Expand Down Expand Up @@ -173,7 +174,7 @@ enum class Sport(
GenderDivision.FEMALE -> Sport.entries.filter { it.gender == GenderDivision.FEMALE || it.gender == GenderDivision.ALL }
GenderDivision.ALL,
null -> Sport.entries
}
}.filter { isValidSport(it.displayName) }
Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't think we need to filter here since we already should've done this in the repository.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This does not work if I remove it

Copy link
Collaborator

Choose a reason for hiding this comment

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

oh oops that was definitely wrong of me, I thought that was data coming from the BE, sorry for not looking at that closer.


return listOf(SportSelection.All) + filteredSports.map { SportSelection.SportSelect(it) }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,12 @@ fun parseResultScore(result: String?): Pair<Int, Int>? {
}
}

val validSports = setOf(
"Baseball", "Basketball", "Field Hockey",
"Football", "Ice Hockey", "Lacrosse", "Soccer"
)

fun isValidSport(sportName: String): Boolean {
return sportName in validSports
}

Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import com.cornellappdev.score.model.GenderDivision
import com.cornellappdev.score.model.ScoreRepository
import com.cornellappdev.score.model.Sport
import com.cornellappdev.score.model.SportSelection
import com.cornellappdev.score.model.isValidSport
import com.cornellappdev.score.model.map
import com.cornellappdev.score.model.toGameCardData
import dagger.hilt.android.lifecycle.HiltViewModel
Expand All @@ -25,18 +24,15 @@ data class HomeUiState(
is ApiResponse.Success -> loadedState.data.filter { game ->
(selectedGender == GenderDivision.ALL || game.gender == selectedGender.displayName) &&
(sportSelect is SportSelection.All ||
(sportSelect is SportSelection.SportSelect && game.sport == sportSelect.sport.displayName)) &&
isValidSport(game.sport)
(sportSelect is SportSelection.SportSelect && game.sport == sportSelect.sport.displayName))
}

ApiResponse.Loading -> emptyList()
ApiResponse.Error -> emptyList()
}
val upcomingGames: List<GameCardData>
get() = when (loadedState) {
is ApiResponse.Success -> loadedState.data.filter { game ->
isValidSport(game.sport)
}
is ApiResponse.Success -> loadedState.data

ApiResponse.Loading -> emptyList()
ApiResponse.Error -> emptyList()
Expand All @@ -50,10 +46,7 @@ class HomeViewModel @Inject constructor(
HomeUiState(
selectedGender = GenderDivision.ALL,
sportSelect = SportSelection.All,
selectionList = Sport.getSportSelectionList(GenderDivision.ALL).filter {
it is SportSelection.All ||
(it is SportSelection.SportSelect && isValidSport(it.sport.displayName))
},
selectionList = Sport.getSportSelectionList(GenderDivision.ALL),
loadedState = ApiResponse.Loading
)
) {
Expand Down Expand Up @@ -86,10 +79,7 @@ class HomeViewModel @Inject constructor(
applyMutation {
copy(
selectedGender = gender,
selectionList = Sport.getSportSelectionList(gender).filter {
it is SportSelection.All ||
(it is SportSelection.SportSelect && isValidSport(it.sport.displayName))
},
selectionList = Sport.getSportSelectionList(gender),
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import com.cornellappdev.score.model.GenderDivision
import com.cornellappdev.score.model.ScoreRepository
import com.cornellappdev.score.model.Sport
import com.cornellappdev.score.model.SportSelection
import com.cornellappdev.score.model.isValidSport
import com.cornellappdev.score.model.map
import com.cornellappdev.score.model.toGameCardData
import dagger.hilt.android.lifecycle.HiltViewModel
Expand All @@ -25,18 +24,15 @@ data class PastGamesUiState(
is ApiResponse.Success -> loadedState.data.filter { game ->
(selectedGender == GenderDivision.ALL || game.gender == selectedGender.displayName) &&
(sportSelect is SportSelection.All ||
(sportSelect is SportSelection.SportSelect && game.sport == sportSelect.sport.displayName)) &&
isValidSport(game.sport)
(sportSelect is SportSelection.SportSelect && game.sport == sportSelect.sport.displayName))
}

ApiResponse.Loading -> emptyList()
ApiResponse.Error -> emptyList()
}
val pastGames: List<GameCardData>
get() = when (loadedState) {
is ApiResponse.Success -> loadedState.data.filter { game ->
isValidSport(game.sport)
}
is ApiResponse.Success -> loadedState.data

ApiResponse.Loading -> emptyList()
ApiResponse.Error -> emptyList()
Expand All @@ -50,10 +46,7 @@ class PastGamesViewModel @Inject constructor(
PastGamesUiState(
selectedGender = GenderDivision.ALL,
sportSelect = SportSelection.All,
selectionList = Sport.getSportSelectionList(GenderDivision.ALL).filter {
it is SportSelection.All ||
(it is SportSelection.SportSelect && isValidSport(it.sport.displayName))
},
selectionList = Sport.getSportSelectionList(GenderDivision.ALL),
loadedState = ApiResponse.Loading
)
) {
Expand Down Expand Up @@ -85,10 +78,7 @@ class PastGamesViewModel @Inject constructor(
applyMutation {
copy(
selectedGender = gender,
selectionList = Sport.getSportSelectionList(gender).filter {
it is SportSelection.All ||
(it is SportSelection.SportSelect && isValidSport(it.sport.displayName))
},
selectionList = Sport.getSportSelectionList(gender),
)
}
}
Expand Down
Loading