Skip to content

Commit e98272d

Browse files
committed
abstracted Lazy/DataRow (messed up weights)
1 parent a5255ae commit e98272d

2 files changed

Lines changed: 92 additions & 85 deletions

File tree

app/src/main/java/com/cornellappdev/score/components/ScoreBox.kt

Lines changed: 67 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import androidx.compose.foundation.layout.Arrangement
66
import androidx.compose.foundation.layout.Column
77
import androidx.compose.foundation.layout.IntrinsicSize
88
import androidx.compose.foundation.layout.Row
9+
import androidx.compose.foundation.layout.Spacer
910
import androidx.compose.foundation.layout.fillMaxWidth
1011
import androidx.compose.foundation.layout.height
1112
import androidx.compose.foundation.layout.padding
@@ -28,8 +29,8 @@ import androidx.compose.ui.text.style.TextOverflow
2829
import androidx.compose.ui.tooling.preview.Preview
2930
import androidx.compose.ui.unit.dp
3031
import com.cornellappdev.score.model.GameData
32+
import com.cornellappdev.score.model.ScoresByPeriod
3133
import com.cornellappdev.score.model.TeamScore
32-
import com.cornellappdev.score.model.mapToPeriodScores
3334
import com.cornellappdev.score.theme.CrimsonPrimary
3435
import com.cornellappdev.score.theme.GrayMedium
3536
import com.cornellappdev.score.theme.GrayPrimary
@@ -44,23 +45,14 @@ import com.cornellappdev.score.util.mediumGameData
4445
import com.cornellappdev.score.util.shortGameData
4546

4647
private val HEADER_HEIGHT = 35.dp
48+
private val SMALL_BOX_SIZE = 4
49+
private val LARGE_BOX_SIZE = 10
4750

