Improve weather location dialog

This commit is contained in:
MM20 2022-06-11 22:14:46 +02:00
parent 9090d16af2
commit 027d6ff574
No known key found for this signature in database
GPG Key ID: 0B61A8F2DEAFA389
2 changed files with 69 additions and 74 deletions

View File

@ -353,6 +353,7 @@
<!-- Manually set a location, used in weather widget location permission banner as an <!-- Manually set a location, used in weather widget location permission banner as an
alternative to granting the permission --> alternative to granting the permission -->
<string name="weather_widget_set_location">Set location</string> <string name="weather_widget_set_location">Set location</string>
<string name="weather_location_search_no_result">Location not found.</string>
<!-- Missing location permission in weather widget and weather widget settings settings --> <!-- Missing location permission in weather widget and weather widget settings settings -->
<string name="missing_permission_auto_location">Location access is required to determine the location automatically</string> <string name="missing_permission_auto_location">Location access is required to determine the location automatically</string>
<!-- Missing notification permission in music widget settings --> <!-- Missing notification permission in music widget settings -->

View File

@ -2,27 +2,25 @@ package de.mm20.launcher2.ui.common
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.Row
import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.material.icons.Icons
import androidx.compose.material3.CircularProgressIndicator import androidx.compose.material.icons.rounded.Error
import androidx.compose.material3.OutlinedTextField import androidx.compose.material.icons.rounded.Search
import androidx.compose.material3.MaterialTheme import androidx.compose.material3.*
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.* 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.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.lifecycle.viewmodel.compose.viewModel import androidx.lifecycle.viewmodel.compose.viewModel
import de.mm20.launcher2.ui.R import de.mm20.launcher2.ui.R
import de.mm20.launcher2.ui.component.BottomSheetDialog
import de.mm20.launcher2.ui.component.SmallMessage
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
@Composable @Composable
@ -30,31 +28,34 @@ fun WeatherLocationSearchDialog(
onDismissRequest: () -> Unit onDismissRequest: () -> Unit
) { ) {
val scope = rememberCoroutineScope() val scope = rememberCoroutineScope()
val viewModel : WeatherLocationSearchDialogVM = viewModel() val viewModel: WeatherLocationSearchDialogVM = viewModel()
val isSearching by viewModel.isSearchingLocation.observeAsState(initial = false) val isSearching by viewModel.isSearchingLocation.observeAsState(initial = false)
val locations by viewModel.locationResults.observeAsState(emptyList()) val locations by viewModel.locationResults.observeAsState(emptyList())
Dialog(onDismissRequest = onDismissRequest) {
Surface( BottomSheetDialog(
tonalElevation = 16.dp, onDismissRequest = onDismissRequest,
shadowElevation = 16.dp, title = {
shape = MaterialTheme.shapes.extraLarge, Text(
modifier = Modifier text = stringResource(R.string.preference_location),
.fillMaxSize() )
.padding(vertical = 16.dp), },
confirmButton = {
TextButton(
onClick = onDismissRequest,
) { ) {
Text(
text = stringResource(android.R.string.cancel),
)
}
}
) {
var query by remember { mutableStateOf("") }
Column( Column(
modifier = Modifier.fillMaxWidth() modifier = Modifier.fillMaxWidth()
) { ) {
Text( Row(
text = stringResource(R.string.preference_location), Modifier.padding(bottom = 16.dp)
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( OutlinedTextField(
singleLine = true, singleLine = true,
value = query, value = query,
@ -64,23 +65,23 @@ fun WeatherLocationSearchDialog(
scope.launch { scope.launch {
viewModel.searchLocation(it) viewModel.searchLocation(it)
} }
}, modifier = Modifier },
.fillMaxWidth() leadingIcon = {
.padding(24.dp) Icon(imageVector = Icons.Rounded.Search, contentDescription = null)
},
modifier = Modifier
.weight(1f)
) )
}
if (isSearching) { if (isSearching) {
CircularProgressIndicator( CircularProgressIndicator(
modifier = Modifier modifier = Modifier
.align(Alignment.CenterHorizontally) .align(Alignment.CenterHorizontally)
.weight(1f)
.padding(vertical = 16.dp)
) )
} else { } else if (locations.isNotEmpty()) {
LazyColumn( LazyColumn(
modifier = Modifier modifier = Modifier
.fillMaxWidth() .fillMaxWidth()
.weight(1f)
.padding(bottom = 16.dp)
) { ) {
items(locations) { items(locations) {
Text( Text(
@ -92,26 +93,19 @@ fun WeatherLocationSearchDialog(
onDismissRequest() onDismissRequest()
} }
.padding( .padding(
horizontal = 24.dp,
vertical = 16.dp vertical = 16.dp
) )
) )
} }
} }
} } else if (query.isNotEmpty()) {
TextButton( SmallMessage(
onClick = onDismissRequest, modifier = Modifier.fillMaxWidth(),
modifier = Modifier icon = Icons.Rounded.Error,
.align(Alignment.End) text = stringResource(R.string.weather_location_search_no_result)
.padding(24.dp)
) {
Text(
text = stringResource(R.string.close),
) )
} }
} }
}
} }
} }