Skip to content

Commit 5ba1ea9

Browse files
committed
add record shortcuts to backup
1 parent a81327a commit 5ba1ea9

File tree

13 files changed

+228
-13
lines changed

13 files changed

+228
-13
lines changed

core/src/main/java/com/example/util/simpletimetracker/core/mapper/RecordShortcutViewDataMapper.kt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class RecordShortcutViewDataMapper @Inject constructor(
1212
private val iconMapper: IconMapper,
1313
private val colorMapper: ColorMapper,
1414
private val recordTagFullNameMapper: RecordTagFullNameMapper,
15+
private val recordViewDataMapper: RecordViewDataMapper,
1516
) {
1617

1718
fun map(
@@ -45,11 +46,31 @@ class RecordShortcutViewDataMapper @Inject constructor(
4546
// TODO SHORT move to one line after "name - tag"?
4647
// TODO SHORT remove "Shortcut" label and add hint?
4748
// TODO SHORT width wrapContent?
49+
// TODO SHORT show filtered if already started?
50+
// TODO SHORT translate strings
51+
// TODO SHORT add message after adding
4852
)
4953

5054
return RecordShortcutViewData(
5155
id = shortcut.id,
5256
record = data,
5357
)
5458
}
59+
60+
fun mapFiltered(
61+
viewData: RecordShortcutViewData,
62+
isDarkTheme: Boolean,
63+
isFiltered: Boolean,
64+
): RecordShortcutViewData {
65+
return if (isFiltered) {
66+
val newRecordData = recordViewDataMapper.mapFiltered(
67+
viewData = viewData.record,
68+
isDarkTheme = isDarkTheme,
69+
isFiltered = true
70+
)
71+
viewData.copy(record = newRecordData)
72+
} else {
73+
viewData
74+
}
75+
}
5576
}

data_local/src/main/java/com/example/util/simpletimetracker/data_local/backup/BackupPartialRepoImpl.kt

