Skip to content

Commit

Permalink
fix notification switch errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Razeeman committed Nov 16, 2024
1 parent d633a0d commit 66397db
Show file tree
Hide file tree
Showing 12 changed files with 30 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ object Base {
const val namespace = "com.example.util.simpletimetracker"

// Raise by 2 to account for wear version code.
const val versionCode = 73
const val versionCode = 75
const val versionName = "1.46"
const val minSDK = 21
const val currentSDK = 34
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ fun Int?.orZero(): Int = this ?: 0

fun Float?.orZero(): Float = this ?: 0f

fun Range?.orEmpty(): Range = this ?: Range(0,0)
fun Range?.orEmpty(): Range = this ?: Range(0, 0)

fun <T> List<T>?.orEmpty(): List<T> = this ?: emptyList()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ class RemoveRunningRecordMediator @Inject constructor(
suspend fun removeWithRecordAdd(
runningRecord: RunningRecord,
updateWidgets: Boolean = true,
updateNotificationSwitch: Boolean = true,
timeEnded: Long? = null, // null - take current time.
) {
val recordTimeEnded = timeEnded ?: System.currentTimeMillis()
Expand All @@ -39,7 +38,6 @@ class RemoveRunningRecordMediator @Inject constructor(
remove(
typeId = runningRecord.id,
updateWidgets = updateWidgets,
updateNotificationSwitch = updateNotificationSwitch,
)
pomodoroStopInteractor.checkAndStop(runningRecord.id)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,11 @@ class ChangeRunningRecordViewModel @Inject constructor(
doAfter: suspend () -> Unit,
) {
// Widgets will update on adding.
removeRunningRecordMediator.remove(extra.id, updateWidgets = false)
removeRunningRecordMediator.remove(
typeId = extra.id,
updateWidgets = false,
updateNotificationSwitch = false,
)
addRunningRecordMediator.addAfterChange(
typeId = newTypeId,
timeStarted = newTimeStarted,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ class RecordQuickActionsViewModel @Inject constructor(
is Type.RecordRunning -> listOf(
RecordQuickActionsState.Button.Statistics(false),
RecordQuickActionsState.Button.Delete(false),
RecordQuickActionsState.Button.Stop(true)
RecordQuickActionsState.Button.Stop(true),
)
null -> emptyList()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import com.example.util.simpletimetracker.domain.REPEAT_BUTTON_ITEM_ID
import com.example.util.simpletimetracker.domain.model.RecordTag
import com.example.util.simpletimetracker.domain.model.RecordType
import com.example.util.simpletimetracker.domain.model.RecordTypeGoal
import com.example.util.simpletimetracker.domain.model.RunningRecord
import com.example.util.simpletimetracker.feature_notification.R
import com.example.util.simpletimetracker.feature_notification.activitySwitch.manager.NotificationControlsParams
import com.example.util.simpletimetracker.feature_views.viewData.RecordTypeIcon
Expand All @@ -30,7 +29,6 @@ class GetNotificationActivitySwitchControlsInteractor @Inject constructor(
hint: String,
isDarkTheme: Boolean,
types: List<RecordType>,
runningRecords: List<RunningRecord> = emptyList(),
showRepeatButton: Boolean,
typesShift: Int = 0,
tags: List<RecordTag> = emptyList(),
Expand All @@ -40,7 +38,6 @@ class GetNotificationActivitySwitchControlsInteractor @Inject constructor(
allDailyCurrents: Map<Long, GetCurrentRecordsDurationInteractor.Result>,
): NotificationControlsParams {
val typesMap = types.associateBy { it.id }
val runningRecordsIds = runningRecords.map { it.id }

val repeatButtonViewData = if (showRepeatButton) {
val viewData = recordTypeViewDataMapper.mapToRepeatItem(
Expand All @@ -64,11 +61,7 @@ class GetNotificationActivitySwitchControlsInteractor @Inject constructor(
NotificationControlsParams.Type(
id = type.id,
icon = type.icon.let(iconMapper::mapIcon),
color = if (type.id in runningRecordsIds) {
colorMapper.toFilteredColor(isDarkTheme)
} else {
type.color.let { colorMapper.mapToColorInt(it, isDarkTheme) }
},
color = type.color.let { colorMapper.mapToColorInt(it, isDarkTheme) },
isChecked = recordTypeViewDataMapper.mapGoalCheckmark(
type = type,
goals = goals,
Expand Down Expand Up @@ -111,6 +104,7 @@ class GetNotificationActivitySwitchControlsInteractor @Inject constructor(
controlIconPrev = RecordTypeIcon.Image(R.drawable.arrow_left),
controlIconNext = RecordTypeIcon.Image(R.drawable.arrow_right),
controlIconColor = colorMapper.toInactiveColor(isDarkTheme),
filteredTypeColor = colorMapper.toInactiveColor(isDarkTheme),
selectedTypeId = selectedTypeId,
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ class NotificationActivitySwitchInteractorImpl @Inject constructor(
val firstDayOfWeek = prefsInteractor.getFirstDayOfWeek()
val startOfDayShift = prefsInteractor.getStartOfDayShift()
val retroactiveTrackingModeEnabled = prefsInteractor.getRetroactiveTrackingMode()
val runningRecords = runningRecordInteractor.getAll()
val range = timeMapper.getRangeStartAndEnd(
rangeLength = RangeLength.Day,
shift = 0,
Expand Down Expand Up @@ -97,7 +96,7 @@ class NotificationActivitySwitchInteractorImpl @Inject constructor(
val allDailyCurrents = if (goals.isNotEmpty()) {
getCurrentRecordsDurationInteractor.getAllDailyCurrents(
typeIds = recordTypes.keys.toList(),
runningRecords = runningRecords,
runningRecords = runningRecordInteractor.getAll(),
)
} else {
// No goals - no need to calculate durations.
Expand All @@ -107,7 +106,6 @@ class NotificationActivitySwitchInteractorImpl @Inject constructor(
hint = "", // Replaced later.
isDarkTheme = isDarkTheme,
types = recordTypes.values.toList(),
runningRecords = runningRecords,
showRepeatButton = showRepeatButton,
typesShift = typesShift,
tags = viewedTags,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ class NotificationControlsManager @Inject constructor(
// Types buttons
val currentTypes = params.types.drop(params.typesShift).take(TYPES_LIST_SIZE)
currentTypes.forEach {
val recordTypeId = (from as? From.ActivityNotification)?.recordTypeId
val action = when (from) {
is From.ActivityNotification -> {
val recordTypeId = (from as? From.ActivityNotification)?.recordTypeId
if (it.id == recordTypeId) {
ACTION_NOTIFICATION_CONTROLS_STOP
} else {
Expand All @@ -94,9 +94,14 @@ class NotificationControlsManager @Inject constructor(
ACTION_NOTIFICATION_CONTROLS_TYPE_CLICK
}
}
val color = if (recordTypeId == it.id) {
params.filteredTypeColor
} else {
it.color
}
getTypeControlView(
icon = it.icon,
color = it.color,
color = color,
isChecked = it.isChecked,
isComplete = it.isComplete,
intent = getPendingSelfIntent(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ sealed interface NotificationControlsParams {
val controlIconPrev: RecordTypeIcon,
val controlIconNext: RecordTypeIcon,
val controlIconColor: Int,
val filteredTypeColor: Int,
val selectedTypeId: Long?,
) : NotificationControlsParams

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,8 @@ class NotificationReceiver : BroadcastReceiver() {
typeController.onActionActivityStop(typeId)
}
ACTION_NOTIFICATION_CONTROLS_STOP -> {
val from = intent.getIntExtra(ARGS_CONTROLS_FROM, 0)
val typeId = intent.getLongExtra(ARGS_TYPE_ID, 0)
typeController.onActionActivityStop(from, typeId)
typeController.onActionActivityStop(typeId)
}
ACTION_NOTIFICATION_CONTROLS_TYPE_CLICK -> {
val from = intent.getIntExtra(ARGS_CONTROLS_FROM, 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package com.example.util.simpletimetracker.feature_notification.recordType.contr

import com.example.util.simpletimetracker.domain.interactor.NotificationActivitySwitchInteractor
import com.example.util.simpletimetracker.domain.interactor.NotificationTypeInteractor
import com.example.util.simpletimetracker.feature_notification.activitySwitch.manager.NotificationControlsManager
import com.example.util.simpletimetracker.feature_notification.activitySwitch.mapper.NotificationControlsMapper
import com.example.util.simpletimetracker.feature_notification.recordType.interactor.ActivityStartStopFromBroadcastInteractor
import kotlinx.coroutines.DelicateCoroutinesApi
Expand Down Expand Up @@ -45,27 +44,7 @@ class NotificationTypeBroadcastController @Inject constructor(
) {
if (typeId == 0L) return
GlobalScope.launch {
activityStartStopFromBroadcastInteractor.onActionActivityStop(
typeId = typeId,
fromControls = false,
)
}
}

fun onActionActivityStop(
from: Int,
typeId: Long,
) {
if (typeId == 0L) return
val fromData = notificationControlsMapper.mapExtraToFrom(
extra = from,
recordTypeId = typeId,
)
GlobalScope.launch {
activityStartStopFromBroadcastInteractor.onActionActivityStop(
typeId = typeId,
fromControls = fromData is NotificationControlsManager.From.ActivitySwitch,
)
activityStartStopFromBroadcastInteractor.onActionActivityStop(typeId)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,19 +58,17 @@ class ActivityStartStopFromBroadcastInteractor @Inject constructor(
name: String,
) {
val typeId = getTypeIdByName(name) ?: return
onActionActivityStop(typeId = typeId, fromControls = false)
onActionActivityStop(typeId)
}

suspend fun onActionActivityStop(
typeId: Long,
fromControls: Boolean,
) {
val runningRecord = runningRecordInteractor.get(typeId)
?: return // Not running.

removeRunningRecordMediator.removeWithRecordAdd(
runningRecord = runningRecord,
updateNotificationSwitch = !fromControls,
)
}

Expand Down Expand Up @@ -146,22 +144,11 @@ class ActivityStartStopFromBroadcastInteractor @Inject constructor(
return
}

// Switch controls are updated separately right from here,
// so no need to update after record change.
if (from is NotificationControlsManager.From.ActivitySwitch) {
if (runningRecordInteractor.get(selectedTypeId) != null) {
onActionActivityStop(
typeId = selectedTypeId,
fromControls = true,
)
update(from, typesShift)
return
}
}
val updateNotificationSwitch = from !is NotificationControlsManager.From.ActivitySwitch
val started = addRunningRecordMediator.tryStartTimer(
typeId = selectedTypeId,
updateNotificationSwitch = updateNotificationSwitch,
// Switch controls are updated separately right from here,
// so no need to update after record change.
updateNotificationSwitch = false,
commentInputAvailable = false, // TODO open activity?
) {
// Update to show tag selection.
Expand Down Expand Up @@ -196,9 +183,12 @@ class ActivityStartStopFromBroadcastInteractor @Inject constructor(
typeId = selectedTypeId,
comment = "",
tagIds = listOfNotNull(tagId.takeUnless { it == 0L }),
updateNotificationSwitch = from !is NotificationControlsManager.From.ActivitySwitch,
)
update(from, typesShift)
if (from !is NotificationControlsManager.From.ActivitySwitch) {
// Hide tag selection on current notification.
// Switch would be hidden on start timer.
update(from, typesShift)
}
}

suspend fun onRequestUpdate(
Expand Down

0 comments on commit 66397db

Please sign in to comment.