Improves the speed of updating Notification notes
This commit is contained in:
@@ -37,7 +37,7 @@ open class CardFeedViewModel(val localFilter: FeedFilter<Note>) : ViewModel() {
|
|||||||
val feedContent = _feedContent.asStateFlow()
|
val feedContent = _feedContent.asStateFlow()
|
||||||
|
|
||||||
private var lastAccount: Account? = null
|
private var lastAccount: Account? = null
|
||||||
private var lastNotes: List<Note>? = null
|
private var lastNotes: Set<Note>? = null
|
||||||
|
|
||||||
fun refresh() {
|
fun refresh() {
|
||||||
val scope = CoroutineScope(Job() + Dispatchers.Default)
|
val scope = CoroutineScope(Job() + Dispatchers.Default)
|
||||||
@@ -57,13 +57,13 @@ open class CardFeedViewModel(val localFilter: FeedFilter<Note>) : ViewModel() {
|
|||||||
if (lastNotesCopy != null && oldNotesState is CardFeedState.Loaded) {
|
if (lastNotesCopy != null && oldNotesState is CardFeedState.Loaded) {
|
||||||
val newCards = convertToCard(notes.minus(lastNotesCopy))
|
val newCards = convertToCard(notes.minus(lastNotesCopy))
|
||||||
if (newCards.isNotEmpty()) {
|
if (newCards.isNotEmpty()) {
|
||||||
lastNotes = notes
|
lastNotes = notes.toSet()
|
||||||
lastAccount = (localFilter as? NotificationFeedFilter)?.account
|
lastAccount = (localFilter as? NotificationFeedFilter)?.account
|
||||||
updateFeed((oldNotesState.feed.value + newCards).distinctBy { it.id() }.sortedWith(compareBy({ it.createdAt() }, { it.id() })).reversed())
|
updateFeed((oldNotesState.feed.value + newCards).distinctBy { it.id() }.sortedWith(compareBy({ it.createdAt() }, { it.id() })).reversed())
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
val cards = convertToCard(notes)
|
val cards = convertToCard(notes)
|
||||||
lastNotes = notes
|
lastNotes = notes.toSet()
|
||||||
lastAccount = (localFilter as? NotificationFeedFilter)?.account
|
lastAccount = (localFilter as? NotificationFeedFilter)?.account
|
||||||
updateFeed(cards)
|
updateFeed(cards)
|
||||||
}
|
}
|
||||||
@@ -184,13 +184,26 @@ open class CardFeedViewModel(val localFilter: FeedFilter<Note>) : ViewModel() {
|
|||||||
|
|
||||||
if (lastNotesCopy != null && localFilter is AdditiveFeedFilter && oldNotesState is CardFeedState.Loaded) {
|
if (lastNotesCopy != null && localFilter is AdditiveFeedFilter && oldNotesState is CardFeedState.Loaded) {
|
||||||
val filteredNewList = localFilter.applyFilter(newItems)
|
val filteredNewList = localFilter.applyFilter(newItems)
|
||||||
|
|
||||||
|
if (filteredNewList.isEmpty()) return
|
||||||
|
|
||||||
val actuallyNew = filteredNewList.minus(lastNotesCopy)
|
val actuallyNew = filteredNewList.minus(lastNotesCopy)
|
||||||
|
|
||||||
|
if (actuallyNew.isEmpty()) return
|
||||||
|
|
||||||
val newCards = convertToCard(actuallyNew)
|
val newCards = convertToCard(actuallyNew)
|
||||||
|
|
||||||
if (newCards.isNotEmpty()) {
|
if (newCards.isNotEmpty()) {
|
||||||
lastNotes = lastNotesCopy + newItems
|
lastNotes = lastNotesCopy + actuallyNew
|
||||||
lastAccount = (localFilter as? NotificationFeedFilter)?.account
|
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 {
|
} else {
|
||||||
// Refresh Everything
|
// Refresh Everything
|
||||||
@@ -216,10 +229,13 @@ open class CardFeedViewModel(val localFilter: FeedFilter<Note>) : ViewModel() {
|
|||||||
@OptIn(ExperimentalTime::class)
|
@OptIn(ExperimentalTime::class)
|
||||||
fun invalidateInsertData(newItems: Set<Note>) {
|
fun invalidateInsertData(newItems: Set<Note>) {
|
||||||
bundlerInsert.invalidateList(newItems) {
|
bundlerInsert.invalidateList(newItems) {
|
||||||
|
val newObjects = it.flatten().toSet()
|
||||||
val (value, elapsed) = measureTimedValue {
|
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}")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user