Show missing permission banner in weather widget if there's no weather data and auto location is enabled
This commit is contained in:
parent
c5803b7e75
commit
e7b4a05685
@ -4,6 +4,7 @@ import android.content.Context
|
|||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import android.net.Uri
|
import android.net.Uri
|
||||||
import android.text.format.DateUtils
|
import android.text.format.DateUtils
|
||||||
|
import androidx.appcompat.app.AppCompatActivity
|
||||||
import androidx.compose.animation.AnimatedVisibility
|
import androidx.compose.animation.AnimatedVisibility
|
||||||
import androidx.compose.animation.ExperimentalAnimationApi
|
import androidx.compose.animation.ExperimentalAnimationApi
|
||||||
import androidx.compose.foundation.clickable
|
import androidx.compose.foundation.clickable
|
||||||
@ -28,6 +29,7 @@ import androidx.compose.ui.unit.sp
|
|||||||
import androidx.lifecycle.viewmodel.compose.viewModel
|
import androidx.lifecycle.viewmodel.compose.viewModel
|
||||||
import de.mm20.launcher2.ktx.tryStartActivity
|
import de.mm20.launcher2.ktx.tryStartActivity
|
||||||
import de.mm20.launcher2.ui.R
|
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.AnimatedWeatherIcon
|
||||||
import de.mm20.launcher2.ui.weather.WeatherIcon
|
import de.mm20.launcher2.ui.weather.WeatherIcon
|
||||||
import de.mm20.launcher2.weather.DailyForecast
|
import de.mm20.launcher2.weather.DailyForecast
|
||||||
@ -42,11 +44,23 @@ import kotlin.math.roundToInt
|
|||||||
fun WeatherWidget() {
|
fun WeatherWidget() {
|
||||||
val viewModel: WeatherWidgetWM = viewModel()
|
val viewModel: WeatherWidgetWM = viewModel()
|
||||||
|
|
||||||
|
val context = LocalContext.current
|
||||||
|
|
||||||
val selectedForecast by viewModel.currentForecast.observeAsState()
|
val selectedForecast by viewModel.currentForecast.observeAsState()
|
||||||
|
|
||||||
val imperialUnits by viewModel.imperialUnits.observeAsState(false)
|
val imperialUnits by viewModel.imperialUnits.observeAsState(false)
|
||||||
|
|
||||||
val forecast = selectedForecast ?: run {
|
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()
|
NoData()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,6 +1,9 @@
|
|||||||
package de.mm20.launcher2.ui.launcher.widgets.weather
|
package de.mm20.launcher2.ui.launcher.widgets.weather
|
||||||
|
|
||||||
|
import androidx.appcompat.app.AppCompatActivity
|
||||||
import androidx.lifecycle.*
|
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.LauncherDataStore
|
||||||
import de.mm20.launcher2.preferences.LauncherPreferences
|
import de.mm20.launcher2.preferences.LauncherPreferences
|
||||||
import de.mm20.launcher2.weather.DailyForecast
|
import de.mm20.launcher2.weather.DailyForecast
|
||||||
@ -16,12 +19,17 @@ import kotlin.math.min
|
|||||||
class WeatherWidgetWM : ViewModel(), KoinComponent {
|
class WeatherWidgetWM : ViewModel(), KoinComponent {
|
||||||
private val weatherRepository: WeatherRepository by inject()
|
private val weatherRepository: WeatherRepository by inject()
|
||||||
|
|
||||||
|
private val permissionsManager: PermissionsManager by inject()
|
||||||
|
|
||||||
private val dataStore: LauncherDataStore by inject()
|
private val dataStore: LauncherDataStore by inject()
|
||||||
|
|
||||||
private var selectedDayIndex = 0
|
private var selectedDayIndex = 0
|
||||||
set(value) {
|
set(value) {
|
||||||
field = min(value, forecasts.lastIndex)
|
field = min(value, forecasts.lastIndex)
|
||||||
if (field < 0) return
|
if (field < 0) {
|
||||||
|
currentForecast.postValue(null)
|
||||||
|
return
|
||||||
|
}
|
||||||
selectedForecastIndex = min(
|
selectedForecastIndex = min(
|
||||||
selectedForecastIndex,
|
selectedForecastIndex,
|
||||||
forecasts[value].hourlyForecasts.lastIndex
|
forecasts[value].hourlyForecasts.lastIndex
|
||||||
@ -33,7 +41,10 @@ class WeatherWidgetWM : ViewModel(), KoinComponent {
|
|||||||
|
|
||||||
private var selectedForecastIndex = 0
|
private var selectedForecastIndex = 0
|
||||||
set(value) {
|
set(value) {
|
||||||
if (selectedDayIndex < 0) return
|
if (selectedDayIndex < 0) {
|
||||||
|
currentForecast.postValue(null)
|
||||||
|
return
|
||||||
|
}
|
||||||
field = min(value, forecasts[selectedDayIndex].hourlyForecasts.lastIndex)
|
field = min(value, forecasts[selectedDayIndex].hourlyForecasts.lastIndex)
|
||||||
currentForecast.postValue(getCurrentlySelectedForecast())
|
currentForecast.postValue(getCurrentlySelectedForecast())
|
||||||
}
|
}
|
||||||
@ -61,6 +72,12 @@ class WeatherWidgetWM : ViewModel(), KoinComponent {
|
|||||||
val currentDayForecasts = MutableLiveData<List<Forecast>>(emptyList())
|
val currentDayForecasts = MutableLiveData<List<Forecast>>(emptyList())
|
||||||
val currentDailyForecast = MutableLiveData<DailyForecast>(null)
|
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()
|
val imperialUnits = dataStore.data.map { it.weather.imperialUnits }.asLiveData()
|
||||||
|
|
||||||
fun selectDay(index: Int) {
|
fun selectDay(index: Int) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user