Repository navigation
Add favorite sports selection screen #122
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
Open
+212
−6
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
4363802
Add standalone empty profile screen
EmilJiang 46f31b3
Remove profile screenshot from source tree
EmilJiang 05d30ed
Use default profile screen preview configuration
EmilJiang 02cc1e7
Use explicit empty Preview annotation arguments
EmilJiang d84192d
Add populated profile screen with sample data
EmilJiang cfdf7ae
Add favorite sports selector to profile
EmilJiang 4555312
Format profile screen
EmilJiang 500d683
Merge profile formatting and address favorite sports review
EmilJiang f42d78e
Merge main into favorite sports stack
EmilJiang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
173 changes: 173 additions & 0 deletions
173
app/src/main/java/com/cornellappdev/score/screen/EditFavoriteSportsScreen.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,173 @@ | ||
| package com.cornellappdev.score.screen | ||
|
|
||
| import androidx.compose.foundation.background | ||
| import androidx.compose.foundation.border | ||
| import androidx.compose.foundation.layout.* | ||
| import androidx.compose.foundation.lazy.grid.GridCells | ||
| import androidx.compose.foundation.lazy.grid.LazyVerticalGrid | ||
| import androidx.compose.foundation.lazy.grid.items | ||
| import androidx.compose.foundation.selection.selectable | ||
| import androidx.compose.foundation.selection.selectableGroup | ||
| import androidx.compose.foundation.selection.toggleable | ||
| import androidx.compose.foundation.shape.CircleShape | ||
| import androidx.compose.foundation.shape.RoundedCornerShape | ||
| import androidx.compose.material3.Icon | ||
| import androidx.compose.material3.Text | ||
| import androidx.compose.runtime.* | ||
| import androidx.compose.runtime.saveable.rememberSaveable | ||
| import androidx.compose.ui.Alignment | ||
| import androidx.compose.ui.Modifier | ||
| import androidx.compose.ui.draw.clip | ||
| import androidx.compose.ui.graphics.Color | ||
| import androidx.compose.ui.res.painterResource | ||
| import androidx.compose.ui.res.stringResource | ||
| import androidx.compose.ui.semantics.Role | ||
| import androidx.compose.ui.semantics.contentDescription | ||
| import androidx.compose.ui.semantics.semantics | ||
| import androidx.compose.ui.text.style.TextAlign | ||
| import androidx.compose.ui.tooling.preview.Preview | ||
| import androidx.compose.ui.unit.dp | ||
| import com.cornellappdev.score.R | ||
| import com.cornellappdev.score.components.NavigationHeader | ||
| import com.cornellappdev.score.model.GenderDivision | ||
| import com.cornellappdev.score.model.ProfileFavorite | ||
| import com.cornellappdev.score.model.Sport | ||
| import com.cornellappdev.score.theme.* | ||
|
|
||
| private val favoriteSportComparator = compareBy<ProfileFavorite>( | ||
| { it.sport.displayName }, | ||
| { it.gender == GenderDivision.FEMALE } | ||
| ) | ||
|
|
||
| private fun selectedFavoritesFirst(favorites: List<ProfileFavorite>): Comparator<ProfileFavorite> = | ||
| compareBy<ProfileFavorite> { it !in favorites }.then(favoriteSportComparator) | ||
|
|
||
| // Use the complete icon catalog, independent of sports with games in the current season. | ||
| private val favoriteSportOptions = Sport.entries.flatMap { sport -> | ||
| // The profile design includes both golf divisions, unlike the schedule catalog. | ||
| val divisions = if (sport.gender == GenderDivision.ALL || sport == Sport.GOLF) { | ||
| listOf(GenderDivision.MALE, GenderDivision.FEMALE) | ||
| } else listOf(sport.gender) | ||
| divisions.map { ProfileFavorite(sport, it) } | ||
| }.sortedWith(favoriteSportComparator) | ||
|
|
||
| @Composable | ||
| fun EditFavoriteSportsScreen( | ||
| favorites: List<ProfileFavorite>, | ||
| onToggleFavorite: (ProfileFavorite) -> Unit, | ||
| onBack: () -> Unit, | ||
| modifier: Modifier = Modifier | ||
| ) { | ||
| var gender by rememberSaveable { mutableStateOf(GenderDivision.ALL) } | ||
| val options = (favorites + favoriteSportOptions).distinct() | ||
| .filter { gender == GenderDivision.ALL || it.gender == gender } | ||
| .sortedWith(selectedFavoritesFirst(favorites)) | ||
|
|
||
| Column(modifier.fillMaxSize().background(Color.White)) { | ||
| NavigationHeader(stringResource(R.string.edit_favorite_sports), onBack) | ||
| Row( | ||
| Modifier.padding(horizontal = 24.dp, vertical = 32.dp) | ||
| .fillMaxWidth().border(1.dp, GrayLight, CircleShape) | ||
| .padding(6.dp).selectableGroup() | ||
| ) { | ||
| GenderDivision.entries.forEach { division -> | ||
| val selected = gender == division | ||
| Box( | ||
| Modifier.weight(1f).clip(CircleShape) | ||
| .background(if (selected) CrimsonPrimary else Color.Transparent) | ||
| .selectable(selected, role = Role.Tab, onClick = { gender = division }) | ||
| .padding(vertical = 8.dp), | ||
| contentAlignment = Alignment.Center | ||
| ) { | ||
| Text( | ||
| division.displayName, | ||
| style = if (selected) Style.bodySemibold else Style.bodyMedium, | ||
| color = if (selected) Color.White else GrayMedium | ||
| ) | ||
| } | ||
| } | ||
| } | ||
| LazyVerticalGrid( | ||
| columns = GridCells.Fixed(3), | ||
| modifier = Modifier.weight(1f), | ||
| contentPadding = PaddingValues(start = 24.dp, end = 24.dp, top = 8.dp, bottom = 32.dp), | ||
| horizontalArrangement = Arrangement.spacedBy(24.dp), | ||
| verticalArrangement = Arrangement.spacedBy(24.dp) | ||
| ) { | ||
| items(options, key = { "${it.sport.name}:${it.gender.name}" }) { favorite -> | ||
| FavoriteSportOption(favorite, favorite in favorites) { onToggleFavorite(favorite) } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Composable | ||
| private fun FavoriteSportOption(favorite: ProfileFavorite, selected: Boolean, onClick: () -> Unit) { | ||
| val shape = RoundedCornerShape(20.dp) | ||
| Column( | ||
| Modifier.fillMaxWidth() | ||
| .semantics(mergeDescendants = true) { | ||
| contentDescription = "${favorite.gender.displayName} ${favorite.sport.displayName}" | ||
| } | ||
| .toggleable(selected, role = Role.Checkbox, onValueChange = { onClick() }), | ||
| horizontalAlignment = Alignment.CenterHorizontally | ||
| ) { | ||
| Box(Modifier.fillMaxWidth().aspectRatio(1f), contentAlignment = Alignment.Center) { | ||
| Box( | ||
| Modifier.fillMaxSize() | ||
| .background(if (selected) GreenBackground else GrayStroke, shape) | ||
| .border( | ||
| if (selected) 3.dp else 1.dp, | ||
| if (selected) saturatedGreen else GrayStroke, | ||
| shape | ||
| ), | ||
| contentAlignment = Alignment.Center | ||
| ) { | ||
| Icon( | ||
| painterResource(favorite.sport.emptyIcon), null, Modifier.size(45.dp), | ||
| tint = if (selected) saturatedGreen else GrayPrimary | ||
| ) | ||
| } | ||
| if (selected) { | ||
| Box( | ||
| Modifier.align(Alignment.TopStart).offset(x = (-5).dp, y = (-5).dp) | ||
| .size(32.dp).background(CrimsonPrimary, CircleShape), | ||
| contentAlignment = Alignment.Center | ||
| ) { | ||
| Icon( | ||
| painterResource(R.drawable.ic_round_minus), | ||
| null, | ||
| Modifier.size(24.dp), | ||
| tint = Color.White | ||
| ) | ||
| } | ||
| } | ||
| } | ||
| Spacer(Modifier.height(8.dp)) | ||
| Text( | ||
| favorite.sport.displayName, style = Style.bodyMedium, color = GrayPrimary, | ||
| textAlign = TextAlign.Center, modifier = Modifier.fillMaxWidth() | ||
| ) | ||
| Spacer(Modifier.height(2.dp)) | ||
| Row( | ||
| verticalAlignment = Alignment.CenterVertically, | ||
| horizontalArrangement = Arrangement.spacedBy(2.dp) | ||
| ) { | ||
| Icon( | ||
| painterResource(if (favorite.gender == GenderDivision.FEMALE) R.drawable.ic_gender_women else R.drawable.ic_gender_men), | ||
| null, Modifier.size(18.dp), tint = Color.Unspecified | ||
| ) | ||
| Text( | ||
| if (favorite.gender == GenderDivision.FEMALE) "W" else "M", | ||
| style = Style.labelsNormal, | ||
| color = GrayPrimary | ||
| ) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Preview(showBackground = true, widthDp = 393, heightDp = 805) | ||
| @Composable | ||
| private fun EditFavoriteSportsScreenPreview() { | ||
| EditFavoriteSportsScreen(sampleProfileData.favorites, {}, {}) | ||
| } |
|
EmilJiang marked this conversation as resolved.
Resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.