Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ fun FeaturedGameCard(
bottomEnd = 16.dp
)
),
isFeatured = true,
onClick = onClick
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import androidx.compose.ui.draw.shadow
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.painter.Painter
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import coil3.compose.AsyncImage
Expand All @@ -57,6 +58,7 @@ fun GameCard(
sportIcon: Painter,
topCornerRound: Boolean,
modifier: Modifier = Modifier,
isFeatured: Boolean = false,
onClick: () -> Unit
) {
val cardShape = if (topCornerRound) {
Expand Down Expand Up @@ -116,7 +118,9 @@ fun GameCard(
Text(
text = team,
style = heading2,
color = GrayPrimary
color = GrayPrimary,
maxLines = if (isFeatured) 1 else Int.MAX_VALUE,
overflow = if (isFeatured) TextOverflow.Ellipsis else TextOverflow.Clip
)
}
Row(
Expand Down
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 @@ -23,13 +23,20 @@ data class HomeUiState(
get() = when (loadedState) {
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))
(sportSelect is SportSelection.All ||
(sportSelect is SportSelection.SportSelect && game.sport == sportSelect.sport.displayName))
}

ApiResponse.Loading -> emptyList()
ApiResponse.Error -> emptyList()
}
val upcomingGames: List<GameCardData> = filteredGames.take(3)
val upcomingGames: List<GameCardData>
get() = when (loadedState) {
is ApiResponse.Success -> loadedState.data

ApiResponse.Loading -> emptyList()
ApiResponse.Error -> emptyList()
}.take(3)
}

@HiltViewModel
Expand Down Expand Up @@ -72,7 +79,7 @@ class HomeViewModel @Inject constructor(
applyMutation {
copy(
selectedGender = gender,
selectionList = Sport.getSportSelectionList(gender)
selectionList = Sport.getSportSelectionList(gender),
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,20 @@ data class PastGamesUiState(
get() = when (loadedState) {
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))
(sportSelect is SportSelection.All ||
(sportSelect is SportSelection.SportSelect && game.sport == sportSelect.sport.displayName))
}

ApiResponse.Loading -> emptyList()
ApiResponse.Error -> emptyList()
}
val pastGames: List<GameCardData> = filteredGames.take(3)
val pastGames: List<GameCardData>
get() = when (loadedState) {
is ApiResponse.Success -> loadedState.data

ApiResponse.Loading -> emptyList()
ApiResponse.Error -> emptyList()
}.take(3)
}

@HiltViewModel
Expand Down Expand Up @@ -63,7 +70,7 @@ class PastGamesViewModel @Inject constructor(
applyMutation {
copy(
selectedGender = gender,
selectionList = Sport.getSportSelectionList(gender)
selectionList = Sport.getSportSelectionList(gender),
)
}
}
Expand Down
Loading