···11+/*
22+ * This Source Code Form is subject to the terms of the Mozilla Public
33+ * License, v. 2.0. If a copy of the MPL was not distributed with this
44+ * file, You can obtain one at https://mozilla.org/MPL/2.0/.
55+ */
66+package com.example.util.simpletimetracker.wearrpc
77+88+class StartActivityMediator(
99+ private val api: SimpleTimeTrackerAPI,
1010+ private val onRequestStartActivity: suspend (activity: Activity) -> Unit,
1111+ private val onRequestTagSelection: suspend (activity: Activity) -> Unit,
1212+) {
1313+ suspend fun requestStart(activity: Activity) {
1414+ val settings = api.querySettings()
1515+ if (settings.showRecordTagSelection) {
1616+ requestTagSelectionIfNeeded(activity, settings)
1717+ } else {
1818+ onRequestStartActivity(activity)
1919+ }
2020+ }
2121+2222+ private suspend fun requestTagSelectionIfNeeded(activity: Activity, settings: Settings) {
2323+ val tags = api.queryTagsForActivity(activity.id)
2424+ val generalTags = tags.filter { it.isGeneral }
2525+ val nonGeneralTags = tags.filter { !it.isGeneral }
2626+ val tagSelectionNeeded =
2727+ nonGeneralTags.isNotEmpty() || generalTags.isNotEmpty() && settings.recordTagSelectionEvenForGeneralTags
2828+ if (tagSelectionNeeded) {
2929+ onRequestTagSelection(activity)
3030+ } else {
3131+ onRequestStartActivity(activity)
3232+ }
3333+ }
3434+}