fix(internal): ensure continuous history on app startup
This commit is contained in:
@@ -44,4 +44,7 @@ interface StatDao {
|
||||
|
||||
@Query("SELECT EXISTS (SELECT * FROM stat WHERE date = :date)")
|
||||
suspend fun statExists(date: String): Boolean
|
||||
|
||||
@Query("SELECT date FROM stat ORDER BY date DESC LIMIT 1")
|
||||
suspend fun getLastDate(): String?
|
||||
}
|
||||
@@ -20,6 +20,8 @@ import java.time.LocalTime
|
||||
* ViewModel
|
||||
*/
|
||||
interface StatRepository {
|
||||
suspend fun insertStat(stat: Stat)
|
||||
|
||||
suspend fun addFocusTime(focusTime: Long)
|
||||
|
||||
suspend fun addBreakTime(breakTime: Long)
|
||||
@@ -29,6 +31,8 @@ interface StatRepository {
|
||||
fun getLastWeekStatsSummary(): Flow<List<StatSummary>>
|
||||
|
||||
fun getAverageFocusTimes(): Flow<StatFocusTime?>
|
||||
|
||||
suspend fun getLastDate(): String?
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -38,6 +42,8 @@ class AppStatRepository(
|
||||
private val statDao: StatDao,
|
||||
private val ioDispatcher: CoroutineDispatcher = Dispatchers.IO
|
||||
) : StatRepository {
|
||||
override suspend fun insertStat(stat: Stat) = statDao.insertStat(stat)
|
||||
|
||||
override suspend fun addFocusTime(focusTime: Long) = withContext(ioDispatcher) {
|
||||
val currentDate = LocalDate.now().toString()
|
||||
val currentTime = LocalTime.now().toSecondOfDay()
|
||||
@@ -99,4 +105,6 @@ class AppStatRepository(
|
||||
statDao.getLastWeekStatsSummary()
|
||||
|
||||
override fun getAverageFocusTimes(): Flow<StatFocusTime?> = statDao.getAvgFocusTimes()
|
||||
|
||||
override suspend fun getLastDate(): String? = statDao.getLastDate()
|
||||
}
|
||||
@@ -25,9 +25,11 @@ import kotlinx.coroutines.flow.update
|
||||
import kotlinx.coroutines.launch
|
||||
import org.nsh07.pomodoro.TomatoApplication
|
||||
import org.nsh07.pomodoro.data.PreferenceRepository
|
||||
import org.nsh07.pomodoro.data.Stat
|
||||
import org.nsh07.pomodoro.data.StatRepository
|
||||
import org.nsh07.pomodoro.data.TimerRepository
|
||||
import org.nsh07.pomodoro.utils.millisecondsToStr
|
||||
import java.time.LocalDate
|
||||
|
||||
@OptIn(FlowPreview::class)
|
||||
class TimerViewModel(
|
||||
@@ -83,6 +85,15 @@ class TimerViewModel(
|
||||
|
||||
resetTimer()
|
||||
|
||||
var lastDate = LocalDate.parse(statRepository.getLastDate())
|
||||
val today = LocalDate.now()
|
||||
|
||||
// Fills dates between today and lastDate with 0s to ensure continuous history
|
||||
while (lastDate.until(today).days > 0) {
|
||||
lastDate = lastDate.plusDays(1)
|
||||
statRepository.insertStat(Stat(lastDate.toString(), 0, 0, 0, 0, 0))
|
||||
}
|
||||
|
||||
delay(1500)
|
||||
|
||||
_timerState.update { currentState ->
|
||||
|
||||
Reference in New Issue
Block a user