Skip to content

Commit

Permalink
fix partial data restore for existing data
Browse files Browse the repository at this point in the history
  • Loading branch information
Razeeman committed Jan 18, 2025
1 parent b641898 commit c2e3623
Show file tree
Hide file tree
Showing 46 changed files with 115 additions and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class ChangeRunningRecordTest : BaseUiTest() {
range = RecordTypeGoal.Range.Session,
type = RecordTypeGoal.Type.Duration(firstGoalTime),
subtype = RecordTypeGoal.Subtype.Goal,
daysOfWeek = emptyList(),
daysOfWeek = emptySet(),
),
),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import com.example.util.simpletimetracker.GoalsTestUtils.checkRunningGoal
import com.example.util.simpletimetracker.GoalsTestUtils.checkStatisticsGoal
import com.example.util.simpletimetracker.GoalsTestUtils.checkTypeMark
import com.example.util.simpletimetracker.core.R
import com.example.util.simpletimetracker.data_local.base.removeIf
import com.example.util.simpletimetracker.domain.extension.removeIf
import com.example.util.simpletimetracker.utils.BaseUiTest
import com.example.util.simpletimetracker.utils.NavUtils
import com.example.util.simpletimetracker.utils.checkViewDoesNotExist
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ object GoalsTestUtils {
range = range,
type = RecordTypeGoal.Type.Duration(duration),
subtype = RecordTypeGoal.Subtype.Goal,
daysOfWeek = DayOfWeek.entries,
daysOfWeek = DayOfWeek.entries.toSet(),
)
}

