-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WTA #44: Created helper class to re-calculate streak.
- Loading branch information
Showing
1 changed file
with
44 additions
and
0 deletions.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
home/src/main/java/com/jacob/wakatimeapp/home/domain/RecalculateLatestStreakService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } | ||
} | ||
} |