Skip to content

Commit

Permalink
WTA #82: added method to get date in InstantProvider and updated netw…
Browse files Browse the repository at this point in the history
…ork data api to use local dates
  • Loading branch information
Jacob3075 committed Nov 1, 2023
1 parent 977c76b commit 74c2c7a
Show file tree
Hide file tree
Showing 15 changed files with 102 additions and 105 deletions.
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@ package com.jacob.wakatimeapp.core.common.utils
import javax.inject.Inject
import kotlinx.datetime.Clock
import kotlinx.datetime.Instant
import kotlinx.datetime.LocalDate
import kotlinx.datetime.TimeZone
import kotlinx.datetime.toLocalDateTime

interface InstantProvider {
val timeZone: TimeZone
fun now(): Instant

fun date(): LocalDate = now().toLocalDateTime(timeZone).date
}

class DefaultInstantProvider @Inject constructor() : InstantProvider {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package com.jacob.wakatimeapp.details.domain.usecases

import arrow.core.raise.either
import com.jacob.wakatimeapp.core.common.utils.InstantProvider
import com.jacob.wakatimeapp.core.common.utils.toDate
import com.jacob.wakatimeapp.core.models.ProjectStats
import com.jacob.wakatimeapp.core.models.Time
import com.jacob.wakatimeapp.details.data.ProjectDetailsNetworkData
Expand All @@ -29,7 +28,7 @@ internal class GetProjectStatsUC @Inject constructor(
val totalProjectTime = projectStatsNetworkData.getTotalTimeForProject(projectName).bind()

val batchSize = DatePeriod(months = 6)
val now = instantProvider.now().toDate()
val now = instantProvider.date()

val projectStats = generateSequence(totalProjectTime.startDate) { it + batchSize }
.takeWhile { it < now }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import com.jacob.wakatimeapp.home.data.network.dtos.GetStatsForRangeResDTO
import com.jacob.wakatimeapp.home.data.network.mappers.toModel
import javax.inject.Inject
import javax.inject.Singleton
import kotlinx.datetime.LocalDate

@Singleton
internal class HomePageNetworkData @Inject constructor(
Expand All @@ -29,13 +30,13 @@ internal class HomePageNetworkData @Inject constructor(
methodName = ::getStatsForToday.name,
).map(GetDailyStatsResDTO::toModel)

suspend fun getStatsForRange(start: String, end: String): Either<Error, DailyStatsAggregate> =
suspend fun getStatsForRange(start: LocalDate, end: LocalDate): Either<Error, DailyStatsAggregate> =
makeSafeApiCall(
apiCall = {
homePageAPI.getStatsForRange(
it,
start = start,
end = end,
start = start.toString(),
end = end.toString(),
)
},
methodName = ::getStatsForRange.name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import arrow.core.Either
import arrow.core.raise.either
import arrow.core.right
import com.jacob.wakatimeapp.core.common.utils.InstantProvider
import com.jacob.wakatimeapp.core.common.utils.toDate
import com.jacob.wakatimeapp.core.models.Error
import com.jacob.wakatimeapp.core.models.Time
import com.jacob.wakatimeapp.home.data.local.HomePageCache
Expand All @@ -29,7 +28,7 @@ internal class CalculateCurrentStreakUC @Inject constructor(
homePageCache.getLast7DaysStats().first().bind() ?: return@either Streak.ZERO
val currentStreak = homePageCache.getCurrentStreak().first().bind()

val today = instantProvider.now().toDate()
val today = instantProvider.date()
val todaysStats = last7DaysStats.weeklyTimeSpent[today] ?: Time.ZERO

val endOfCurrentStreakIsYesterday = currentStreak.end == today.minus(1, DateTimeUnit.DAY)
Expand All @@ -56,15 +55,15 @@ internal class CalculateCurrentStreakUC @Inject constructor(
todaysStats: Time,
) = when (todaysStats) {
Time.ZERO -> currentStreak
else -> currentStreak.copy(end = instantProvider.now().toDate())
else -> currentStreak.copy(end = instantProvider.date())
}

@Suppress("MagicNumber")
private suspend fun whenFailedToCombine(
recalculatedStreakForLast7Days: Streak,
) = when (recalculatedStreakForLast7Days.days) {
7 -> recalculateLatestStreakUC.calculate(
start = instantProvider.now().toDate().minus(8, DateTimeUnit.DAY),
start = instantProvider.date().minus(8, DateTimeUnit.DAY),
batchSize = DatePeriod(months = 1),
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package com.jacob.wakatimeapp.home.domain.usecases
import arrow.core.raise.either
import com.jacob.wakatimeapp.core.common.auth.AuthDataStore
import com.jacob.wakatimeapp.core.common.utils.InstantProvider
import com.jacob.wakatimeapp.core.common.utils.toDate
import com.jacob.wakatimeapp.core.models.DailyStatsAggregate
import com.jacob.wakatimeapp.core.models.Error.NetworkErrors.Timeout
import com.jacob.wakatimeapp.core.models.Time
Expand Down Expand Up @@ -41,7 +40,7 @@ internal class CalculateLongestStreakUC @Inject constructor(
if (currentStreak > cachedLongestStreak) return@either currentStreak
if (cachedLongestStreak != Streak.ZERO) return@either cachedLongestStreak

val currentDay = instantProvider.now().toDate(timeZone = instantProvider.timeZone)
val currentDay = instantProvider.date()
val userJoinedData = userDetails.createdAt

generateSequence(userJoinedData) { it + batchSize }
Expand All @@ -64,8 +63,8 @@ internal class CalculateLongestStreakUC @Inject constructor(

private fun getStreaksInBatchAsync(batchStartEnd: Pair<LocalDate, LocalDate>) = ioScope.async {
homePageNetworkData.getStatsForRange(
start = batchStartEnd.first.toString(),
end = batchStartEnd.second.toString(),
start = batchStartEnd.first,
end = batchStartEnd.second,
).map {
it.groupConsecutiveDaysWithStats()
.filter(List<Entry<LocalDate, Time>>::isNotEmpty)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ internal class GetCachedHomePageUiDataUC @Inject constructor(

private fun Instant.isFirstRequestOfDay(): Boolean {
val lastRequestDate = toLocalDateTime(instantProvider.timeZone).date.toEpochDays()
val currentDate =
instantProvider.now().toLocalDateTime(instantProvider.timeZone).date.toEpochDays()
val currentDate = instantProvider.date().toEpochDays()
return currentDate - lastRequestDate >= 1
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ internal class RecalculateLatestStreakUC @Inject constructor(
}

private suspend fun getStatsInRange(start: LocalDate, end: LocalDate) =
homePageNetworkData.getStatsForRange(start.toString(), end.toString())
homePageNetworkData.getStatsForRange(start, end)
.map { stats ->
stats.values
.associate { it.date to it.timeSpent }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,13 @@ import io.mockk.coEvery
import io.mockk.coVerify
import io.mockk.mockk
import kotlinx.collections.immutable.toImmutableList
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.test.TestDispatcher
import kotlinx.datetime.DatePeriod
import kotlinx.datetime.Instant
import kotlinx.datetime.LocalDate
import kotlinx.datetime.TimeZone

@OptIn(ExperimentalCoroutinesApi::class)
internal class CalculateLongestStreakUCRobot {
private lateinit var calculateLongestStreakUC: CalculateLongestStreakUC
private var result: Either<Error, Streak>? = null
Expand Down Expand Up @@ -90,8 +88,8 @@ internal class CalculateLongestStreakUCRobot {
}

fun mockGetStatsForRange(
start: String? = null,
end: String? = null,
start: LocalDate? = null,
end: LocalDate? = null,
data: Either<Error, DailyStatsAggregate>,
) = apply {
coEvery {
Expand All @@ -102,7 +100,7 @@ internal class CalculateLongestStreakUCRobot {
} returns data
}

fun verifyGetStatsForRangeCalled(count: Int, start: String? = null, end: String? = null) =
fun verifyGetStatsForRangeCalled(count: Int, start: LocalDate? = null, end: LocalDate? = null) =
apply {
coVerify(exactly = count) {
mockHomePageNetworkData.getStatsForRange(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ internal class CalculateLongestStreakUCTest {
.mockHomePageCacheGetLongestStreak()
.mockHomePageCacheGetCurrentStreak()
.mockGetStatsForRange(
start = userCreatedAt.toString(),
end = currentInstant.toDate().toString(),
start = userCreatedAt,
end = currentInstant.toDate(),
data = stats.right(),
)
.callUseCase(batchSize = DatePeriod(months = 1))
Expand All @@ -98,13 +98,13 @@ internal class CalculateLongestStreakUCTest {
.mockHomePageCacheGetLongestStreak()
.mockHomePageCacheGetCurrentStreak()
.mockGetStatsForRange(
start = userCreatedAt.toString(),
end = LocalDate(2022, 2, 1).toString(),
start = userCreatedAt,
end = LocalDate(2022, 2, 1),
data = stats.right(),
)
.mockGetStatsForRange(
start = LocalDate(2022, 2, 1).toString(),
end = currentInstant.toDate().toString(),
start = LocalDate(2022, 2, 1),
end = currentInstant.toDate(),
data = stats.right(),
)
.callUseCase(batchSize = DatePeriod(months = 1))
Expand All @@ -124,8 +124,8 @@ internal class CalculateLongestStreakUCTest {
.mockHomePageCacheGetLongestStreak()
.mockHomePageCacheGetCurrentStreak()
.mockGetStatsForRange(
start = userCreatedAt.toString(),
end = currentInstant.toDate().toString(),
start = userCreatedAt,
end = currentInstant.toDate(),
data = stats.copy(
values = List(30) {
dailyStats.copy(
Expand All @@ -152,8 +152,8 @@ internal class CalculateLongestStreakUCTest {
.mockHomePageCacheGetLongestStreak()
.mockHomePageCacheGetCurrentStreak()
.mockGetStatsForRange(
start = userCreatedAt.toString(),
end = currentInstant.toDate().toString(),
start = userCreatedAt,
end = currentInstant.toDate(),
data = stats.copy(
values = List(10) {
dailyStats.copy(date = userCreatedAt + it.days)
Expand Down Expand Up @@ -199,8 +199,8 @@ internal class CalculateLongestStreakUCTest {
.mockHomePageCacheGetLongestStreak()
.mockHomePageCacheGetCurrentStreak()
.mockGetStatsForRange(
start = userCreatedAt.toString(),
end = currentInstant.toDate().toString(),
start = userCreatedAt,
end = currentInstant.toDate(),
data = stats.copy(
values = testDailyStats,
).right(),
Expand Down Expand Up @@ -238,18 +238,18 @@ internal class CalculateLongestStreakUCTest {
.mockHomePageCacheGetLongestStreak()
.mockHomePageCacheGetCurrentStreak()
.mockGetStatsForRange(
start = userCreatedAt.toString(),
end = (userCreatedAt + 7.days).toString(),
start = userCreatedAt,
end = (userCreatedAt + 7.days),
data = stats.copy(values = testDailyStats1).right(),
)
.mockGetStatsForRange(
start = (userCreatedAt + 7.days).toString(),
end = (userCreatedAt + 14.days).toString(),
start = (userCreatedAt + 7.days),
end = (userCreatedAt + 14.days),
data = stats.copy(values = testDailyStats2).right(),
)
.mockGetStatsForRange(
start = (userCreatedAt + 14.days).toString(),
end = (userCreatedAt + 21.days).toString(),
start = (userCreatedAt + 14.days),
end = (userCreatedAt + 21.days),
data = stats.copy(values = testDailyStats3).right(),
)
.callUseCase(batchSize = DatePeriod(days = 7))
Expand Down Expand Up @@ -329,28 +329,28 @@ internal class CalculateLongestStreakUCTest {
.mockHomePageCacheGetLongestStreak()
.mockHomePageCacheGetCurrentStreak()
.mockGetStatsForRange(
start = userCreatedAt.toString(),
end = (userCreatedAt + 7.days).toString(),
start = userCreatedAt,
end = (userCreatedAt + 7.days),
data = stats.copy(values = testDailyStats1).right(),
)
.mockGetStatsForRange(
start = (userCreatedAt + 7.days).toString(),
end = (userCreatedAt + 14.days).toString(),
start = (userCreatedAt + 7.days),
end = (userCreatedAt + 14.days),
data = stats.copy(values = testDailyStats2).right(),
)
.mockGetStatsForRange(
start = (userCreatedAt + 14.days).toString(),
end = (userCreatedAt + 21.days).toString(),
start = (userCreatedAt + 14.days),
end = (userCreatedAt + 21.days),
data = stats.copy(values = testDailyStats3).right(),
)
.mockGetStatsForRange(
start = (userCreatedAt + 21.days).toString(),
end = (userCreatedAt + 28.days).toString(),
start = (userCreatedAt + 21.days),
end = (userCreatedAt + 28.days),
data = stats.copy(values = testDailyStats4).right(),
)
.mockGetStatsForRange(
start = (userCreatedAt + 28.days).toString(),
end = currentInstant.toDate().toString(),
start = (userCreatedAt + 28.days),
end = currentInstant.toDate(),
data = stats.copy(values = testDailyStats5).right(),
)
.callUseCase(batchSize = DatePeriod(days = 7))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ internal class RecalculateLatestStreakUCRobot {
}
}

fun mockGetDataForRange(start: String, end: String, data: Either<Error, DailyStatsAggregate>) =
fun mockGetDataForRange(start: LocalDate, end: LocalDate, data: Either<Error, DailyStatsAggregate>) =
apply {
coEvery { mockHomePageNetworkData.getStatsForRange(start, end) } returns data
}

fun verifyGetDataForRange(start: String, end: String, count: Int = 1) = apply {
fun verifyGetDataForRange(start: LocalDate, end: LocalDate, count: Int = 1) = apply {
coVerify(exactly = count) { mockHomePageNetworkData.getStatsForRange(start, end) }
}

Expand Down
Loading

0 comments on commit 74c2c7a

Please sign in to comment.