Expand All @@ -240,7 +240,7 @@ object GoalsTestUtils {
range = range,
type = RecordTypeGoal.Type.Count(count),
subtype = RecordTypeGoal.Subtype.Goal,
daysOfWeek = DayOfWeek.entries,
daysOfWeek = DayOfWeek.entries.toSet(),
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class StartRecordTest : BaseUiTest() {
range = RecordTypeGoal.Range.Session,
type = RecordTypeGoal.Type.Duration(firstGoalTime),
subtype = RecordTypeGoal.Subtype.Goal,
daysOfWeek = emptyList(),
daysOfWeek = emptySet(),
),
),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ class RecordFilterInteractor @Inject constructor(
val filteredTaggedIds: List<Long> = filteredTagItems.getTaggedIds()
val filteredUntagged: Boolean = filteredTagItems.hasUntaggedItem()
val manuallyFilteredIds: Map<Long, Boolean> = filters.getManuallyFilteredRecordIds()
val daysOfWeek: List<DayOfWeek> = filters.getDaysOfWeek()
val daysOfWeek: Set<DayOfWeek> = filters.getDaysOfWeek()
val timeOfDay: Range? = filters.getTimeOfDay()
val durations: List<Range> = filters.getDuration()?.let(::listOf).orEmpty()
val duplicationItems: List<RecordsFilter.DuplicationsItem> = filters.getDuplicationItems()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ class DayOfWeekViewDataMapper @Inject constructor(
) {

fun mapViewData(
selectedDaysOfWeek: List<DayOfWeek>,
selectedDaysOfWeek: Set<DayOfWeek>,
isDarkTheme: Boolean,
width: DayOfWeekViewData.Width,
paddingHorizontalDp: Int,
): List<ViewHolderType> {
return DayOfWeek.values().map {
return DayOfWeek.entries.map {
val selected = it in selectedDaysOfWeek
DayOfWeekViewData(
dayOfWeek = it,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ class TestUtils @Inject constructor(
availableCategories.firstOrNull { it.name == name }?.id
}
}
}
}.toSet()

val data = ActivityFilter(
selectedIds = selectedIds,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ class ActivityFilterDataLocalMapper @Inject constructor() {
fun map(dbo: ActivityFilterDBO): ActivityFilter {
return ActivityFilter(
id = dbo.id,
selectedIds = dbo.selectedIds.split(',').mapNotNull(String::toLongOrNull),
selectedIds = dbo.selectedIds
.split(',')
.mapNotNull(String::toLongOrNull)
.toSet(),
type = when (dbo.type) {
0L -> ActivityFilter.Type.Activity
1L -> ActivityFilter.Type.Category
Expand All @@ -27,7 +30,8 @@ class ActivityFilterDataLocalMapper @Inject constructor() {
fun map(domain: ActivityFilter): ActivityFilterDBO {
return ActivityFilterDBO(
id = domain.id,
selectedIds = domain.selectedIds.joinToString(separator = ","),
selectedIds = domain.selectedIds
.joinToString(separator = ","),
type = when (domain.type) {
is ActivityFilter.Type.Activity -> 0L
is ActivityFilter.Type.Category -> 1L
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.example.util.simpletimetracker.data_local.activityFilter

import com.example.util.simpletimetracker.data_local.base.withLockedCache
import com.example.util.simpletimetracker.data_local.base.removeIf
import com.example.util.simpletimetracker.domain.activityFilter.model.ActivityFilter
import com.example.util.simpletimetracker.domain.activityFilter.repo.ActivityFilterRepo
import com.example.util.simpletimetracker.domain.extension.removeIf
import kotlinx.coroutines.sync.Mutex
import javax.inject.Inject
import javax.inject.Singleton
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,19 @@ class ActivitySuggestionDataLocalMapper @Inject constructor() {
return ActivitySuggestion(
id = dbo.id,
forTypeId = dbo.forTypeId,
suggestionIds = dbo.suggestionIds.split(',').mapNotNull(String::toLongOrNull),
suggestionIds = dbo.suggestionIds
.split(',')
.mapNotNull(String::toLongOrNull)
.toSet(),
)
}

fun map(domain: ActivitySuggestion): ActivitySuggestionDBO {
return ActivitySuggestionDBO(
id = domain.id,
forTypeId = domain.forTypeId,
suggestionIds = domain.suggestionIds.joinToString(separator = ","),
suggestionIds = domain.suggestionIds
.joinToString(separator = ","),
)
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.example.util.simpletimetracker.data_local.activitySuggestion

import com.example.util.simpletimetracker.data_local.base.removeIf
import com.example.util.simpletimetracker.data_local.base.withLockedCache
import com.example.util.simpletimetracker.domain.activitySuggestion.model.ActivitySuggestion
import com.example.util.simpletimetracker.domain.activitySuggestion.repo.ActivitySuggestionRepo
import com.example.util.simpletimetracker.domain.extension.removeIf
import kotlinx.coroutines.sync.Mutex
import javax.inject.Inject
import javax.inject.Singleton
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ class BackupPartialRepoImpl @Inject constructor(
params.data.activityFilters.values.getNotExistingValues().forEach { activityFilter ->
val newTypeIds = activityFilter.selectedIds
.mapNotNull { originalTypeIdToAddedId[it] }
.toSet()
activityFilter.copy(
id = 0,
selectedIds = newTypeIds,
Expand Down Expand Up @@ -210,7 +211,7 @@ class BackupPartialRepoImpl @Inject constructor(
originalTypeIdToAddedId[it]
}.takeIf {
it.isNotEmpty()
} ?: return@forEach
}?.toSet() ?: return@forEach
// Remove already existing suggestions for this typeId
// to avoid duplications.
activitySuggestionRepo.getByTypeId(newForTypeId)
Expand Down Expand Up @@ -384,7 +385,7 @@ class BackupPartialRepoImpl @Inject constructor(
} else {
it
}
}
}.toSet()
item.copy(
selectedIds = newTypeIds,
)
Expand Down Expand Up @@ -441,7 +442,7 @@ class BackupPartialRepoImpl @Inject constructor(
?: return@mapNotNull null
val newSuggestionIds = item.suggestionIds.mapNotNull {
originalTypeIdToExistingId[it]
}
}.toSet()
item.copy(
forTypeId = newForTypeId,
suggestionIds = newSuggestionIds,
Expand Down Expand Up @@ -479,6 +480,18 @@ class BackupPartialRepoImpl @Inject constructor(
)
}

@Suppress("UNCHECKED_CAST", "IMPLICIT_CAST_TO_ANY")
private fun <T> getCleanItem(
value: T,
): T {
return when (value) {
is RecordType -> value.copy(hidden = false)
is RecordTag -> value.copy(archived = false)
is ComplexRule -> value.copy(disabled = false)
else -> value
} as T
}

@Suppress("UNCHECKED_CAST")
private fun <T> getIdData(
value: T?,
Expand Down Expand Up @@ -508,13 +521,18 @@ class BackupPartialRepoImpl @Inject constructor(
currentData: List<T>,
): ReadData<T> {
if (dataFromFile.isEmpty()) return ReadData(emptyList(), emptyMap())

val idData = getIdData(dataFromFile.firstOrNull())
val replaceId: T.(Long) -> T = { id -> idData.idSetter(this, id) }
val id: T.() -> Long = { idData.idGetter(this) }
val currentDataClean: Map<T, Long> = currentData.associate { it.replaceId(0) to it.id() }
val clean: T.() -> T = { getCleanItem(this) }

val currentDataClean: Map<T, Long> = currentData.associate {
it.replaceId(0).clean() to it.id()
}
val originalIdsToExistingId = mutableMapOf<Long, Long>()
val list = dataFromFile.map { item ->
val cleanItem = item.replaceId(0)
val cleanItem = item.replaceId(0).clean()
val existingId = currentDataClean[cleanItem]
val itemId = item.id()
if (itemId != 0L) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,7 @@ class BackupRepoImpl @Inject constructor(
is ComplexRule.Action.AssignTag -> 2L
}.toString()
val daysOfWeekString = daysOfWeekDataLocalMapper
.mapDaysOfWeek(complexRule.conditionDaysOfWeek.toList())
.mapDaysOfWeek(complexRule.conditionDaysOfWeek)

return String.format(
"$ROW_COMPLEX_RULE\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n",
Expand Down Expand Up @@ -577,7 +577,7 @@ class BackupRepoImpl @Inject constructor(
val weeklyGoalTime = parts.getOrNull(9)?.toLongOrNull().orZero()
val monthlyGoalTime = parts.getOrNull(10)?.toLongOrNull().orZero()
// Didn't exist when goal time was in type db, no need to migrate.
val daysOfWeek = DayOfWeek.entries
val daysOfWeek = DayOfWeek.entries.toSet()
val subtype = RecordTypeGoal.Subtype.Goal

val goalTimes = mutableListOf<RecordTypeGoal>().apply {
Expand Down Expand Up @@ -719,7 +719,7 @@ class BackupRepoImpl @Inject constructor(
return ActivityFilter(
id = parts.getOrNull(1)?.toLongOrNull().orZero(),
selectedIds = parts.getOrNull(2)?.split(",")
?.mapNotNull { it.toLongOrNull() }.orEmpty(),
?.mapNotNull { it.toLongOrNull() }.orEmpty().toSet(),
type = parts.getOrNull(3)?.toLongOrNull()?.let {
when (it) {
0L -> ActivityFilter.Type.Activity
Expand Down Expand Up @@ -825,7 +825,7 @@ class BackupRepoImpl @Inject constructor(
id = parts.getOrNull(1)?.toLongOrNull().orZero(),
forTypeId = parts.getOrNull(2)?.toLongOrNull().orZero(),
suggestionIds = parts.getOrNull(3)?.split(",")
?.mapNotNull { it.toLongOrNull() }.orEmpty(),
?.mapNotNull { it.toLongOrNull() }.orEmpty().toSet(),
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,17 +108,3 @@ internal inline fun logDataAccess(logMessage: String) {
internal inline fun logPrefsDataAccess(logMessage: String) {
logDataAccessInternal("$LOG_MESSAGE_PREFS_PREFIX $logMessage")
}

/**
* Produces a new list from original list by removing elements satisfying filter block.
*/
inline fun <T> List<T>.removeIf(crossinline filter: (T) -> Boolean): List<T> {
return this.toMutableList().apply { removeAll { filter(it) } }
}

/**
* Produces a new list from original list by replacing elements satisfying filter block with new element.
*/
inline fun <T> List<T>.replaceWith(new: T, crossinline filter: (T) -> Boolean): List<T> {
return this.removeIf(filter).toMutableList().apply { add(new) }
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.example.util.simpletimetracker.data_local.category

import com.example.util.simpletimetracker.data_local.base.withLockedCache
import com.example.util.simpletimetracker.data_local.base.removeIf
import com.example.util.simpletimetracker.domain.category.model.Category
import com.example.util.simpletimetracker.domain.category.repo.CategoryRepo
import com.example.util.simpletimetracker.domain.extension.removeIf
import kotlinx.coroutines.sync.Mutex
import javax.inject.Inject
import javax.inject.Singleton
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.example.util.simpletimetracker.data_local.category

import com.example.util.simpletimetracker.data_local.base.removeIf
import com.example.util.simpletimetracker.data_local.base.withLockedCache
import com.example.util.simpletimetracker.domain.category.model.RecordTypeCategory
import com.example.util.simpletimetracker.domain.category.repo.RecordTypeCategoryRepo
import com.example.util.simpletimetracker.domain.extension.removeIf
import kotlinx.coroutines.sync.Mutex
import javax.inject.Inject
import javax.inject.Singleton
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class ComplexRuleDataLocalMapper @Inject constructor(
conditionStartingTypeIds = mapIds(domain.conditionStartingTypeIds),
conditionCurrentTypeIds = mapIds(domain.conditionCurrentTypeIds),
conditionDaysOfWeek = daysOfWeekDataLocalMapper
.mapDaysOfWeek(domain.conditionDaysOfWeek.toList()),
.mapDaysOfWeek(domain.conditionDaysOfWeek),
)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.example.util.simpletimetracker.data_local.complexRule

import com.example.util.simpletimetracker.data_local.base.removeIf
import com.example.util.simpletimetracker.data_local.base.withLockedCache
import com.example.util.simpletimetracker.domain.complexRule.model.ComplexRule
import com.example.util.simpletimetracker.domain.complexRule.repo.ComplexRuleRepo
import com.example.util.simpletimetracker.domain.extension.removeIf
import kotlinx.coroutines.sync.Mutex
import javax.inject.Inject
import javax.inject.Singleton
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import javax.inject.Inject

class DaysOfWeekDataLocalMapper @Inject constructor() {

fun mapDaysOfWeek(dbo: String): List<DayOfWeek> {
fun mapDaysOfWeek(dbo: String): Set<DayOfWeek> {
return daysOfWeek.mapIndexedNotNull { index, dayOfWeek ->
when (dbo.getOrNull(index)) {
// Selected days are marked with 1, days that are not selected - with 0,
Expand All @@ -15,10 +15,10 @@ class DaysOfWeekDataLocalMapper @Inject constructor() {
'0' -> null
else -> null
}
}
}.toSet()
}

fun mapDaysOfWeek(domain: List<DayOfWeek>): String {
fun mapDaysOfWeek(domain: Set<DayOfWeek>): String {
return daysOfWeek.map { dayOfWeek ->
if (dayOfWeek in domain) '1' else '0'
}.joinToString(separator = "")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.example.util.simpletimetracker.data_local.favourite

import com.example.util.simpletimetracker.data_local.base.removeIf
import com.example.util.simpletimetracker.data_local.base.withLockedCache
import com.example.util.simpletimetracker.domain.extension.removeIf
import com.example.util.simpletimetracker.domain.favourite.model.FavouriteIcon
import com.example.util.simpletimetracker.domain.favourite.repo.FavouriteIconRepo
import kotlinx.coroutines.sync.Mutex
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.example.util.simpletimetracker.data_local.record

import com.example.util.simpletimetracker.data_local.base.logDataAccess
import com.example.util.simpletimetracker.data_local.base.removeIf
import com.example.util.simpletimetracker.data_local.base.replaceWith
import com.example.util.simpletimetracker.data_local.base.withLockedCache
import com.example.util.simpletimetracker.domain.extension.dropMillis
import com.example.util.simpletimetracker.domain.extension.removeIf
import com.example.util.simpletimetracker.domain.extension.replaceWith
import com.example.util.simpletimetracker.domain.record.model.RunningRecord
import com.example.util.simpletimetracker.domain.record.repo.RunningRecordRepo
import kotlinx.coroutines.sync.Mutex
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.example.util.simpletimetracker.data_local.recordTag

import com.example.util.simpletimetracker.data_local.base.withLockedCache
import com.example.util.simpletimetracker.data_local.base.removeIf
import com.example.util.simpletimetracker.domain.extension.removeIf
import com.example.util.simpletimetracker.domain.recordTag.model.RecordTag
import com.example.util.simpletimetracker.domain.recordTag.repo.RecordTagRepo
import kotlinx.coroutines.sync.Mutex
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.example.util.simpletimetracker.data_local.recordTag

import com.example.util.simpletimetracker.data_local.base.removeIf
import com.example.util.simpletimetracker.data_local.base.withLockedCache
import com.example.util.simpletimetracker.domain.extension.removeIf
import com.example.util.simpletimetracker.domain.recordTag.model.RecordTypeToDefaultTag
import com.example.util.simpletimetracker.domain.recordTag.repo.RecordTypeToDefaultTagRepo
import kotlinx.coroutines.sync.Mutex
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.example.util.simpletimetracker.data_local.recordTag

import com.example.util.simpletimetracker.data_local.base.removeIf
import com.example.util.simpletimetracker.data_local.base.withLockedCache
import com.example.util.simpletimetracker.domain.extension.removeIf
import com.example.util.simpletimetracker.domain.recordTag.model.RecordTypeToTag
import com.example.util.simpletimetracker.domain.recordTag.repo.RecordTypeToTagRepo
import kotlinx.coroutines.sync.Mutex
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.example.util.simpletimetracker.data_local.recordType

import com.example.util.simpletimetracker.data_local.base.logDataAccess
import com.example.util.simpletimetracker.data_local.base.removeIf
import com.example.util.simpletimetracker.data_local.base.replaceWith
import com.example.util.simpletimetracker.data_local.base.withLockedCache
import com.example.util.simpletimetracker.domain.extension.removeIf
import com.example.util.simpletimetracker.domain.extension.replaceWith
import com.example.util.simpletimetracker.domain.recordType.model.RecordTypeGoal
import com.example.util.simpletimetracker.domain.recordType.model.RecordTypeGoal.IdData
import com.example.util.simpletimetracker.domain.recordType.repo.RecordTypeGoalRepo
Expand Down
Loading

0 comments on commit c2e3623

Please sign in to comment.