From 2c19b2ffef7f2a8e18a63cff2bfb6e298dae0256 Mon Sep 17 00:00:00 2001 From: Vitor Pamplona Date: Tue, 1 Aug 2023 21:52:18 -0400 Subject: [PATCH] Moves Geolocation search to the IO thread --- .../amethyst/service/LocationUtil.kt | 2 +- .../amethyst/ui/actions/NewPostView.kt | 34 +++++++++++++----- .../amethyst/ui/note/NoteCompose.kt | 15 ++++++-- .../amethyst/ui/screen/ThreadFeedView.kt | 5 +++ .../ui/screen/loggedIn/GeoHashScreen.kt | 36 +++++++++++++------ 5 files changed, 70 insertions(+), 22 deletions(-) diff --git a/app/src/main/java/com/vitorpamplona/amethyst/service/LocationUtil.kt b/app/src/main/java/com/vitorpamplona/amethyst/service/LocationUtil.kt index 7645a12f4..43f9bb39d 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/service/LocationUtil.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/service/LocationUtil.kt @@ -63,7 +63,7 @@ class LocationUtil(context: Context) { } class ReverseGeoLocationUtil { - fun execute( + suspend fun execute( location: Location, context: Context ): String? { diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/NewPostView.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/NewPostView.kt index a1c97a294..9e49b2824 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/NewPostView.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/NewPostView.kt @@ -76,6 +76,7 @@ import com.google.accompanist.permissions.isGranted import com.google.accompanist.permissions.rememberPermissionState import com.vitorpamplona.amethyst.R import com.vitorpamplona.amethyst.model.Note +import com.vitorpamplona.amethyst.model.ServersAvailable import com.vitorpamplona.amethyst.model.User import com.vitorpamplona.amethyst.service.NostrSearchEventOrUserDataSource import com.vitorpamplona.amethyst.service.ReverseGeoLocationUtil @@ -94,6 +95,7 @@ import com.vitorpamplona.amethyst.ui.theme.ButtonBorder import com.vitorpamplona.amethyst.ui.theme.QuoteBorder import com.vitorpamplona.amethyst.ui.theme.Size10dp import com.vitorpamplona.amethyst.ui.theme.Size5dp +import com.vitorpamplona.amethyst.ui.theme.StdHorzSpacer import com.vitorpamplona.amethyst.ui.theme.mediumImportanceLink import com.vitorpamplona.amethyst.ui.theme.placeholderText import com.vitorpamplona.amethyst.ui.theme.replyModifier @@ -736,16 +738,32 @@ fun DisplayLocationObserver(geoLocation: Flow) { @Composable fun DisplayLocationInTitle(geohash: String) { val context = LocalContext.current - val cityName = remember(geohash) { - ReverseGeoLocationUtil().execute(geohash.toGeoHash().toLocation(), context) + + var cityName by remember(geohash) { + mutableStateOf(geohash) } - Text( - text = cityName ?: geohash, - fontSize = 20.sp, - fontWeight = FontWeight.W500, - modifier = Modifier.padding(start = Size5dp) - ) + LaunchedEffect(key1 = geohash) { + launch(Dispatchers.IO) { + val newCityName = ReverseGeoLocationUtil().execute(geohash.toGeoHash().toLocation(), context)?.ifBlank { null } + + if (newCityName != null && newCityName != cityName) { + cityName = newCityName + } + } + } + + if (geohash != "s0000") { + Text( + text = cityName, + fontSize = 20.sp, + fontWeight = FontWeight.W500, + modifier = Modifier.padding(start = Size5dp) + ) + } else { + Spacer(modifier = StdHorzSpacer) + LoadingAnimation() + } } @OptIn(ExperimentalLayoutApi::class) diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/note/NoteCompose.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/note/NoteCompose.kt index e172ab68d..b08e89f6e 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/note/NoteCompose.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/note/NoteCompose.kt @@ -2427,12 +2427,21 @@ fun SecondUserInfoRow( @Composable fun DisplayLocation(geohash: String, nav: (String) -> Unit) { val context = LocalContext.current - val cityName = remember(geohash) { - ReverseGeoLocationUtil().execute(geohash.toGeoHash().toLocation(), context) + var cityName by remember(geohash) { + mutableStateOf(geohash) + } + + LaunchedEffect(key1 = geohash) { + launch(Dispatchers.IO) { + val newCityName = ReverseGeoLocationUtil().execute(geohash.toGeoHash().toLocation(), context)?.ifBlank { null } + if (newCityName != null && newCityName != cityName) { + cityName = newCityName + } + } } ClickableText( - text = AnnotatedString(cityName ?: geohash), + text = AnnotatedString(cityName), onClick = { nav("Geohash/$geohash") }, style = LocalTextStyle.current.copy( color = MaterialTheme.colors.primary.copy( diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/ThreadFeedView.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/ThreadFeedView.kt index 477450708..d8fbb41f0 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/ThreadFeedView.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/ThreadFeedView.kt @@ -329,6 +329,11 @@ fun NoteMaster( Row(verticalAlignment = Alignment.CenterVertically) { ObserveDisplayNip05Status(baseNote, remember { Modifier.weight(1f) }) + val geo = remember { noteEvent.getGeoHash() } + if (geo != null) { + DisplayLocation(geo, nav) + } + val baseReward = remember { noteEvent.getReward()?.let { Reward(it) } } if (baseReward != null) { DisplayReward(baseReward, baseNote, accountViewModel, nav) diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/GeoHashScreen.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/GeoHashScreen.kt index 72ea8fca7..c6ab9a8df 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/GeoHashScreen.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/GeoHashScreen.kt @@ -16,6 +16,7 @@ import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.derivedStateOf import androidx.compose.runtime.getValue import androidx.compose.runtime.livedata.observeAsState +import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.runtime.rememberCoroutineScope import androidx.compose.runtime.setValue @@ -124,16 +125,7 @@ fun GeoHashHeader(tag: String, account: AccountViewModel, onClick: () -> Unit = horizontalArrangement = Arrangement.Center, modifier = Modifier.fillMaxWidth() ) { - val context = LocalContext.current - val cityName = remember(tag) { - ReverseGeoLocationUtil().execute(tag.toGeoHash().toLocation(), context) - } - - Text( - "$cityName ($tag)", - fontWeight = FontWeight.Bold, - modifier = Modifier.weight(1f) - ) + DislayGeoTagHeader(tag, remember { Modifier.weight(1f) }) HashtagActionOptions(tag, account) } @@ -148,6 +140,30 @@ fun GeoHashHeader(tag: String, account: AccountViewModel, onClick: () -> Unit = } } +@Composable +fun DislayGeoTagHeader(geohash: String, modifier: Modifier) { + val context = LocalContext.current + + var cityName by remember(geohash) { + mutableStateOf(geohash) + } + + LaunchedEffect(key1 = geohash) { + launch(Dispatchers.IO) { + val newCityName = ReverseGeoLocationUtil().execute(geohash.toGeoHash().toLocation(), context)?.ifBlank { null } + if (newCityName != null && newCityName != cityName) { + cityName = "$newCityName ($geohash)" + } + } + } + + Text( + cityName, + fontWeight = FontWeight.Bold, + modifier = modifier + ) +} + @Composable private fun HashtagActionOptions( tag: String,