134 lines
3.3 KiB
Kotlin
134 lines
3.3 KiB
Kotlin
package com.module.breeze
|
|
|
|
import retrofit2.Retrofit
|
|
import retrofit2.converter.gson.GsonConverterFactory
|
|
import retrofit2.http.GET
|
|
import retrofit2.http.Query
|
|
import okhttp3.OkHttpClient
|
|
import okhttp3.logging.HttpLoggingInterceptor
|
|
import java.util.Date
|
|
|
|
// Whole File DeepSeek
|
|
// Data classes for API responses
|
|
data class WeatherResponse(
|
|
val main: Main,
|
|
val weather: List<Weather>,
|
|
val dt: Long
|
|
)
|
|
|
|
data class Main(
|
|
val temp: Double,
|
|
val feels_like: Double,
|
|
val temp_min: Double,
|
|
val temp_max: Double,
|
|
val pressure: Int,
|
|
val humidity: Int
|
|
)
|
|
|
|
data class Weather(
|
|
val id: Int,
|
|
val main: String,
|
|
val description: String,
|
|
val icon: String
|
|
)
|
|
|
|
data class ForecastResponse(
|
|
val list: List<Forecast>
|
|
)
|
|
|
|
data class Forecast(
|
|
val dt: Long,
|
|
val main: Main,
|
|
val weather: List<Weather>,
|
|
val wind: Wind,
|
|
val rain: Rain?,
|
|
val snow: Snow?
|
|
)
|
|
|
|
data class Wind(
|
|
val speed: Double,
|
|
val deg: Int
|
|
)
|
|
|
|
data class Rain(
|
|
val `3h`: Double?
|
|
)
|
|
|
|
data class Snow(
|
|
val `3h`: Double?
|
|
)
|
|
|
|
// Retrofit API interface
|
|
interface WeatherApiService {
|
|
@GET("weather")
|
|
suspend fun getCurrentWeather(
|
|
@Query("lat") lat: Double,
|
|
@Query("lon") lon: Double,
|
|
@Query("appid") apiKey: String,
|
|
@Query("units") units: String = "metric"
|
|
): WeatherResponse
|
|
|
|
@GET("forecast")
|
|
suspend fun getWeatherForecast(
|
|
@Query("zip") zip: String,
|
|
@Query("appid") apiKey: String,
|
|
@Query("units") units: String = "metric"
|
|
): ForecastResponse
|
|
}
|
|
|
|
// Retrofit client setup
|
|
object RetrofitClient {
|
|
private const val BASE_URL = "https://api.openweathermap.org/data/2.5/"
|
|
|
|
private val logging = HttpLoggingInterceptor().apply {
|
|
level = HttpLoggingInterceptor.Level.BODY
|
|
}
|
|
|
|
private val httpClient = OkHttpClient.Builder()
|
|
.addInterceptor(logging)
|
|
.build()
|
|
|
|
val instance: WeatherApiService by lazy {
|
|
Retrofit.Builder()
|
|
.baseUrl(BASE_URL)
|
|
.client(httpClient)
|
|
.addConverterFactory(GsonConverterFactory.create())
|
|
.build()
|
|
.create(WeatherApiService::class.java)
|
|
}
|
|
}
|
|
|
|
// Helper function to filter today's forecast
|
|
fun getTodaysForecast(forecastResponse: ForecastResponse): ForecastSummary {
|
|
val today = Date().time / 1000 // Current timestamp in seconds
|
|
val tomorrow = today + 86400 // 24 hours later
|
|
|
|
// Filter forecasts for today
|
|
val todaysForecasts = forecastResponse.list.filter { it.dt in today..tomorrow }
|
|
|
|
// Extract min and max temperatures
|
|
val minTemp = todaysForecasts.minOfOrNull { it.main.temp_min } ?: 0.0
|
|
val maxTemp = todaysForecasts.maxOfOrNull { it.main.temp_max } ?: 0.0
|
|
|
|
// Check for rain, snow, or strong winds
|
|
val hasRain = todaysForecasts.any { it.rain != null }
|
|
val hasSnow = todaysForecasts.any { it.snow != null }
|
|
val hasStrongWinds = todaysForecasts.any { it.wind.speed > 10.0 } // Wind speed > 10 m/s
|
|
|
|
return ForecastSummary(
|
|
minTemp = minTemp,
|
|
maxTemp = maxTemp,
|
|
hasRain = hasRain,
|
|
hasSnow = hasSnow,
|
|
hasStrongWinds = hasStrongWinds
|
|
)
|
|
}
|
|
|
|
// Data class for today's forecast summary
|
|
data class ForecastSummary(
|
|
val minTemp: Double,
|
|
val maxTemp: Double,
|
|
val hasRain: Boolean,
|
|
val hasSnow: Boolean,
|
|
val hasStrongWinds: Boolean
|
|
) |