From d18feccfe4fe66b0f97d79fda978dcc2736f1807 Mon Sep 17 00:00:00 2001
From: MM20 <15646950+MM2-0@users.noreply.github.com>
Date: Sat, 1 Jan 2022 22:43:25 +0100
Subject: [PATCH] Weather settings
---
i18n/src/main/res/values-de/strings.xml | 1 +
i18n/src/main/res/values/strings.xml | 1 +
preferences/src/main/proto/settings.proto | 12 ++
.../launcher2/ui/settings/SettingsActivity.kt | 4 +
.../launcher2/ui/settings/main/MainScreen.kt | 5 +-
.../ui/settings/weather/WeatherScreen.kt | 194 ++++++++++++++++++
.../ui/settings/weather/WeatherScreenVM.kt | 110 ++++++++++
7 files changed, 326 insertions(+), 1 deletion(-)
create mode 100644 ui/src/main/java/de/mm20/launcher2/ui/settings/weather/WeatherScreen.kt
create mode 100644 ui/src/main/java/de/mm20/launcher2/ui/settings/weather/WeatherScreenVM.kt
diff --git a/i18n/src/main/res/values-de/strings.xml b/i18n/src/main/res/values-de/strings.xml
index 9d1109c6..4f49e5ca 100644
--- a/i18n/src/main/res/values-de/strings.xml
+++ b/i18n/src/main/res/values-de/strings.xml
@@ -416,6 +416,7 @@
Eingefärbte Symbole
Symbole an das Farbschema der App anpassen
Keine Wetterdaten verfügbar.
+ Dieser Standort konnte nicht gefunden werden.
Raster
Spaltenanzahl
diff --git a/i18n/src/main/res/values/strings.xml b/i18n/src/main/res/values/strings.xml
index ef2b5308..d4be9850 100644
--- a/i18n/src/main/res/values/strings.xml
+++ b/i18n/src/main/res/values/strings.xml
@@ -446,6 +446,7 @@
Humidity:
Wind:
No weather data available.
+ This location could not be found.
Precipitation:
License
diff --git a/preferences/src/main/proto/settings.proto b/preferences/src/main/proto/settings.proto
index c28d63f2..fba61172 100644
--- a/preferences/src/main/proto/settings.proto
+++ b/preferences/src/main/proto/settings.proto
@@ -80,4 +80,16 @@ message Settings {
}
BadgeSettings badges = 3;
+ message WeatherSettings {
+ enum WeatherProvider {
+ MetNo = 0;
+ OpenWeatherMap = 1;
+ Here = 2;
+ BrightSky = 3;
+ }
+ WeatherProvider provider = 1;
+ bool imperial_units = 2;
+ }
+ WeatherSettings weather = 4;
+
}
\ No newline at end of file
diff --git a/ui/src/main/java/de/mm20/launcher2/ui/settings/SettingsActivity.kt b/ui/src/main/java/de/mm20/launcher2/ui/settings/SettingsActivity.kt
index 6875c62a..2945e4ae 100644
--- a/ui/src/main/java/de/mm20/launcher2/ui/settings/SettingsActivity.kt
+++ b/ui/src/main/java/de/mm20/launcher2/ui/settings/SettingsActivity.kt
@@ -27,6 +27,7 @@ import de.mm20.launcher2.ui.settings.about.AboutScreen
import de.mm20.launcher2.ui.settings.appearance.AppearanceScreen
import de.mm20.launcher2.ui.settings.license.LicenseScreen
import de.mm20.launcher2.ui.settings.main.MainScreen
+import de.mm20.launcher2.ui.settings.weather.WeatherScreen
class SettingsActivity : AppCompatActivity() {
@@ -82,6 +83,9 @@ class SettingsActivity : AppCompatActivity() {
composable("settings/appearance") {
AppearanceScreen()
}
+ composable("settings/weather") {
+ WeatherScreen()
+ }
composable("settings/about") {
AboutScreen()
}
diff --git a/ui/src/main/java/de/mm20/launcher2/ui/settings/main/MainScreen.kt b/ui/src/main/java/de/mm20/launcher2/ui/settings/main/MainScreen.kt
index 48a3e0f7..8ca00212 100644
--- a/ui/src/main/java/de/mm20/launcher2/ui/settings/main/MainScreen.kt
+++ b/ui/src/main/java/de/mm20/launcher2/ui/settings/main/MainScreen.kt
@@ -46,7 +46,10 @@ fun MainScreen() {
Preference(
icon = Icons.Rounded.LightMode,
title = stringResource(id = R.string.preference_screen_weather),
- summary = stringResource(id = R.string.preference_screen_weather_summary)
+ summary = stringResource(id = R.string.preference_screen_weather_summary),
+ onClick = {
+ navController?.navigate("settings/weather")
+ }
)
Preference(
icon = Icons.Rounded.Today,
diff --git a/ui/src/main/java/de/mm20/launcher2/ui/settings/weather/WeatherScreen.kt b/ui/src/main/java/de/mm20/launcher2/ui/settings/weather/WeatherScreen.kt
new file mode 100644
index 00000000..d53f4dde
--- /dev/null
+++ b/ui/src/main/java/de/mm20/launcher2/ui/settings/weather/WeatherScreen.kt
@@ -0,0 +1,194 @@
+package de.mm20.launcher2.ui.settings.weather
+
+import androidx.compose.foundation.clickable
+import androidx.compose.foundation.layout.Column
+import androidx.compose.foundation.layout.fillMaxSize
+import androidx.compose.foundation.layout.fillMaxWidth
+import androidx.compose.foundation.layout.padding
+import androidx.compose.foundation.lazy.LazyColumn
+import androidx.compose.foundation.lazy.items
+import androidx.compose.foundation.shape.RoundedCornerShape
+import androidx.compose.material.CircularProgressIndicator
+import androidx.compose.material.OutlinedTextField
+import androidx.compose.material3.MaterialTheme
+import androidx.compose.material3.Surface
+import androidx.compose.material3.Text
+import androidx.compose.material3.TextButton
+import androidx.compose.runtime.*
+import androidx.compose.runtime.livedata.observeAsState
+import androidx.compose.ui.Alignment
+import androidx.compose.ui.Modifier
+import androidx.compose.ui.res.stringResource
+import androidx.compose.ui.unit.dp
+import androidx.compose.ui.window.Dialog
+import androidx.lifecycle.viewmodel.compose.viewModel
+import de.mm20.launcher2.preferences.Settings.WeatherSettings.WeatherProvider
+import de.mm20.launcher2.ui.R
+import de.mm20.launcher2.ui.component.preferences.*
+import de.mm20.launcher2.weather.WeatherLocation
+import kotlinx.coroutines.launch
+
+@Composable
+fun WeatherScreen() {
+ val viewModel: WeatherScreenVM = viewModel()
+
+ PreferenceScreen(title = stringResource(R.string.preference_screen_weather)) {
+ item {
+ PreferenceCategory {
+ val weatherProvider by viewModel.weatherProvider.observeAsState()
+ ListPreference(
+ title = stringResource(R.string.preference_weather_provider),
+ items = listOf(
+ stringResource(R.string.provider_metno) to WeatherProvider.MetNo,
+ stringResource(R.string.provider_openweathermap) to WeatherProvider.OpenWeatherMap,
+ stringResource(R.string.provider_here) to WeatherProvider.Here,
+ stringResource(R.string.provider_brightsky) to WeatherProvider.BrightSky,
+ ),
+ onValueChanged = {
+ if (it != null) viewModel.setWeatherProvider(it)
+ },
+ value = weatherProvider
+ )
+ val imperialUnits by viewModel.imperialUnits.observeAsState(false)
+ SwitchPreference(
+ title = stringResource(R.string.preference_imperial_units),
+ summary = stringResource(R.string.preference_imperial_units_summary),
+ value = imperialUnits,
+ onValueChanged = {
+ viewModel.setImperialUnits(it)
+ }
+ )
+ }
+ }
+ item {
+ PreferenceCategory(title = stringResource(R.string.preference_category_location)) {
+ val autoLocation by viewModel.autoLocation.observeAsState(false)
+ SwitchPreference(
+ title = stringResource(R.string.preference_automatic_location),
+ summary = stringResource(R.string.preference_automatic_location_summary),
+ value = autoLocation,
+ onValueChanged = {
+ viewModel.setAutoLocation(it)
+ }
+ )
+ val scope = rememberCoroutineScope()
+
+ val location by viewModel.location.observeAsState()
+ val isSearching by viewModel.isSearchingLocation.observeAsState(initial = false)
+ val locations by viewModel.locationResults.observeAsState(emptyList())
+ LocationPreference(
+ title = stringResource(R.string.preference_location),
+ value = location,
+ locations = locations,
+ onValueChanged = {
+ viewModel.setLocation(it)
+ },
+ onLocationSearch = {
+ scope.launch {
+ viewModel.searchLocation(it)
+ }
+ },
+ enabled = !autoLocation,
+ isSearching = isSearching,
+ )
+ }
+ }
+ }
+}
+
+@Composable
+fun LocationPreference(
+ title: String,
+ value: WeatherLocation?,
+ onValueChanged: (WeatherLocation) -> Unit,
+ onLocationSearch: (String) -> Unit,
+ isSearching: Boolean,
+ locations: List,
+ enabled: Boolean = true
+) {
+ var showDialog by remember { mutableStateOf(false) }
+ Preference(
+ title = title,
+ summary = value?.name,
+ enabled = enabled,
+ onClick = {
+ showDialog = true
+ }
+ )
+ if (showDialog) {
+ Dialog(onDismissRequest = { showDialog = false }) {
+ Surface(
+ tonalElevation = 16.dp,
+ shadowElevation = 16.dp,
+ shape = RoundedCornerShape(16.dp),
+ modifier = Modifier
+ .fillMaxSize()
+ .padding(vertical = 16.dp),
+ ) {
+ Column(
+ modifier = Modifier.fillMaxWidth()
+ ) {
+ Text(
+ text = title,
+ style = MaterialTheme.typography.titleLarge,
+ modifier = Modifier
+ .fillMaxWidth()
+ .padding(
+ start = 24.dp, end = 24.dp, top = 16.dp, bottom = 8.dp
+ )
+ )
+ var query by remember { mutableStateOf("") }
+ OutlinedTextField(
+ value = query, onValueChange = {
+ query = it
+ onLocationSearch(it)
+ }, modifier = Modifier
+ .fillMaxWidth()
+ .padding(24.dp)
+ )
+ if (isSearching) {
+ CircularProgressIndicator(
+ modifier = Modifier
+ .align(Alignment.CenterHorizontally)
+ .weight(1f)
+ .padding(vertical = 16.dp)
+ )
+ } else {
+ LazyColumn(
+ modifier = Modifier
+ .fillMaxWidth()
+ .weight(1f)
+ .padding(bottom = 16.dp)
+ ) {
+ items(locations) {
+ Text(
+ text = it.name,
+ modifier = Modifier
+ .clickable {
+ onValueChanged(it)
+ showDialog = false
+ }
+ .padding(
+ horizontal = 24.dp,
+ vertical = 16.dp
+ )
+ )
+ }
+ }
+
+ }
+ TextButton(
+ onClick = { showDialog = false },
+ modifier = Modifier.align(Alignment.End).padding(24.dp)
+ ) {
+ Text(
+ text = stringResource(R.string.close),
+ style = MaterialTheme.typography.labelLarge
+ )
+ }
+ }
+
+ }
+ }
+ }
+}
\ No newline at end of file
diff --git a/ui/src/main/java/de/mm20/launcher2/ui/settings/weather/WeatherScreenVM.kt b/ui/src/main/java/de/mm20/launcher2/ui/settings/weather/WeatherScreenVM.kt
new file mode 100644
index 00000000..914a3532
--- /dev/null
+++ b/ui/src/main/java/de/mm20/launcher2/ui/settings/weather/WeatherScreenVM.kt
@@ -0,0 +1,110 @@
+package de.mm20.launcher2.ui.settings.weather
+
+import android.app.Application
+import android.util.Log
+import androidx.lifecycle.AndroidViewModel
+import androidx.lifecycle.MutableLiveData
+import androidx.lifecycle.asLiveData
+import androidx.lifecycle.viewModelScope
+import de.mm20.launcher2.preferences.LauncherDataStore
+import de.mm20.launcher2.preferences.Settings.WeatherSettings
+import de.mm20.launcher2.weather.WeatherLocation
+import de.mm20.launcher2.weather.WeatherProvider
+import de.mm20.launcher2.weather.brightsky.BrightskyProvider
+import de.mm20.launcher2.weather.here.HereProvider
+import de.mm20.launcher2.weather.metno.MetNoProvider
+import de.mm20.launcher2.weather.openweathermap.OpenWeatherMapProvider
+import kotlinx.coroutines.*
+import kotlinx.coroutines.flow.collectLatest
+import kotlinx.coroutines.flow.map
+import org.koin.core.component.KoinComponent
+import org.koin.core.component.inject
+import kotlin.coroutines.coroutineContext
+
+class WeatherScreenVM(private val context: Application) : AndroidViewModel(context), KoinComponent {
+ val dataStore: LauncherDataStore by inject()
+
+ val weatherProvider = MutableLiveData(null)
+ fun setWeatherProvider(provider: WeatherSettings.WeatherProvider) {
+ viewModelScope.launch {
+ dataStore.updateData {
+ it.toBuilder()
+ .setWeather(it.weather.toBuilder().setProvider(provider))
+ .build()
+ }
+ }
+ }
+
+ val imperialUnits = dataStore.data.map { it.weather.imperialUnits }.asLiveData()
+ fun setImperialUnits(imperialUnits: Boolean) {
+ viewModelScope.launch {
+ dataStore.updateData {
+ it.toBuilder()
+ .setWeather(it.weather.toBuilder().setImperialUnits(imperialUnits))
+ .build()
+ }
+ }
+ }
+
+ private var provider: WeatherProvider? = null
+ set(value) {
+ field = value
+ if (value != null) {
+ val autoLocation = value.autoLocation
+ this.autoLocation.postValue(autoLocation)
+ location.postValue(if (autoLocation) value.getLastLocation() else value.getLocation())
+ }
+ }
+
+ val autoLocation = MutableLiveData(false)
+ fun setAutoLocation(autoLocation: Boolean) {
+ provider?.autoLocation = autoLocation
+ location.postValue(if (autoLocation) provider?.getLastLocation() else provider?.getLocation())
+ this.autoLocation.postValue(autoLocation)
+ }
+
+ val location = MutableLiveData(null)
+ fun setLocation(location: WeatherLocation) {
+ provider?.setLocation(location)
+ this.location.postValue(location)
+ }
+
+ private var debounceSearchJob : Job? = null
+ suspend fun searchLocation(query: String) {
+ debounceSearchJob?.cancelAndJoin()
+ if (query.isBlank()) {
+ locationResults.value = emptyList()
+ isSearchingLocation.value = false
+ return
+ }
+ withContext(coroutineContext) {
+ debounceSearchJob = launch {
+ delay(1000)
+ Log.d("MM20", "Searching for $query")
+ val provider = provider ?: return@launch
+ isSearchingLocation.value = true
+ val results = provider
+ locationResults.value = results.lookupLocation(query)
+ isSearchingLocation.value = false
+ }
+ }
+ }
+
+ val isSearchingLocation = MutableLiveData(false)
+ val locationResults = MutableLiveData>(emptyList())
+
+ init {
+ viewModelScope.launch {
+ dataStore.data.map { it.weather.provider }.collectLatest {
+ weatherProvider.postValue(it)
+ provider = when (it) {
+ WeatherSettings.WeatherProvider.OpenWeatherMap -> OpenWeatherMapProvider(context)
+ WeatherSettings.WeatherProvider.Here -> HereProvider(context)
+ WeatherSettings.WeatherProvider.BrightSky -> BrightskyProvider(context)
+ else -> MetNoProvider(context)
+ }
+ }
+ }
+ }
+
+}
\ No newline at end of file