Skip to content

Commit

Permalink
Use hooks on weather fragment
Browse files Browse the repository at this point in the history
  • Loading branch information
kylecorry31 committed Apr 5, 2024
1 parent 76b229f commit c25a021
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ class AstronomyFragment : BoundFragment<ActivityAstronomyBinding>() {
updateAstronomyDetails()
}
}

effect("seek_details", currentSeekChartTime, displayDate, isSeeking, location) {
if (!isSeeking) {
return@effect
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import android.view.ViewGroup
import androidx.core.view.isVisible
import com.kylecorry.andromeda.alerts.dialog
import com.kylecorry.andromeda.alerts.toast
import com.kylecorry.andromeda.core.coroutines.onIO
import com.kylecorry.andromeda.core.coroutines.onMain
import com.kylecorry.andromeda.core.system.Resources
import com.kylecorry.andromeda.core.topics.generic.asLiveData
import com.kylecorry.andromeda.core.topics.generic.replay
Expand All @@ -26,10 +28,8 @@ import com.kylecorry.trail_sense.shared.Units
import com.kylecorry.trail_sense.shared.UserPreferences
import com.kylecorry.trail_sense.shared.alerts.ResettableLoadingIndicator
import com.kylecorry.trail_sense.shared.alerts.ViewLoadingIndicator
import com.kylecorry.trail_sense.shared.extensions.getOrNull
import com.kylecorry.trail_sense.shared.debugging.isDebug
import com.kylecorry.andromeda.core.coroutines.onIO
import com.kylecorry.andromeda.core.coroutines.onMain
import com.kylecorry.trail_sense.shared.extensions.getOrNull
import com.kylecorry.trail_sense.shared.permissions.RequestRemoveBatteryRestrictionCommand
import com.kylecorry.trail_sense.tools.weather.domain.CurrentWeather
import com.kylecorry.trail_sense.tools.weather.domain.WeatherObservation
Expand Down Expand Up @@ -66,11 +66,11 @@ class WeatherFragment : BoundFragment<ActivityWeatherBinding>() {

private val formatService by lazy { FormatService.getInstance(requireContext()) }

private var history: List<WeatherObservation> = listOf()
private var rawHistory: List<Reading<Pressure>> = listOf()
private var history by state<List<WeatherObservation>>(emptyList())
private var rawHistory by state<List<Reading<Pressure>>>(emptyList())

private val weatherSubsystem by lazy { WeatherSubsystem.getInstance(requireContext()) }
private var weather: CurrentWeather? = null
private var weather by state<CurrentWeather?>(null)
private val loadingIndicator by lazy {
ResettableLoadingIndicator(
ViewLoadingIndicator(binding.weatherUpdating)
Expand Down Expand Up @@ -136,6 +136,7 @@ class WeatherFragment : BoundFragment<ActivityWeatherBinding>() {
weatherSubsystem.enableMonitor()
RequestRemoveBatteryRestrictionCommand(this).execute()
}

null -> {}
}
}
Expand Down Expand Up @@ -209,8 +210,23 @@ class WeatherFragment : BoundFragment<ActivityWeatherBinding>() {

weather = weatherSubsystem.getWeather()
}
onMain {
update()
}
}

override fun onUpdate() {
super.onUpdate()

effect("chart", history, rawHistory) {
displayPressureChart(history, rawHistory)
}

effect("list", weather) {
updateList()
}

effect("forecast", weather) {
inBackground {
updateForecast()
}
}
}
Expand All @@ -232,15 +248,6 @@ class WeatherFragment : BoundFragment<ActivityWeatherBinding>() {
}
}

private fun update() {
if (!isBound) return
displayPressureChart(history, rawHistory)
updateList()
inBackground {
updateForecast()
}
}

private fun displayPressureChart(
readings: List<WeatherObservation>,
rawReadings: List<Reading<Pressure>>
Expand Down

0 comments on commit c25a021

Please sign in to comment.