Batch updates to enhance performance.

This commit is contained in:
Vitor Pamplona
2023-01-18 17:12:43 -05:00
parent 3846ae2af5
commit 4271ae77d2
2 changed files with 39 additions and 12 deletions

View File

@@ -1,5 +1,7 @@
package com.vitorpamplona.amethyst.model package com.vitorpamplona.amethyst.model
import android.os.Handler
import android.os.Looper
import androidx.lifecycle.LiveData import androidx.lifecycle.LiveData
import com.vitorpamplona.amethyst.service.NostrSingleEventDataSource import com.vitorpamplona.amethyst.service.NostrSingleEventDataSource
import com.vitorpamplona.amethyst.ui.note.toShortenHex import com.vitorpamplona.amethyst.ui.note.toShortenHex
@@ -40,7 +42,7 @@ class Note(val idHex: String) {
this.mentions = mentions this.mentions = mentions
this.replyTo = replyTo this.replyTo = replyTo
refreshObservers() invalidateData()
} }
fun formattedDateTime(timestamp: Long): String { fun formattedDateTime(timestamp: Long): String {
@@ -72,17 +74,17 @@ class Note(val idHex: String) {
fun addReply(note: Note) { fun addReply(note: Note) {
if (replies.add(note)) if (replies.add(note))
refreshObservers() invalidateData()
} }
fun addBoost(note: Note) { fun addBoost(note: Note) {
if (boosts.add(note)) if (boosts.add(note))
refreshObservers() invalidateData()
} }
fun addReaction(note: Note) { fun addReaction(note: Note) {
if (reactions.add(note)) if (reactions.add(note))
refreshObservers() invalidateData()
} }
fun isReactedBy(user: User): Boolean { fun isReactedBy(user: User): Boolean {
@@ -96,8 +98,18 @@ class Note(val idHex: String) {
// Observers line up here. // Observers line up here.
val live: NoteLiveData = NoteLiveData(this) val live: NoteLiveData = NoteLiveData(this)
private fun refreshObservers() { // Refreshes observers in batches.
live.refresh() val filterHandler = Handler(Looper.getMainLooper())
var handlerWaiting = false
@Synchronized
fun invalidateData() {
if (handlerWaiting) return
handlerWaiting = true
filterHandler.postDelayed({
live.refresh()
handlerWaiting = false
}, 100)
} }
} }

View File

@@ -1,5 +1,7 @@
package com.vitorpamplona.amethyst.model package com.vitorpamplona.amethyst.model
import android.os.Handler
import android.os.Looper
import androidx.lifecycle.LiveData import androidx.lifecycle.LiveData
import com.vitorpamplona.amethyst.service.NostrSingleEventDataSource import com.vitorpamplona.amethyst.service.NostrSingleEventDataSource
import com.vitorpamplona.amethyst.service.NostrSingleUserDataSource import com.vitorpamplona.amethyst.service.NostrSingleUserDataSource
@@ -28,7 +30,6 @@ class User(val pubkey: ByteArray) {
val taggedPosts = Collections.synchronizedSet(mutableSetOf<Note>()) val taggedPosts = Collections.synchronizedSet(mutableSetOf<Note>())
val followers = Collections.synchronizedSet(mutableSetOf<User>()) val followers = Collections.synchronizedSet(mutableSetOf<User>())
val messages = ConcurrentHashMap<User, MutableSet<Note>>() val messages = ConcurrentHashMap<User, MutableSet<Note>>()
fun toBestDisplayName(): String { fun toBestDisplayName(): String {
@@ -51,10 +52,16 @@ class User(val pubkey: ByteArray) {
fun follow(user: User) { fun follow(user: User) {
follows.add(user) follows.add(user)
user.followers.add(this) user.followers.add(this)
invalidateData()
user.invalidateData()
} }
fun unfollow(user: User) { fun unfollow(user: User) {
follows.remove(user) follows.remove(user)
user.followers.remove(this) user.followers.remove(this)
invalidateData()
user.invalidateData()
} }
@Synchronized @Synchronized
@@ -86,15 +93,13 @@ class User(val pubkey: ByteArray) {
} }
updatedFollowsAt = updateAt updatedFollowsAt = updateAt
refreshObservers()
} }
fun updateUserInfo(newUserInfo: UserMetadata, updateAt: Long) { fun updateUserInfo(newUserInfo: UserMetadata, updateAt: Long) {
info = newUserInfo info = newUserInfo
updatedMetadataAt = updateAt updatedMetadataAt = updateAt
refreshObservers() invalidateData()
} }
fun isFollowing(user: User): Boolean { fun isFollowing(user: User): Boolean {
@@ -106,8 +111,18 @@ class User(val pubkey: ByteArray) {
// Observers line up here. // Observers line up here.
val live: UserLiveData = UserLiveData(this) val live: UserLiveData = UserLiveData(this)
private fun refreshObservers() { // Refreshes observers in batches.
live.refresh() val filterHandler = Handler(Looper.getMainLooper())
var handlerWaiting = false
@Synchronized
fun invalidateData() {
if (handlerWaiting) return
handlerWaiting = true
filterHandler.postDelayed({
live.refresh()
handlerWaiting = false
}, 100)
} }
} }