add exception handling for parsing geohash

This commit is contained in:
jiftechnify
2023-10-16 16:52:28 +09:00
parent 202af650de
commit 407807d871
2 changed files with 30 additions and 22 deletions
@@ -54,6 +54,7 @@ import androidx.lifecycle.map
import androidx.lifecycle.viewModelScope import androidx.lifecycle.viewModelScope
import androidx.navigation.NavBackStackEntry import androidx.navigation.NavBackStackEntry
import coil.Coil import coil.Coil
import com.fonfon.kgeohash.toGeoHash
import com.vitorpamplona.amethyst.R import com.vitorpamplona.amethyst.R
import com.vitorpamplona.amethyst.model.Account import com.vitorpamplona.amethyst.model.Account
import com.vitorpamplona.amethyst.model.AddressableNote import com.vitorpamplona.amethyst.model.AddressableNote
@@ -751,12 +752,15 @@ fun SimpleTextSpinner(
fun RenderOption(option: Name) { fun RenderOption(option: Name) {
when (option) { when (option) {
is GeoHashName -> { is GeoHashName -> {
LoadCityName(option.geoHashTag) { val geohash = runCatching { option.geoHashTag.toGeoHash() }.getOrNull()
Row( if (geohash != null) {
horizontalArrangement = Arrangement.Center, LoadCityName(geohash) {
modifier = Modifier.fillMaxWidth() Row(
) { horizontalArrangement = Arrangement.Center,
Text(text = "/g/$it", color = MaterialTheme.colorScheme.onSurface) modifier = Modifier.fillMaxWidth()
) {
Text(text = "/g/$it", color = MaterialTheme.colorScheme.onSurface)
}
} }
} }
} }
@@ -79,6 +79,7 @@ import androidx.lifecycle.map
import coil.compose.AsyncImage import coil.compose.AsyncImage
import coil.compose.AsyncImagePainter import coil.compose.AsyncImagePainter
import coil.request.SuccessResult import coil.request.SuccessResult
import com.fonfon.kgeohash.GeoHash
import com.fonfon.kgeohash.toGeoHash import com.fonfon.kgeohash.toGeoHash
import com.vitorpamplona.amethyst.R import com.vitorpamplona.amethyst.R
import com.vitorpamplona.amethyst.model.AddressableNote import com.vitorpamplona.amethyst.model.AddressableNote
@@ -2595,15 +2596,15 @@ fun LoadStatuses(
} }
@Composable @Composable
fun LoadCityName(geohash: String, content: @Composable (String) -> Unit) { fun LoadCityName(geohash: GeoHash, content: @Composable (String) -> Unit) {
val context = LocalContext.current val context = LocalContext.current
var cityName by remember(geohash) { var cityName by remember(geohash) {
mutableStateOf<String>(geohash) mutableStateOf<String>(geohash.toString())
} }
LaunchedEffect(key1 = geohash) { LaunchedEffect(key1 = geohash) {
launch(Dispatchers.IO) { launch(Dispatchers.IO) {
val newCityName = ReverseGeoLocationUtil().execute(geohash.toGeoHash().toLocation(), context)?.ifBlank { null } val newCityName = ReverseGeoLocationUtil().execute(geohash.toLocation(), context)?.ifBlank { null }
if (newCityName != null && newCityName != cityName) { if (newCityName != null && newCityName != cityName) {
cityName = newCityName cityName = newCityName
} }
@@ -2614,20 +2615,23 @@ fun LoadCityName(geohash: String, content: @Composable (String) -> Unit) {
} }
@Composable @Composable
fun DisplayLocation(geohash: String, nav: (String) -> Unit) { fun DisplayLocation(geohashStr: String, nav: (String) -> Unit) {
LoadCityName(geohash) { cityName -> val geoHash = runCatching { geohashStr.toGeoHash() }.getOrNull()
ClickableText( if (geoHash != null) {
text = AnnotatedString(cityName), LoadCityName(geoHash) { cityName ->
onClick = { nav("Geohash/$geohash") }, ClickableText(
style = LocalTextStyle.current.copy( text = AnnotatedString(cityName),
color = MaterialTheme.colorScheme.primary.copy( onClick = { nav("Geohash/$geoHash") },
alpha = 0.52f style = LocalTextStyle.current.copy(
color = MaterialTheme.colorScheme.primary.copy(
alpha = 0.52f
),
fontSize = Font14SP,
fontWeight = FontWeight.Bold
), ),
fontSize = Font14SP, maxLines = 1
fontWeight = FontWeight.Bold )
), }
maxLines = 1
)
} }
} }