perf: bridge objects

- bridge oneway methods
- scope sync
This commit is contained in:
rhunk
2024-01-16 00:01:37 +01:00
parent 0994b8f36d
commit 8c71e4af27
16 changed files with 232 additions and 187 deletions

View File

@@ -8,6 +8,7 @@ import java.io.ByteArrayOutputStream
plugins {
alias(libs.plugins.androidApplication)
alias(libs.plugins.kotlinAndroid)
id("kotlin-parcelize")
}
android {

View File

@@ -12,9 +12,8 @@ import me.rhunk.snapenhance.common.bridge.wrapper.LocaleWrapper
import me.rhunk.snapenhance.common.data.MessagingFriendInfo
import me.rhunk.snapenhance.common.data.MessagingGroupInfo
import me.rhunk.snapenhance.common.data.SocialScope
import me.rhunk.snapenhance.common.database.impl.FriendInfo
import me.rhunk.snapenhance.common.logger.LogLevel
import me.rhunk.snapenhance.common.util.SerializableDataObject
import me.rhunk.snapenhance.common.util.toParcelable
import me.rhunk.snapenhance.download.DownloadProcessor
import kotlin.system.measureTimeMillis
@@ -61,12 +60,12 @@ class BridgeService : Service() {
when (scope) {
SocialScope.FRIEND -> {
SerializableDataObject.fromJson<FriendInfo>(syncedObject).let {
toParcelable<MessagingFriendInfo>(syncedObject)?.let {
modDatabase.syncFriend(it)
}
}
SocialScope.GROUP -> {
SerializableDataObject.fromJson<MessagingGroupInfo>(syncedObject).let {
toParcelable<MessagingGroupInfo>(syncedObject)?.let {
modDatabase.syncGroupInfo(it)
}
}
@@ -169,8 +168,8 @@ class BridgeService : Service() {
) {
remoteSideContext.log.verbose("Received ${groups.size} groups and ${friends.size} friends")
remoteSideContext.modDatabase.receiveMessagingDataCallback(
friends.map { SerializableDataObject.fromJson<MessagingFriendInfo>(it) },
groups.map { SerializableDataObject.fromJson<MessagingGroupInfo>(it) }
friends.mapNotNull { toParcelable<MessagingFriendInfo>(it) },
groups.mapNotNull { toParcelable<MessagingGroupInfo>(it) }
)
}

View File

@@ -6,7 +6,6 @@ import me.rhunk.snapenhance.common.data.FriendStreaks
import me.rhunk.snapenhance.common.data.MessagingFriendInfo
import me.rhunk.snapenhance.common.data.MessagingGroupInfo
import me.rhunk.snapenhance.common.data.MessagingRuleType
import me.rhunk.snapenhance.common.database.impl.FriendInfo
import me.rhunk.snapenhance.common.scripting.type.ModuleInfo
import me.rhunk.snapenhance.common.util.SQLiteDatabaseHelper
import me.rhunk.snapenhance.common.util.ktx.getInteger
@@ -56,7 +55,7 @@ class ModDatabase(
"targetUuid VARCHAR"
),
"streaks" to listOf(
"userId VARCHAR PRIMARY KEY",
"id VARCHAR PRIMARY KEY",
"notify BOOLEAN",
"expirationTimestamp BIGINT",
"length INTEGER"
@@ -78,10 +77,10 @@ class ModDatabase(
while (cursor.moveToNext()) {
groups.add(
MessagingGroupInfo(
conversationId = cursor.getStringOrNull("conversationId")!!,
name = cursor.getStringOrNull("name")!!,
participantsCount = cursor.getInteger("participantsCount")
)
conversationId = cursor.getStringOrNull("conversationId")!!,
name = cursor.getStringOrNull("name")!!,
participantsCount = cursor.getInteger("participantsCount")
)
)
}
groups
@@ -89,18 +88,25 @@ class ModDatabase(
}
fun getFriends(descOrder: Boolean = false): List<MessagingFriendInfo> {
return database.rawQuery("SELECT * FROM friends ORDER BY id ${if (descOrder) "DESC" else "ASC"}", null).use { cursor ->
return database.rawQuery("SELECT * FROM friends LEFT OUTER JOIN streaks ON friends.userId = streaks.id ORDER BY id ${if (descOrder) "DESC" else "ASC"}", null).use { cursor ->
val friends = mutableListOf<MessagingFriendInfo>()
while (cursor.moveToNext()) {
runCatching {
friends.add(
MessagingFriendInfo(
userId = cursor.getStringOrNull("userId")!!,
displayName = cursor.getStringOrNull("displayName"),
mutableUsername = cursor.getStringOrNull("mutableUsername")!!,
bitmojiId = cursor.getStringOrNull("bitmojiId"),
selfieId = cursor.getStringOrNull("selfieId")
)
userId = cursor.getStringOrNull("userId")!!,
displayName = cursor.getStringOrNull("displayName"),
mutableUsername = cursor.getStringOrNull("mutableUsername")!!,
bitmojiId = cursor.getStringOrNull("bitmojiId"),
selfieId = cursor.getStringOrNull("selfieId"),
streaks = cursor.getLongOrNull("expirationTimestamp")?.let {
FriendStreaks(
notify = cursor.getInteger("notify") == 1,
expirationTimestamp = it,
length = cursor.getInteger("length")
)
}
)
)
}.onFailure {
context.log.error("Failed to parse friend", it)
@@ -125,7 +131,7 @@ class ModDatabase(
}
}
fun syncFriend(friend: FriendInfo) {
fun syncFriend(friend: MessagingFriendInfo) {
executeAsync {
try {
database.execSQL(
@@ -133,24 +139,22 @@ class ModDatabase(
arrayOf(
friend.userId,
friend.displayName,
friend.usernameForSorting!!,
friend.bitmojiAvatarId,
friend.bitmojiSelfieId
friend.mutableUsername,
friend.bitmojiId,
friend.selfieId
)
)
//sync streaks
if (friend.streakLength > 0) {
val streaks = getFriendStreaks(friend.userId!!)
friend.streaks?.takeIf { it.length > 0 }?.let {
val streaks = getFriendStreaks(friend.userId)
database.execSQL("INSERT OR REPLACE INTO streaks (userId, notify, expirationTimestamp, length) VALUES (?, ?, ?, ?)", arrayOf(
database.execSQL("INSERT OR REPLACE INTO streaks (id, notify, expirationTimestamp, length) VALUES (?, ?, ?, ?)", arrayOf(
friend.userId,
streaks?.notify ?: true,
friend.streakExpirationTimestamp,
friend.streakLength
it.expirationTimestamp,
it.length
))
} else {
database.execSQL("DELETE FROM streaks WHERE userId = ?", arrayOf(friend.userId))
}
} ?: database.execSQL("DELETE FROM streaks WHERE id = ?", arrayOf(friend.userId))
} catch (e: Exception) {
throw e
}
@@ -190,14 +194,21 @@ class ModDatabase(
}
fun getFriendInfo(userId: String): MessagingFriendInfo? {
return database.rawQuery("SELECT * FROM friends WHERE userId = ?", arrayOf(userId)).use { cursor ->
return database.rawQuery("SELECT * FROM friends LEFT OUTER JOIN streaks ON friends.userId = streaks.id WHERE userId = ?", arrayOf(userId)).use { cursor ->
if (!cursor.moveToFirst()) return@use null
MessagingFriendInfo(
userId = cursor.getStringOrNull("userId")!!,
displayName = cursor.getStringOrNull("displayName"),
mutableUsername = cursor.getStringOrNull("mutableUsername")!!,
bitmojiId = cursor.getStringOrNull("bitmojiId"),
selfieId = cursor.getStringOrNull("selfieId")
selfieId = cursor.getStringOrNull("selfieId"),
streaks = cursor.getLongOrNull("expirationTimestamp")?.let {
FriendStreaks(
notify = cursor.getInteger("notify") == 1,
expirationTimestamp = it,
length = cursor.getInteger("length")
)
}
)
}
}
@@ -205,7 +216,7 @@ class ModDatabase(
fun deleteFriend(userId: String) {
executeAsync {
database.execSQL("DELETE FROM friends WHERE userId = ?", arrayOf(userId))
database.execSQL("DELETE FROM streaks WHERE userId = ?", arrayOf(userId))
database.execSQL("DELETE FROM streaks WHERE id = ?", arrayOf(userId))
database.execSQL("DELETE FROM rules WHERE targetUuid = ?", arrayOf(userId))
}
}
@@ -229,10 +240,9 @@ class ModDatabase(
}
fun getFriendStreaks(userId: String): FriendStreaks? {
return database.rawQuery("SELECT * FROM streaks WHERE userId = ?", arrayOf(userId)).use { cursor ->
return database.rawQuery("SELECT * FROM streaks WHERE id = ?", arrayOf(userId)).use { cursor ->
if (!cursor.moveToFirst()) return@use null
FriendStreaks(
userId = cursor.getStringOrNull("userId")!!,
notify = cursor.getInteger("notify") == 1,
expirationTimestamp = cursor.getLongOrNull("expirationTimestamp") ?: 0L,
length = cursor.getInteger("length")
@@ -242,7 +252,7 @@ class ModDatabase(
fun setFriendStreaksNotify(userId: String, notify: Boolean) {
executeAsync {
database.execSQL("UPDATE streaks SET notify = ? WHERE userId = ?", arrayOf(
database.execSQL("UPDATE streaks SET notify = ? WHERE id = ?", arrayOf(
if (notify) 1 else 0,
userId
))

View File

@@ -26,7 +26,9 @@ import androidx.compose.ui.window.Dialog
import androidx.navigation.NavGraphBuilder
import androidx.navigation.compose.composable
import androidx.navigation.navigation
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import me.rhunk.snapenhance.R
import me.rhunk.snapenhance.common.data.MessagingFriendInfo
import me.rhunk.snapenhance.common.data.MessagingGroupInfo
@@ -214,8 +216,13 @@ class SocialSection : Section() {
SocialScope.FRIEND -> {
val friend = friendList[index]
val streaks =
remember { context.modDatabase.getFriendStreaks(friend.userId) }
var streaks by remember { mutableStateOf(friend.streaks) }
LaunchedEffect(friend.userId) {
withContext(Dispatchers.IO) {
streaks = context.modDatabase.getFriendStreaks(friend.userId)
}
}
BitmojiImage(
context = context,
@@ -244,7 +251,7 @@ class SocialSection : Section() {
)
}
Row(verticalAlignment = Alignment.CenterVertically) {
if (streaks != null && streaks.notify) {
streaks?.takeIf { it.notify }?.let { streaks ->
Icon(
imageVector = ImageVector.vectorResource(id = R.drawable.streak_icon),
contentDescription = null,
@@ -256,7 +263,7 @@ class SocialSection : Section() {
Text(
text = context.translation.format(
"manager.sections.social.streaks_expiration_short",
"hours" to ((streaks.expirationTimestamp - System.currentTimeMillis()) / 3600000).toInt()
"hours" to (((streaks.expirationTimestamp - System.currentTimeMillis()) / 3600000).toInt().takeIf { it > 0 } ?: 0)
.toString()
),
maxLines = 1,