-
Notifications
You must be signed in to change notification settings - Fork 71
/
BackupRepoImpl.kt
701 lines (636 loc) · 27.9 KB
/
BackupRepoImpl.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
package com.example.util.simpletimetracker.data_local.resolver
import android.content.ContentResolver
import android.net.Uri
import android.os.ParcelFileDescriptor
import com.example.util.simpletimetracker.core.R
import com.example.util.simpletimetracker.core.repo.ResourceRepo
import com.example.util.simpletimetracker.data_local.mapper.RecordTypeGoalDataLocalMapper
import com.example.util.simpletimetracker.domain.extension.orZero
import com.example.util.simpletimetracker.domain.interactor.ClearDataInteractor
import com.example.util.simpletimetracker.domain.model.ActivityFilter
import com.example.util.simpletimetracker.domain.model.AppColor
import com.example.util.simpletimetracker.domain.model.Category
import com.example.util.simpletimetracker.domain.model.DayOfWeek
import com.example.util.simpletimetracker.domain.model.FavouriteComment
import com.example.util.simpletimetracker.domain.model.FavouriteIcon
import com.example.util.simpletimetracker.domain.model.Record
import com.example.util.simpletimetracker.domain.model.RecordTag
import com.example.util.simpletimetracker.domain.model.RecordToRecordTag
import com.example.util.simpletimetracker.domain.model.RecordType
import com.example.util.simpletimetracker.domain.model.RecordTypeCategory
import com.example.util.simpletimetracker.domain.model.RecordTypeGoal
import com.example.util.simpletimetracker.domain.model.RecordTypeToDefaultTag
import com.example.util.simpletimetracker.domain.model.RecordTypeToTag
import com.example.util.simpletimetracker.domain.repo.ActivityFilterRepo
import com.example.util.simpletimetracker.domain.repo.CategoryRepo
import com.example.util.simpletimetracker.domain.repo.FavouriteCommentRepo
import com.example.util.simpletimetracker.domain.repo.FavouriteIconRepo
import com.example.util.simpletimetracker.domain.repo.RecordRepo
import com.example.util.simpletimetracker.domain.repo.RecordTagRepo
import com.example.util.simpletimetracker.domain.repo.RecordToRecordTagRepo
import com.example.util.simpletimetracker.domain.repo.RecordTypeCategoryRepo
import com.example.util.simpletimetracker.domain.repo.RecordTypeGoalRepo
import com.example.util.simpletimetracker.domain.repo.RecordTypeRepo
import com.example.util.simpletimetracker.domain.repo.RecordTypeToDefaultTagRepo
import com.example.util.simpletimetracker.domain.repo.RecordTypeToTagRepo
import com.example.util.simpletimetracker.domain.resolver.BackupRepo
import com.example.util.simpletimetracker.domain.resolver.ResultCode
import java.io.BufferedReader
import java.io.FileOutputStream
import java.io.IOException
import java.io.InputStream
import java.io.InputStreamReader
import javax.inject.Inject
import javax.inject.Singleton
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import timber.log.Timber
import java.io.BufferedOutputStream
/**
* Do not change backup parts order, always add new to the end.
* Otherwise previous version's backups will be broken.
*/
@Singleton
class BackupRepoImpl @Inject constructor(
private val contentResolver: ContentResolver,
private val recordTypeRepo: RecordTypeRepo,
private val recordRepo: RecordRepo,
private val categoryRepo: CategoryRepo,
private val recordTypeCategoryRepo: RecordTypeCategoryRepo,
private val recordTypeToTagRepo: RecordTypeToTagRepo,
private val recordTypeToDefaultTagRepo: RecordTypeToDefaultTagRepo,
private val recordToRecordTagRepo: RecordToRecordTagRepo,
private val recordTagRepo: RecordTagRepo,
private val activityFilterRepo: ActivityFilterRepo,
private val favouriteCommentRepo: FavouriteCommentRepo,
private val favouriteIconRepo: FavouriteIconRepo,
private val recordTypeGoalRepo: RecordTypeGoalRepo,
private val clearDataInteractor: ClearDataInteractor,
private val resourceRepo: ResourceRepo,
private val recordTypeGoalDataLocalMapper: RecordTypeGoalDataLocalMapper,
) : BackupRepo {
override suspend fun saveBackupFile(
uriString: String,
): ResultCode = withContext(Dispatchers.IO) {
var fileDescriptor: ParcelFileDescriptor? = null
var fileOutputStream: BufferedOutputStream? = null
try {
val uri = Uri.parse(uriString)
fileDescriptor = contentResolver.openFileDescriptor(uri, "wt")
fileOutputStream = fileDescriptor?.fileDescriptor
?.let(::FileOutputStream)?.buffered()
// Write file identification
val identificationBackupRow: String = BACKUP_IDENTIFICATION + "\n"
fileOutputStream?.write(identificationBackupRow.toByteArray())
// Write data
recordTypeRepo.getAll().forEach {
fileOutputStream?.write(
it.let(::toBackupString).toByteArray(),
)
}
recordRepo.getAll().forEach {
fileOutputStream?.write(
it.let(::toBackupString).toByteArray(),
)
}
categoryRepo.getAll().forEach {
fileOutputStream?.write(
it.let(::toBackupString).toByteArray(),
)
}
recordTypeCategoryRepo.getAll().forEach {
fileOutputStream?.write(
it.let(::toBackupString).toByteArray(),
)
}
recordTagRepo.getAll().forEach {
fileOutputStream?.write(
it.let(::toBackupString).toByteArray(),
)
}
recordToRecordTagRepo.getAll().forEach {
fileOutputStream?.write(
it.let(::toBackupString).toByteArray(),
)
}
recordTypeToTagRepo.getAll().forEach {
fileOutputStream?.write(
it.let(::toBackupString).toByteArray(),
)
}
recordTypeToDefaultTagRepo.getAll().forEach {
fileOutputStream?.write(
it.let(::toBackupString).toByteArray(),
)
}
activityFilterRepo.getAll().forEach {
fileOutputStream?.write(
it.let(::toBackupString).toByteArray(),
)
}
favouriteCommentRepo.getAll().forEach {
fileOutputStream?.write(
it.let(::toBackupString).toByteArray(),
)
}
favouriteIconRepo.getAll().forEach {
fileOutputStream?.write(
it.let(::toBackupString).toByteArray(),
)
}
recordTypeGoalRepo.getAll().forEach {
fileOutputStream?.write(
it.let(::toBackupString).toByteArray(),
)
}
fileOutputStream?.close()
fileDescriptor?.close()
ResultCode.Success(resourceRepo.getString(R.string.message_backup_saved))
} catch (e: Exception) {
Timber.e(e)
ResultCode.Error(resourceRepo.getString(R.string.message_save_error))
} finally {
try {
fileOutputStream?.close()
fileDescriptor?.close()
} catch (e: IOException) {
// Do nothing
}
}
}
override suspend fun restoreBackupFile(
uriString: String,
): ResultCode = withContext(Dispatchers.IO) {
var inputStream: InputStream? = null
var reader: BufferedReader? = null
val errorCode = ResultCode.Error(resourceRepo.getString(R.string.message_restore_error))
try {
val uri = Uri.parse(uriString)
inputStream = contentResolver.openInputStream(uri)
reader = inputStream?.let(::InputStreamReader)?.let(::BufferedReader)
var line: String
var parts: List<String>
// Check file identification
line = reader?.readLine().orEmpty()
if (line != BACKUP_IDENTIFICATION) return@withContext errorCode
clearDataInteractor.execute()
// List of tag to related type id.
val tagsMigrationData: MutableList<Pair<RecordTag, Long>> = mutableListOf()
// Read data
while (reader?.readLine()?.also { line = it } != null) {
parts = line.split("\t")
when (parts[0]) {
ROW_RECORD_TYPE -> {
recordTypeFromBackupString(parts).let { (type, goals) ->
recordTypeRepo.add(type)
goals.forEach {
recordTypeGoalRepo.add(it)
}
}
}
ROW_RECORD -> {
recordFromBackupString(parts).let { (record, recordToRecordTag) ->
recordRepo.add(record)
if (recordToRecordTag != null) {
recordToRecordTagRepo.add(recordToRecordTag)
}
}
}
ROW_CATEGORY -> {
categoryFromBackupString(parts).let {
categoryRepo.add(it)
}
}
ROW_TYPE_CATEGORY -> {
typeCategoryFromBackupString(parts).let {
recordTypeCategoryRepo.add(it)
}
}
ROW_RECORD_TAG -> {
recordTagFromBackupString(parts).let { (tag, typeId) ->
recordTagRepo.add(tag)
if (typeId != null) {
val typeToTag = RecordTypeToTag(
recordTypeId = typeId,
tagId = tag.id,
)
recordTypeToTagRepo.add(typeToTag)
tagsMigrationData.add(tag to typeId)
}
}
}
ROW_RECORD_TO_RECORD_TAG -> {
recordToRecordTagFromBackupString(parts).let {
recordToRecordTagRepo.add(it)
}
}
ROW_TYPE_TO_RECORD_TAG -> {
typeToRecordTagFromBackupString(parts).let {
recordTypeToTagRepo.add(it)
}
}
ROW_TYPE_TO_DEFAULT_TAG -> {
typeToDefaultTagFromBackupString(parts).let {
recordTypeToDefaultTagRepo.add(it)
}
}
ROW_ACTIVITY_FILTER -> {
activityFilterFromBackupString(parts).let {
activityFilterRepo.add(it)
}
}
ROW_FAVOURITE_COMMENT -> {
favouriteCommentFromBackupString(parts)?.let {
favouriteCommentRepo.add(it)
}
}
ROW_FAVOURITE_ICON -> {
favouriteIconFromBackupString(parts)?.let {
favouriteIconRepo.add(it)
}
}
ROW_RECORD_TYPE_GOAL -> {
recordTypeGoalFromBackupString(parts).let {
recordTypeGoalRepo.add(it)
}
}
}
}
migrateTags(tagsMigrationData)
ResultCode.Success(resourceRepo.getString(R.string.message_backup_restored))
} catch (e: Exception) {
Timber.e(e)
errorCode
} finally {
try {
inputStream?.close()
reader?.close()
} catch (e: IOException) {
// Do nothing
}
}
}
private fun toBackupString(recordType: RecordType): String {
return String.format(
"$ROW_RECORD_TYPE\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n",
recordType.id.toString(),
recordType.name.clean(),
recordType.icon,
recordType.color.colorId.toString(),
(if (recordType.hidden) 1 else 0).toString(),
"", // goal time is moved to separate table
recordType.color.colorInt,
"", // daily goal time is moved to separate table
"", // weekly goal time is moved to separate table
"", // monthly goal time is moved to separate table
)
}
private fun toBackupString(record: Record): String {
return String.format(
"$ROW_RECORD\t%s\t%s\t%s\t%s\t%s\t%s\n",
record.id.toString(),
record.typeId.toString(),
record.timeStarted.toString(),
record.timeEnded.toString(),
record.comment.cleanTabs().replaceNewline(),
"", // record tag id is removed from record dbo
)
}
private fun toBackupString(category: Category): String {
return String.format(
"$ROW_CATEGORY\t%s\t%s\t%s\t%s\n",
category.id.toString(),
category.name.clean(),
category.color.colorId.toString(),
category.color.colorInt,
)
}
private fun toBackupString(recordTypeCategory: RecordTypeCategory): String {
return String.format(
"$ROW_TYPE_CATEGORY\t%s\t%s\n",
recordTypeCategory.recordTypeId.toString(),
recordTypeCategory.categoryId.toString(),
)
}
private fun toBackupString(recordTag: RecordTag): String {
return String.format(
"$ROW_RECORD_TAG\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n",
recordTag.id.toString(),
"", // type relation is removed from tag dbo
recordTag.name.clean(),
(if (recordTag.archived) 1 else 0).toString(),
recordTag.color.colorId.toString(),
recordTag.color.colorInt,
recordTag.icon,
recordTag.iconColorSource,
)
}
private fun toBackupString(recordToRecordTag: RecordToRecordTag): String {
return String.format(
"$ROW_RECORD_TO_RECORD_TAG\t%s\t%s\n",
recordToRecordTag.recordId.toString(),
recordToRecordTag.recordTagId.toString(),
)
}
private fun toBackupString(typeToTag: RecordTypeToTag): String {
return String.format(
"$ROW_TYPE_TO_RECORD_TAG\t%s\t%s\n",
typeToTag.recordTypeId.toString(),
typeToTag.tagId.toString(),
)
}
private fun toBackupString(typeToDefaultTag: RecordTypeToDefaultTag): String {
return String.format(
"$ROW_TYPE_TO_DEFAULT_TAG\t%s\t%s\n",
typeToDefaultTag.recordTypeId.toString(),
typeToDefaultTag.tagId.toString(),
)
}
private fun toBackupString(activityFilter: ActivityFilter): String {
val typeString = when (activityFilter.type) {
is ActivityFilter.Type.Activity -> 0L
is ActivityFilter.Type.Category -> 1L
}.toString()
return String.format(
"$ROW_ACTIVITY_FILTER\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n",
activityFilter.id.toString(),
activityFilter.selectedIds.joinToString(separator = ","),
typeString,
activityFilter.name.clean(),
activityFilter.color.colorId.toString(),
activityFilter.color.colorInt,
(if (activityFilter.selected) 1 else 0).toString(),
)
}
private fun toBackupString(favouriteComment: FavouriteComment): String {
return String.format(
"$ROW_FAVOURITE_COMMENT\t%s\t%s\n",
favouriteComment.id.toString(),
favouriteComment.comment.cleanTabs().replaceNewline(),
)
}
private fun toBackupString(favouriteIcon: FavouriteIcon): String {
return String.format(
"$ROW_FAVOURITE_ICON\t%s\t%s\n",
favouriteIcon.id.toString(),
favouriteIcon.icon,
)
}
private fun toBackupString(recordTypeGoal: RecordTypeGoal): String {
val rangeString = when (recordTypeGoal.range) {
is RecordTypeGoal.Range.Session -> 0L
is RecordTypeGoal.Range.Daily -> 1L
is RecordTypeGoal.Range.Weekly -> 2L
is RecordTypeGoal.Range.Monthly -> 3L
}.toString()
val typeString = when (recordTypeGoal.type) {
is RecordTypeGoal.Type.Duration -> 0L
is RecordTypeGoal.Type.Count -> 1L
}.toString()
val daysOfWeekString = recordTypeGoalDataLocalMapper
.mapDaysOfWeek(recordTypeGoal.daysOfWeek)
return String.format(
"$ROW_RECORD_TYPE_GOAL\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n",
recordTypeGoal.id.toString(),
(recordTypeGoal.idData as? RecordTypeGoal.IdData.Type)?.value.orZero(),
rangeString,
typeString,
recordTypeGoal.type.value.toString(),
(recordTypeGoal.idData as? RecordTypeGoal.IdData.Category)?.value.orZero(),
daysOfWeekString,
)
}
private fun recordTypeFromBackupString(parts: List<String>): Pair<RecordType, List<RecordTypeGoal>> {
val typeId = parts.getOrNull(1)?.toLongOrNull().orZero()
// goal times are moved to separate database, need to support old backup files.
val goalTime = parts.getOrNull(6)?.toLongOrNull().orZero()
val dailyGoalTime = parts.getOrNull(8)?.toLongOrNull().orZero()
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.values().toList()
val goalTimes = mutableListOf<RecordTypeGoal>().apply {
if (goalTime != 0L) {
RecordTypeGoal(
// Didn't exist when goal time was in type db, no need to migrate.
idData = RecordTypeGoal.IdData.Type(typeId),
range = RecordTypeGoal.Range.Session,
type = RecordTypeGoal.Type.Duration(goalTime),
daysOfWeek = daysOfWeek,
).let(::add)
}
if (dailyGoalTime != 0L) {
RecordTypeGoal(
idData = RecordTypeGoal.IdData.Type(typeId),
range = RecordTypeGoal.Range.Daily,
type = RecordTypeGoal.Type.Duration(dailyGoalTime),
daysOfWeek = daysOfWeek,
).let(::add)
}
if (weeklyGoalTime != 0L) {
RecordTypeGoal(
idData = RecordTypeGoal.IdData.Type(typeId),
range = RecordTypeGoal.Range.Weekly,
type = RecordTypeGoal.Type.Duration(weeklyGoalTime),
daysOfWeek = daysOfWeek,
).let(::add)
}
if (monthlyGoalTime != 0L) {
RecordTypeGoal(
idData = RecordTypeGoal.IdData.Type(typeId),
range = RecordTypeGoal.Range.Monthly,
type = RecordTypeGoal.Type.Duration(monthlyGoalTime),
daysOfWeek = daysOfWeek,
).let(::add)
}
}
return RecordType(
id = typeId,
name = parts.getOrNull(2).orEmpty(),
icon = parts.getOrNull(3).orEmpty(),
color = AppColor(
colorId = parts.getOrNull(4)?.toIntOrNull().orZero(),
colorInt = parts.getOrNull(7).orEmpty(),
),
hidden = parts.getOrNull(5)?.toIntOrNull() == 1,
// parts[6] - goal time is moved to separate table
// parts[8] - daily time is moved to separate table
// parts[9] - weekly time is moved to separate table
// parts[10] - monthly time is moved to separate table
) to goalTimes
}
private fun recordFromBackupString(parts: List<String>): Pair<Record, RecordToRecordTag?> {
val recordId = parts.getOrNull(1)?.toLongOrNull().orZero()
// tag id is removed from record dbo, need to support old backup files.
val tagId = parts.getOrNull(6)?.toLongOrNull().orZero()
// replaces all return symbols to newlines by creating a temporary mutable list to replace return symbols from
val mutableComment = parts.toMutableList()
for (i in mutableComment.indices) {
mutableComment[i] = mutableComment[i].restoreNewline()
}
return Record(
id = recordId,
typeId = parts.getOrNull(2)?.toLongOrNull() ?: 1L,
timeStarted = parts.getOrNull(3)?.toLongOrNull().orZero(),
timeEnded = parts.getOrNull(4)?.toLongOrNull().orZero(),
comment = mutableComment.toList().getOrNull(5).orEmpty(),
// parts[6] - tag id is removed from record dbo.
) to RecordToRecordTag(
recordId = recordId,
recordTagId = tagId,
).takeUnless { tagId == 0L }
}
private fun categoryFromBackupString(parts: List<String>): Category {
return Category(
id = parts.getOrNull(1)?.toLongOrNull().orZero(),
name = parts.getOrNull(2).orEmpty(),
color = AppColor(
colorId = parts.getOrNull(3)?.toIntOrNull().orZero(),
colorInt = parts.getOrNull(4).orEmpty(),
),
)
}
private fun typeCategoryFromBackupString(parts: List<String>): RecordTypeCategory {
return RecordTypeCategory(
recordTypeId = parts.getOrNull(1)?.toLongOrNull().orZero(),
categoryId = parts.getOrNull(2)?.toLongOrNull().orZero(),
)
}
private fun recordTagFromBackupString(parts: List<String>): Pair<RecordTag, Long?> {
// type id is removed from tag dbo, need to support old backup files.
val typeId = parts.getOrNull(2)?.toLongOrNull().orZero()
return RecordTag(
id = parts.getOrNull(1)?.toLongOrNull().orZero(),
// parts[2] - type id is removed from tag dbo.
name = parts.getOrNull(3).orEmpty(),
archived = parts.getOrNull(4)?.toIntOrNull() == 1,
color = AppColor(
colorId = parts.getOrNull(5)?.toIntOrNull().orZero(),
colorInt = parts.getOrNull(6).orEmpty(),
),
icon = parts.getOrNull(7).orEmpty(),
iconColorSource = parts.getOrNull(8)?.toLongOrNull().orZero(),
) to typeId.takeUnless { it == 0L }
}
private fun recordToRecordTagFromBackupString(parts: List<String>): RecordToRecordTag {
return RecordToRecordTag(
recordId = parts.getOrNull(1)?.toLongOrNull().orZero(),
recordTagId = parts.getOrNull(2)?.toLongOrNull().orZero(),
)
}
private fun typeToRecordTagFromBackupString(parts: List<String>): RecordTypeToTag {
return RecordTypeToTag(
recordTypeId = parts.getOrNull(1)?.toLongOrNull().orZero(),
tagId = parts.getOrNull(2)?.toLongOrNull().orZero(),
)
}
private fun typeToDefaultTagFromBackupString(parts: List<String>): RecordTypeToDefaultTag {
return RecordTypeToDefaultTag(
recordTypeId = parts.getOrNull(1)?.toLongOrNull().orZero(),
tagId = parts.getOrNull(2)?.toLongOrNull().orZero(),
)
}
private fun activityFilterFromBackupString(parts: List<String>): ActivityFilter {
return ActivityFilter(
id = parts.getOrNull(1)?.toLongOrNull().orZero(),
selectedIds = parts.getOrNull(2)?.split(",")
?.mapNotNull { it.toLongOrNull() }.orEmpty(),
type = parts.getOrNull(3)?.toLongOrNull()?.let {
when (it) {
0L -> ActivityFilter.Type.Activity
1L -> ActivityFilter.Type.Category
else -> ActivityFilter.Type.Activity
}
} ?: ActivityFilter.Type.Activity,
name = parts.getOrNull(4).orEmpty(),
color = AppColor(
colorId = parts.getOrNull(5)?.toIntOrNull().orZero(),
colorInt = parts.getOrNull(6).orEmpty(),
),
selected = parts.getOrNull(7)?.toIntOrNull() == 1,
)
}
private fun favouriteCommentFromBackupString(parts: List<String>): FavouriteComment? {
return FavouriteComment(
id = parts.getOrNull(1)?.toLongOrNull().orZero(),
comment = parts.getOrNull(2)?.restoreNewline()
?.takeUnless(String::isEmpty) ?: return null,
)
}
private fun favouriteIconFromBackupString(parts: List<String>): FavouriteIcon? {
return FavouriteIcon(
id = parts.getOrNull(1)?.toLongOrNull().orZero(),
icon = parts.getOrNull(2)
?.takeUnless(String::isEmpty) ?: return null,
)
}
private fun recordTypeGoalFromBackupString(parts: List<String>): RecordTypeGoal {
val typeId = parts.getOrNull(2)?.toLongOrNull().orZero()
val categoryId = parts.getOrNull(6)?.toLongOrNull().orZero()
val daysOfWeekString = parts.getOrNull(7).orEmpty()
return RecordTypeGoal(
id = parts.getOrNull(1)?.toLongOrNull().orZero(),
idData = if (typeId != 0L) {
RecordTypeGoal.IdData.Type(typeId)
} else {
RecordTypeGoal.IdData.Category(categoryId)
},
range = when (parts.getOrNull(3)?.toLongOrNull()) {
0L -> RecordTypeGoal.Range.Session
1L -> RecordTypeGoal.Range.Daily
2L -> RecordTypeGoal.Range.Weekly
3L -> RecordTypeGoal.Range.Monthly
else -> RecordTypeGoal.Range.Session
},
type = run {
val value = parts.getOrNull(5)?.toLongOrNull().orZero()
when (parts.getOrNull(4)?.toLongOrNull()) {
0L -> RecordTypeGoal.Type.Duration(value)
1L -> RecordTypeGoal.Type.Count(value)
else -> RecordTypeGoal.Type.Duration(value)
}
},
daysOfWeek = recordTypeGoalDataLocalMapper.mapDaysOfWeek(daysOfWeekString),
)
}
private suspend fun migrateTags(
data: List<Pair<RecordTag, Long>>,
) {
if (data.isEmpty()) return
val types = recordTypeRepo.getAll().associateBy { it.id }
data.forEach { (tag, typeId) ->
val type = types[typeId] ?: return@forEach
val updatedTag = tag.copy(
icon = type.icon,
color = type.color,
iconColorSource = typeId,
)
recordTagRepo.add(updatedTag)
}
}
private fun String.clean() =
cleanTabs().cleanNewline()
private fun String.cleanTabs() =
replace("\t", " ")
private fun String.cleanNewline() =
replace("\n", " ")
private fun String.replaceNewline() =
replace("\n", "")
private fun String.restoreNewline() =
replace("", "\n")
companion object {
private const val BACKUP_IDENTIFICATION = "app simple time tracker"
private const val ROW_RECORD_TYPE = "recordType"
private const val ROW_RECORD = "record"
private const val ROW_CATEGORY = "category"
private const val ROW_TYPE_CATEGORY = "typeCategory"
private const val ROW_RECORD_TAG = "recordTag"
private const val ROW_RECORD_TO_RECORD_TAG = "recordToRecordTag"
private const val ROW_TYPE_TO_RECORD_TAG = "typeToRecordTag"
private const val ROW_TYPE_TO_DEFAULT_TAG = "typeToDefaultTag"
private const val ROW_ACTIVITY_FILTER = "activityFilter"
private const val ROW_FAVOURITE_COMMENT = "favouriteComment"
private const val ROW_FAVOURITE_ICON = "favouriteIcon"
private const val ROW_RECORD_TYPE_GOAL = "recordTypeGoal"
}
}