Skip to content

Commit

Permalink
add repeat button to wear
Browse files Browse the repository at this point in the history
  • Loading branch information
Razeeman committed Oct 13, 2024
1 parent be91e4f commit 194a4f2
Show file tree
Hide file tree
Showing 36 changed files with 370 additions and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class RecordRepeatInteractor @Inject constructor(
private val resourceRepo: ResourceRepo,
) {

suspend fun repeat(): Boolean {
suspend fun repeat(): ActionResult {
return execute { messageResId ->
SnackBarParams(
message = resourceRepo.getString(messageResId),
Expand All @@ -39,9 +39,13 @@ class RecordRepeatInteractor @Inject constructor(
}
}

suspend fun repeatWithoutMessage(): ActionResult {
return execute(messageShower = {})
}

private suspend fun execute(
messageShower: (messageResId: Int) -> Unit,
): Boolean {
): ActionResult {
val type = prefsInteractor.getRepeatButtonType()

val prevRecord = recordInteractor.getPrev(
Expand All @@ -54,18 +58,24 @@ class RecordRepeatInteractor @Inject constructor(
}
} ?: run {
messageShower(R.string.running_records_repeat_no_prev_record)
return false
return ActionResult.NoPreviousFound
}
if (runningRecordInteractor.get(prevRecord.typeId) != null) {
messageShower(R.string.running_records_repeat_already_tracking)
return false
return ActionResult.AlreadyTracking
}

addRunningRecordMediator.startTimer(
typeId = prevRecord.typeId,
tagIds = prevRecord.tagIds,
comment = prevRecord.comment,
)
return true
return ActionResult.Started
}

sealed interface ActionResult {
object Started : ActionResult
object NoPreviousFound : ActionResult
object AlreadyTracking : ActionResult
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import com.example.util.simpletimetracker.domain.interactor.NotificationTypeInte
import com.example.util.simpletimetracker.domain.interactor.PomodoroStopInteractor
import com.example.util.simpletimetracker.domain.interactor.PrefsInteractor
import com.example.util.simpletimetracker.domain.interactor.RecordsContainerUpdateInteractor
import com.example.util.simpletimetracker.domain.interactor.WearInteractor
import com.example.util.simpletimetracker.domain.interactor.WidgetInteractor
import com.example.util.simpletimetracker.domain.model.CardOrder
import com.example.util.simpletimetracker.domain.model.CardTagOrder
Expand All @@ -34,6 +35,7 @@ class SettingsDisplayViewModelDelegate @Inject constructor(
private val settingsMapper: SettingsMapper,
private val notificationTypeInteractor: NotificationTypeInteractor,
private val notificationActivitySwitchInteractor: NotificationActivitySwitchInteractor,
private val wearInteractor: WearInteractor,
private val widgetInteractor: WidgetInteractor,
private val settingsDisplayViewDataInteractor: SettingsDisplayViewDataInteractor,
private val pomodoroStopInteractor: PomodoroStopInteractor,
Expand Down Expand Up @@ -295,6 +297,7 @@ class SettingsDisplayViewModelDelegate @Inject constructor(
prefsInteractor.setEnableRepeatButton(newValue)
notificationTypeInteractor.updateNotifications()
notificationActivitySwitchInteractor.updateNotification()
wearInteractor.update()
parent?.updateContent()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/
package com.example.util.simpletimetracker.feature_wear

import com.example.util.simpletimetracker.core.interactor.RecordRepeatInteractor
import com.example.util.simpletimetracker.core.mapper.RecordTagViewDataMapper
import com.example.util.simpletimetracker.domain.mapper.AppColorMapper
import com.example.util.simpletimetracker.domain.model.AppColor
Expand All @@ -13,6 +14,7 @@ import com.example.util.simpletimetracker.domain.model.RecordType
import com.example.util.simpletimetracker.domain.model.RunningRecord
import com.example.util.simpletimetracker.wear_api.WearActivityDTO
import com.example.util.simpletimetracker.wear_api.WearCurrentActivityDTO
import com.example.util.simpletimetracker.wear_api.WearRecordRepeatResponse
import com.example.util.simpletimetracker.wear_api.WearSettingsDTO
import com.example.util.simpletimetracker.wear_api.WearTagDTO
import javax.inject.Inject
Expand Down Expand Up @@ -61,10 +63,27 @@ class WearDataLocalMapper @Inject constructor(
fun map(
allowMultitasking: Boolean,
recordTagSelectionCloseAfterOne: Boolean,
enableRepeatButton: Boolean,
): WearSettingsDTO {
return WearSettingsDTO(
allowMultitasking = allowMultitasking,
recordTagSelectionCloseAfterOne = recordTagSelectionCloseAfterOne,
enableRepeatButton = enableRepeatButton,
)
}

fun map(
result: RecordRepeatInteractor.ActionResult,
): WearRecordRepeatResponse {
return WearRecordRepeatResponse(
result = when (result) {
is RecordRepeatInteractor.ActionResult.Started ->
WearRecordRepeatResponse.ActionResult.STARTED
is RecordRepeatInteractor.ActionResult.NoPreviousFound ->
WearRecordRepeatResponse.ActionResult.NO_PREVIOUS_FOUND
is RecordRepeatInteractor.ActionResult.AlreadyTracking ->
WearRecordRepeatResponse.ActionResult.ALREADY_TRACKING
},
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/
package com.example.util.simpletimetracker.feature_wear

import com.example.util.simpletimetracker.core.interactor.RecordRepeatInteractor
import com.example.util.simpletimetracker.domain.interactor.AddRunningRecordMediator
import com.example.util.simpletimetracker.domain.interactor.GetSelectableTagsInteractor
import com.example.util.simpletimetracker.domain.interactor.PrefsInteractor
Expand All @@ -20,6 +21,7 @@ import com.example.util.simpletimetracker.navigation.Router
import com.example.util.simpletimetracker.wear_api.WearActivityDTO
import com.example.util.simpletimetracker.wear_api.WearCommunicationAPI
import com.example.util.simpletimetracker.wear_api.WearCurrentActivityDTO
import com.example.util.simpletimetracker.wear_api.WearRecordRepeatResponse
import com.example.util.simpletimetracker.wear_api.WearSettingsDTO
import com.example.util.simpletimetracker.wear_api.WearShouldShowTagSelectionRequest
import com.example.util.simpletimetracker.wear_api.WearShouldShowTagSelectionResponse
Expand All @@ -38,6 +40,7 @@ class WearDataRepo @Inject constructor(
private val shouldShowTagSelectionInteractor: ShouldShowTagSelectionInteractor,
private val removeRunningRecordMediator: Lazy<RemoveRunningRecordMediator>,
private val addRunningRecordMediator: Lazy<AddRunningRecordMediator>,
private val recordRepeatInteractor: Lazy<RecordRepeatInteractor>,
private val router: Router,
private val widgetInteractor: WidgetInteractor,
private val settingsDataUpdateInteractor: SettingsDataUpdateInteractor,
Expand Down Expand Up @@ -77,6 +80,11 @@ class WearDataRepo @Inject constructor(
removeRunningRecordMediator.get().removeWithRecordAdd(current)
}

override suspend fun repeatActivity(): WearRecordRepeatResponse {
return recordRepeatInteractor.get().repeatWithoutMessage()
.let(wearDataLocalMapper::map)
}

override suspend fun queryTagsForActivity(activityId: Long): List<WearTagDTO> {
val types = recordTypeInteractor.getAll().associateBy { it.id }
return getSelectableTagsInteractor.execute(activityId)
Expand All @@ -101,6 +109,7 @@ class WearDataRepo @Inject constructor(
return wearDataLocalMapper.map(
allowMultitasking = prefsInteractor.getAllowMultitasking(),
recordTagSelectionCloseAfterOne = prefsInteractor.getRecordTagSelectionCloseAfterOne(),
enableRepeatButton = prefsInteractor.getEnableRepeatButton(),
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class WearRPCServer @Inject constructor(
WearRequests.QUERY_CURRENT_ACTIVITIES -> onQueryCurrentActivities()
WearRequests.START_ACTIVITY -> onStartActivity(request)
WearRequests.STOP_ACTIVITY -> onStopActivity(request)
WearRequests.REPEAT_ACTIVITY -> onRepeatActivity()
WearRequests.QUERY_TAGS_FOR_ACTIVITY -> onQueryTagsForActivity(request)
WearRequests.QUERY_SHOULD_SHOW_TAG_SELECTION -> onQueryShouldShowTagSelection(request)
WearRequests.QUERY_SETTINGS -> onQuerySettings()
Expand Down Expand Up @@ -96,6 +97,10 @@ class WearRPCServer @Inject constructor(
return ByteArray(0)
}

private suspend fun onRepeatActivity(): ByteArray {
return mapToBytes(repo.repeatActivity())
}

private suspend fun onQueryActivities(): ByteArray {
return mapToBytes(repo.queryActivities())
}
Expand Down
1 change: 1 addition & 0 deletions features/feature_wear/src/main/res/values/wear.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<item>/stt/QUERY_CURRENT_ACTIVITIES</item>
<item>/stt/START_ACTIVITY</item>
<item>/stt/STOP_ACTIVITY</item>
<item>/stt/REPEAT_ACTIVITY</item>
<item>/stt/QUERY_TAGS_FOR_ACTIVITY</item>
<item>/stt/QUERY_SHOULD_SHOW_TAG_SELECTION</item>
<item>/stt/QUERY_SETTINGS</item>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ class WidgetUniversalViewModel @Inject constructor(

when (item.type) {
is RunningRecordTypeSpecialViewData.Type.Repeat -> {
started = recordRepeatInteractor.repeat()
val result = recordRepeatInteractor.repeat()
started = result is RecordRepeatInteractor.ActionResult.Started
}
else -> return@launch
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ package com.example.util.simpletimetracker.data

import com.example.util.simpletimetracker.domain.model.WearActivity
import com.example.util.simpletimetracker.domain.model.WearCurrentActivity
import com.example.util.simpletimetracker.domain.model.WearRecordRepeatResult
import com.example.util.simpletimetracker.domain.model.WearSettings
import com.example.util.simpletimetracker.domain.model.WearTag
import com.example.util.simpletimetracker.wear_api.WearActivityDTO
import com.example.util.simpletimetracker.wear_api.WearCurrentActivityDTO
import com.example.util.simpletimetracker.wear_api.WearRecordRepeatResponse
import com.example.util.simpletimetracker.wear_api.WearSettingsDTO
import com.example.util.simpletimetracker.wear_api.WearTagDTO
import javax.inject.Inject
Expand Down Expand Up @@ -46,13 +48,28 @@ class WearDataLocalMapper @Inject constructor() {
return WearSettings(
allowMultitasking = dto.allowMultitasking,
recordTagSelectionCloseAfterOne = dto.recordTagSelectionCloseAfterOne,
enableRepeatButton = dto.enableRepeatButton,
)
}

fun map(domain: WearSettings): WearSettingsDTO {
return WearSettingsDTO(
allowMultitasking = domain.allowMultitasking,
recordTagSelectionCloseAfterOne = domain.recordTagSelectionCloseAfterOne,
enableRepeatButton = domain.enableRepeatButton,
)
}

fun map(dto: WearRecordRepeatResponse): WearRecordRepeatResult {
return WearRecordRepeatResult(
result = when (dto.result) {
WearRecordRepeatResponse.ActionResult.STARTED ->
WearRecordRepeatResult.ActionResult.Started
WearRecordRepeatResponse.ActionResult.NO_PREVIOUS_FOUND ->
WearRecordRepeatResult.ActionResult.NoPreviousFound
WearRecordRepeatResponse.ActionResult.ALREADY_TRACKING ->
WearRecordRepeatResult.ActionResult.AlreadyTracking
},
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ package com.example.util.simpletimetracker.data
import com.example.util.simpletimetracker.complication.WearComplicationManager
import com.example.util.simpletimetracker.domain.model.WearActivity
import com.example.util.simpletimetracker.domain.model.WearCurrentActivity
import com.example.util.simpletimetracker.domain.model.WearRecordRepeatResult
import com.example.util.simpletimetracker.domain.model.WearSettings
import com.example.util.simpletimetracker.domain.model.WearTag
import com.example.util.simpletimetracker.notification.WearNotificationManager
import com.example.util.simpletimetracker.wear_api.WearActivityDTO
import com.example.util.simpletimetracker.wear_api.WearCurrentActivityDTO
import com.example.util.simpletimetracker.wear_api.WearSettingsDTO
import com.example.util.simpletimetracker.wear_api.WearShouldShowTagSelectionRequest
import com.example.util.simpletimetracker.wear_api.WearStartActivityRequest
import com.example.util.simpletimetracker.wear_api.WearStopActivityRequest
Expand Down Expand Up @@ -47,6 +49,7 @@ class WearDataRepo @Inject constructor(

private var activitiesCache: List<WearActivityDTO>? = null
private var currentActivitiesCache: List<WearCurrentActivityDTO>? = null
private var settingsCache: WearSettingsDTO? = null
private val mutex: Mutex = Mutex()

init {
Expand All @@ -55,6 +58,7 @@ class WearDataRepo @Inject constructor(
val deferred = mutableListOf<Deferred<Any>>()
deferred += async { loadActivities(forceReload = true) }
deferred += async { loadCurrentActivities(forceReload = true) }
deferred += async { loadSettings(forceReload = true) }
deferred.awaitAll()
wearComplicationManager.updateComplications()
wearNotificationManager.get().updateNotifications()
Expand Down Expand Up @@ -99,6 +103,13 @@ class WearDataRepo @Inject constructor(
}
}

suspend fun repeatActivity(): Result<WearRecordRepeatResult> = mutex.withLock {
return runCatching {
val response = wearRPCClient.repeatActivity()
wearDataLocalMapper.map(response)
}
}

suspend fun loadTagsForActivity(activityId: Long): Result<List<WearTag>> = mutex.withLock {
return runCatching {
val data = wearRPCClient.queryTagsForActivity(activityId)
Expand All @@ -113,10 +124,14 @@ class WearDataRepo @Inject constructor(
}
}

suspend fun loadSettings(): Result<WearSettings> = mutex.withLock {
suspend fun loadSettings(
forceReload: Boolean,
): Result<WearSettings> = mutex.withLock {
return runCatching {
val data = wearRPCClient.querySettings()
wearDataLocalMapper.map(data)
val data = settingsCache.takeUnless { forceReload }
?: wearRPCClient.querySettings()
.also { settingsCache = it }
data.let(wearDataLocalMapper::map)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package com.example.util.simpletimetracker.data
import com.example.util.simpletimetracker.wear_api.WearActivityDTO
import com.example.util.simpletimetracker.wear_api.WearCommunicationAPI
import com.example.util.simpletimetracker.wear_api.WearCurrentActivityDTO
import com.example.util.simpletimetracker.wear_api.WearRecordRepeatResponse
import com.example.util.simpletimetracker.wear_api.WearRequests
import com.example.util.simpletimetracker.wear_api.WearSettingsDTO
import com.example.util.simpletimetracker.wear_api.WearShouldShowTagSelectionRequest
Expand Down Expand Up @@ -52,6 +53,14 @@ class WearRPCClient @Inject constructor(
messenger.send(WearRequests.STOP_ACTIVITY, mapToBytes(request))
}

override suspend fun repeatActivity(): WearRecordRepeatResponse {
val response: WearRecordRepeatResponse? = messenger
.send(WearRequests.REPEAT_ACTIVITY)
?.let(::mapFromBytes)

return response ?: throw WearRPCException
}

override suspend fun queryTagsForActivity(activityId: Long): List<WearTagDTO> {
val response: List<WearTagDTO>? = messenger
.send(WearRequests.QUERY_TAGS_FOR_ACTIVITY, mapToBytes(activityId))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package com.example.util.simpletimetracker.domain.mediator

import com.example.util.simpletimetracker.data.WearDataRepo
import com.example.util.simpletimetracker.data.WearRPCException
import com.example.util.simpletimetracker.domain.model.WearRecordRepeatResult
import javax.inject.Inject

class StartActivityMediator @Inject constructor(
Expand Down Expand Up @@ -42,4 +43,8 @@ class StartActivityMediator @Inject constructor(
suspend fun stop(currentId: Long): Result<Unit> {
return wearDataRepo.stopActivity(currentId)
}

suspend fun repeat(): Result<WearRecordRepeatResult> {
return wearDataRepo.repeatActivity()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
package com.example.util.simpletimetracker.domain.model

data class WearRecordRepeatResult(
val result: ActionResult,
) {

sealed interface ActionResult {
object Started : ActionResult
object NoPreviousFound : ActionResult
object AlreadyTracking : ActionResult
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ package com.example.util.simpletimetracker.domain.model
data class WearSettings(
val allowMultitasking: Boolean,
val recordTagSelectionCloseAfterOne: Boolean,
val enableRepeatButton: Boolean,
)
Loading

0 comments on commit 194a4f2

Please sign in to comment.