Skip to content

Commit

Permalink
fix some external views updates
Browse files Browse the repository at this point in the history
  • Loading branch information
Razeeman committed Oct 19, 2024
1 parent f424a40 commit 2b4e725
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import javax.inject.Inject

class CsvExportInteractor @Inject constructor(
private val csvRepo: CsvRepo,
private val runningRecordInteractor: RunningRecordInteractor,
private val externalViewsInteractor: UpdateExternalViewsInteractor,
) {

Expand All @@ -17,10 +16,7 @@ class CsvExportInteractor @Inject constructor(

suspend fun importCsvFile(uriString: String): ResultCode {
val resultCode = csvRepo.importCsvFile(uriString)

val runningRecords = runningRecordInteractor.getAll()
externalViewsInteractor.onCsvImport(runningRecords.map { it.id })

externalViewsInteractor.onCsvImport()
return resultCode
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,17 @@ class RemoveRecordTagMediator @Inject constructor(
private val externalViewsInteractor: UpdateExternalViewsInteractor,
) {

suspend fun remove(tagId: Long) {
suspend fun remove(
tagId: Long,
fromArchive: Boolean,
) {
recordTagInteractor.remove(tagId)
doAfterRemove()
doAfterRemove(fromArchive)
}

private suspend fun doAfterRemove() {
externalViewsInteractor.onTagRemove()
private suspend fun doAfterRemove(
fromArchive: Boolean,
) {
externalViewsInteractor.onTagRemove(fromArchive)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,18 @@ class RemoveRecordTypeMediator @Inject constructor(
private val externalViewsInteractor: UpdateExternalViewsInteractor,
) {

suspend fun remove(typeId: Long) {
suspend fun remove(
typeId: Long,
fromArchive: Boolean,
) {
recordTypeInteractor.remove(typeId)
doAfterRemove(typeId)
doAfterRemove(typeId, fromArchive)
}

private suspend fun doAfterRemove(typeId: Long) {
externalViewsInteractor.onTypeRemove(typeId)
private suspend fun doAfterRemove(
typeId: Long,
fromArchive: Boolean,
) {
externalViewsInteractor.onTypeRemove(typeId, fromArchive)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import com.example.util.simpletimetracker.domain.model.RunningRecord
import com.example.util.simpletimetracker.domain.model.WidgetType
import javax.inject.Inject

// TODO check all after actions that need to be done after type delete,
// also tag, category, record, running record etc.
class UpdateExternalViewsInteractor @Inject constructor(
private val runningRecordInteractor: RunningRecordInteractor,
private val notificationTypeInteractor: NotificationTypeInteractor,
Expand All @@ -22,26 +20,25 @@ class UpdateExternalViewsInteractor @Inject constructor(
// Categories are affected.
suspend fun onTypeRemove(
typeId: Long,
fromArchive: Boolean,
) {
val runningRecordIds = runningRecordInteractor.getAll().map(RunningRecord::id)

runUpdates(
Update.GoalCancel(RecordTypeGoal.IdData.Type(typeId)),
Update.GoalReschedule(runningRecordIds + typeId),
Update.WidgetStatistics,
Update.Wear,
)
}

suspend fun onTypeRemoveWithoutArchive() {
runUpdates(
Update.NotificationTypes,
Update.WidgetSingleType(typeId),
Update.Wear.takeIf { !fromArchive },
Update.NotificationTypes.takeIf { !fromArchive },
Update.NotificationWithControls.takeIf { !fromArchive }
)
}

suspend fun onTypeArchive() {
runUpdates(
Update.NotificationTypes,
Update.NotificationWithControls,
Update.Wear,
)
}
Expand All @@ -61,6 +58,8 @@ class UpdateExternalViewsInteractor @Inject constructor(

suspend fun onDefaultTypesAdd() {
runUpdates(
Update.NotificationTypes,
Update.NotificationWithControls,
Update.Wear,
)
}
Expand Down Expand Up @@ -157,7 +156,6 @@ class UpdateExternalViewsInteractor @Inject constructor(
) {
runUpdates(
Update.NotificationType(originalTypeId),
Update.NotificationWithControls,
Update.GoalReschedule(listOf(originalTypeId)),
)
}
Expand All @@ -171,19 +169,16 @@ class UpdateExternalViewsInteractor @Inject constructor(
)
}

suspend fun onTagRemove() {
suspend fun onTagRemove(
fromArchive: Boolean,
) {
runUpdates(
Update.NotificationTypes.takeIf { !fromArchive },
Update.NotificationWithControls.takeIf { !fromArchive },
Update.Wear,
)
}

suspend fun onTagRemoveWithoutArchiving() {
runUpdates(
Update.NotificationTypes,
Update.NotificationWithControls,
)
}

suspend fun onTagAddOrChange() {
runUpdates(
Update.NotificationTypes,
Expand Down Expand Up @@ -349,11 +344,13 @@ class UpdateExternalViewsInteractor @Inject constructor(
)
}

suspend fun onCsvImport(
typeIds: List<Long>,
) {
suspend fun onCsvImport() {
val runningRecordIds = runningRecordInteractor.getAll().map { it.id }

runUpdates(
Update.GoalReschedule(typeIds),
Update.NotificationTypes,
Update.NotificationWithControls,
Update.GoalReschedule(runningRecordIds),
Update.WidgetStatistics,
Update.WidgetSingleTypes,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,11 @@ class ArchiveViewModel @Inject constructor(
viewModelScope.launch {
val message = when (params) {
is ArchiveDialogParams.Activity -> {
removeRecordTypeMediator.remove(params.id)
removeRecordTypeMediator.remove(params.id, fromArchive = true)
resourceRepo.getString(R.string.archive_activity_deleted)
}
is ArchiveDialogParams.RecordTag -> {
removeRecordTagMediator.remove(params.id)
removeRecordTagMediator.remove(params.id, fromArchive = true)
resourceRepo.getString(R.string.archive_tag_deleted)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -310,8 +310,7 @@ class ChangeRecordTagViewModel @Inject constructor(
deleteButtonEnabled.set(false)
viewModelScope.launch {
if (recordTagId != 0L) {
removeRecordTagMediator.remove(recordTagId)
externalViewsInteractor.onTagRemoveWithoutArchiving()
removeRecordTagMediator.remove(recordTagId, fromArchive = false)
showMessage(R.string.archive_tag_deleted)
keyboardVisibility.set(false)
router.back()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -354,8 +354,7 @@ class ChangeRecordTypeViewModel @Inject constructor(
viewModelScope.launch {
if (recordTypeId != 0L) {
removeRunningRecordMediator.remove(recordTypeId)
removeRecordTypeMediator.remove(recordTypeId)
externalViewsInteractor.onTypeRemoveWithoutArchive()
removeRecordTypeMediator.remove(recordTypeId, fromArchive = false)
showMessage(R.string.archive_activity_deleted)
keyboardVisibility.set(false)
router.back()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,6 @@ class WidgetSingleProvider : AppWidgetProvider() {
)
} else {
val recordType = recordTypeInteractor.get(recordTypeId)
?.takeUnless { it.hidden }
val goal = filterGoalsByDayOfWeekInteractor
.execute(recordTypeGoalInteractor.getByType(recordTypeId))
.getDaily()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import android.view.LayoutInflater
import android.view.View
import android.widget.RemoteViews
import androidx.core.text.bold
import com.example.util.simpletimetracker.core.extension.allowDiskRead
import com.example.util.simpletimetracker.core.interactor.StatisticsChartViewDataInteractor
import com.example.util.simpletimetracker.core.interactor.StatisticsMediator
import com.example.util.simpletimetracker.core.mapper.TimeMapper
Expand All @@ -34,6 +35,7 @@ import com.example.util.simpletimetracker.feature_widget.R
import com.example.util.simpletimetracker.feature_widget.statistics.customView.WidgetStatisticsChartView
import com.example.util.simpletimetracker.navigation.Router
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
Expand Down Expand Up @@ -89,14 +91,15 @@ class WidgetStatisticsChartProvider : AppWidgetProvider() {
updateAppWidget(context, appWidgetManager, appWidgetId)
}

@OptIn(DelicateCoroutinesApi::class)
private fun updateAppWidget(
context: Context?,
appWidgetManager: AppWidgetManager?,
appWidgetId: Int,
) {
if (context == null || appWidgetManager == null) return

GlobalScope.launch(Dispatchers.Main) {
GlobalScope.launch(allowDiskRead { Dispatchers.Main }) {
val view = prepareView(context, appWidgetId)
val options = appWidgetManager.getAppWidgetOptions(appWidgetId)
measureView(context, options, view)
Expand Down

0 comments on commit 2b4e725

Please sign in to comment.