Skip to content

Commit

Permalink
Refactor Routine to Habit
Browse files Browse the repository at this point in the history
Keep Routine in all places where it represents an abstract term and may imply a simple task, for example.
  • Loading branch information
DanielRendox committed Jan 7, 2024
1 parent 4cebc17 commit 6a96719
Show file tree
Hide file tree
Showing 15 changed files with 43 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ internal fun AddRoutineRoute(
saveRoutine = { routine ->
GlobalScope.launch {
withTimeout(10_000L) {
println("resulting routine = $routine")
habitRepository.insertHabit(routine)
}
}
Expand Down Expand Up @@ -181,7 +180,7 @@ private fun NavigationProgressIndicator(
}

@Composable
fun AddRoutineDestinationTopAppBar(
fun AddHabitDestinationTopAppBar(
modifier: Modifier = Modifier,
destination: AddRoutineDestination,
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import com.rendox.routinetracker.add_routine.choose_schedule.ChooseSchedulePageS
import com.rendox.routinetracker.add_routine.choose_schedule.assembleSchedule
import com.rendox.routinetracker.add_routine.navigation.AddRoutineDestination
import com.rendox.routinetracker.add_routine.navigation.navigate
import com.rendox.routinetracker.add_routine.navigation.yesNoRoutineDestinations
import com.rendox.routinetracker.add_routine.navigation.yesNoHabitDestinations
import com.rendox.routinetracker.add_routine.set_goal.SetGoalPageState
import com.rendox.routinetracker.add_routine.tweak_routine.TweakRoutinePageState
import com.rendox.routinetracker.core.model.Habit
Expand Down Expand Up @@ -46,7 +46,7 @@ class AddRoutineScreenState(
var tweakRoutinePageState by mutableStateOf(tweakRoutinePageState)
private set

var navDestinations by mutableStateOf(yesNoRoutineDestinations)
var navDestinations by mutableStateOf(yesNoHabitDestinations)
private set

var navigateBackButtonText: UiText by mutableStateOf(UiText.DynamicString(""))
Expand Down Expand Up @@ -133,13 +133,13 @@ class AddRoutineScreenState(

private fun updateNavDestinations(routineType: RoutineTypeUi) {
navDestinations = when (routineType) {
RoutineTypeUi.YesNoRoutine -> yesNoRoutineDestinations
RoutineTypeUi.MeasurableRoutine -> TODO()
RoutineTypeUi.YesNoHabit -> yesNoHabitDestinations
RoutineTypeUi.MeasurableHabit -> TODO()
}
}

private fun assembleRoutine(): Habit = when (chooseRoutineTypePageState.routineType) {
is RoutineTypeUi.YesNoRoutine -> Habit.YesNoHabit(
is RoutineTypeUi.YesNoHabit -> Habit.YesNoHabit(
name = setGoalPageState.routineName,
description = setGoalPageState.routineDescription,
schedule = chooseSchedulePageState.selectedSchedulePickerState.assembleSchedule(
Expand All @@ -149,7 +149,7 @@ class AddRoutineScreenState(
defaultCompletionTime = tweakRoutinePageState.sessionTime?.toKotlinLocalTime(),
)

is RoutineTypeUi.MeasurableRoutine -> TODO()
is RoutineTypeUi.MeasurableHabit -> TODO()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import com.rendox.routinetracker.add_routine.AddRoutineDestinationTopAppBar
import com.rendox.routinetracker.add_routine.AddHabitDestinationTopAppBar
import com.rendox.routinetracker.add_routine.navigation.AddRoutineDestination
import com.rendox.routinetracker.feature.agenda.R

Expand All @@ -29,11 +29,11 @@ fun ChooseRoutineTypePage(
modifier = modifier.verticalScroll(rememberScrollState()),
horizontalAlignment = Alignment.CenterHorizontally,
) {
AddRoutineDestinationTopAppBar(
AddHabitDestinationTopAppBar(
destination = AddRoutineDestination.ChooseRoutineType
)

for (routineType in routineTypes) {
for (routineType in habitTypes) {
ChooseRoutineTypeButton(
modifier = Modifier.padding(top = 16.dp, start = 16.dp, end = 16.dp),
routineType = routineType,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import androidx.compose.runtime.setValue

@Stable
class ChooseRoutineTypePageState(
routineType: RoutineTypeUi = RoutineTypeUi.YesNoRoutine
routineType: RoutineTypeUi = RoutineTypeUi.YesNoHabit
) {
var routineType: RoutineTypeUi by mutableStateOf(routineType)
private set
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,30 @@ sealed class RoutineTypeUi(
@StringRes val descriptionId: Int,
val inDevelopment: Boolean,
) {
object YesNoRoutine : RoutineTypeUi(
object YesNoHabit : RoutineTypeUi(
routineTypeId = 1,
titleId = R.string.yes_no_routine_title,
descriptionId = R.string.yes_no_routine_description,
titleId = R.string.yes_no_habit_title,
descriptionId = R.string.yes_no_habit_description,
inDevelopment = false,
)

object MeasurableRoutine : RoutineTypeUi(
object MeasurableHabit : RoutineTypeUi(
routineTypeId = 2,
titleId = R.string.measurable_routine_title,
descriptionId = R.string.measurable_routine_description,
titleId = R.string.measurable_habit_title,
descriptionId = R.string.measurable_habit_description,
inDevelopment = true,
)

companion object {
fun getTypeById(id: Int) = when (id) {
YesNoRoutine.routineTypeId -> YesNoRoutine
MeasurableRoutine.routineTypeId -> MeasurableRoutine
YesNoHabit.routineTypeId -> YesNoHabit
MeasurableHabit.routineTypeId -> MeasurableHabit
else -> throw IllegalArgumentException()
}
}
}

val routineTypes = listOf(
RoutineTypeUi.YesNoRoutine,
RoutineTypeUi.MeasurableRoutine,
val habitTypes = listOf(
RoutineTypeUi.YesNoHabit,
RoutineTypeUi.MeasurableHabit,
)
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.rendox.routinetracker.add_routine.AddRoutineDestinationTopAppBar
import com.rendox.routinetracker.add_routine.AddHabitDestinationTopAppBar
import com.rendox.routinetracker.add_routine.choose_schedule.schedule_pickers.ScheduleTypeUi
import com.rendox.routinetracker.add_routine.choose_schedule.schedule_pickers.AlternateDaysSchedulePicker
import com.rendox.routinetracker.add_routine.choose_schedule.schedule_pickers.EveryDaySchedulePicker
Expand All @@ -26,7 +26,7 @@ fun ChooseSchedulePage(
.padding(start = 8.dp, end = 16.dp)
.verticalScroll(rememberScrollState())
) {
AddRoutineDestinationTopAppBar(destination = AddRoutineDestination.ChooseSchedule)
AddHabitDestinationTopAppBar(destination = AddRoutineDestination.ChooseSchedule)

EveryDaySchedulePicker(
everyDaySchedulePickerState = chooseSchedulePageState.everyDaySchedulePickerState,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ sealed class AddRoutineDestination(
)
}

val yesNoRoutineDestinations = listOf(
val yesNoHabitDestinations = listOf(
AddRoutineDestination.ChooseRoutineType,
AddRoutineDestination.SetGoal,
AddRoutineDestination.ChooseSchedule,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.input.ImeAction
import androidx.compose.ui.unit.dp
import com.rendox.routinetracker.add_routine.AddRoutineDestinationTopAppBar
import com.rendox.routinetracker.add_routine.AddHabitDestinationTopAppBar
import com.rendox.routinetracker.add_routine.navigation.AddRoutineDestination
import com.rendox.routinetracker.feature.agenda.R

Expand All @@ -23,7 +23,7 @@ fun SetGoalPage(
setGoalPageState: SetGoalPageState,
) {
Column(modifier = modifier.verticalScroll(rememberScrollState())) {
AddRoutineDestinationTopAppBar(destination = AddRoutineDestination.SetGoal)
AddHabitDestinationTopAppBar(destination = AddRoutineDestination.SetGoal)

OutlinedTextField(
modifier = Modifier
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import androidx.compose.ui.text.input.ImeAction
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.rendox.routinetracker.add_routine.AddRoutineDestinationTopAppBar
import com.rendox.routinetracker.add_routine.AddHabitDestinationTopAppBar
import com.rendox.routinetracker.add_routine.navigation.AddRoutineDestination
import com.rendox.routinetracker.core.ui.components.CustomIconSetting
import com.rendox.routinetracker.core.ui.components.Setting
Expand Down Expand Up @@ -83,7 +83,7 @@ fun TweakRoutinePage(
Column(
modifier = modifier.verticalScroll(rememberScrollState())
) {
AddRoutineDestinationTopAppBar(
AddHabitDestinationTopAppBar(
modifier = Modifier.padding(bottom = 8.dp),
destination = AddRoutineDestination.TweakRoutine,
)
Expand Down
8 changes: 4 additions & 4 deletions feature/add_edit_routine/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
<string name="choose_schedule_page_name">Which days work best for your schedule?</string>
<string name="tweak_routine_page_name">Fine-tune your habit</string>

<string name="yes_no_routine_title">Yes or No</string>
<string name="yes_no_routine_description">Click a checkmark when you complete the habit.</string>
<string name="measurable_routine_title">Measurable</string>
<string name="measurable_routine_description">
<string name="yes_no_habit_title">Yes or No</string>
<string name="yes_no_habit_description">Click a checkmark when you complete the habit.</string>
<string name="measurable_habit_title">Measurable</string>
<string name="measurable_habit_description">
Establish a daily goal for a habit. E.g. read 10 pages of the book.
</string>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ private val routines = listOf(
id = 1,
hasGrayedOutLook = false,
statusToggleIsDisabled = false,
type = DisplayHabitType.YesNoHabit,
type = DisplayRoutineType.YesNoHabit,
numOfTimesCompleted = 1f,
),
DisplayRoutine(
Expand All @@ -260,7 +260,7 @@ private val routines = listOf(
id = 2,
hasGrayedOutLook = false,
statusToggleIsDisabled = false,
type = DisplayHabitType.YesNoHabit,
type = DisplayRoutineType.YesNoHabit,
numOfTimesCompleted = 0f,
),
DisplayRoutine(
Expand All @@ -270,7 +270,7 @@ private val routines = listOf(
id = 3,
hasGrayedOutLook = false,
statusToggleIsDisabled = true,
type = DisplayHabitType.YesNoHabit,
type = DisplayRoutineType.YesNoHabit,
numOfTimesCompleted = 0f,
),
DisplayRoutine(
Expand All @@ -280,7 +280,7 @@ private val routines = listOf(
id = 4,
hasGrayedOutLook = true,
statusToggleIsDisabled = false,
type = DisplayHabitType.YesNoHabit,
type = DisplayRoutineType.YesNoHabit,
numOfTimesCompleted = 0f,
)
)
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ internal fun AgendaRoute(
onAddRoutineClick = onAddRoutineClick,
onRoutineClick = onRoutineClick,
insertCompletion = { routineId, completionRecord ->
viewModel.onHabitComplete(routineId, completionRecord)
viewModel.onRoutineComplete(routineId, completionRecord)
},
onDateChange = { viewModel.onDateChange(it.toKotlinLocalDate()) },
onNotDueRoutinesVisibilityToggle = {
Expand Down Expand Up @@ -143,7 +143,7 @@ internal fun AgendaScreen(
onRoutineClick = onRoutineClick,
onStatusCheckmarkClick = { routine ->
when (routine.type) {
DisplayHabitType.YesNoHabit -> {
DisplayRoutineType.YesNoHabit -> {
val completion = Habit.YesNoHabit.CompletionRecord(
date = currentDate.toKotlinLocalDate(),
numOfTimesCompleted = if (routine.numOfTimesCompleted > 0F) 0F else 1F,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ class AgendaScreenViewModel(
return DisplayRoutine(
name = habit.name,
id = habit.id!!,
type = DisplayHabitType.YesNoHabit,
type = DisplayRoutineType.YesNoHabit,
status = habitStatus,
numOfTimesCompleted = numOfTimesCompleted,
completionTime = null,
Expand All @@ -102,7 +102,7 @@ class AgendaScreenViewModel(
)
}

fun onHabitComplete(
fun onRoutineComplete(
routineId: Long,
completionRecord: Habit.CompletionRecord,
) {
Expand Down Expand Up @@ -146,19 +146,6 @@ class AgendaScreenViewModel(

// TODO update todayFlow when the date changes (in case the screen is opened at midnight)

// fun onAddRoutineClick() {
// viewModelScope.launch {
// routineRepository.insertRoutine(routineList.first())
// allRoutinesFlow.update { routineRepository.getAllRoutines() }
// val currentDateRoutines = getRoutinesForDate(_currentDateFlow.value)
// cashedRoutinesFlow.update {
// mapOf(_currentDateFlow.value to currentDateRoutines)
// }
// println("current date = ${_currentDateFlow.value}")
// println("cashed routines flow = $cashedRoutinesFlow")
// }
// }

companion object {
private val dueOrCompletedStatuses = listOf(
HabitStatus.Planned,
Expand All @@ -179,14 +166,14 @@ class AgendaScreenViewModel(
data class DisplayRoutine(
val name: String,
val id: Long,
val type: DisplayHabitType,
val type: DisplayRoutineType,
val status: HabitStatus,
val numOfTimesCompleted: Float,
val completionTime: LocalTime?,
val hasGrayedOutLook: Boolean,
val statusToggleIsDisabled: Boolean,
)

enum class DisplayHabitType {
enum class DisplayRoutineType {
YesNoHabit,
}

This file was deleted.

0 comments on commit 6a96719

Please sign in to comment.