Skip to content

Commit

Permalink
add ability to send multiple tags for automated tracking
Browse files Browse the repository at this point in the history
  • Loading branch information
Razeeman committed Sep 15, 2024
1 parent 8cdf428 commit 4b31afb
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 43 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 = 67
const val versionCode = 69
const val versionName = "1.45"
const val minSDK = 21
const val currentSDK = 34
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,12 @@ class NotificationReceiver : BroadcastReceiver() {
ACTION_START_ACTIVITY -> {
val name = intent.getStringExtra(EXTRA_ACTIVITY_NAME)
val comment = intent.getStringExtra(EXTRA_RECORD_COMMENT)
val tagName = intent.getStringExtra(EXTRA_RECORD_TAG_NAME)
val tagNames = intent.getStringExtra(EXTRA_RECORD_TAG_NAME)
?.slipTagNames().orEmpty()
typeController.onActionActivityStart(
name = name,
comment = comment,
tagName = tagName,
tagNames = tagNames,
)
}
ACTION_STOP_ACTIVITY -> {
Expand All @@ -151,24 +152,26 @@ class NotificationReceiver : BroadcastReceiver() {
}
ACTION_RESTART_ACTIVITY -> {
val comment = intent.getStringExtra(EXTRA_RECORD_COMMENT)
val tagName = intent.getStringExtra(EXTRA_RECORD_TAG_NAME)
val tagNames = intent.getStringExtra(EXTRA_RECORD_TAG_NAME)
?.slipTagNames().orEmpty()
typeController.onActionActivityRestart(
comment = comment,
tagName = tagName,
tagNames = tagNames,
)
}
ACTION_ADD_RECORD -> {
val name = intent.getStringExtra(EXTRA_ACTIVITY_NAME)
val timeStarted = intent.getStringExtra(EXTRA_TIME_STARTED)
val timeEnded = intent.getStringExtra(EXTRA_TIME_ENDED)
val comment = intent.getStringExtra(EXTRA_RECORD_COMMENT)
val tagName = intent.getStringExtra(EXTRA_RECORD_TAG_NAME)
val tagNames = intent.getStringExtra(EXTRA_RECORD_TAG_NAME)
?.slipTagNames().orEmpty()
typeController.onActionRecordAdd(
name = name,
timeStarted = timeStarted,
timeEnded = timeEnded,
comment = comment,
tagName = tagName,
tagNames = tagNames,
)
}
ACTION_NOTIFICATION_STOP -> {
Expand Down Expand Up @@ -233,6 +236,10 @@ class NotificationReceiver : BroadcastReceiver() {
pomodoroController.onBootCompleted()
}

private fun String.slipTagNames(): List<String> {
return split(',').map(String::trim)
}

companion object {
const val ACTION_INACTIVITY_REMINDER =
"com.razeeman.util.simpletimetracker.ACTION_INACTIVITY_REMINDER"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ class NotificationTypeBroadcastController @Inject constructor(
fun onActionActivityStart(
name: String?,
comment: String?,
tagName: String?,
tagNames: List<String>,
) {
name ?: return
GlobalScope.launch {
activityStartStopFromBroadcastInteractor.onActionActivityStart(
name = name, comment = comment, tagName = tagName,
name = name, comment = comment, tagNames = tagNames,
)
}
}
Expand Down Expand Up @@ -64,11 +64,11 @@ class NotificationTypeBroadcastController @Inject constructor(

fun onActionActivityRestart(
comment: String?,
tagName: String?,
tagNames: List<String>,
) {
GlobalScope.launch {
activityStartStopFromBroadcastInteractor.onActionActivityRestart(
comment = comment, tagName = tagName,
comment = comment, tagNames = tagNames,
)
}
}
Expand All @@ -78,7 +78,7 @@ class NotificationTypeBroadcastController @Inject constructor(
timeStarted: String?,
timeEnded: String?,
comment: String?,
tagName: String?,
tagNames: List<String>,
) {
name ?: return
timeStarted ?: return
Expand All @@ -89,7 +89,7 @@ class NotificationTypeBroadcastController @Inject constructor(
timeStarted = timeStarted,
timeEnded = timeEnded,
comment = comment,
tagName = tagName,
tagNames = tagNames,
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.example.util.simpletimetracker.feature_notification.recordType.inter
import com.example.util.simpletimetracker.core.interactor.CompleteTypesStateInteractor
import com.example.util.simpletimetracker.core.interactor.RecordRepeatInteractor
import com.example.util.simpletimetracker.domain.REPEAT_BUTTON_ITEM_ID
import com.example.util.simpletimetracker.domain.extension.orEmpty
import com.example.util.simpletimetracker.domain.extension.orZero
import com.example.util.simpletimetracker.domain.interactor.AddRecordMediator
import com.example.util.simpletimetracker.domain.interactor.AddRunningRecordMediator
Expand Down Expand Up @@ -36,17 +37,17 @@ class ActivityStartStopFromBroadcastInteractor @Inject constructor(
suspend fun onActionActivityStart(
name: String,
comment: String?,
tagName: String?,
tagNames: List<String>,
) {
val typeId = getTypeIdByName(name) ?: return
val runningRecord = runningRecordInteractor.get(typeId)
if (runningRecord != null) return // Already running.
val tagId = findTagIdByName(tagName, typeId)
val tagIds = findTagIdByName(tagNames, typeId)

addRunningRecordMediator.startTimer(
typeId = typeId,
comment = comment.orEmpty(),
tagIds = listOfNotNull(tagId),
tagIds = tagIds,
)
}

Expand Down Expand Up @@ -87,20 +88,20 @@ class ActivityStartStopFromBroadcastInteractor @Inject constructor(

suspend fun onActionActivityRestart(
comment: String?,
tagName: String?,
tagNames: List<String>,
) {
val previousRecord = recordInteractor.getPrev(
timeStarted = System.currentTimeMillis(),
).firstOrNull() ?: return
val typeId = previousRecord.typeId
val tagId = findTagIdByName(tagName, typeId)
val tagIds = findTagIdByName(tagNames, typeId)

addRunningRecordMediator.startTimer(
typeId = typeId,
comment = comment
?: previousRecord.comment,
tagIds = listOfNotNull(tagId)
.takeUnless { tagName == null }
tagIds = tagIds
.takeUnless { tagNames.isEmpty() }
?: previousRecord.tagIds,
)
}
Expand All @@ -110,20 +111,20 @@ class ActivityStartStopFromBroadcastInteractor @Inject constructor(
timeStarted: String,
timeEnded: String,
comment: String?,
tagName: String?,
tagNames: List<String>,
) {
val typeId = getTypeIdByName(name) ?: return
val newTimeStarted = parseTimestamp(timeStarted) ?: return
val newTimeEnded = parseTimestamp(timeEnded) ?: return
val tagId = findTagIdByName(tagName, typeId)
val tagIds = findTagIdByName(tagNames, typeId)

Record(
id = 0, // Zero creates new record.
typeId = typeId,
timeStarted = newTimeStarted,
timeEnded = newTimeEnded,
comment = comment.orEmpty(),
tagIds = listOfNotNull(tagId),
tagIds = tagIds,
).let {
addRecordMediator.add(it)
recordsUpdateInteractor.send()
Expand Down Expand Up @@ -191,12 +192,14 @@ class ActivityStartStopFromBroadcastInteractor @Inject constructor(
}

private suspend fun findTagIdByName(
name: String?,
names: List<String>,
typeId: Long,
): Long? {
name ?: return null
): List<Long> {
if (names.isEmpty()) return emptyList()
return getSelectableTagsInteractor.execute(typeId)
.firstOrNull { it.name == name && !it.archived }?.id
.filter { it.name in names && !it.archived }
.map { it.id }
.orEmpty()
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import com.example.util.simpletimetracker.domain.interactor.ActivityStartedStopp
import com.example.util.simpletimetracker.domain.interactor.PrefsInteractor
import com.example.util.simpletimetracker.domain.interactor.RecordTagInteractor
import com.example.util.simpletimetracker.domain.interactor.RecordTypeInteractor
import com.example.util.simpletimetracker.domain.model.RecordTag
import dagger.hilt.android.qualifiers.ApplicationContext
import javax.inject.Inject

Expand All @@ -28,14 +29,11 @@ class ActivityStartedStoppedBroadcastInteractorImpl @Inject constructor(
) {
if (!prefsInteractor.getAutomatedTrackingSendEvents()) return

val activityName = getActivityName(typeId) ?: return
val tagName = getTagName(tagIds.firstOrNull())

sendBroadcast(
actionString = EVENT_STARTED_ACTIVITY,
activityName = activityName,
activityName = getActivityName(typeId) ?: return,
comment = comment,
tagName = tagName,
tagNames = getTagNames(tagIds),
)
}

Expand All @@ -46,28 +44,26 @@ class ActivityStartedStoppedBroadcastInteractorImpl @Inject constructor(
) {
if (!prefsInteractor.getAutomatedTrackingSendEvents()) return

val activityName = getActivityName(typeId) ?: return
val tagName = getTagName(tagIds.firstOrNull())

sendBroadcast(
actionString = EVENT_STOPPED_ACTIVITY,
activityName = activityName,
activityName = getActivityName(typeId) ?: return,
comment = comment,
tagName = tagName,
tagNames = getTagNames(tagIds),
)
}

private fun sendBroadcast(
actionString: String,
activityName: String,
comment: String,
tagName: String,
tagNames: List<String>,
) {
val tagsString = tagNames.joinToString(separator = ",")
Intent().apply {
action = actionString
putExtra(EXTRA_ACTIVITY_NAME, activityName)
if (comment.isNotEmpty()) putExtra(EXTRA_RECORD_COMMENT, comment)
if (tagName.isNotEmpty()) putExtra(EXTRA_RECORD_TAG_NAME, tagName)
if (tagNames.isNotEmpty()) putExtra(EXTRA_RECORD_TAG_NAME, tagsString)
}.let(context::sendBroadcast)
}

Expand All @@ -77,9 +73,10 @@ class ActivityStartedStoppedBroadcastInteractorImpl @Inject constructor(
return recordTypeInteractor.get(typeId)?.name
}

private suspend fun getTagName(
tagId: Long?,
): String {
return recordTagInteractor.getAll().firstOrNull { it.id == tagId }?.name.orEmpty()
private suspend fun getTagNames(
tagIds: List<Long>,
): List<String> {
val tags = recordTagInteractor.getAll().associateBy(RecordTag::id)
return tagIds.mapNotNull { tagId -> tags[tagId]?.name }
}
}

0 comments on commit 4b31afb

Please sign in to comment.