Add Location, WeatherForecastData, Searchable and PreviewableItem models

This commit is contained in:
Nebojsa Vuksic 2025-07-23 14:53:39 +02:00
parent 9a6945d9e1
commit 6b1deab598
4 changed files with 109 additions and 0 deletions

View File

@ -0,0 +1,34 @@
package org.jetbrains.plugins.template.weatherApp.model
import androidx.compose.runtime.Immutable
/**
* Represents a location with a name and associated country.
*
* @property name The name of the location.
* @property country The associated country of the location.
* @property id A derived unique identifier for the location in the format `name, country`.
*/
@Immutable
internal data class Location(val name: String, val country: String) : PreviewableItem, Searchable {
val id: String = "$name, $country"
override fun isSearchApplicable(query: String): Boolean {
val applicableCandidates = listOf(
id,
name,
country,
name.split(" ").map { it.first() }.joinToString(""),
"${name.first()}${country.first()}",
"${country.first()}${name.first()}"
)
return applicableCandidates.any { it.contains(query, ignoreCase = true) }
}
override val label: String
get() = id
}
@Immutable
internal data class SelectableLocation(val location: Location, val isSelected: Boolean)

View File

@ -0,0 +1,5 @@
package org.jetbrains.plugins.template.weatherApp.model
interface PreviewableItem {
val label: String
}

View File

@ -0,0 +1,8 @@
package org.jetbrains.plugins.template.weatherApp.model
/**
* Represents an entity that can be filtered by a search query.
*/
internal interface Searchable {
fun isSearchApplicable(query: String): Boolean
}

View File

@ -0,0 +1,62 @@
package org.jetbrains.plugins.template.weatherApp.model
import java.time.LocalDateTime
/**
* Data class representing weather information to be displayed in the Weather Card.
*/
data class WeatherForecastData(
val cityName: String,
val temperature: Float,
val currentTime: LocalDateTime,
val windSpeed: Float,
val windDirection: WindDirection,
val humidity: Int, // Percentage
val weatherType: WeatherType
) {
companion object Companion {
val EMPTY: WeatherForecastData = WeatherForecastData(
"",
0f,
LocalDateTime.now(),
0f,
WindDirection.NORTH,
0,
WeatherType.SUNNY
)
}
}
/**
* Enum representing different weather types.
*/
enum class WeatherType(val label: String) {
SUNNY("Sunny"),
CLOUDY("Cloudy"),
PARTLY_CLOUDY("Partly Cloudy"),
RAINY("Rainy"),
SNOWY("Snowy"),
STORMY("Stormy");
companion object {
fun random(): WeatherType = entries.toTypedArray().random()
}
}
/**
* Enum representing wind directions.
*/
enum class WindDirection(val label: String) {
NORTH("N"),
NORTH_EAST("NE"),
EAST("E"),
SOUTH_EAST("SE"),
SOUTH("S"),
SOUTH_WEST("SW"),
WEST("W"),
NORTH_WEST("NW");
companion object {
fun random(): WindDirection = entries.toTypedArray().random()
}
}