commit 4bd7328ac45b4a3fcb2af10d68105fb527554a20
parent 242ff1712e08439991dadb8995fa5ade9858b815
Author: rhunk <101876869+rhunk@users.noreply.github.com>
Date:   Wed, 21 Aug 2024 11:22:25 +0200

feat(scripting/messaging): conversation manager ready event

Diffstat:
Mcore/src/main/kotlin/me/rhunk/snapenhance/core/features/impl/messaging/Messaging.kt | 24+++++++++++++++++-------
Mcore/src/main/kotlin/me/rhunk/snapenhance/core/scripting/impl/CoreMessaging.kt | 7+++++++
2 files changed, 24 insertions(+), 7 deletions(-)

diff --git a/core/src/main/kotlin/me/rhunk/snapenhance/core/features/impl/messaging/Messaging.kt b/core/src/main/kotlin/me/rhunk/snapenhance/core/features/impl/messaging/Messaging.kt @@ -40,6 +40,13 @@ class Messaging : Feature("Messaging") { private set private val feedCachedSnapMessages = EvictingMap<String, List<Long>>(100) + private val conversationManagerReadyListeners = mutableListOf<() -> Unit>() + + fun onConversationManagerReady(listener: () -> Unit) { + synchronized(conversationManagerReadyListeners) { + conversationManager?.let { listener() } ?: conversationManagerReadyListeners.add(listener) + } + } fun resetLastFocusedConversation() { lastFocusedConversationId = null @@ -49,13 +56,16 @@ class Messaging : Feature("Messaging") { override fun init() { val stealthMode = context.feature(StealthMode::class) context.classCache.conversationManager.hookConstructor(HookStage.BEFORE) { param -> - conversationManager = ConversationManager(context, param.thisObject()) - context.messagingBridge.triggerSessionStart() - context.mainActivity?.takeIf { it.intent.getBooleanExtra(ReceiversConfig.MESSAGING_PREVIEW_EXTRA, false) }?.run { - startActivity(Intent().apply { - setComponent(ComponentName(Constants.SE_PACKAGE_NAME, "me.rhunk.snapenhance.ui.manager.MainActivity")) - addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) - }) + synchronized(conversationManagerReadyListeners) { + conversationManager = ConversationManager(context, param.thisObject()) + context.messagingBridge.triggerSessionStart() + context.mainActivity?.takeIf { it.intent.getBooleanExtra(ReceiversConfig.MESSAGING_PREVIEW_EXTRA, false) }?.run { + startActivity(Intent().apply { + setComponent(ComponentName(Constants.SE_PACKAGE_NAME, "me.rhunk.snapenhance.ui.manager.MainActivity")) + addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) + }) + } + conversationManagerReadyListeners.removeIf { it(); true } } } diff --git a/core/src/main/kotlin/me/rhunk/snapenhance/core/scripting/impl/CoreMessaging.kt b/core/src/main/kotlin/me/rhunk/snapenhance/core/scripting/impl/CoreMessaging.kt @@ -20,6 +20,13 @@ class CoreMessaging( private val conversationManager get() = messaging.conversationManager @JSFunction + fun onConversationManagerReady(callback: () -> Unit) { + messaging.onConversationManagerReady { + callback() + } + } + + @JSFunction fun isPresent() = conversationManager != null @JSFunction