Skip to content

Commit c5ba13c

Browse files
author
Aidan Pieper
committed
Better word box formatting
Fixes: #2 (now using alphabetical sorting)
1 parent ca47a23 commit c5ba13c

File tree

4 files changed

+51
-32
lines changed

4 files changed

+51
-32
lines changed

app/src/main/java/io/github/plastix/buzz/detail/PuzzleDetailUi.kt

Lines changed: 47 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import androidx.compose.foundation.background
77
import androidx.compose.foundation.clickable
88
import androidx.compose.foundation.layout.*
99
import androidx.compose.foundation.lazy.LazyColumn
10+
import androidx.compose.foundation.lazy.items
1011
import androidx.compose.foundation.shape.CircleShape
1112
import androidx.compose.foundation.shape.RoundedCornerShape
1213
import androidx.compose.material.*
@@ -31,6 +32,7 @@ import androidx.compose.ui.platform.LocalConfiguration
3132
import androidx.compose.ui.platform.LocalDensity
3233
import androidx.compose.ui.res.stringArrayResource
3334
import androidx.compose.ui.res.stringResource
35+
import androidx.compose.ui.text.AnnotatedString
3436
import androidx.compose.ui.text.SpanStyle
3537
import androidx.compose.ui.text.buildAnnotatedString
3638
import androidx.compose.ui.text.font.FontWeight
@@ -162,6 +164,7 @@ fun PuzzleBoard(
162164
ScoreBox(viewModel, state.currentRank, state.currentScore)
163165
DiscoveredWordBox(
164166
words = state.discoveredWords,
167+
pangrams = state.discoveredPangrams,
165168
state.wordBoxExpanded,
166169
viewModel::toggleWorldBox
167170
)
@@ -218,6 +221,7 @@ fun PuzzleBoardLandscape(
218221
) {
219222
DiscoveredWordBox(
220223
words = state.discoveredWords,
224+
pangrams = state.discoveredPangrams,
221225
expanded = state.wordBoxExpanded,
222226
toggleExpand = viewModel::toggleWorldBox
223227
)
@@ -443,6 +447,7 @@ fun MaxWidthText(
443447
@Composable
444448
fun DiscoveredWordBox(
445449
words: Set<String>,
450+
pangrams: Set<String>,
446451
expanded: Boolean = false,
447452
toggleExpand: () -> Unit = {}
448453
) {
@@ -458,24 +463,36 @@ fun DiscoveredWordBox(
458463
.animateContentSize(),
459464
) {
460465
if (!expanded) {
461-
val text = if (words.isEmpty()) {
462-
stringResource(R.string.puzzle_detail_word_list_empty)
466+
val text: AnnotatedString = if (words.isEmpty()) {
467+
AnnotatedString(stringResource(R.string.puzzle_detail_word_list_empty))
463468
} else {
464-
words.reversed()
465-
.joinToString(separator = " ") { word -> word.capitalize(Locale.getDefault()) }
469+
buildAnnotatedString {
470+
words.reversed().forEachIndexed { index, word ->
471+
val fontWeight =
472+
if (word in pangrams) FontWeight.ExtraBold else FontWeight.Medium
473+
474+
withStyle(style = SpanStyle(fontWeight = fontWeight)) {
475+
append(word.capitalize(Locale.ENGLISH))
476+
}
477+
478+
if (index < words.size - 1) {
479+
append(" ")
480+
}
481+
}
482+
}
466483
}
467484
ChevronRow(text, expanded = false)
468485
} else {
469486
Column(modifier = Modifier.fillMaxSize()) {
470487
ChevronRow(
471-
stringResource(
488+
AnnotatedString(stringResource(
472489
R.string.puzzle_detail_word_list_word_count,
473490
words.size
474-
), true
491+
)), true
475492
)
476493
if (words.isNotEmpty()) {
477494
Spacer(Modifier.height(16.dp))
478-
ColumnGridList(words.toList())
495+
ColumnGridList(words.toList(), pangrams)
479496
}
480497
}
481498
}
@@ -484,37 +501,34 @@ fun DiscoveredWordBox(
484501
}
485502

486503
@Composable
487-
fun ColumnGridList(words: List<String>, columnNum: Int = 3) {
488-
val wordsPerColumn = ceil(words.size / columnNum.toDouble()).toInt()
489-
val columns = words.windowed(wordsPerColumn, step = wordsPerColumn, partialWindows = true)
504+
fun ColumnGridList(words: List<String>, pangrams: Set<String>, columnNum: Int = 3) {
505+
val rows = words.sorted().chunked(columnNum)
490506
LazyColumn(modifier = Modifier.fillMaxSize()) {
491-
for (i in 0 until wordsPerColumn) {
492-
item {
493-
Row(
494-
modifier = Modifier.fillMaxWidth(),
495-
horizontalArrangement = Arrangement.SpaceEvenly
496-
) {
497-
for (j in 0 until columnNum) {
498-
val word = columns.getOrNull(j)?.getOrNull(i) ?: ""
499-
Text(
500-
text = word.capitalize(Locale.getDefault()),
501-
maxLines = 1,
502-
textAlign = TextAlign.Start,
503-
modifier = Modifier.weight(1f),
504-
overflow = TextOverflow.Ellipsis
505-
)
506-
}
507+
items(rows) { rowWords ->
508+
Row(
509+
modifier = Modifier.fillMaxWidth(),
510+
horizontalArrangement = Arrangement.SpaceEvenly
511+
) {
512+
for (i in 0 until columnNum) {
513+
val word = rowWords.getOrNull(i) ?: ""
514+
Text(
515+
text = word.capitalize(Locale.ENGLISH),
516+
maxLines = 1,
517+
textAlign = TextAlign.Start,
518+
modifier = Modifier.weight(1f),
519+
overflow = TextOverflow.Ellipsis,
520+
fontWeight = if (word in pangrams) FontWeight.ExtraBold else FontWeight.Medium
521+
)
507522
}
508-
Spacer(modifier = Modifier.height(4.dp))
509523
}
510-
524+
Spacer(modifier = Modifier.height(4.dp))
511525
}
512526
}
513527
}
514528

515529
@Composable
516530
fun ChevronRow(
517-
text: String,
531+
text: AnnotatedString,
518532
expanded: Boolean,
519533
textColor: Color = MaterialTheme.colors.onSurface
520534
) {
@@ -543,7 +557,7 @@ fun ChevronRow(
543557
@Preview
544558
@Composable
545559
fun PreviewDiscoveredWordBoxEmpty() {
546-
DiscoveredWordBox(words = emptySet(), false)
560+
DiscoveredWordBox(words = emptySet(), pangrams = emptySet(), false)
547561
}
548562

549563
@Preview
@@ -552,7 +566,8 @@ fun PreviewDiscoveredWordBoxFull() {
552566
DiscoveredWordBox(
553567
words = setOf(
554568
"handle", "story", "rabbit", "cloud", "couch", "towel", "anger", "greeting"
555-
)
569+
),
570+
pangrams = emptySet()
556571
)
557572
}
558573

@@ -563,6 +578,7 @@ fun PreviewDiscoveredWordBoxFullExpanded() {
563578
words = setOf(
564579
"handle", "story", "rabbit", "cloud"
565580
),
581+
pangrams = emptySet(),
566582
expanded = true
567583
)
568584
}

app/src/main/java/io/github/plastix/buzz/detail/PuzzleDetailViewModel.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ class PuzzleDetailViewModel @AssistedInject constructor(
8585
outerLetters = board.gameState.outerLetters,
8686
currentWord = board.gameState.currentWord,
8787
discoveredWords = board.gameState.discoveredWords,
88+
discoveredPangrams = board.gameState.discoveredWords
89+
.filter { word -> word in board.puzzle.pangrams }.toSet(),
8890
currentRank = board.currentRank,
8991
currentScore = board.currentScore,
9092
activeDialog = activeDialog,

app/src/main/java/io/github/plastix/buzz/detail/PuzzleDetailViewState.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class BoardGameViewState(
1414
val outerLetters: List<Char>,
1515
val currentWord: String,
1616
val discoveredWords: Set<String>,
17+
val discoveredPangrams: Set<String>,
1718
val currentRank: PuzzleRanking,
1819
val currentScore: Int,
1920
val activeDialog: Dialog?,

app/src/main/java/io/github/plastix/buzz/list/PuzzleListViewModel.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class PuzzleListViewModel @Inject constructor(
4242
puzzleId = puzzle.id,
4343
dateString = puzzle.date.toDisplayString(),
4444
puzzleString = puzzle.centerLetter.plus(puzzle.outerLetters.joinToString(separator = ""))
45-
.toUpperCase(Locale.getDefault()),
45+
.toUpperCase(Locale.ENGLISH),
4646
puzzleRank = currentRank,
4747
currentScore = currentScore,
4848
type = puzzle.type

0 commit comments

Comments
 (0)