Updating search as new notes come in.

This commit is contained in:
Vitor Pamplona
2023-03-30 18:53:20 -04:00
parent 3137750a58
commit 3193a5e0ef
2 changed files with 32 additions and 17 deletions
@@ -35,25 +35,25 @@ object NostrSearchEventOrUserDataSource : NostrDataSource("SingleEventFeed") {
null null
} }
if (hexToWatch == null) {
return null
}
// downloads all the reactions to a given event. // downloads all the reactions to a given event.
return listOf( return listOfNotNull(
TypedFilter( hexToWatch?.let {
types = FeedType.values().toSet(), TypedFilter(
filter = JsonFilter( types = FeedType.values().toSet(),
ids = listOfNotNull(hexToWatch) filter = JsonFilter(
ids = listOfNotNull(hexToWatch)
)
) )
), },
TypedFilter( hexToWatch?.let {
types = FeedType.values().toSet(), TypedFilter(
filter = JsonFilter( types = FeedType.values().toSet(),
kinds = listOf(MetadataEvent.kind), filter = JsonFilter(
authors = listOfNotNull(hexToWatch) kinds = listOf(MetadataEvent.kind),
authors = listOfNotNull(hexToWatch)
)
) )
), },
TypedFilter( TypedFilter(
types = FeedType.values().toSet(), types = FeedType.values().toSet(),
filter = JsonFilter( filter = JsonFilter(
@@ -140,6 +140,9 @@ private fun SearchBar(accountViewModel: AccountViewModel, navController: NavCont
val onlineSearch = NostrSearchEventOrUserDataSource val onlineSearch = NostrSearchEventOrUserDataSource
val dbState = LocalCache.live.observeAsState()
val db = dbState.value ?: return
val isTrailingIconVisible by remember { val isTrailingIconVisible by remember {
derivedStateOf { derivedStateOf {
searchValue.isNotBlank() searchValue.isNotBlank()
@@ -151,6 +154,18 @@ private fun SearchBar(accountViewModel: AccountViewModel, navController: NavCont
CoroutineChannel<String>(CoroutineChannel.CONFLATED) CoroutineChannel<String>(CoroutineChannel.CONFLATED)
} }
LaunchedEffect(db) {
if (searchValue.length > 1) {
withContext(Dispatchers.IO) {
searchResults.value = LocalCache.findUsersStartingWith(searchValue)
searchResultsNotes.value =
LocalCache.findNotesStartingWith(searchValue).sortedBy { it.createdAt() }
.reversed()
searchResultsChannels.value = LocalCache.findChannelsStartingWith(searchValue)
}
}
}
LaunchedEffect(Unit) { LaunchedEffect(Unit) {
// Wait for text changes to stop for 300 ms before firing off search. // Wait for text changes to stop for 300 ms before firing off search.
withContext(Dispatchers.IO) { withContext(Dispatchers.IO) {
@@ -161,7 +176,7 @@ private fun SearchBar(accountViewModel: AccountViewModel, navController: NavCont
.collectLatest { .collectLatest {
hashtagResults.value = findHashtags(it) hashtagResults.value = findHashtags(it)
if (it.removePrefix("npub").removePrefix("note").length >= 4) { if (it.length >= 4) {
onlineSearch.search(it.trim()) onlineSearch.search(it.trim())
} }