Add managed location to weather plugin API

This commit is contained in:
MM20
2024-05-03 13:02:17 +02:00
parent b0f9ffd473
commit 1d6688831c
19 changed files with 256 additions and 100 deletions
@@ -6,5 +6,6 @@ import de.mm20.launcher2.plugin.config.WeatherPluginConfig
internal fun WeatherPluginConfig.toBundle(): Bundle {
return Bundle().apply {
putLong("minUpdateInterval", minUpdateInterval)
putBoolean("managedLocation", managedLocation)
}
}
@@ -7,4 +7,8 @@ sealed interface WeatherLocation {
data class LatLon(override val name: String, val lat: Double, val lon: Double) :
WeatherLocation
data object Managed: WeatherLocation {
override val name: String = ""
}
}
@@ -107,6 +107,9 @@ abstract class WeatherProvider(
if (locationName != null && lat != null && lon != null) {
return getWeatherData(WeatherLocation.LatLon(locationName, lat, lon), lang)
}
if (lat == null && lon == null && id == null) {
return getWeatherData(WeatherLocation.Managed, lang)
}
return null
}
@@ -226,6 +229,9 @@ abstract class WeatherProvider(
*/
open suspend fun findLocations(query: String, lang: String): List<WeatherLocation> {
val context = context ?: return emptyList()
if (config.managedLocation) {
return listOf(WeatherLocation.Managed)
}
val parts = query.split(" ", limit = 3)
val lat = parts.getOrNull(0)?.toDoubleOrNull()
val lon = parts.getOrNull(1)?.toDoubleOrNull()