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

Commit

Permalink
Support lbs in plate calculator
Browse files Browse the repository at this point in the history
  • Loading branch information
AnkitSuda committed Nov 15, 2022
1 parent 3cb9956 commit c2dbab9
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 33 deletions.
12 changes: 6 additions & 6 deletions app/src/main/java/com/ankitsuda/rebound/ui/main/MainScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,12 @@ private fun MainLayout(

ReboundThemeWrapper(themeState = themeState) {
NavigatorHost {
ReboundKeyboardHost {
CompositionLocalProvider(
LocalDialog provides dialog,
LocalPanel provides mainPanel,
LocalAppSettings provides appSettings
) {
CompositionLocalProvider(
LocalDialog provides dialog,
LocalPanel provides mainPanel,
LocalAppSettings provides appSettings
) {
ReboundKeyboardHost {
Box() {
/**
* Temporary using ModalBottomSheetLayout
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import androidx.room.Dao
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import com.ankitsuda.rebound.domain.WeightUnit
import com.ankitsuda.rebound.domain.entities.Muscle
import com.ankitsuda.rebound.domain.entities.Plate
import kotlinx.coroutines.flow.Flow
Expand All @@ -31,8 +32,8 @@ interface PlatesDao {
@Query("SELECT * FROM plates ORDER BY weight")
fun getPlates(): Flow<List<Plate>>

@Query("SELECT * FROM plates WHERE is_active = 1")
fun getActivePlates(): Flow<List<Plate>>
@Query("SELECT * FROM plates WHERE for_weight_unit = :forWeightUnit AND is_active = 1")
fun getActivePlates(forWeightUnit: WeightUnit): Flow<List<Plate>>

@Query("UPDATE plates SET is_active = :isActive WHERE id = :plateId")
suspend fun updateIsActive(plateId: String, isActive: Boolean)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@ package com.ankitsuda.rebound.data.repositories

import com.ankitsuda.rebound.data.db.daos.MusclesDao
import com.ankitsuda.rebound.data.db.daos.PlatesDao
import com.ankitsuda.rebound.domain.WeightUnit
import com.ankitsuda.rebound.domain.entities.Plate
import javax.inject.Inject

class PlatesRepository @Inject constructor(private val platesDao: PlatesDao) {

fun getPlates() = platesDao.getPlates()

fun getActivePlates() = platesDao.getActivePlates()
fun getActivePlates(forWeightUnit: WeightUnit) = platesDao.getActivePlates(forWeightUnit)

fun getPlate(plateId: String) = platesDao.getPlate(plateId)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,10 @@ data class Plate(
@ColumnInfo(name = "id")
val id: String,

// weight column is stored in kg
// weight column should be stored according to the weight unit
@ColumnInfo(name = "weight")
var weight: Double? = null,

// forWeightUnit does not represent weight unit of weight column
@ColumnInfo(name = "for_weight_unit")
var forWeightUnit: WeightUnit? = null,

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import com.ankitsuda.common.compose.LocalAppSettings
import com.ankitsuda.rebound.ui.keyboard.enums.KeyboardModeType
import com.ankitsuda.rebound.ui.keyboard.enums.ReboundKeyboardType
import com.ankitsuda.rebound.ui.keyboard.models.ClearNumKey
Expand Down Expand Up @@ -90,14 +91,16 @@ fun ReboundSetKeyboardComponent(
)
}
KeyboardModeType.PLATE_CALCULATOR -> {
PlateCalculatorComponent(
modifier = Modifier
.fillMaxWidth()
.height(
height = 250.dp,
),
weight = inputConnection?.getText()?.toDoubleOrNull() ?: 0.0
)
key(LocalAppSettings.current.weightUnit) {
PlateCalculatorComponent(
modifier = Modifier
.fillMaxWidth()
.height(
height = 250.dp,
),
weight = inputConnection?.getText()?.toDoubleOrNull() ?: 0.0
)
}
}
KeyboardModeType.WARMUP_PICKER -> {
WarmUpListPickerComponent(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.hilt.navigation.compose.hiltViewModel
import com.ankitsuda.base.util.toReadableString
import com.ankitsuda.common.compose.userPrefWeightUnitStr
import com.ankitsuda.rebound.domain.entities.Plate
import com.ankitsuda.rebound.ui.components.RSpacer
import com.ankitsuda.rebound.ui.theme.LocalThemeState
Expand Down Expand Up @@ -58,13 +59,13 @@ fun PlateCalculatorComponent(
Column(modifier = Modifier.height(comHeight * 0.35F)) {
Text(
modifier = Modifier.padding(start = 16.dp, top = 16.dp),
text = "${weight.toReadableString()} kg", // TODO: Move to strings.xml
text = "${weight.toReadableString()} ${userPrefWeightUnitStr()}",
color = theme.keyboardContentColor
)
if (remainingWeight > 0.0) {
Text(
modifier = Modifier.padding(start = 16.dp, top = 4.dp),
text = "Remaining weight: ${remainingWeight.toReadableString()} kg", // TODO: Move to strings.xml
text = "Remaining weight: ${remainingWeight.toReadableString()} ${userPrefWeightUnitStr()}", // TODO: Move to strings.xml
style = ReboundTheme.typography.caption,
color = theme.keyboardContentColor.copy(alpha = 0.75f)
)
Expand Down Expand Up @@ -111,7 +112,7 @@ private fun BarbellComponent(
contentAlignment = Alignment.Center
) {
Text(
text = "${barbellWeight.toReadableString()} kg",
text = "${barbellWeight.toReadableString()} ${userPrefWeightUnitStr()}",
fontSize = 12.sp,
color = onBarbellColor
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package com.ankitsuda.rebound.ui.keyboard.platecalculator

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.ankitsuda.rebound.data.datastore.AppPreferences
import com.ankitsuda.rebound.data.repositories.PlatesRepository
import com.ankitsuda.rebound.domain.entities.Plate
import dagger.hilt.android.lifecycle.HiltViewModel
Expand All @@ -29,6 +30,7 @@ import kotlin.math.roundToInt

@HiltViewModel
class PlateCalculatorComponentViewModel @Inject constructor(
private val prefs: AppPreferences,
private val platesRepository: PlatesRepository
) : ViewModel() {

Expand All @@ -45,11 +47,13 @@ class PlateCalculatorComponentViewModel @Inject constructor(

init {
viewModelScope.launch {
platesRepository.getActivePlates().collectLatest {
_allPlates.clear()
_allPlates.addAll(it)
if (lastWeight != null) {
refreshPlates(lastWeight!!)
prefs.weightUnit.collectLatest { unit ->
platesRepository.getActivePlates(forWeightUnit = unit).collectLatest {
_allPlates.clear()
_allPlates.addAll(it)
if (lastWeight != null) {
refreshPlates(lastWeight!!)
}
}
}
}
Expand All @@ -58,12 +62,8 @@ class PlateCalculatorComponentViewModel @Inject constructor(
fun refreshPlates(newWeight: Double) {
platesJob?.cancel()
platesJob = viewModelScope.launch {
if (_allPlates.isEmpty()) {
val availablePlates = platesRepository.getActivePlates().first()
_allPlates.clear()
_allPlates.addAll(availablePlates)
}
val platesNeeded = calculatePlates(newWeight, _allPlates.sortedByDescending { it.weight })
val platesNeeded =
calculatePlates(newWeight, _allPlates.sortedByDescending { it.weight })
val sumOfPlates = platesNeeded.sumOf { it.weight ?: 0.0 }
_plates.value = platesNeeded
_remainingWeight.value = try {
Expand Down

0 comments on commit c2dbab9

Please sign in to comment.