Moves Geolocation search to the IO thread

This commit is contained in:
Vitor Pamplona
2023-08-01 21:52:18 -04:00
parent 3694d1b0f5
commit 2c19b2ffef
5 changed files with 70 additions and 22 deletions
@@ -63,7 +63,7 @@ class LocationUtil(context: Context) {
} }
class ReverseGeoLocationUtil { class ReverseGeoLocationUtil {
fun execute( suspend fun execute(
location: Location, location: Location,
context: Context context: Context
): String? { ): String? {
@@ -76,6 +76,7 @@ import com.google.accompanist.permissions.isGranted
import com.google.accompanist.permissions.rememberPermissionState import com.google.accompanist.permissions.rememberPermissionState
import com.vitorpamplona.amethyst.R import com.vitorpamplona.amethyst.R
import com.vitorpamplona.amethyst.model.Note import com.vitorpamplona.amethyst.model.Note
import com.vitorpamplona.amethyst.model.ServersAvailable
import com.vitorpamplona.amethyst.model.User import com.vitorpamplona.amethyst.model.User
import com.vitorpamplona.amethyst.service.NostrSearchEventOrUserDataSource import com.vitorpamplona.amethyst.service.NostrSearchEventOrUserDataSource
import com.vitorpamplona.amethyst.service.ReverseGeoLocationUtil 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.QuoteBorder
import com.vitorpamplona.amethyst.ui.theme.Size10dp import com.vitorpamplona.amethyst.ui.theme.Size10dp
import com.vitorpamplona.amethyst.ui.theme.Size5dp 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.mediumImportanceLink
import com.vitorpamplona.amethyst.ui.theme.placeholderText import com.vitorpamplona.amethyst.ui.theme.placeholderText
import com.vitorpamplona.amethyst.ui.theme.replyModifier import com.vitorpamplona.amethyst.ui.theme.replyModifier
@@ -736,16 +738,32 @@ fun DisplayLocationObserver(geoLocation: Flow<String>) {
@Composable @Composable
fun DisplayLocationInTitle(geohash: String) { fun DisplayLocationInTitle(geohash: String) {
val context = LocalContext.current val context = LocalContext.current
val cityName = remember(geohash) {
ReverseGeoLocationUtil().execute(geohash.toGeoHash().toLocation(), context) var cityName by remember(geohash) {
mutableStateOf<String>(geohash)
} }
Text( LaunchedEffect(key1 = geohash) {
text = cityName ?: geohash, launch(Dispatchers.IO) {
fontSize = 20.sp, val newCityName = ReverseGeoLocationUtil().execute(geohash.toGeoHash().toLocation(), context)?.ifBlank { null }
fontWeight = FontWeight.W500,
modifier = Modifier.padding(start = Size5dp) 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) @OptIn(ExperimentalLayoutApi::class)
@@ -2427,12 +2427,21 @@ fun SecondUserInfoRow(
@Composable @Composable
fun DisplayLocation(geohash: String, nav: (String) -> Unit) { fun DisplayLocation(geohash: String, nav: (String) -> Unit) {
val context = LocalContext.current val context = LocalContext.current
val cityName = remember(geohash) { var cityName by remember(geohash) {
ReverseGeoLocationUtil().execute(geohash.toGeoHash().toLocation(), context) mutableStateOf<String>(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( ClickableText(
text = AnnotatedString(cityName ?: geohash), text = AnnotatedString(cityName),
onClick = { nav("Geohash/$geohash") }, onClick = { nav("Geohash/$geohash") },
style = LocalTextStyle.current.copy( style = LocalTextStyle.current.copy(
color = MaterialTheme.colors.primary.copy( color = MaterialTheme.colors.primary.copy(
@@ -329,6 +329,11 @@ fun NoteMaster(
Row(verticalAlignment = Alignment.CenterVertically) { Row(verticalAlignment = Alignment.CenterVertically) {
ObserveDisplayNip05Status(baseNote, remember { Modifier.weight(1f) }) 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) } } val baseReward = remember { noteEvent.getReward()?.let { Reward(it) } }
if (baseReward != null) { if (baseReward != null) {
DisplayReward(baseReward, baseNote, accountViewModel, nav) DisplayReward(baseReward, baseNote, accountViewModel, nav)
@@ -16,6 +16,7 @@ import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.derivedStateOf import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.getValue import androidx.compose.runtime.getValue
import androidx.compose.runtime.livedata.observeAsState import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue import androidx.compose.runtime.setValue
@@ -124,16 +125,7 @@ fun GeoHashHeader(tag: String, account: AccountViewModel, onClick: () -> Unit =
horizontalArrangement = Arrangement.Center, horizontalArrangement = Arrangement.Center,
modifier = Modifier.fillMaxWidth() modifier = Modifier.fillMaxWidth()
) { ) {
val context = LocalContext.current DislayGeoTagHeader(tag, remember { Modifier.weight(1f) })
val cityName = remember(tag) {
ReverseGeoLocationUtil().execute(tag.toGeoHash().toLocation(), context)
}
Text(
"$cityName ($tag)",
fontWeight = FontWeight.Bold,
modifier = Modifier.weight(1f)
)
HashtagActionOptions(tag, account) 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<String>(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 @Composable
private fun HashtagActionOptions( private fun HashtagActionOptions(
tag: String, tag: String,