Skip to content

Commit a6ded26

Browse files
committed
add time change for auto backup and export
1 parent ff0e748 commit a6ded26

44 files changed

Lines changed: 220 additions & 48 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

data_local/src/main/java/com/example/util/simpletimetracker/data_local/prefs/PrefsRepoImpl.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,10 @@ class PrefsRepoImpl @Inject constructor(
369369
KEY_AUTOMATIC_BACKUP_LAST_SAVE_TIME, 0,
370370
)
371371

372+
override var automaticBackupTriggerTime: Long by prefs.delegate(
373+
KEY_AUTOMATIC_BACKUP_TRIGGER_TIME, 0,
374+
)
375+
372376
override var automaticExportUri: String by prefs.delegate(
373377
KEY_AUTOMATIC_EXPORT_URI, "",
374378
)
@@ -381,6 +385,10 @@ class PrefsRepoImpl @Inject constructor(
381385
KEY_AUTOMATIC_EXPORT_LAST_SAVE_TIME, 0,
382386
)
383387

388+
override var automaticExportTriggerTime: Long by prefs.delegate(
389+
KEY_AUTOMATIC_EXPORT_TRIGGER_TIME, 0,
390+
)
391+
384392
override var repeatButtonType: Int by prefs.delegate(
385393
KEY_REPEAT_BUTTON_TYPE, 0,
386394
)
@@ -710,9 +718,11 @@ class PrefsRepoImpl @Inject constructor(
710718
private const val KEY_AUTOMATIC_BACKUP_URI = "automaticBackupUri"
711719
private const val KEY_AUTOMATIC_BACKUP_ERROR = "automaticBackupError"
712720
private const val KEY_AUTOMATIC_BACKUP_LAST_SAVE_TIME = "automaticBackupLastSaveTime"
721+
private const val KEY_AUTOMATIC_BACKUP_TRIGGER_TIME = "automaticBackupTriggerTime"
713722
private const val KEY_AUTOMATIC_EXPORT_URI = "automaticExportUri"
714723
private const val KEY_AUTOMATIC_EXPORT_ERROR = "automaticExportError"
715724
private const val KEY_AUTOMATIC_EXPORT_LAST_SAVE_TIME = "automaticExportLastSaveTime"
725+
private const val KEY_AUTOMATIC_EXPORT_TRIGGER_TIME = "automaticExportTriggerTime"
716726
private const val KEY_POMODORO_MODE_STARTED_TIMESTAMP = "pomodoroModeStartedTimestamp"
717727
private const val KEY_POMODORO_MODE_PAUSED_TIMESTAMP = "pomodoroModePausedTimestamp"
718728
private const val KEY_WIDGET = "widget_"

domain/src/main/java/com/example/util/simpletimetracker/domain/backup/interactor/AutomaticBackupInteractor.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package com.example.util.simpletimetracker.domain.backup.interactor
22

33
interface AutomaticBackupInteractor {
44

5-
fun schedule()
5+
suspend fun schedule()
66

77
fun cancel()
88

domain/src/main/java/com/example/util/simpletimetracker/domain/backup/interactor/AutomaticExportInteractor.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package com.example.util.simpletimetracker.domain.backup.interactor
22

33
interface AutomaticExportInteractor {
44

5-
fun schedule()
5+
suspend fun schedule()
66

77
fun cancel()
88

domain/src/main/java/com/example/util/simpletimetracker/domain/prefs/interactor/PrefsInteractor.kt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -835,6 +835,14 @@ class PrefsInteractor @Inject constructor(
835835
prefsRepo.automaticBackupLastSaveTime
836836
}
837837

838+
suspend fun setAutomaticBackupTriggerTime(value: Long) = withContext(Dispatchers.IO) {
839+
prefsRepo.automaticBackupTriggerTime = value
840+
}
841+
842+
suspend fun getAutomaticBackupTriggerTime(): Long = withContext(Dispatchers.IO) {
843+
prefsRepo.automaticBackupTriggerTime
844+
}
845+
838846
suspend fun setAutomaticExportUri(uri: String) = withContext(Dispatchers.IO) {
839847
prefsRepo.automaticExportUri = uri
840848
}
@@ -859,6 +867,14 @@ class PrefsInteractor @Inject constructor(
859867
prefsRepo.automaticExportLastSaveTime
860868
}
861869

870+
suspend fun setAutomaticExportTriggerTime(value: Long) = withContext(Dispatchers.IO) {
871+
prefsRepo.automaticExportTriggerTime = value
872+
}
873+
874+
suspend fun getAutomaticExportTriggerTime(): Long = withContext(Dispatchers.IO) {
875+
prefsRepo.automaticExportTriggerTime
876+
}
877+
862878
suspend fun getRepeatButtonType(): RepeatButtonType = withContext(Dispatchers.IO) {
863879
when (prefsRepo.repeatButtonType) {
864880
0 -> RepeatButtonType.RepeatLast

domain/src/main/java/com/example/util/simpletimetracker/domain/prefs/repo/PrefsRepo.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,12 +158,16 @@ interface PrefsRepo {
158158

159159
var automaticBackupLastSaveTime: Long
160160

161+
var automaticBackupTriggerTime: Long
162+
161163
var automaticExportUri: String
162164

163165
var automaticExportError: Boolean
164166

165167
var automaticExportLastSaveTime: Long
166168

169+
var automaticExportTriggerTime: Long
170+
167171
var repeatButtonType: Int
168172

169173
var widgetBackgroundTransparencyPercent: Long

features/feature_notification/src/main/java/com/example/util/simpletimetracker/feature_notification/automaticBackup/controller/AutomaticBackupBroadcastController.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package com.example.util.simpletimetracker.feature_notification.automaticBackup.controller
22

3+
import com.example.util.simpletimetracker.core.extension.allowDiskRead
34
import com.example.util.simpletimetracker.domain.backup.interactor.AutomaticBackupInteractor
5+
import kotlinx.coroutines.MainScope
6+
import kotlinx.coroutines.launch
47
import javax.inject.Inject
58

69
class AutomaticBackupBroadcastController @Inject constructor(
@@ -15,7 +18,7 @@ class AutomaticBackupBroadcastController @Inject constructor(
1518
automaticBackupInteractor.onFinished()
1619
}
1720

18-
fun onBootCompleted() {
21+
fun onBootCompleted() = allowDiskRead { MainScope() }.launch {
1922
automaticBackupInteractor.schedule()
2023
}
2124
}

features/feature_notification/src/main/java/com/example/util/simpletimetracker/feature_notification/automaticBackup/interactor/AutomaticBackupInteractorImpl.kt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,20 @@ import com.example.util.simpletimetracker.domain.prefs.interactor.PrefsInteracto
88
import com.example.util.simpletimetracker.domain.backup.model.BackupOptionsData
99
import com.example.util.simpletimetracker.domain.backup.model.ResultCode
1010
import com.example.util.simpletimetracker.feature_notification.automaticBackup.scheduler.AutomaticBackupScheduler
11-
import com.example.util.simpletimetracker.feature_notification.core.GetTimeToDayEndInteractor
11+
import com.example.util.simpletimetracker.feature_notification.core.GetTimeLeftToTimestampInteractor
1212
import javax.inject.Inject
1313

1414
class AutomaticBackupInteractorImpl @Inject constructor(
1515
private val scheduler: AutomaticBackupScheduler,
1616
private val backupInteractor: BackupInteractor,
1717
private val prefsInteractor: PrefsInteractor,
1818
private val automaticBackupRepo: AutomaticBackupRepo,
19-
private val getTimeToDayEndInteractor: GetTimeToDayEndInteractor,
19+
private val getTimeLeftToTimestampInteractor: GetTimeLeftToTimestampInteractor,
2020
) : AutomaticBackupInteractor {
2121

22-
override fun schedule() {
23-
val timestamp = getTimeToDayEndInteractor.execute()
22+
override suspend fun schedule() {
23+
val triggerTime = prefsInteractor.getAutomaticBackupTriggerTime()
24+
val timestamp = getTimeLeftToTimestampInteractor.execute(triggerTime)
2425
scheduler.schedule(timestamp)
2526
}
2627

features/feature_notification/src/main/java/com/example/util/simpletimetracker/feature_notification/automaticExport/controller/AutomaticExportBroadcastController.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package com.example.util.simpletimetracker.feature_notification.automaticExport.controller
22

3+
import com.example.util.simpletimetracker.core.extension.allowDiskRead
34
import com.example.util.simpletimetracker.domain.backup.interactor.AutomaticExportInteractor
5+
import kotlinx.coroutines.MainScope
6+
import kotlinx.coroutines.launch
47
import javax.inject.Inject
58

69
class AutomaticExportBroadcastController @Inject constructor(
@@ -15,7 +18,7 @@ class AutomaticExportBroadcastController @Inject constructor(
1518
automaticExportInteractor.onFinished()
1619
}
1720

18-
fun onBootCompleted() {
21+
fun onBootCompleted() = allowDiskRead { MainScope() }.launch {
1922
automaticExportInteractor.schedule()
2023
}
2124
}

features/feature_notification/src/main/java/com/example/util/simpletimetracker/feature_notification/automaticExport/interactor/AutomaticExportInteractorImpl.kt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,20 @@ import com.example.util.simpletimetracker.domain.backup.interactor.CsvExportInte
77
import com.example.util.simpletimetracker.domain.prefs.interactor.PrefsInteractor
88
import com.example.util.simpletimetracker.domain.backup.model.ResultCode
99
import com.example.util.simpletimetracker.feature_notification.automaticExport.scheduler.AutomaticExportScheduler
10-
import com.example.util.simpletimetracker.feature_notification.core.GetTimeToDayEndInteractor
10+
import com.example.util.simpletimetracker.feature_notification.core.GetTimeLeftToTimestampInteractor
1111
import javax.inject.Inject
1212

1313
class AutomaticExportInteractorImpl @Inject constructor(
1414
private val scheduler: AutomaticExportScheduler,
1515
private val csvExportInteractor: CsvExportInteractor,
1616
private val prefsInteractor: PrefsInteractor,
1717
private val automaticExportRepo: AutomaticExportRepo,
18-
private val getTimeToDayEndInteractor: GetTimeToDayEndInteractor,
18+
private val getTimeLeftToTimestampInteractor: GetTimeLeftToTimestampInteractor,
1919
) : AutomaticExportInteractor {
2020

21-
override fun schedule() {
22-
val timestamp = getTimeToDayEndInteractor.execute()
21+
override suspend fun schedule() {
22+
val triggerTime = prefsInteractor.getAutomaticExportTriggerTime()
23+
val timestamp = getTimeLeftToTimestampInteractor.execute(triggerTime)
2324
scheduler.schedule(timestamp)
2425
}
2526

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.example.util.simpletimetracker.feature_notification.core
2+
3+
import com.example.util.simpletimetracker.core.extension.shiftTimeStamp
4+
import com.example.util.simpletimetracker.core.mapper.TimeMapper
5+
import java.util.Calendar
6+
import javax.inject.Inject
7+
8+
class GetTimeLeftToTimestampInteractor @Inject constructor(
9+
private val timeMapper: TimeMapper,
10+
) {
11+
12+
fun execute(
13+
dayTimestamp: Long,
14+
): Long {
15+
val calendar = Calendar.getInstance()
16+
val current = System.currentTimeMillis()
17+
18+
val triggerTime = calendar.shiftTimeStamp(
19+
timestamp = timeMapper.getStartOfDayTimeStamp(),
20+
shift = dayTimestamp,
21+
).let {
22+
if (it > current) {
23+
it
24+
} else {
25+
calendar.timeInMillis = it
26+
calendar.apply { add(Calendar.DATE, 1) }
27+
calendar.timeInMillis
28+
}
29+
}
30+
31+
return triggerTime - current
32+
}
33+
}

0 commit comments

Comments
 (0)