Skip to content

Commit

Permalink
Use weather logger on barometer settings
Browse files Browse the repository at this point in the history
  • Loading branch information
kylecorry31 committed Jul 20, 2024
1 parent f26e6b8 commit c4265c7
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import com.kylecorry.trail_sense.shared.UserPreferences
import com.kylecorry.trail_sense.shared.sensors.SensorService
import com.kylecorry.trail_sense.tools.weather.domain.RawWeatherObservation
import com.kylecorry.trail_sense.tools.weather.domain.WeatherObservation
import com.kylecorry.trail_sense.tools.weather.infrastructure.WeatherLogger
import com.kylecorry.trail_sense.tools.weather.infrastructure.subsystem.WeatherSubsystem
import java.time.Duration
import java.time.Instant
Expand Down Expand Up @@ -54,6 +55,14 @@ class CalibrateBarometerFragment : AndromedaPreferenceFragment() {

private val runner = CoroutineQueueRunner()

private val logger by lazy {
WeatherLogger(
requireContext(),
Duration.ofSeconds(30),
Duration.ofMillis(500),
)
}

override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
setPreferencesFromResource(R.xml.barometer_calibration, rootKey)

Expand Down Expand Up @@ -186,11 +195,13 @@ class CalibrateBarometerFragment : AndromedaPreferenceFragment() {
override fun onResume() {
super.onResume()
updateTimer.interval(200)
logger.start()
}

override fun onPause() {
super.onPause()
updateTimer.stop()
logger.stop()
}

private fun update() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,23 @@ import java.time.Duration
class WeatherLogger(
context: Context,
private val interval: Duration,
private val intialDelay: Duration = Duration.ZERO,
private val loadingIndicator: ILoadingIndicator
private val initialDelay: Duration = Duration.ZERO,
private val loadingIndicator: ILoadingIndicator? = null
) {
private val weather = WeatherSubsystem.getInstance(context)
private val runner = CoroutineQueueRunner()
private val timer = CoroutineTimer {
onIO {
runner.skipIfRunning {
onMain { loadingIndicator.show() }
onMain { loadingIndicator?.show() }
weather.updateWeather()
onMain { loadingIndicator.hide() }
onMain { loadingIndicator?.hide() }
}
}
}

fun start() {
timer.interval(interval, intialDelay)
timer.interval(interval, initialDelay)
}

fun stop() {
Expand Down

0 comments on commit c4265c7

Please sign in to comment.