Weather settings
This commit is contained in:
parent
1a3fde5bf5
commit
d18feccfe4
@ -416,6 +416,7 @@
|
||||
<string name="preference_themed_icons">Eingefärbte Symbole</string>
|
||||
<string name="preference_themed_icons_summary">Symbole an das Farbschema der App anpassen</string>
|
||||
<string name="weather_no_data">Keine Wetterdaten verfügbar.</string>
|
||||
<string name="weather_location_not_found">Dieser Standort konnte nicht gefunden werden.</string>
|
||||
|
||||
<string name="preference_category_grid">Raster</string>
|
||||
<string name="preference_grid_column_count">Spaltenanzahl</string>
|
||||
|
||||
@ -446,6 +446,7 @@
|
||||
<string name="weather_humidity">Humidity:</string>
|
||||
<string name="weather_wind">Wind:</string>
|
||||
<string name="weather_no_data">No weather data available.</string>
|
||||
<string name="weather_location_not_found">This location could not be found.</string>
|
||||
|
||||
<string name="weather_precipitation">Precipitation:</string>
|
||||
<string name="preference_category_license">License</string>
|
||||
|
||||
@ -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;
|
||||
|
||||
}
|
||||
@ -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()
|
||||
}
|
||||
|
||||
@ -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,
|
||||
|
||||
@ -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<WeatherLocation>,
|
||||
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
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -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<WeatherSettings.WeatherProvider?>(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<out WeatherLocation>? = 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<WeatherLocation?>(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<List<WeatherLocation>>(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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user