From 64a5efab1bfd7aed573db9f2df0f7104b287e7f3 Mon Sep 17 00:00:00 2001 From: Kyle Corry Date: Fri, 13 Oct 2023 13:39:49 -0400 Subject: [PATCH] Use sol Time readings helpers --- .../astronomy/domain/AstronomyService.kt | 10 ++--- .../shared/extensions/TimeExtensions.kt | 43 ------------------- .../tools/tides/domain/TideService.kt | 3 +- .../temperatures/HistoricTemperatureRepo.kt | 3 +- 4 files changed, 7 insertions(+), 52 deletions(-) delete mode 100644 app/src/main/java/com/kylecorry/trail_sense/shared/extensions/TimeExtensions.kt diff --git a/app/src/main/java/com/kylecorry/trail_sense/astronomy/domain/AstronomyService.kt b/app/src/main/java/com/kylecorry/trail_sense/astronomy/domain/AstronomyService.kt index 182292dbe..ef7155256 100644 --- a/app/src/main/java/com/kylecorry/trail_sense/astronomy/domain/AstronomyService.kt +++ b/app/src/main/java/com/kylecorry/trail_sense/astronomy/domain/AstronomyService.kt @@ -8,11 +8,11 @@ import com.kylecorry.sol.science.astronomy.meteors.MeteorShowerPeak import com.kylecorry.sol.science.astronomy.moon.MoonPhase import com.kylecorry.sol.science.astronomy.moon.MoonTruePhase import com.kylecorry.sol.science.shared.Season +import com.kylecorry.sol.time.Time import com.kylecorry.sol.time.Time.toZonedDateTime import com.kylecorry.sol.units.Bearing import com.kylecorry.sol.units.Coordinate import com.kylecorry.sol.units.Reading -import com.kylecorry.trail_sense.shared.extensions.getReadings import java.time.Clock import java.time.Duration import java.time.LocalDate @@ -54,7 +54,7 @@ class AstronomyService(private val clock: Clock = Clock.systemDefaultZone()) { ): List> { val startTime = time.minusHours(12) val endTime = time.plusHours(12) - return getReadings( + return Time.getReadings( startTime, endTime, altitudeGranularity @@ -64,7 +64,7 @@ class AstronomyService(private val clock: Clock = Clock.systemDefaultZone()) { } fun getMoonAltitudes(location: Coordinate, date: LocalDate): List> { - return getReadings( + return Time.getReadings( date, ZoneId.systemDefault(), altitudeGranularity @@ -122,7 +122,7 @@ class AstronomyService(private val clock: Clock = Clock.systemDefaultZone()) { } fun getSunAltitudes(location: Coordinate, date: LocalDate): List> { - return getReadings( + return Time.getReadings( date, ZoneId.systemDefault(), altitudeGranularity @@ -137,7 +137,7 @@ class AstronomyService(private val clock: Clock = Clock.systemDefaultZone()) { ): List> { val startTime = time.minusHours(12) val endTime = time.plusHours(12) - return getReadings( + return Time.getReadings( startTime, endTime, altitudeGranularity diff --git a/app/src/main/java/com/kylecorry/trail_sense/shared/extensions/TimeExtensions.kt b/app/src/main/java/com/kylecorry/trail_sense/shared/extensions/TimeExtensions.kt deleted file mode 100644 index 2b08708c8..000000000 --- a/app/src/main/java/com/kylecorry/trail_sense/shared/extensions/TimeExtensions.kt +++ /dev/null @@ -1,43 +0,0 @@ -package com.kylecorry.trail_sense.shared.extensions - -import com.kylecorry.sol.time.Time.atEndOfDay -import com.kylecorry.sol.units.Reading -import java.time.Duration -import java.time.LocalDate -import java.time.ZoneId -import java.time.ZonedDateTime - -fun getReadings( - date: LocalDate, - zone: ZoneId, - step: Duration, - valueFn: (time: ZonedDateTime) -> T -): List> { - return getReadings( - date.atStartOfDay().atZone(zone), - date.atEndOfDay().atZone(zone), - step, - valueFn - ) -} - -inline fun getReadings( - start: ZonedDateTime, - end: ZonedDateTime, - step: Duration, - valueFn: (time: ZonedDateTime) -> T -): List> { - val readings = mutableListOf>() - var time = start - while (time <= end) { - readings.add(Reading(valueFn(time), time.toInstant())) - time = time.plus(step) - } - return readings -} - -fun List>.duration(): Duration { - val start = minByOrNull { it.time } ?: return Duration.ZERO - val end = maxByOrNull { it.time } ?: return Duration.ZERO - return Duration.between(start.time, end.time) -} \ No newline at end of file diff --git a/app/src/main/java/com/kylecorry/trail_sense/tools/tides/domain/TideService.kt b/app/src/main/java/com/kylecorry/trail_sense/tools/tides/domain/TideService.kt index ef08e3720..80d689431 100644 --- a/app/src/main/java/com/kylecorry/trail_sense/tools/tides/domain/TideService.kt +++ b/app/src/main/java/com/kylecorry/trail_sense/tools/tides/domain/TideService.kt @@ -7,7 +7,6 @@ import com.kylecorry.sol.science.oceanography.Tide import com.kylecorry.sol.science.oceanography.TideType import com.kylecorry.sol.time.Time import com.kylecorry.sol.units.Reading -import com.kylecorry.trail_sense.shared.extensions.getReadings import com.kylecorry.trail_sense.tools.tides.domain.range.TideTableRangeCalculator import com.kylecorry.trail_sense.tools.tides.domain.waterlevel.TideTableWaterLevelCalculator import java.time.* @@ -36,7 +35,7 @@ class TideService : ITideService { } override fun getWaterLevels(table: TideTable, date: LocalDate): List> { - return getReadings( + return Time.getReadings( date, ZoneId.systemDefault(), Duration.ofMinutes(10) diff --git a/app/src/main/java/com/kylecorry/trail_sense/weather/infrastructure/temperatures/HistoricTemperatureRepo.kt b/app/src/main/java/com/kylecorry/trail_sense/weather/infrastructure/temperatures/HistoricTemperatureRepo.kt index f1a57a25c..56a5245a6 100644 --- a/app/src/main/java/com/kylecorry/trail_sense/weather/infrastructure/temperatures/HistoricTemperatureRepo.kt +++ b/app/src/main/java/com/kylecorry/trail_sense/weather/infrastructure/temperatures/HistoricTemperatureRepo.kt @@ -8,7 +8,6 @@ import com.kylecorry.sol.time.Time.daysUntil import com.kylecorry.sol.units.Coordinate import com.kylecorry.sol.units.Reading import com.kylecorry.sol.units.Temperature -import com.kylecorry.trail_sense.shared.extensions.getReadings import com.kylecorry.trail_sense.shared.extensions.onDefault import com.kylecorry.trail_sense.weather.infrastructure.temperatures.calculators.DailyTemperatureCalculator import java.time.Duration @@ -42,7 +41,7 @@ internal class HistoricTemperatureRepo(private val context: Context) : ITemperat ): List> { val calculator = DailyTemperatureCalculator(location, this::getDailyRange) - return getReadings(start, end, Duration.ofMinutes(10)) { + return Time.getReadings(start, end, Duration.ofMinutes(10)) { calculator.calculate(it) } }