-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathExerciseCardSmall.kt
More file actions
301 lines (291 loc) · 13.6 KB
/
Copy pathExerciseCardSmall.kt
File metadata and controls
301 lines (291 loc) · 13.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/*
* SPDX-License-Identifier: GPL-3.0-or-later
* Copyright (c) 2025-2026. The LibreFit Contributors
*
* LibreFit is subject to additional terms covering author attribution and trademark usage;
* see the ADDITIONAL_TERMS.md and TRADEMARK_POLICY.md files in the project root.
*/
package org.librefit.ui.components
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.AnimatedVisibilityScope
import androidx.compose.animation.ExperimentalSharedTransitionApi
import androidx.compose.animation.SharedTransitionLayout
import androidx.compose.animation.SharedTransitionScope
import androidx.compose.animation.animateColorAsState
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.CornerSize
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.Checkbox
import androidx.compose.material3.ElevatedCard
import androidx.compose.material3.ExperimentalMaterial3ExpressiveApi
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.ColorFilter
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import coil3.compose.AsyncImage
import kotlinx.collections.immutable.persistentListOf
import org.librefit.R
import org.librefit.enums.SetMode
import org.librefit.enums.userPreferences.ThemeMode
import org.librefit.ui.models.UiExercise
import org.librefit.ui.models.UiExerciseDC
import org.librefit.ui.models.UiExerciseWithSets
import org.librefit.ui.models.UiSet
import org.librefit.ui.models.autoUnitSuffix
import org.librefit.ui.models.doubleValue
import org.librefit.ui.theme.LibreFitTheme
import org.librefit.util.Formatter
import org.librefit.util.Formatter.formatDetails
import org.librefit.util.Formatter.formatTime
/**
* This is a smaller version of [ExerciseCard] when actually is a button. It is suitable to only show data of [UiExerciseWithSets]
* but not to modify it.
*
* @param exerciseWithSets A [UiExerciseWithSets] that holds the data
* @param animatedVisibilityScope Used for image's animation transition
* @param isRoutine When `false`, the card shows checkboxes of set completion
* @param onDetail A lambda function triggered when the `Info` icon is clicked or when [ExerciseCardSmall] is clicked.
* It should open the [org.librefit.ui.screens.infoExercise.InfoExerciseScreen].
*/
@OptIn(ExperimentalSharedTransitionApi::class, ExperimentalMaterial3ExpressiveApi::class)
@Composable
fun SharedTransitionScope.ExerciseCardSmall(
exerciseWithSets: UiExerciseWithSets,
isRoutine: Boolean = false,
showExercisesImages: Boolean?,
animatedVisibilityScope: AnimatedVisibilityScope,
onDetail: () -> Unit
) {
Button(
onClick = onDetail,
modifier = Modifier
.padding(5.dp),
shapes = ButtonDefaults.shapes(
shape = MaterialTheme.shapes.extraLarge
),
contentPadding = ButtonDefaults.MediumContentPadding,
colors = ButtonDefaults.buttonColors(
containerColor = MaterialTheme.colorScheme.surfaceContainerLow,
contentColor = MaterialTheme.colorScheme.onSurface
)
) {
Column(
verticalArrangement = Arrangement.spacedBy(10.dp)
) {
Row(
verticalAlignment = Alignment.CenterVertically,
) {
val model =
remember(exerciseWithSets.exerciseDC.images) { exerciseWithSets.exerciseDC.images.firstOrNull() }
if (showExercisesImages == true) {
AsyncImage(
model = model?.let { "file:///android_asset/${it}" },
fallback = painterResource(R.drawable.no_image),
contentDescription = exerciseWithSets.exerciseDC.name,
contentScale = ContentScale.Crop,
colorFilter = if (model == null) ColorFilter.tint(MaterialTheme.colorScheme.onSurfaceVariant) else null,
modifier = Modifier
.padding(end = 10.dp)
.sharedElement(
sharedContentState = rememberSharedContentState(
key = exerciseWithSets.exercise.id.toString() + exerciseWithSets.exerciseDC.id
),
animatedVisibilityScope = animatedVisibilityScope
)
.size(50.dp)
.clip(MaterialTheme.shapes.medium)
)
}
Text(
modifier = Modifier.weight(1f),
text = exerciseWithSets.exerciseDC.name,
style = MaterialTheme.typography.titleLarge,
fontWeight = FontWeight.SemiBold,
overflow = TextOverflow.Ellipsis,
maxLines = 1,
)
IconButton(
onClick = onDetail
) {
Icon(
painter = painterResource(R.drawable.ic_info),
contentDescription = stringResource(R.string.info)
)
}
}
HorizontalDivider()
Text(
text = formatDetails(
stringResource(R.string.type_of_set),
stringResource(
Formatter.setModeToStringId(exerciseWithSets.exercise.setMode)
)
)
)
if (exerciseWithSets.exercise.restTime != 0) {
Text(
formatDetails(
stringResource(R.string.rest_time),
exerciseWithSets.exercise.restTime.toString()
+ " " + stringResource(R.string.seconds).replaceFirstChar { it.lowercase() })
)
}
if (exerciseWithSets.exercise.notes.isNotBlank()) {
HorizontalDivider()
Text(formatDetails(stringResource(R.string.notes), exerciseWithSets.exercise.notes))
}
if (exerciseWithSets.sets.isNotEmpty()) {
val setMode = exerciseWithSets.exercise.setMode
ElevatedCard(
shape = MaterialTheme.shapes.largeIncreased,
colors = CardDefaults.elevatedCardColors(
containerColor = MaterialTheme.colorScheme.surfaceContainerHighest
)
) {
Row(
modifier = Modifier.fillMaxWidth(),
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.SpaceAround
) {
Text(stringResource(R.string.set))
if (setMode == SetMode.DURATION) {
Text(stringResource(R.string.time))
} else {
if (setMode == SetMode.LOAD || setMode == SetMode.BODYWEIGHT_WITH_LOAD) {
Text(
stringResource(R.string.load) + " (" + autoUnitSuffix() + ")"
)
}
Text(stringResource(R.string.reps))
}
if (!isRoutine) {
Icon(
painter = painterResource(R.drawable.ic_check),
contentDescription = stringResource(R.string.done)
)
}
}
Column {
exerciseWithSets.sets.forEachIndexed { index, set ->
val backgroundColor by animateColorAsState(
targetValue = if (set.completed) {
MaterialTheme.colorScheme.tertiaryContainer
} else {
MaterialTheme.colorScheme.surfaceContainerHighest
},
label = "animated_color_for_set_background"
)
val contentColor by animateColorAsState(
targetValue = if (set.completed) {
MaterialTheme.colorScheme.onTertiaryContainer
} else {
MaterialTheme.colorScheme.onSurface
},
label = "animated_color_for_set_content"
)
Row(
modifier = Modifier
.fillMaxWidth()
.clip(
RoundedCornerShape(
topStart = CornerSize(if (index == 0) 45 else 0),
topEnd = CornerSize(if (index == 0) 45 else 0),
bottomEnd = CornerSize(
if (index == exerciseWithSets.sets.lastIndex) 45 else 0
),
bottomStart = CornerSize(
if (index == exerciseWithSets.sets.lastIndex) 45 else 0
),
)
)
.background(backgroundColor)
.padding(5.dp),
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.SpaceAround
) {
Text(
text = "${index + 1}",
color = contentColor
)
if (setMode == SetMode.DURATION) {
Text(
text = formatTime(set.elapsedTime).substring(3),
color = contentColor
)
} else {
if (setMode == SetMode.LOAD || setMode == SetMode.BODYWEIGHT_WITH_LOAD) {
Text(
text = set.load.doubleValue().toString(),
color = contentColor
)
}
Text(
text = "${set.reps}",
color = contentColor
)
}
if (!isRoutine) {
Checkbox(
checked = set.completed,
onCheckedChange = null
)
}
}
}
}
}
}
}
}
}
@OptIn(ExperimentalSharedTransitionApi::class)
@Preview
@Composable
private fun ExerciseCardSmallPreview() {
LibreFitTheme(dynamicColor = false, themeMode = ThemeMode.DARK) {
SharedTransitionLayout {
AnimatedVisibility(visible = true) {
ExerciseCardSmall(
exerciseWithSets = UiExerciseWithSets(
exercise = UiExercise(
notes = "Notes",
restTime = 100,
setMode = SetMode.BODYWEIGHT
),
exerciseDC = UiExerciseDC(
name = "Name exercise long long long long",
images = persistentListOf("3_4_Sit-Up/0.jpg")
),
sets = persistentListOf(UiSet(completed = true), UiSet(reps = 10), UiSet())
),
showExercisesImages = null,
animatedVisibilityScope = this
) { }
}
}
}
}