Lines changed: 78 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ import com.example.util.simpletimetracker.domain.backup.repo.BackupPartialRepo
4242
import com.example.util.simpletimetracker.domain.backup.model.ResultCode
4343
import com.example.util.simpletimetracker.domain.category.model.Category
4444
import com.example.util.simpletimetracker.domain.record.model.RecordBase
45+
import com.example.util.simpletimetracker.domain.recordShortcut.model.RecordShortcut
46+
import com.example.util.simpletimetracker.domain.recordShortcut.repo.RecordShortcutRepo
47+
import com.example.util.simpletimetracker.domain.recordTag.model.RecordShortcutToRecordTag
48+
import com.example.util.simpletimetracker.domain.recordTag.repo.RecordShortcutToRecordTagRepo
4549
import kotlinx.coroutines.Dispatchers
4650
import kotlinx.coroutines.withContext
4751
import javax.inject.Inject
@@ -55,6 +59,7 @@ class BackupPartialRepoImpl @Inject constructor(
5559
private val recordTypeToTagRepo: RecordTypeToTagRepo,
5660
private val recordTypeToDefaultTagRepo: RecordTypeToDefaultTagRepo,
5761
private val recordToRecordTagRepo: RecordToRecordTagRepo,
62+
private val recordShortcutToRecordTagRepo: RecordShortcutToRecordTagRepo,
5863
private val recordTagRepo: RecordTagRepo,
5964
private val activityFilterRepo: ActivityFilterRepo,
6065
private val activitySuggestionRepo: ActivitySuggestionRepo,
@@ -63,6 +68,7 @@ class BackupPartialRepoImpl @Inject constructor(
6368
private val favouriteIconRepo: FavouriteIconRepo,
6469
private val recordTypeGoalRepo: RecordTypeGoalRepo,
6570
private val complexRuleRepo: ComplexRuleRepo,
71+
private val recordShortcutRepo: RecordShortcutRepo,
6672
private val resourceRepo: ResourceRepo,
6773
) : BackupPartialRepo {
6874

@@ -79,6 +85,8 @@ class BackupPartialRepoImpl @Inject constructor(
7985
.values.getExistingValues().associate { it.id to it.id }.toMutableMap()
8086
val originalRecordIdToAddedId: MutableMap<Long, Long> = params.data.records
8187
.values.getExistingValues().associate { it.id to it.id }.toMutableMap()
88+
val originalRecordShortcutIdToAddedId: MutableMap<Long, Long> = params.data.recordShortcuts
89+
.values.getExistingValues().associate { it.id to it.id }.toMutableMap()
8290

8391
params.data.types.values.getNotExistingValues().forEach { type ->
8492
val originalId = type.id
@@ -97,7 +105,16 @@ class BackupPartialRepoImpl @Inject constructor(
97105
).let { recordRepo.add(it) }
98106
originalRecordIdToAddedId[originalId] = addedId
99107
}
100-
// TODO SHORT
108+
params.data.recordShortcuts.values.getNotExistingValues().forEach { recordShortcut ->
109+
val originalId = recordShortcut.id
110+
val newTypeId = originalTypeIdToAddedId[recordShortcut.typeId]
111+
?: return@forEach
112+
val addedId = recordShortcut.copy(
113+
id = 0,
114+
typeId = newTypeId,
115+
).let { recordShortcutRepo.add(it) }
116+
originalRecordShortcutIdToAddedId[originalId] = addedId
117+
}
101118
params.data.categories.values.getNotExistingValues().forEach { category ->
102119
val originalId = category.id
103120
val addedId = category.copy(
@@ -134,7 +151,16 @@ class BackupPartialRepoImpl @Inject constructor(
134151
recordTagId = newTagId,
135152
).let { recordToRecordTagRepo.add(it) }
136153
}
137-
// TODO SHORT
154+
params.data.recordShortcutToTag.getNotExistingValues().forEach { recordShortcutToTag ->
155+
val newRecordShortcutId = originalRecordShortcutIdToAddedId[recordShortcutToTag.shortcutId]
156+
?: return@forEach
157+
val newTagId = originalTagIdToAddedId[recordShortcutToTag.recordTagId]
158+
?: return@forEach
159+
recordShortcutToTag.copy(
160+
shortcutId = newRecordShortcutId,
161+
recordTagId = newTagId,
162+
).let { recordShortcutToRecordTagRepo.add(it) }
163+
}
138164
params.data.typeToTag.getNotExistingValues().forEach { typeToTag ->
139165
val newTypeId = originalTypeIdToAddedId[typeToTag.recordTypeId]
140166
?: return@forEach
@@ -243,8 +269,11 @@ class BackupPartialRepoImpl @Inject constructor(
243269
val types: MutableList<RecordType> = mutableListOf()
244270
val typesCurrent: List<RecordType> = recordTypeRepo.getAll()
245271
val records: MutableList<Record> = mutableListOf()
246-
val recordsCurrent: List<Record> = recordRepo.getAll().map { it.copy(tags = emptyList()) }
247-
// TODO SHORT
272+
val recordsCurrent: List<Record> = recordRepo.getAll()
273+
.map { it.copy(tags = emptyList()) }
274+
val recordShortcuts: MutableList<RecordShortcut> = mutableListOf()
275+
val recordShortcutsCurrent: List<RecordShortcut> = recordShortcutRepo.getAll()
276+
.map { it.copy(tags = emptyList()) }
248277
val categories: MutableList<Category> = mutableListOf()
249278
val categoriesCurrent: List<Category> = categoryRepo.getAll()
250279
val typeToCategory: MutableList<RecordTypeCategory> = mutableListOf()
@@ -253,7 +282,8 @@ class BackupPartialRepoImpl @Inject constructor(
253282
val tagsCurrent: List<RecordTag> = recordTagRepo.getAll()
254283
val recordToTag: MutableList<RecordToRecordTag> = mutableListOf()
255284
val recordToTagCurrent: List<RecordToRecordTag> = recordToRecordTagRepo.getAll()
256-
// TODO SHORT
285+
val recordShortcutToTag: MutableList<RecordShortcutToRecordTag> = mutableListOf()
286+
val recordShortcutToTagCurrent: List<RecordShortcutToRecordTag> = recordShortcutToRecordTagRepo.getAll()
257287
val typeToTag: MutableList<RecordTypeToTag> = mutableListOf()
258288
val typeToTagCurrent: List<RecordTypeToTag> = recordTypeToTagRepo.getAll()
259289
val typeToDefaultTag: MutableList<RecordTypeToDefaultTag> = mutableListOf()
@@ -288,10 +318,12 @@ class BackupPartialRepoImpl @Inject constructor(
288318
dataHandler = DataHandler(
289319
types = types::add,
290320
records = records::add,
321+
recordShortcuts = recordShortcuts::add,
291322
categories = categories::add,
292323
typeToCategory = typeToCategory::add,
293324
tags = tags::add,
294325
recordToTag = recordToTag::add,
326+
recordShortcutToTag = recordShortcutToTag::add,
295327
typeToTag = typeToTag::add,
296328
typeToDefaultTag = typeToDefaultTag::add,
297329
activityFilters = activityFilters::add,
@@ -318,6 +350,14 @@ class BackupPartialRepoImpl @Inject constructor(
318350
mapToHolder(it, recordsCurrent)
319351
}
320352

353+
val (newRecordShortcuts, originalRecordShortcutIdToExistingId) = recordShortcuts.mapNotNull { item ->
354+
val newTypeId = originalTypeIdToExistingId[item.typeId]
355+
?: return@mapNotNull null
356+
item.copy(typeId = newTypeId)
357+
}.let {
358+
mapToHolder(it, recordShortcutsCurrent)
359+
}
360+
321361
val (newCategories, originalCategoryIdToExistingId) = categories.let {
322362
mapToHolder(it, categoriesCurrent)
323363
}
@@ -357,6 +397,19 @@ class BackupPartialRepoImpl @Inject constructor(
357397
mapToHolder(it, recordToTagCurrent)
358398
}.list
359399

400+
val newRecordShortcutToTag = recordShortcutToTag.mapNotNull { item ->
401+
val newRecordShortcutId = originalRecordShortcutIdToExistingId[item.shortcutId]
402+
?: return@mapNotNull null
403+
val newTagId = originalTagIdToExistingId[item.recordTagId]
404+
?: return@mapNotNull null
405+
item.copy(
406+
shortcutId = newRecordShortcutId,
407+
recordTagId = newTagId,
408+
)
409+
}.let {
410+
mapToHolder(it, recordShortcutToTagCurrent)
411+
}.list
412+
360413
val newTypeToTag = typeToTag.mapNotNull { item ->
361414
val newTypeId = originalTypeIdToExistingId[item.recordTypeId]
362415
?: return@mapNotNull null
@@ -473,13 +526,32 @@ class BackupPartialRepoImpl @Inject constructor(
473526
record.copy(data = newData)
474527
}
475528

529+
// Fill tags after all data processed, with actual tagIds.
530+
val newRecordShortcutToTagMap = newRecordShortcutToTag.groupBy {
531+
it.data.shortcutId
532+
}
533+
val newRecordShortcutsWithTags = newRecordShortcuts.map { record ->
534+
val thisTags = newRecordShortcutToTagMap[record.data.id].orEmpty().map { it.data }
535+
val newData = record.data.copy(
536+
tags = thisTags.map {
537+
RecordBase.Tag(
538+
tagId = it.recordTagId,
539+
numericValue = it.recordTagNumericValue,
540+
)
541+
},
542+
)
543+
record.copy(data = newData)
544+
}
545+
476546
result to PartialBackupRestoreData(
477547
types = newTypes.associateBy { it.data.id },
478548
records = newRecordsWithTags.associateBy { it.data.id },
549+
recordShortcuts = newRecordShortcutsWithTags.associateBy { it.data.id },
479550
categories = newCategories.associateBy { it.data.id },
480551
typeToCategory = newTypeToCategory,
481552
tags = newTags.associateBy { it.data.id },
482553
recordToTag = newRecordToTag,
554+
recordShortcutToTag = newRecordShortcutToTag,
483555
typeToTag = newTypeToTag,
484556
typeToDefaultTag = newTypeToDefaultTag,
485557
activityFilters = newActivityFilters.associateBy { it.data.id },
@@ -511,6 +583,7 @@ class BackupPartialRepoImpl @Inject constructor(
511583
return when (value) {
512584
is RecordType -> IdData<RecordType>({ copy(id = it) }, { id })
513585
is Record -> IdData<Record>({ copy(id = it) }, { id })
586+
is RecordShortcut -> IdData<RecordShortcut>({ copy(id = it) }, { id })
514587
is Category -> IdData<Category>({ copy(id = it) }, { id })
515588
is RecordTypeCategory -> IdData<RecordTypeCategory>({ this }, { 0 })
516589
is RecordTag -> IdData<RecordTag>({ copy(id = it) }, { id })

0 commit comments

Comments
 (0)