Skip to content

Commit

Permalink
connect limits logic to views
Browse files Browse the repository at this point in the history
  • Loading branch information
Razeeman committed Dec 21, 2024
1 parent db65d82 commit dbe2b28
Show file tree
Hide file tree
Showing 93 changed files with 1,170 additions and 263 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,32 @@ fun ChangeRunningRecordParams.Preview.GoalTimeParams.toViewData(): GoalTimeViewD
return GoalTimeViewData(
text = this.text,
complete = this.complete,
state = this.state.toViewData(),
)
}

fun GoalTimeViewData.toParams(): ChangeRunningRecordParams.Preview.GoalTimeParams {
return ChangeRunningRecordParams.Preview.GoalTimeParams(
text = this.text,
complete = this.complete,
state = this.state.toParams(),
)
}

fun ChangeRunningRecordParams.Preview.GoalSubtypeParams.toViewData(): GoalTimeViewData.Subtype {
return when (this) {
is ChangeRunningRecordParams.Preview.GoalSubtypeParams.Goal -> GoalTimeViewData.Subtype.Goal
is ChangeRunningRecordParams.Preview.GoalSubtypeParams.Limit -> GoalTimeViewData.Subtype.Limit
}
}

fun GoalTimeViewData.Subtype.toParams(): ChangeRunningRecordParams.Preview.GoalSubtypeParams {
return when (this) {
is GoalTimeViewData.Subtype.Goal -> ChangeRunningRecordParams.Preview.GoalSubtypeParams.Goal
is GoalTimeViewData.Subtype.Limit -> ChangeRunningRecordParams.Preview.GoalSubtypeParams.Limit
}
}

