Moves relay state to a new watcher

This commit is contained in:
Vitor Pamplona
2023-06-03 21:46:26 -04:00
parent abdfe531f7
commit e171120836
2 changed files with 81 additions and 79 deletions
@@ -39,9 +39,6 @@ import kotlinx.coroutines.launch
fun MessageSetCompose(messageSetCard: MessageSetCard, routeForLastRead: String, accountViewModel: AccountViewModel, nav: (String) -> Unit) { fun MessageSetCompose(messageSetCard: MessageSetCard, routeForLastRead: String, accountViewModel: AccountViewModel, nav: (String) -> Unit) {
val baseNote = remember { messageSetCard.note } val baseNote = remember { messageSetCard.note }
val noteState by baseNote.live().metadata.observeAsState()
val note = remember(noteState) { noteState?.note }
val accountState by accountViewModel.accountLiveData.observeAsState() val accountState by accountViewModel.accountLiveData.observeAsState()
val loggedIn = remember(accountState) { accountState?.account?.userProfile() } ?: return val loggedIn = remember(accountState) { accountState?.account?.userProfile() } ?: return
@@ -49,71 +46,69 @@ fun MessageSetCompose(messageSetCard: MessageSetCard, routeForLastRead: String,
val scope = rememberCoroutineScope() val scope = rememberCoroutineScope()
if (note == null) { var isNew by remember { mutableStateOf(false) }
BlankNote(Modifier)
} else {
var isNew by remember { mutableStateOf(false) }
LaunchedEffect(key1 = messageSetCard.createdAt()) { LaunchedEffect(key1 = messageSetCard.createdAt()) {
scope.launch(Dispatchers.IO) { launch(Dispatchers.IO) {
val newIsNew = val newIsNew =
messageSetCard.createdAt() > NotificationCache.load(routeForLastRead) messageSetCard.createdAt() > NotificationCache.load(routeForLastRead)
NotificationCache.markAsRead(routeForLastRead, messageSetCard.createdAt()) NotificationCache.markAsRead(routeForLastRead, messageSetCard.createdAt())
if (newIsNew != isNew) { if (newIsNew != isNew) {
isNew = newIsNew isNew = newIsNew
}
} }
} }
}
val backgroundColor = if (isNew) { val backgroundColor = if (isNew) {
MaterialTheme.colors.newItemBackgroundColor.compositeOver(MaterialTheme.colors.background) MaterialTheme.colors.newItemBackgroundColor.compositeOver(MaterialTheme.colors.background)
} else { } else {
MaterialTheme.colors.background MaterialTheme.colors.background
} }
val columnModifier = remember(isNew) { val columnModifier = remember(isNew) {
Modifier Modifier
.background(backgroundColor) .background(backgroundColor)
.padding( .padding(
start = 12.dp, start = 12.dp,
end = 12.dp, end = 12.dp,
top = 10.dp top = 10.dp
) )
.combinedClickable( .combinedClickable(
onClick = { onClick = {
scope.launch { scope.launch {
routeFor( routeFor(
baseNote, baseNote,
loggedIn loggedIn
)?.let { nav(it) } )?.let { nav(it) }
} }
}, },
onLongClick = { popupExpanded = true } onLongClick = { popupExpanded = true }
) )
.fillMaxWidth() .fillMaxWidth()
} }
Column(columnModifier) { Column(columnModifier) {
Row(Modifier.fillMaxWidth()) { Row(Modifier.fillMaxWidth()) {
MessageIcon() MessageIcon()
Column(modifier = remember { Modifier.padding(start = 10.dp) }) { Column(modifier = remember { Modifier.padding(start = 10.dp) }) {
val routeForLastRead = "Room/${(baseNote.event as? PrivateDmEvent)?.talkingWith(loggedIn.pubkeyHex)}" val routeForLastRead = remember(baseNote) {
"Room/${(baseNote.event as? PrivateDmEvent)?.talkingWith(loggedIn.pubkeyHex)}"
NoteCompose(
baseNote = baseNote,
routeForLastRead = routeForLastRead,
isBoostedNote = true,
addMarginTop = false,
parentBackgroundColor = null,
accountViewModel = accountViewModel,
nav = nav
)
NoteDropDownMenu(note, popupExpanded, { popupExpanded = false }, accountViewModel)
} }
NoteCompose(
baseNote = baseNote,
routeForLastRead = routeForLastRead,
isBoostedNote = true,
addMarginTop = false,
parentBackgroundColor = null,
accountViewModel = accountViewModel,
nav = nav
)
NoteDropDownMenu(baseNote, popupExpanded, { popupExpanded = false }, accountViewModel)
} }
} }
} }
@@ -138,6 +138,8 @@ import com.vitorpamplona.amethyst.ui.screen.loggedIn.ReportNoteDialog
import com.vitorpamplona.amethyst.ui.theme.BitcoinOrange import com.vitorpamplona.amethyst.ui.theme.BitcoinOrange
import com.vitorpamplona.amethyst.ui.theme.Following import com.vitorpamplona.amethyst.ui.theme.Following
import com.vitorpamplona.amethyst.ui.theme.newItemBackgroundColor import com.vitorpamplona.amethyst.ui.theme.newItemBackgroundColor
import kotlinx.collections.immutable.ImmutableList
import kotlinx.collections.immutable.toImmutableList
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
@@ -1528,7 +1530,7 @@ fun TimeAgo(time: Long) {
var timeStr by remember { mutableStateOf("") } var timeStr by remember { mutableStateOf("") }
LaunchedEffect(key1 = time) { LaunchedEffect(key1 = time) {
withContext(Dispatchers.IO) { launch(Dispatchers.IO) {
val newTimeStr = timeAgo(time, context = context) val newTimeStr = timeAgo(time, context = context)
if (newTimeStr != timeStr) { if (newTimeStr != timeStr) {
timeStr = newTimeStr timeStr = newTimeStr
@@ -2279,31 +2281,22 @@ private fun CreateImageHeader(
@Composable @Composable
private fun RelayBadges(baseNote: Note) { private fun RelayBadges(baseNote: Note) {
val noteRelaysState by baseNote.live().relays.observeAsState()
val noteRelays = remember(noteRelaysState) { noteRelaysState?.note } ?: return
var expanded by remember { mutableStateOf(false) } var expanded by remember { mutableStateOf(false) }
var showShowMore by remember { mutableStateOf(false) } var showShowMore by remember { mutableStateOf(false) }
var lazyRelayList by remember { mutableStateOf(emptyList<String>()) } var lazyRelayList by remember { mutableStateOf(emptyList<String>()) }
LaunchedEffect(key1 = noteRelaysState, key2 = expanded) { WatchRelayLists(baseNote) { relayList ->
launch(Dispatchers.IO) { val relaysToDisplay = if (expanded) relayList else relayList.take(3)
val relayList = noteRelays.relays.map { val shouldListChange = lazyRelayList.size < 3 || lazyRelayList.size != relayList.size
it.removePrefix("wss://").removePrefix("ws://")
}
val relaysToDisplay = if (expanded) relayList else relayList.take(3) if (shouldListChange) {
val shouldListChange = lazyRelayList.size < 3 || lazyRelayList.size != relayList.size lazyRelayList = relaysToDisplay
}
if (shouldListChange) { val nextShowMore = relayList.size > 3 && !expanded
lazyRelayList = relaysToDisplay if (nextShowMore != showShowMore) {
} // only triggers recomposition when actually different
showShowMore = nextShowMore
val nextShowMore = relayList.size > 3 && !expanded
if (nextShowMore != showShowMore) {
// only triggers recomposition when actually different
showShowMore = nextShowMore
}
} }
} }
@@ -2318,6 +2311,21 @@ private fun RelayBadges(baseNote: Note) {
} }
} }
@Composable
private fun WatchRelayLists(baseNote: Note, onListChanges: (ImmutableList<String>) -> Unit) {
val noteRelaysState by baseNote.live().relays.observeAsState()
LaunchedEffect(key1 = noteRelaysState) {
launch(Dispatchers.IO) {
val relayList = noteRelaysState?.note?.relays?.map {
it.removePrefix("wss://").removePrefix("ws://")
} ?: emptyList()
onListChanges(relayList.toImmutableList())
}
}
}
@OptIn(ExperimentalLayoutApi::class) @OptIn(ExperimentalLayoutApi::class)
@Composable @Composable
@Stable @Stable
@@ -2467,7 +2475,6 @@ fun UserPicture(
} }
} }
@OptIn(ExperimentalTime::class)
@Composable @Composable
fun UserPicture( fun UserPicture(
userHex: String, userHex: String,