keep past hour in the forecast

This commit is contained in:
PranshulGG committed 2026-08-12 22:02:39 +05:30
1 parent 794ccfca76
commit 47848a5fa2
14 files changed
+19 -27

No files matched your search

@@ -35,15 +35,14 @@ fun findMatchingHourly(
currentMilli: Long,
source: WeatherSource,
zoneId: String,
alwaysReturn24Hrs: Boolean = false
alwaysReturn24Hrs: Boolean = false,
keepPastHour: Boolean = false
): List<WeatherHourly> {
val startIndex = data.indexOfFirst { it.time >= currentMilli }.minus(1)
val startIndex = data.indexOfFirst { it.time >= currentMilli }.takeIf { it >= 0 }
?.minus(if (keepPastHour) 1 else 0) ?: return emptyList()
if (startIndex == -1) {
return emptyList()
}
val startDay = data[startIndex].time
return if (alwaysReturn24Hrs) {
@@ -164,7 +164,7 @@ private fun getHourlyConditionsForDay(
time: Long
): List<WeatherCondition> {
val startIndex =
data.indexOfFirst { it.datetime.iso8601TimestampToMilliseconds() >= time }.minus(1)
data.indexOfFirst { it.datetime.iso8601TimestampToMilliseconds() >= time }
.takeIf { it != -1 } ?: 0
@@ -174,7 +174,7 @@ private fun getHourlyConditionsForDay(
time: Long
): List<WeatherCondition> {
val startIndex =
data.indexOfFirst { it.timestamp.iso8601TimestampToMilliseconds() >= time }.minus(1)
data.indexOfFirst { it.timestamp.iso8601TimestampToMilliseconds() >= time }
.takeIf { it != -1 } ?: 0
@@ -159,8 +159,7 @@ private fun getMaxPrecipitationProbability(
time: Long
): Int? {
val startIndex =
hourly.indexOfFirst { it.epochTime.secondsToMilliseconds() >= time }.minus(1)
.takeIf { it != -1 }
hourly.indexOfFirst { it.epochTime.secondsToMilliseconds() >= time }.takeIf { it != -1 }
?: 0
@@ -282,7 +282,7 @@ private fun getHourlyConditionsForDay(
time: Long
): List<WeatherCondition> {
val startIndex =
data.indexOfFirst { it.time!!.iso8601TimestampToMilliseconds() >= time }.minus(1)
data.indexOfFirst { it.time!!.iso8601TimestampToMilliseconds() >= time }
.takeIf { it != -1 } ?: 0
@@ -150,7 +150,6 @@ fun GismeteoModel.toDomain(location: Location): Weather {
private fun hourlyForDay(data: List<GismeteoModelHourly>, time: Long): List<GismeteoModelHourly> {
val startIndex =
data.indexOfFirst { fixTimeString(it.time).iso8601TimestampToMilliseconds() >= time }
.minus(1)
.takeIf { it != -1 }
?: 0
@@ -219,7 +219,7 @@ private fun getHourlyConditionsForDay(
time: Long
): List<WeatherCondition> {
val startIndex =
data.indexOfFirst { it.time.iso8601TimestampToMilliseconds() >= time }.minus(1)
data.indexOfFirst { it.time.iso8601TimestampToMilliseconds() >= time }
.takeIf { it != -1 } ?: 0
@@ -181,7 +181,7 @@ private fun getHourlyConditionsForDay(
time: Long
): List<WeatherCondition> {
val startIndex =
data.indexOfFirst { it.time >= time }.minus(1)
data.indexOfFirst { it.time >= time }
.takeIf { it != -1 } ?: 0
@@ -199,7 +199,7 @@ private fun getHourlyConditionsForDay(
time: Long
): List<WeatherCondition> {
val startIndex =
data.indexOfFirst { it.time.iso8601TimestampToMilliseconds() >= time }.minus(1)
data.indexOfFirst { it.time.iso8601TimestampToMilliseconds() >= time }
.takeIf { it != -1 } ?: 0
@@ -397,7 +397,7 @@ private fun getHourlyConditionsForDay(
time: Long
): List<WeatherCondition> {
val startIndex =
data.periods.indexOfFirst { it.startTime.iso8601TimestampToMilliseconds() >= time }.minus(1)
data.periods.indexOfFirst { it.startTime.iso8601TimestampToMilliseconds() >= time }
.takeIf { it != -1 } ?: 0
val conditions = data.periods.drop(maxOf(0, startIndex - 1))
@@ -127,7 +127,6 @@ private fun getHourlyConditionsForDay(
val startIndex =
data.time.indexOfFirst { it.secondsToMilliseconds() >= time.secondsToMilliseconds() }
.minus(1)
.takeIf { it != -1 } ?: 0
val conditions = data.weatherCode.drop(maxOf(0, startIndex)).take(24)
@@ -190,7 +190,7 @@ private fun getHourlyConditionsForDay(
time: Long
): List<WeatherCondition> {
val startIndex =
data.indexOfFirst { it.time.iso8601TimestampToMilliseconds() >= time }.minus(1)
data.indexOfFirst { it.time.iso8601TimestampToMilliseconds() >= time }
.takeIf { it != -1 } ?: 0
@@ -111,7 +111,8 @@ fun DailyScreen(navController: NavController, index: Int = 0, locationId: String
HourlyCard(
weather,
units,
if (selectedIndex != 0) selectedDaily.time else weather.current.time
if (selectedIndex != 0) selectedDaily.time else weather.current.time,
isDaily = true
)
WeatherBlocks(
weather,
@@ -1,6 +1,5 @@
package com.pranshulgg.weather_master_app.feature.shared.ui
import android.util.Log
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
@@ -9,7 +8,6 @@ import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyRow
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.material3.ExperimentalMaterial3ExpressiveApi
@@ -34,8 +32,6 @@ import com.pranshulgg.weather_master_app.core.prefs.LocalAppPrefs
import com.pranshulgg.weather_master_app.core.ui.components.Gap
import com.pranshulgg.weather_master_app.core.ui.components.WeatherIconBox
import com.pranshulgg.weather_master_app.core.ui.theme.ShadowElevation
import com.pranshulgg.weather_master_app.core.utils.extensions.DateTimeExtensions.secondsToMilliseconds
import com.pranshulgg.weather_master_app.core.utils.formatters.safeZoneId
import com.pranshulgg.weather_master_app.core.utils.formatters.to12HourTimeString
import com.pranshulgg.weather_master_app.core.utils.formatters.to24HourTimeString
import com.pranshulgg.weather_master_app.core.utils.weather.cache.isWeatherHourlyDomainSafe
@@ -44,16 +40,14 @@ import com.pranshulgg.weather_master_app.core.utils.weather.forecast.findMatchin
import com.pranshulgg.weather_master_app.core.utils.weather.forecast.isSameDay
import com.pranshulgg.weather_master_app.feature.blocks.components.NoHourlyDataAvailable
import com.pranshulgg.weather_master_app.feature.shared.components.CardsHeader
import java.time.ZoneId
import java.time.ZonedDateTime
import kotlin.math.max
import kotlin.math.roundToInt
@Composable
fun HourlyCard(
weather: Weather,
units: WeatherUnits,
currentMilli: Long = weather.current.time
currentMilli: Long = weather.current.time,
isDaily: Boolean = false
) {
if (!isWeatherHourlyDomainSafe(weather)) return
@@ -66,7 +60,8 @@ fun HourlyCard(
currentMilli,
weather.location.source,
weather.location.timezone,
alwaysReturn24Hrs = true
alwaysReturn24Hrs = true,
keepPastHour = !isDaily
)