fun ChangeRecordDateTimeStateParams.toViewData(): ChangeRecordDateTimeState {
val state = when (val state = this.state) {
is ChangeRecordDateTimeStateParams.State.DateTime -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import com.example.util.simpletimetracker.domain.model.Category
import com.example.util.simpletimetracker.domain.model.RecordTag
import com.example.util.simpletimetracker.domain.model.RecordType
import com.example.util.simpletimetracker.feature_base_adapter.ViewHolderType
import com.example.util.simpletimetracker.feature_views.GoalCheckmarkView
import javax.inject.Inject

class ChartFilterViewDataInteractor @Inject constructor(
Expand Down Expand Up @@ -43,7 +44,7 @@ class ChartFilterViewDataInteractor @Inject constructor(
isFiltered = type.id in typeIdsFiltered,
numberOfCards = numberOfCards,
isDarkTheme = isDarkTheme,
isChecked = null,
checkState = GoalCheckmarkView.CheckState.HIDDEN,
isComplete = false,
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.example.util.simpletimetracker.feature_base_adapter.ViewHolderType
import com.example.util.simpletimetracker.core.mapper.RecordTypeViewDataMapper
import com.example.util.simpletimetracker.domain.interactor.PrefsInteractor
import com.example.util.simpletimetracker.domain.interactor.RecordTypeInteractor
import com.example.util.simpletimetracker.feature_views.GoalCheckmarkView
import javax.inject.Inject

class RecordTypesViewDataInteractor @Inject constructor(
Expand All @@ -24,7 +25,7 @@ class RecordTypesViewDataInteractor @Inject constructor(
recordType = it,
numberOfCards = numberOfCards,
isDarkTheme = isDarkTheme,
isChecked = null,
checkState = GoalCheckmarkView.CheckState.HIDDEN,
isComplete = false,
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import com.example.util.simpletimetracker.domain.model.RecordTypeGoal
import com.example.util.simpletimetracker.domain.model.Statistics
import com.example.util.simpletimetracker.feature_base_adapter.runningRecord.GoalTimeViewData
import com.example.util.simpletimetracker.feature_base_adapter.statisticsGoal.StatisticsGoalViewData
import com.example.util.simpletimetracker.feature_views.GoalCheckmarkView
import javax.inject.Inject
import kotlin.math.roundToLong

Expand All @@ -33,6 +34,7 @@ class GoalViewDataMapper @Inject constructor(
val noGoal = GoalTimeViewData(
text = "",
complete = false,
state = GoalTimeViewData.Subtype.Goal,
)
if (goal == null || goal.value <= 0L || !goalsVisible) {
return noGoal
Expand Down Expand Up @@ -78,9 +80,15 @@ class GoalViewDataMapper @Inject constructor(
"$typeString $formatted"
}

val state = when (goal.subtype) {
is RecordTypeGoal.Subtype.Goal -> GoalTimeViewData.Subtype.Goal
is RecordTypeGoal.Subtype.Limit -> GoalTimeViewData.Subtype.Limit
}

return GoalTimeViewData(
text = durationLeftString,
complete = complete,
state = state,
)
}

Expand Down Expand Up @@ -184,7 +192,15 @@ class GoalViewDataMapper @Inject constructor(
is RecordTypeGoal.Type.Count -> statistics?.data?.count.orZero()
}

val goalComplete = goalValue - current <= 0L
val goalSubtype = goal.subtype
val goalState = if (goalValue - current <= 0L) {
when (goalSubtype) {
is RecordTypeGoal.Subtype.Goal -> GoalCheckmarkView.CheckState.GOAL_REACHED
is RecordTypeGoal.Subtype.Limit -> GoalCheckmarkView.CheckState.LIMIT_REACHED
}
} else {
GoalCheckmarkView.CheckState.HIDDEN
}
val (currentValueString, goalValueString) = when (goal.type) {
is RecordTypeGoal.Type.Duration -> {
mapDuration(current) to mapDuration(goalValue)
Expand All @@ -193,8 +209,10 @@ class GoalViewDataMapper @Inject constructor(
mapCount(current) to mapCount(goalValue)
}
}
val goalHint = resourceRepo.getString(R.string.change_record_type_goal_time_hint)
.lowercase()
val goalHint = when (goalSubtype) {
is RecordTypeGoal.Subtype.Goal -> R.string.change_record_type_goal_time_hint
is RecordTypeGoal.Subtype.Limit -> R.string.change_record_type_limit_time_hint
}.let(resourceRepo::getString).lowercase()
val goalString = "$goalHint - $goalValueString"
val goalPercent = if (goalValue == 0L) {
0
Expand All @@ -206,7 +224,7 @@ class GoalViewDataMapper @Inject constructor(
goalCurrent = currentValueString,
goal = goalString,
goalPercent = goalPercent.let { "$it%" },
goalComplete = goalComplete,
goalState = goalState,
percent = goalPercent,
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import com.example.util.simpletimetracker.feature_base_adapter.ViewHolderType
import com.example.util.simpletimetracker.feature_base_adapter.empty.EmptyViewData
import com.example.util.simpletimetracker.feature_base_adapter.recordType.RecordTypeViewData
import com.example.util.simpletimetracker.feature_base_adapter.recordTypeSpecial.RunningRecordTypeSpecialViewData
import com.example.util.simpletimetracker.feature_views.GoalCheckmarkView
import com.example.util.simpletimetracker.feature_views.viewData.RecordTypeIcon
import javax.inject.Inject

Expand Down Expand Up @@ -48,7 +49,7 @@ class RecordTypeViewDataMapper @Inject constructor(
recordType: RecordType,
numberOfCards: Int,
isDarkTheme: Boolean,
isChecked: Boolean?,
checkState: GoalCheckmarkView.CheckState,
isComplete: Boolean,
): RecordTypeViewData {
return RecordTypeViewData(
Expand All @@ -60,7 +61,7 @@ class RecordTypeViewDataMapper @Inject constructor(
width = recordTypeCardSizeMapper.toCardWidth(numberOfCards),
height = recordTypeCardSizeMapper.toCardHeight(numberOfCards),
asRow = recordTypeCardSizeMapper.toCardAsRow(numberOfCards),
isChecked = isChecked,
checkState = checkState,
isComplete = isComplete,
)
}
Expand All @@ -70,14 +71,14 @@ class RecordTypeViewDataMapper @Inject constructor(
numberOfCards: Int,
isDarkTheme: Boolean,
isFiltered: Boolean,
isChecked: Boolean?,
checkState: GoalCheckmarkView.CheckState,
isComplete: Boolean,
): RecordTypeViewData {
val default = map(
recordType = recordType,
numberOfCards = numberOfCards,
isDarkTheme = isDarkTheme,
isChecked = isChecked,
checkState = checkState,
isComplete = isComplete,
)

Expand All @@ -103,7 +104,7 @@ class RecordTypeViewDataMapper @Inject constructor(
icon = RecordTypeIcon.Image(R.drawable.add),
numberOfCards = numberOfCards,
isDarkTheme = isDarkTheme,
isChecked = null,
checkState = GoalCheckmarkView.CheckState.HIDDEN,
)
}

Expand All @@ -117,7 +118,7 @@ class RecordTypeViewDataMapper @Inject constructor(
icon = RecordTypeIcon.Image(R.drawable.add),
numberOfCards = numberOfCards,
isDarkTheme = isDarkTheme,
isChecked = null,
checkState = GoalCheckmarkView.CheckState.HIDDEN,
)
}

Expand All @@ -131,7 +132,7 @@ class RecordTypeViewDataMapper @Inject constructor(
icon = RecordTypeIcon.Image(R.drawable.repeat),
numberOfCards = numberOfCards,
isDarkTheme = isDarkTheme,
isChecked = null,
checkState = GoalCheckmarkView.CheckState.HIDDEN,
)
}

Expand All @@ -146,16 +147,20 @@ class RecordTypeViewDataMapper @Inject constructor(
icon = RecordTypeIcon.Image(R.drawable.pomodoro),
numberOfCards = numberOfCards,
isDarkTheme = isDarkTheme,
// Somewhat weird logic, null - means do not show, false - red dot not checked.
isChecked = if (isPomodoroStarted) false else null,
// Somewhat weird logic, GOAL_NOT_REACHED - red dot not checked.
checkState = if (isPomodoroStarted) {
GoalCheckmarkView.CheckState.GOAL_NOT_REACHED
} else {
GoalCheckmarkView.CheckState.HIDDEN
},
)
}

fun mapGoalCheckmark(
type: RecordType,
goals: Map<Long, List<RecordTypeGoal>>,
allDailyCurrents: Map<Long, GetCurrentRecordsDurationInteractor.Result>,
): Boolean? {
): GoalCheckmarkView.CheckState {
return mapGoalCheckmark(
goal = goals[type.id].orEmpty().getDaily(),
dailyCurrent = allDailyCurrents[type.id],
Expand All @@ -165,7 +170,7 @@ class RecordTypeViewDataMapper @Inject constructor(
fun mapGoalCheckmark(
goal: RecordTypeGoal?,
dailyCurrent: GetCurrentRecordsDurationInteractor.Result?,
): Boolean? {
): GoalCheckmarkView.CheckState {
val goalValue = when (goal?.type) {
is RecordTypeGoal.Type.Duration -> goal.value * 1000
is RecordTypeGoal.Type.Count -> goal.value
Expand All @@ -177,8 +182,28 @@ class RecordTypeViewDataMapper @Inject constructor(
else -> 0
}
val valueLeft = goalValue - current

return if (goal != null) valueLeft <= 0L else null
val isLimit = goal?.subtype == RecordTypeGoal.Subtype.Limit

// TODO GOAL excess for goal count?
// TODO GOAL detailed stats, excess graph, count deficit when should have a goal.
// TODO GOAL streaks, skip count days when should not have a goal (daily goals).
return if (goal != null) {
if (valueLeft <= 0L) {
if (isLimit) {
GoalCheckmarkView.CheckState.LIMIT_REACHED
} else {
GoalCheckmarkView.CheckState.GOAL_REACHED
}
} else {
if (isLimit) {
GoalCheckmarkView.CheckState.LIMIT_NOT_REACHED
} else {
GoalCheckmarkView.CheckState.GOAL_NOT_REACHED
}
}
} else {
GoalCheckmarkView.CheckState.HIDDEN
}
}

private fun mapToSpecial(
Expand All @@ -187,7 +212,7 @@ class RecordTypeViewDataMapper @Inject constructor(
icon: RecordTypeIcon,
numberOfCards: Int,
isDarkTheme: Boolean,
isChecked: Boolean?,
checkState: GoalCheckmarkView.CheckState,
): RunningRecordTypeSpecialViewData {
return RunningRecordTypeSpecialViewData(
type = type,
Expand All @@ -197,7 +222,7 @@ class RecordTypeViewDataMapper @Inject constructor(
width = recordTypeCardSizeMapper.toCardWidth(numberOfCards),
height = recordTypeCardSizeMapper.toCardHeight(numberOfCards),
asRow = recordTypeCardSizeMapper.toCardAsRow(numberOfCards),
isChecked = isChecked,
checkState = checkState,
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import androidx.cardview.widget.CardView
import androidx.recyclerview.widget.RecyclerView
import com.example.util.simpletimetracker.core.R
import com.example.util.simpletimetracker.domain.interactor.UpdateRunningRecordFromChangeScreenInteractor
import com.example.util.simpletimetracker.domain.interactor.UpdateRunningRecordFromChangeScreenInteractor.GoalState
import com.example.util.simpletimetracker.feature_base_adapter.ViewHolderType
import com.example.util.simpletimetracker.feature_base_adapter.runningRecord.RunningRecordViewData
import com.example.util.simpletimetracker.feature_views.GoalCheckmarkView.CheckState
import com.example.util.simpletimetracker.feature_views.RunningRecordView
import com.example.util.simpletimetracker.feature_views.extension.getThemedAttr

Expand Down Expand Up @@ -44,6 +46,10 @@ fun updateRunningRecordPreview(
if (it.itemGoalTime.isNotEmpty() && update.goalText.isNotEmpty()) {
it.itemGoalTime = update.goalText
it.itemGoalTimeComplete = update.goalComplete
it.itemGoalTimeCheck = when (update.goalState) {
is GoalState.Goal -> CheckState.GOAL_REACHED
is GoalState.Limit -> CheckState.LIMIT_REACHED
}
}

update.additionalData?.let { additionalUpdate ->
Expand Down
Loading

0 comments on commit dbe2b28

Please sign in to comment.