Skip to content

Commit

Permalink
Improves the speed of updating Notification notes
Browse files Browse the repository at this point in the history
  • Loading branch information
vitorpamplona committed May 23, 2023
1 parent a29d9ad commit 15621f1
Showing 1 changed file with 23 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ open class CardFeedViewModel(val localFilter: FeedFilter<Note>) : ViewModel() {
val feedContent = _feedContent.asStateFlow()

private var lastAccount: Account? = null
private var lastNotes: List<Note>? = null
private var lastNotes: Set<Note>? = null

fun refresh() {
val scope = CoroutineScope(Job() + Dispatchers.Default)
Expand All @@ -57,13 +57,13 @@ open class CardFeedViewModel(val localFilter: FeedFilter<Note>) : ViewModel() {
if (lastNotesCopy != null && oldNotesState is CardFeedState.Loaded) {
val newCards = convertToCard(notes.minus(lastNotesCopy))
if (newCards.isNotEmpty()) {
lastNotes = notes
lastNotes = notes.toSet()
lastAccount = (localFilter as? NotificationFeedFilter)?.account
updateFeed((oldNotesState.feed.value + newCards).distinctBy { it.id() }.sortedWith(compareBy({ it.createdAt() }, { it.id() })).reversed())
}
} else {
val cards = convertToCard(notes)
lastNotes = notes
lastNotes = notes.toSet()
lastAccount = (localFilter as? NotificationFeedFilter)?.account
updateFeed(cards)
}
Expand Down Expand Up @@ -184,13 +184,26 @@ open class CardFeedViewModel(val localFilter: FeedFilter<Note>) : ViewModel() {

if (lastNotesCopy != null && localFilter is AdditiveFeedFilter && oldNotesState is CardFeedState.Loaded) {
val filteredNewList = localFilter.applyFilter(newItems)

if (filteredNewList.isEmpty()) return

val actuallyNew = filteredNewList.minus(lastNotesCopy)

if (actuallyNew.isEmpty()) return

val newCards = convertToCard(actuallyNew)

if (newCards.isNotEmpty()) {
lastNotes = lastNotesCopy + newItems
lastNotes = lastNotesCopy + actuallyNew
lastAccount = (localFilter as? NotificationFeedFilter)?.account
updateFeed((oldNotesState.feed.value + newCards).distinctBy { it.id() }.sortedWith(compareBy({ it.createdAt() }, { it.id() })).reversed())

val updatedCards = (oldNotesState.feed.value + newCards)
.distinctBy { it.id() }
.sortedWith(compareBy({ it.createdAt() }, { it.id() }))
.reversed()
.take(1000)

updateFeed(updatedCards)
}
} else {
// Refresh Everything
Expand All @@ -216,10 +229,13 @@ open class CardFeedViewModel(val localFilter: FeedFilter<Note>) : ViewModel() {
@OptIn(ExperimentalTime::class)
fun invalidateInsertData(newItems: Set<Note>) {
bundlerInsert.invalidateList(newItems) {
val newObjects = it.flatten().toSet()
val (value, elapsed) = measureTimedValue {
refreshFromOldState(it.flatten().toSet())
if (newObjects.isNotEmpty()) {
refreshFromOldState(newObjects)
}
}
Log.d("Time", "${this.javaClass.simpleName} Card additive update $elapsed")
Log.d("Time", "${this.javaClass.simpleName} Card additive update $elapsed. ${newObjects.size}")
}
}

Expand Down

0 comments on commit 15621f1

Please sign in to comment.