Skip to content

Commit

Permalink
WTA #44: Created helper class to re-calculate streak.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacob3075 committed Oct 27, 2022
1 parent cb66140 commit 1316ab4
Showing 1 changed file with 44 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.jacob.wakatimeapp.home.domain

import arrow.core.getOrElse
import com.jacob.wakatimeapp.home.data.network.HomePageNetworkData
import com.jacob.wakatimeapp.home.domain.models.StreakRange
import com.jacob.wakatimeapp.home.domain.usecases.getLatestStreakInRange
import javax.inject.Inject
import javax.inject.Singleton
import kotlinx.datetime.DateTimeUnit
import kotlinx.datetime.DateTimeUnit.DateBased
import kotlinx.datetime.LocalDate
import kotlinx.datetime.minus

@Singleton
class RecalculateLatestStreakService @Inject constructor(
private val homePageNetworkData: HomePageNetworkData,
) {
suspend fun calculate(
start: LocalDate,
value: Int,
unit: DateBased,
): StreakRange {
val end = start.minus(value, unit)
return homePageNetworkData.getStatsForRange(start.toString(), end.toString())
.map { stats ->
stats.dailyStats
.associate { it.date to it.timeSpent }
.getLatestStreakInRange()
}
.map { StreakRange(start = it.last().key, end = it.first().key) }
.map {
if (it.days == value) {
it + calculate(
start = end.minus(1, DateTimeUnit.DAY),
value = value,
unit = unit
)
} else {
it
}
}
.getOrElse { StreakRange.ZERO }
}
}

0 comments on commit 1316ab4

Please sign in to comment.