Skip to content
This repository has been archived by the owner on Mar 18, 2024. It is now read-only.

Commit

Permalink
Warm up calculator improvements and other fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
AnkitSuda committed Nov 30, 2022
1 parent 6fb13a0 commit d0447ba
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import androidx.compose.ui.unit.sp
import com.ankitsuda.base.util.*
import com.ankitsuda.common.compose.*
import com.ankitsuda.rebound.domain.LogSetType
import com.ankitsuda.rebound.domain.WeightUnit
import com.ankitsuda.rebound.domain.entities.*
import com.ankitsuda.rebound.ui.components.RButton
import com.ankitsuda.rebound.ui.components.RSpacer
Expand Down Expand Up @@ -104,17 +105,42 @@ fun LazyListScope.workoutExerciseItemAlt(
onUpdateWarmUpSets(newWarmUpSets)
}

LaunchedEffect(key1 = barbell) {
if (dialogWarmUpSets.any { it.findFormula().contains("Bar") }) {
dialogWarmUpSets = dialogWarmUpSets.map {
if (it.findFormula().contains("Bar")) {
it.copy(
weight = barbell.toWeightUnit(WeightUnit.KG)
)
} else {
it
}
}
}
}

// TODO: fix, sometimes does not get called
LaunchedEffect(key1 = sortedEntries) {
warmUpWorkSetWeight = (sortedEntries.filter { it.setType != LogSetType.WARM_UP }
val newWorkWeight = (sortedEntries.filter { it.setType != LogSetType.WARM_UP }
.getOrNull(0)?.weight ?: warmUpWorkSetWeight)
// warmUpWorkSetWeight = newWarmUpWorkSetWeight
// warmUpSets = WarmUpSet.fromLogEntries(warmUpWorkSetWeight, sortedEntries)

val lastDialogWarmupSets = dialogWarmUpSets.toList()

if (warmUpWorkSetWeight != newWorkWeight) {
warmUpWorkSetWeight = newWorkWeight
dialogWarmUpSets =
WarmUpSet.refreshWarmupSetsWithNewWorkWeight(
newWorkWeight,
lastDialogWarmupSets
)
}
}

if (warmUpSetsDialogVisible) {
key(LocalAppSettings.current.weightUnit) {
WarmUpCalculatorDialog(
startingWorkSetWeight = warmUpWorkSetWeight,
barbell = barbell,
startingSets = dialogWarmUpSets,
onInsert = { newWarmUpWorkSetWeight, newWarmUpSets ->
updateWarmUpSets(newWarmUpWorkSetWeight, newWarmUpSets)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,13 @@ import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Dialog
import androidx.compose.ui.window.DialogProperties
import androidx.hilt.navigation.compose.hiltViewModel
import com.ankitsuda.base.util.fromKgToLbs
import com.ankitsuda.base.util.fromLbsToKg
import com.ankitsuda.base.util.lighterOrDarkerColor
import com.ankitsuda.common.compose.LocalAppSettings
import com.ankitsuda.common.compose.kgToUserPrefStr
import com.ankitsuda.common.compose.toWeightUnit
import com.ankitsuda.rebound.domain.WeightUnit
import com.ankitsuda.rebound.domain.entities.Barbell
import com.ankitsuda.rebound.ui.components.RButton
import com.ankitsuda.rebound.ui.components.RSpacer
import com.ankitsuda.rebound.ui.components.workouteditor.R
Expand All @@ -48,12 +49,12 @@ import com.ankitsuda.rebound.ui.theme.ReboundTheme
@Composable
fun WarmUpCalculatorDialog(
startingWorkSetWeight: Double?,
barbell: Barbell?,
startingSets: List<WarmUpSet>,
onInsert: (workSet: Double, sets: List<WarmUpSet>) -> Unit,
onDismissRequest: () -> Unit,
viewModel: WarmUpCalculatorDialogViewModel = hiltViewModel()
) {

LaunchedEffect(startingSets) {
viewModel.setSets(startingSets)
}
Expand All @@ -71,6 +72,7 @@ fun WarmUpCalculatorDialog(
val sets by viewModel.sets.collectAsState()
WarmUpCalculatorDialogLayout(
sets = sets,
barbell = barbell,
startingWorkSetWeight = startingWorkSetWeight,
onClickCancel = onDismissRequest,
onUpdateWorkSet = { barWeight, workSet ->
Expand Down Expand Up @@ -101,6 +103,7 @@ fun WarmUpCalculatorDialog(
@Composable
private fun WarmUpCalculatorDialogLayout(
sets: List<WarmUpSet>,
barbell: Barbell?,
startingWorkSetWeight: Double?,
onUpdateWorkSet: (barWight: Double, workSet: Double) -> Unit,
onUpdateSet: (WarmUpSet) -> Unit,
Expand Down Expand Up @@ -165,7 +168,7 @@ private fun WarmUpCalculatorDialogLayout(
onValueChange = {
workSetStr = it
onUpdateWorkSet(
20.0,
barbell.toWeightUnit(weightUnit),
when (weightUnit) {
WeightUnit.KG -> it.toDoubleOrNull()
WeightUnit.LBS -> it.toDoubleOrNull()?.fromLbsToKg()
Expand Down Expand Up @@ -202,7 +205,7 @@ private fun WarmUpCalculatorDialogLayout(
WeightUnit.KG -> workSetStr.toDoubleOrNull()
WeightUnit.LBS -> workSetStr.toDoubleOrNull()?.fromLbsToKg()
} ?: 0.0,
barWeightKg = 20.0,
barWeightKg = barbell.toWeightUnit(WeightUnit.KG),
startingSet = set,
onChangeValue = onUpdateSet,
onDeleteSet = {
Expand All @@ -226,7 +229,7 @@ private fun WarmUpCalculatorDialogLayout(
contentColor = ReboundTheme.colors.onBackground
),
onClick = {
onAddSet(20.0)
onAddSet(barbell.toWeightUnit(WeightUnit.KG))
}
) {
Icon(imageVector = Icons.Outlined.Add, contentDescription = null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

package com.ankitsuda.rebound.ui.components.workouteditor.warmupcalculator

import androidx.compose.runtime.MutableState
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.ankitsuda.base.utils.extensions.toArrayList
Expand All @@ -23,7 +22,6 @@ import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch
import java.lang.Exception
import javax.inject.Inject

@HiltViewModel
Expand Down Expand Up @@ -87,6 +85,4 @@ class WarmUpCalculatorDialogViewModel @Inject constructor() : ViewModel() {
_sets.value = newSets
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ package com.ankitsuda.rebound.ui.components.workouteditor.warmupcalculator

import com.ankitsuda.base.utils.generateId
import com.ankitsuda.rebound.domain.LogSetType
import com.ankitsuda.rebound.domain.entities.ExerciseLog
import com.ankitsuda.rebound.domain.entities.ExerciseLogEntry
import com.ankitsuda.rebound.ui.keyboard.picker.getAllWarmUpWeights

data class WarmUpSet(
var id: String,
Expand All @@ -31,14 +29,24 @@ data class WarmUpSet(
return formula ?: "Bar x 1"
}

// companion object {
// fun fromLogEntries(workSetWeight: Double?, entries: List<ExerciseLogEntry>): List<WarmUpSet> {
// val warmUpSets = arrayListOf<WarmUpSet>()
// val filteredList = entries.filter { it.setType == LogSetType.WARM_UP }
// for (entry in filteredList) {
// }
// }
// }
companion object {
fun refreshWarmupSetsWithNewWorkWeight(
workSetWeight: Double?,
lastWarmupSets: List<WarmUpSet>
): List<WarmUpSet> {
return lastWarmupSets.map { set ->
if (set.findFormula()
.contains("Bar") || set.weightPercentage == null || workSetWeight == null
) {
set
} else {
set.copy(
weight = workSetWeight * set.weightPercentage!! / 100
)
}
}
}
}
}

fun WarmUpSet.toExerciseLogEntry() = ExerciseLogEntry(
Expand All @@ -49,5 +57,6 @@ fun WarmUpSet.toExerciseLogEntry() = ExerciseLogEntry(
)

fun List<WarmUpSet>.toExerciseLogEntries(): List<ExerciseLogEntry> =
this.map { it.toExerciseLogEntry()
this.map {
it.toExerciseLogEntry()
}
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ internal fun WarmUpSetComponent(
workSetWeightKg * weightInt / 100
}

val reps = arr.getOrNull(1)?.toIntOrNull() ?: 0
val reps = arr.getOrNull(1)?.toIntOrNull() ?: 1

onChangeValue(
startingSet.copy(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import androidx.compose.ui.res.stringResource
import com.ankitsuda.base.util.*
import com.ankitsuda.rebound.domain.DistanceUnit
import com.ankitsuda.rebound.domain.WeightUnit
import com.ankitsuda.rebound.domain.entities.Barbell

@Composable
fun Double?.kgToUserPrefStr(
Expand Down Expand Up @@ -114,4 +115,15 @@ fun DistanceUnit.localizedStr(case: Int = 2): String = when (this) {
*/
@Composable
fun userPrefDistanceUnitStr(case: Int = 2): String =
LocalAppSettings.current.distanceUnit.localizedStr(case = case)
LocalAppSettings.current.distanceUnit.localizedStr(case = case)


@Composable
fun Barbell?.toWeightUnit() = toWeightUnit(LocalAppSettings.current.weightUnit)

fun Barbell?.toWeightUnit(weightUnit: WeightUnit): Double {
return when (weightUnit) {
WeightUnit.LBS -> this?.weightLbs ?: this?.weightKg?.fromKgToLbs() ?: 0.0
else -> this?.weightKg ?: this?.weightLbs?.fromLbsToKg() ?: 0.0
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import com.ankitsuda.base.util.fromLbsToKg
import com.ankitsuda.base.util.toReadableString
import com.ankitsuda.common.compose.LocalAppSettings
import com.ankitsuda.common.compose.kgToUserPrefStr
import com.ankitsuda.common.compose.toWeightUnit
import com.ankitsuda.common.compose.userPrefWeightUnitStr
import com.ankitsuda.rebound.domain.WeightUnit
import com.ankitsuda.rebound.domain.entities.Barbell
Expand Down Expand Up @@ -107,10 +108,7 @@ private fun BarbellComponent(
val barbellColor = theme.keyboardBarbellColor
val onBarbellColor = theme.keyboardOnBarbellColor

val barbellWeight = when (LocalAppSettings.current.weightUnit) {
WeightUnit.LBS -> barbell?.weightLbs ?: barbell?.weightKg?.fromKgToLbs() ?: 0.0
else -> barbell?.weightKg ?: barbell?.weightLbs?.fromLbsToKg() ?: 0.0
}
val barbellWeight = barbell.toWeightUnit()

val scrollState = rememberScrollState()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.ankitsuda.base.util.fromKgToLbs
import com.ankitsuda.base.util.fromLbsToKg
import com.ankitsuda.common.compose.toWeightUnit
import com.ankitsuda.rebound.data.datastore.AppPreferences
import com.ankitsuda.rebound.data.repositories.PlatesRepository
import com.ankitsuda.rebound.domain.WeightUnit
Expand Down Expand Up @@ -76,10 +77,7 @@ class PlateCalculatorComponentViewModel @Inject constructor(
fun refreshPlates(newWeight: Double) {
platesJob?.cancel()
platesJob = viewModelScope.launch {
val barbellWeight = when (weightUnit) {
WeightUnit.LBS -> barbell?.weightLbs ?: barbell?.weightKg?.fromKgToLbs() ?: 0.0
else -> barbell?.weightKg ?: barbell?.weightLbs?.fromLbsToKg() ?: 0.0
}
val barbellWeight = barbell.toWeightUnit(weightUnit ?: WeightUnit.KG)

val weightForPlates = newWeight - barbellWeight

Expand Down

0 comments on commit d0447ba

Please sign in to comment.