4851
@Composable
4952
fun BoxScore(
5053
gameData: GameData,
5154
modifier: Modifier = Modifier
5255
) {
53-
val maxPeriods = maxOf(
54-
gameData.teamScores.first.scoresByPeriod.size,
55-
gameData.teamScores.second.scoresByPeriod.size
56-
)
57-
58-
val rowTextStyle = if (maxPeriods > 4) {
59-
metricSmallNormal
60-
} else {
61-
metricNormal
62-
}
63-
6456
Surface(
6557
modifier = modifier
6658
//set height required for CompleteLazyTableData case
@@ -75,24 +67,21 @@ fun BoxScore(
7567
modifier = Modifier
7668
.fillMaxWidth()
7769
) {
70+
val rowTextStyle = if (gameData.maxPeriods > SMALL_BOX_SIZE) {
71+
metricSmallNormal
72+
} else {
73+
metricNormal
74+
}
7875
TeamNameColumn(
7976
gameData.teamScores.first.team.name,
8077
gameData.teamScores.second.team.name,
8178
rowTextStyle,
82-
maxPeriods
79+
gameData.maxPeriods
80+
)
81+
CompleteTableData(
82+
gameData = gameData,
83+
rowTextStyle = rowTextStyle
8384
)
84-
if (maxPeriods > 10) {
85-
CompleteLazyTableData(
86-
gameData = gameData,
87-
rowTextStyle = rowTextStyle
88-
)
89-
} else {
90-
CompleteTableData(
91-
gameData = gameData,
92-
rowTextStyle = rowTextStyle,
93-
maxPeriods = maxPeriods
94-
)
95-
}
9685
}
9786
}
9887
}
@@ -113,13 +102,13 @@ private fun TeamNameColumn(
113102
modifier.widthIn(max = 100.dp)
114103
}
115104
) {
116-
Row(
105+
Spacer(
117106
modifier = Modifier
118107
.fillMaxWidth()
119108
.background(color = CrimsonPrimary)
120109
.height(HEADER_HEIGHT)
121110
.padding(top = 6.dp, bottom = 4.dp),
122-
) {}
111+
)
123112
Row(
124113
modifier = Modifier
125114
.fillMaxWidth()
@@ -221,54 +210,37 @@ private fun TableDataColumn(
221210
}
222211

223212
@Composable
224-
private fun CompleteLazyTableData(
225-
gameData: GameData,
213+
private fun LazyDataRow(
214+
periodScores: List<ScoresByPeriod>,
226215
rowTextStyle: TextStyle,
227216
modifier: Modifier = Modifier
228217
) {
229-
val periodScores = mapToPeriodScores(gameData)
230-
231-
if (periodScores.isNotEmpty()) {
232-
Row(
233-
modifier = modifier
234-
) {
235-
LazyRow(
236-
modifier = Modifier.weight(1f)
237-
) {
238-
items(periodScores) { periodScore ->
239-
TableDataColumn(
240-
header = periodScore.header,
241-
teamOneScore = periodScore.teamOneScore,
242-
teamTwoScore = periodScore.teamTwoScore,
243-
rowTextStyle = rowTextStyle,
244-
modifier = Modifier.width(35.dp)
245-
)
246-
}
247-
}
248-
TotalsColumn(
249-
teamOneScores = gameData.teamScores.first,
250-
teamTwoScores = gameData.teamScores.second,
251-
rowTextStyle = rowTextStyle
218+
LazyRow(
219+
modifier = modifier
220+
) {
221+
items(periodScores) { periodScore ->
222+
TableDataColumn(
223+
header = periodScore.header,
224+
teamOneScore = periodScore.teamOneScore,
225+
teamTwoScore = periodScore.teamTwoScore,
226+
rowTextStyle = rowTextStyle,
227+
modifier = Modifier.width(35.dp)
252228
)
253229
}
254230
}
255231
}
256232

257233
@Composable
258-
private fun CompleteTableData(
259-
gameData: GameData,
234+
private fun DataRow(
235+
periodScores: List<ScoresByPeriod>,
260236
rowTextStyle: TextStyle,
261-
maxPeriods: Int,
262237
modifier: Modifier = Modifier
263238
) {
264-
val periodScores = mapToPeriodScores(gameData)
265-
266239
Row(
267-
modifier = modifier,
268-
horizontalArrangement = Arrangement.SpaceEvenly
240+
modifier = modifier.fillMaxWidth()
269241
) {
270242
if (periodScores.isNotEmpty()) {
271-
mapToPeriodScores(gameData).map { periodScore ->
243+
periodScores.map { periodScore ->
272244
TableDataColumn(
273245
header = periodScore.header,
274246
teamOneScore = periodScore.teamOneScore,
@@ -288,20 +260,47 @@ private fun CompleteTableData(
288260
)
289261
}
290262
}
263+
}
264+
}
265+
266+
@Composable
267+
private fun CompleteTableData(
268+
gameData: GameData,
269+
rowTextStyle: TextStyle,
270+
modifier: Modifier = Modifier
271+
) {
272+
val periodScores = gameData.mapToPeriodScores()
273+
274+
Row(
275+
modifier = modifier
276+
) {
277+
if (gameData.maxPeriods > LARGE_BOX_SIZE) {
278+
LazyDataRow(
279+
periodScores,
280+
rowTextStyle,
281+
Modifier.weight(1f)
282+
)
283+
} else {
284+
DataRow(
285+
periodScores,
286+
rowTextStyle,
287+
Modifier.weight(1f)
288+
)
289+
}
290+
291291
TotalsColumn(
292292
teamOneScores = gameData.teamScores.first,
293293
teamTwoScores = gameData.teamScores.second,
294+
rowTextStyle = rowTextStyle,
294295
//if maxPeriods > 8, "Totals" header will wrap to two lines. In this case, don't weight so that space is allocated to TotalsColumn first
295296
//otherwise, TotalsColumn will fit without wrapping and can be allocated equal width as the data columns
296-
modifier = if (maxPeriods < 4) {
297+
modifier = if (gameData.maxPeriods < 4) {
297298
Modifier.weight(1f, fill = true)
298299
} else {
299300
Modifier
300-
},
301-
rowTextStyle = rowTextStyle
301+
}
302302
)
303303
}
304-
305304
}
306305

307306
@Composable
@@ -324,7 +323,8 @@ private fun TotalScoreCell(
324323
style = rowTextStyle,
325324
color = if (showEmpty) Color.Gray else totalTextColor,
326325
fontWeight = if (showEmpty) FontWeight.Normal else FontWeight.Bold,
327-
textAlign = TextAlign.Center
326+
textAlign = TextAlign.Center,
327+
modifier = Modifier.fillMaxWidth()
328328
)
329329
}
330330
}
@@ -351,9 +351,9 @@ private fun TotalsColumn(
351351
) {
352352
Text(
353353
text = "Total",
354-
color = Color.White,
355354
style = rowTextStyle,
356-
textAlign = TextAlign.Center
355+
textAlign = TextAlign.Center,
356+
color = Color.White
357357
)
358358
}
359359

app/src/main/java/com/cornellappdev/score/model/Game.kt

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,31 @@ data class TeamScore(
146146
// Aggregated game data showing scores for both teams
147147
data class GameData(
148148
val teamScores: Pair<TeamScore, TeamScore>
149-
)
149+
){
150+
val maxPeriods: Int
151+
get() =
152+
maxOf(
153+
teamScores.first.scoresByPeriod.size,
154+
teamScores.second.scoresByPeriod.size
155+
)
156+
157+
fun mapToPeriodScores(): List<ScoresByPeriod> {
158+
val teamOneScores = this.teamScores.first.scoresByPeriod
159+
val teamTwoScores = this.teamScores.second.scoresByPeriod
160+
161+
val maxPeriods = maxOf(teamOneScores.size, teamTwoScores.size)
162+
163+
return (0 until maxPeriods).map { i ->
164+
ScoresByPeriod(
165+
header = i + 1,
166+
teamOneScore = teamOneScores.getOrNull(i)?.toString() ?: "-",
167+
teamTwoScore = teamTwoScores.getOrNull(i)?.toString() ?: "-"
168+
169+
)
170+
171+
}
172+
}
173+
}
150174

151175
/**
152176
* Represents a scoring event in a game.
@@ -298,21 +322,4 @@ fun List<GameDetailsBoxScore>.toScoreEvents(teamLogo: String): List<ScoreEvent>
298322
description = boxScore.description
299323
)
300324
}
301-
}
302-
303-
fun mapToPeriodScores(gameData: GameData): List<ScoresByPeriod> {
304-
val teamOneScores = gameData.teamScores.first.scoresByPeriod
305-
val teamTwoScores = gameData.teamScores.second.scoresByPeriod
306-
307-
val maxPeriods = maxOf(teamOneScores.size, teamTwoScores.size)
308-
309-
return (0 until maxPeriods).map { i ->
310-
ScoresByPeriod(
311-
header = i + 1,
312-
teamOneScore = teamOneScores.getOrNull(i)?.toString() ?: "-",
313-
teamTwoScore = teamTwoScores.getOrNull(i)?.toString() ?: "-"
314-
315-
)
316-
317-
}
318325
}

0 commit comments

Comments
 (0)