Show a warning in weather settings if location permission is missing

This commit is contained in:
MM20 2022-01-03 22:36:33 +01:00
parent 60b9b7ecd9
commit 497625a9e6
No known key found for this signature in database
GPG Key ID: 0B61A8F2DEAFA389
5 changed files with 37 additions and 2 deletions

View File

@ -420,4 +420,7 @@
<string name="preference_category_grid">Raster</string> <string name="preference_category_grid">Raster</string>
<string name="preference_grid_column_count">Spaltenanzahl</string> <string name="preference_grid_column_count">Spaltenanzahl</string>
<string name="grant_permission">Gewähren</string>
<string name="missing_permission_auto_location">Standortzugriff wird benötigt, um den Standort automatisch zu ermitteln</string>
</resources> </resources>

View File

@ -458,4 +458,7 @@
<string name="preference_category_grid">Grid</string> <string name="preference_category_grid">Grid</string>
<string name="preference_grid_column_count">Number of columns</string> <string name="preference_grid_column_count">Number of columns</string>
<string name="grant_permission">Grant</string>
<string name="missing_permission_auto_location">Location access is required to determine the location automatically</string>
</resources> </resources>

View File

@ -11,7 +11,9 @@ import androidx.compose.material3.*
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import de.mm20.launcher2.ui.R
@Composable @Composable
fun MissingPermissionBanner( fun MissingPermissionBanner(
@ -53,7 +55,7 @@ fun MissingPermissionBanner(
TextButton( TextButton(
modifier = Modifier.padding(start = 8.dp), modifier = Modifier.padding(start = 8.dp),
onClick = onClick) { onClick = onClick) {
Text("Grant", style = MaterialTheme.typography.labelLarge) Text(stringResource(R.string.grant_permission), style = MaterialTheme.typography.labelLarge)
} }
} }

View File

@ -1,5 +1,7 @@
package de.mm20.launcher2.ui.settings.weather package de.mm20.launcher2.ui.settings.weather
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.foundation.clickable import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxSize
@ -18,12 +20,14 @@ import androidx.compose.runtime.*
import androidx.compose.runtime.livedata.observeAsState import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.ui.Alignment import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Dialog import androidx.compose.ui.window.Dialog
import androidx.lifecycle.viewmodel.compose.viewModel import androidx.lifecycle.viewmodel.compose.viewModel
import de.mm20.launcher2.preferences.Settings.WeatherSettings.WeatherProvider import de.mm20.launcher2.preferences.Settings.WeatherSettings.WeatherProvider
import de.mm20.launcher2.ui.R import de.mm20.launcher2.ui.R
import de.mm20.launcher2.ui.component.MissingPermissionBanner
import de.mm20.launcher2.ui.component.preferences.* import de.mm20.launcher2.ui.component.preferences.*
import de.mm20.launcher2.weather.WeatherLocation import de.mm20.launcher2.weather.WeatherLocation
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
@ -31,6 +35,7 @@ import kotlinx.coroutines.launch
@Composable @Composable
fun WeatherScreen() { fun WeatherScreen() {
val viewModel: WeatherScreenVM = viewModel() val viewModel: WeatherScreenVM = viewModel()
val context = LocalContext.current
PreferenceScreen(title = stringResource(R.string.preference_screen_weather)) { PreferenceScreen(title = stringResource(R.string.preference_screen_weather)) {
item { item {
@ -62,6 +67,16 @@ fun WeatherScreen() {
} }
item { item {
PreferenceCategory(title = stringResource(R.string.preference_category_location)) { PreferenceCategory(title = stringResource(R.string.preference_category_location)) {
val hasPermission by viewModel.hasLocationPermission.observeAsState()
AnimatedVisibility(hasPermission == false) {
MissingPermissionBanner(
text = stringResource(R.string.missing_permission_auto_location),
onClick = {
viewModel.requestLocationPermission(context as AppCompatActivity)
},
modifier = Modifier.padding(16.dp)
)
}
val autoLocation by viewModel.autoLocation.observeAsState(false) val autoLocation by viewModel.autoLocation.observeAsState(false)
SwitchPreference( SwitchPreference(
title = stringResource(R.string.preference_automatic_location), title = stringResource(R.string.preference_automatic_location),
@ -181,7 +196,9 @@ fun LocationPreference(
} }
TextButton( TextButton(
onClick = { showDialog = false }, onClick = { showDialog = false },
modifier = Modifier.align(Alignment.End).padding(24.dp) modifier = Modifier
.align(Alignment.End)
.padding(24.dp)
) { ) {
Text( Text(
text = stringResource(R.string.close), text = stringResource(R.string.close),

View File

@ -1,9 +1,12 @@
package de.mm20.launcher2.ui.settings.weather package de.mm20.launcher2.ui.settings.weather
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.MutableLiveData import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModel
import androidx.lifecycle.asLiveData import androidx.lifecycle.asLiveData
import androidx.lifecycle.viewModelScope import androidx.lifecycle.viewModelScope
import de.mm20.launcher2.permissions.PermissionGroup
import de.mm20.launcher2.permissions.PermissionsManager
import de.mm20.launcher2.preferences.LauncherDataStore import de.mm20.launcher2.preferences.LauncherDataStore
import de.mm20.launcher2.preferences.Settings.WeatherSettings import de.mm20.launcher2.preferences.Settings.WeatherSettings
import de.mm20.launcher2.weather.WeatherLocation import de.mm20.launcher2.weather.WeatherLocation
@ -19,6 +22,7 @@ import kotlin.coroutines.coroutineContext
class WeatherScreenVM : ViewModel(), KoinComponent { class WeatherScreenVM : ViewModel(), KoinComponent {
private val repository: WeatherRepository by inject() private val repository: WeatherRepository by inject()
private val dataStore: LauncherDataStore by inject() private val dataStore: LauncherDataStore by inject()
private val permissionsManager: PermissionsManager by inject()
val imperialUnits = dataStore.data.map { it.weather.imperialUnits }.asLiveData() val imperialUnits = dataStore.data.map { it.weather.imperialUnits }.asLiveData()
fun setImperialUnits(imperialUnits: Boolean) { fun setImperialUnits(imperialUnits: Boolean) {
@ -65,6 +69,12 @@ class WeatherScreenVM : ViewModel(), KoinComponent {
} }
} }
val hasLocationPermission = permissionsManager.hasPermission(PermissionGroup.Location).asLiveData()
fun requestLocationPermission(activity: AppCompatActivity) {
permissionsManager.requestPermission(activity, PermissionGroup.Location)
}
val isSearchingLocation = MutableLiveData(false) val isSearchingLocation = MutableLiveData(false)
val locationResults = MutableLiveData<List<WeatherLocation>>(emptyList()) val locationResults = MutableLiveData<List<WeatherLocation>>(emptyList())