Skip to content

Commit

Permalink
Adding defaultClock function. (#320)
Browse files Browse the repository at this point in the history
  • Loading branch information
AdamMc331 committed Jul 19, 2023
1 parent b41b752 commit 09ec276
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ object DateUtils {
*/
fun isBeforeNow(
utcString: String,
clock: Clock = Clock.System,
clock: Clock = defaultClock(),
): Boolean {
val instant = Instant.parse(utcString)

Expand All @@ -30,7 +30,7 @@ object DateUtils {
*/
fun getRelativeTimestamp(
utcString: String,
clock: Clock = Clock.System,
clock: Clock = defaultClock(),
): String {
require(isBeforeNow(utcString, clock)) {
"getRelativeTimestamp only valid for past dates."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import kotlinx.datetime.Instant
* Allows us to test the app at any given point in time.
*/
class DebugClock(
private val dateString: String = "2023-07-08T16:00:00Z",
private val dateString: String = "2023-03-02T16:00:00Z",
) : Clock {

override fun now(): Instant {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.adammcneilly.pocketleague.core.datetime

import kotlinx.datetime.Clock

/**
* Provide the default [Clock] instance to use everywhere in the app.
*
* Utility function makes this easy to swap in a [DebugClock] when we want
* to test.
*/
fun defaultClock(): Clock = Clock.System
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.adammcneilly.pocketleague.data.event

import com.adammcneilly.pocketleague.core.datetime.defaultClock
import com.adammcneilly.pocketleague.core.models.Event
import com.adammcneilly.pocketleague.core.models.Team
import com.adammcneilly.pocketleague.data.octanegg.OctaneGGAPIClient
Expand All @@ -20,7 +21,7 @@ class OctaneGGEventService(
private val clock: Clock,
) : RemoteEventService {

constructor() : this(OctaneGGAPIClient, Clock.System)
constructor() : this(OctaneGGAPIClient, defaultClock())

override suspend fun getUpcomingEvents(): Result<List<Event>> {
return apiClient.getResponse<OctaneGGEventListResponse>(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.adammcneilly.pocketleague.data.match

import com.adammcneilly.pocketleague.core.datetime.defaultClock
import com.adammcneilly.pocketleague.core.models.Match
import com.adammcneilly.pocketleague.data.octanegg.OctaneGGAPIClient
import com.adammcneilly.pocketleague.data.octanegg.models.OctaneGGMatch
Expand All @@ -18,7 +19,7 @@ class OctaneGGMatchService(
private val clock: Clock,
) : RemoteMatchService {

constructor() : this(OctaneGGAPIClient, Clock.System)
constructor() : this(OctaneGGAPIClient, defaultClock())

override suspend fun getMatchDetail(matchId: String): Result<Match> {
return apiClient.getResponse<OctaneGGMatch>(
Expand Down

0 comments on commit 09ec276

Please sign in to comment.