Show missing permission banner in weather widget if there's no weather data and auto location is enabled

This commit is contained in:
MM20 2022-01-03 22:59:19 +01:00
parent c5803b7e75
commit e7b4a05685
No known key found for this signature in database
GPG Key ID: 0B61A8F2DEAFA389
2 changed files with 33 additions and 2 deletions

View File

@ -4,6 +4,7 @@ import android.content.Context
import android.content.Intent
import android.net.Uri
import android.text.format.DateUtils
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.ExperimentalAnimationApi
import androidx.compose.foundation.clickable
@ -28,6 +29,7 @@ import androidx.compose.ui.unit.sp
import androidx.lifecycle.viewmodel.compose.viewModel
import de.mm20.launcher2.ktx.tryStartActivity
import de.mm20.launcher2.ui.R
import de.mm20.launcher2.ui.component.MissingPermissionBanner
import de.mm20.launcher2.ui.weather.AnimatedWeatherIcon
import de.mm20.launcher2.ui.weather.WeatherIcon
import de.mm20.launcher2.weather.DailyForecast
@ -42,11 +44,23 @@ import kotlin.math.roundToInt
fun WeatherWidget() {
val viewModel: WeatherWidgetWM = viewModel()
val context = LocalContext.current
val selectedForecast by viewModel.currentForecast.observeAsState()
val imperialUnits by viewModel.imperialUnits.observeAsState(false)
val forecast = selectedForecast ?: run {
val hasPermission by viewModel.hasLocationPermission.observeAsState()
val autoLocation by viewModel.autoLocation.observeAsState()
AnimatedVisibility(hasPermission == false && autoLocation == true) {
MissingPermissionBanner(
modifier = Modifier.padding(horizontal = 16.dp).padding(top = 16.dp),
text = stringResource(id = R.string.missing_permission_auto_location),
onClick = {
viewModel.requestLocationPermission(context as AppCompatActivity)
})
}
NoData()
return
}

View File

@ -1,6 +1,9 @@
package de.mm20.launcher2.ui.launcher.widgets.weather
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.*
import de.mm20.launcher2.permissions.PermissionGroup
import de.mm20.launcher2.permissions.PermissionsManager
import de.mm20.launcher2.preferences.LauncherDataStore
import de.mm20.launcher2.preferences.LauncherPreferences
import de.mm20.launcher2.weather.DailyForecast
@ -16,12 +19,17 @@ import kotlin.math.min
class WeatherWidgetWM : ViewModel(), KoinComponent {
private val weatherRepository: WeatherRepository by inject()
private val permissionsManager: PermissionsManager by inject()
private val dataStore: LauncherDataStore by inject()
private var selectedDayIndex = 0
set(value) {
field = min(value, forecasts.lastIndex)
if (field < 0) return
if (field < 0) {
currentForecast.postValue(null)
return
}
selectedForecastIndex = min(
selectedForecastIndex,
forecasts[value].hourlyForecasts.lastIndex
@ -33,7 +41,10 @@ class WeatherWidgetWM : ViewModel(), KoinComponent {
private var selectedForecastIndex = 0
set(value) {
if (selectedDayIndex < 0) return
if (selectedDayIndex < 0) {
currentForecast.postValue(null)
return
}
field = min(value, forecasts[selectedDayIndex].hourlyForecasts.lastIndex)
currentForecast.postValue(getCurrentlySelectedForecast())
}
@ -61,6 +72,12 @@ class WeatherWidgetWM : ViewModel(), KoinComponent {
val currentDayForecasts = MutableLiveData<List<Forecast>>(emptyList())
val currentDailyForecast = MutableLiveData<DailyForecast>(null)
val hasLocationPermission = permissionsManager.hasPermission(PermissionGroup.Location).asLiveData()
fun requestLocationPermission(context: AppCompatActivity) {
permissionsManager.requestPermission(context, PermissionGroup.Location)
}
val autoLocation = weatherRepository.autoLocation.asLiveData()
val imperialUnits = dataStore.data.map { it.weather.imperialUnits }.asLiveData()
fun selectDay(index: Int) {