From 47c21a33b491dca3392006e8f87d75acf2c375a2 Mon Sep 17 00:00:00 2001
From: ialokim <ialokim@mailbox.org>
Date: Sat, 26 Nov 2022 23:02:54 +0100
Subject: [PATCH] manually initialize threadViewModel to subscribe to events

---
 .../briar/desktop/forums/ForumScreen.kt       |  3 +-
 .../briar/desktop/forums/ForumViewModel.kt    | 30 ++++++++++++-------
 .../briar/desktop/forums/PostsState.kt        |  2 +-
 .../forums/ThreadedConversationViewModel.kt   |  4 +--
 4 files changed, 24 insertions(+), 15 deletions(-)

diff --git a/briar-desktop/src/main/kotlin/org/briarproject/briar/desktop/forums/ForumScreen.kt b/briar-desktop/src/main/kotlin/org/briarproject/briar/desktop/forums/ForumScreen.kt
index e38203b3af..a9a0e56950 100644
--- a/briar-desktop/src/main/kotlin/org/briarproject/briar/desktop/forums/ForumScreen.kt
+++ b/briar-desktop/src/main/kotlin/org/briarproject/briar/desktop/forums/ForumScreen.kt
@@ -63,8 +63,7 @@ fun ForumScreen(
             )
             VerticalDivider()
             Column(modifier = Modifier.weight(1f).fillMaxHeight()) {
-                val item = viewModel.selectedGroupItem.value
-                if (item == null) {
+                if (viewModel.selectedGroupId.value == null) {
                     NoForumSelected()
                 } else {
                     GroupConversationScreen(viewModel.threadViewModel)
diff --git a/briar-desktop/src/main/kotlin/org/briarproject/briar/desktop/forums/ForumViewModel.kt b/briar-desktop/src/main/kotlin/org/briarproject/briar/desktop/forums/ForumViewModel.kt
index a2bd6d3b8c..1e5c78569a 100644
--- a/briar-desktop/src/main/kotlin/org/briarproject/briar/desktop/forums/ForumViewModel.kt
+++ b/briar-desktop/src/main/kotlin/org/briarproject/briar/desktop/forums/ForumViewModel.kt
@@ -63,14 +63,14 @@ class ForumViewModel @Inject constructor(
 
     val noForumsYet = derivedStateOf { _fullForumList.isEmpty() }
 
-    private val _selectedGroupItem = mutableStateOf<GroupItem?>(null)
-    val selectedGroupItem = derivedStateOf {
+    private val _selectedGroupId = mutableStateOf<GroupId?>(null)
+    val selectedGroupId = derivedStateOf {
         // reset selected group item to null if not part of list after filtering
-        val groupItem = _selectedGroupItem.value
-        if (groupItem == null || forumList.value.any { it.id == groupItem.id }) {
-            groupItem
+        val groupId = _selectedGroupId.value
+        if (groupId == null || forumList.value.any { it.id == groupId }) {
+            groupId
         } else {
-            _selectedGroupItem.value = null
+            _selectedGroupId.value = null
             null
         }
     }
@@ -80,9 +80,18 @@ class ForumViewModel @Inject constructor(
 
     override fun onInit() {
         super.onInit()
+        // since the threadViewModel is tightly coupled to the ForumViewModel
+        // and not injected using the usual `viewModel()` approach,
+        // we have to manually call the functions for (de)initialization
+        threadViewModel.onInit()
         loadGroups()
     }
 
+    override fun onCleared() {
+        super.onCleared()
+        threadViewModel.onCleared()
+    }
+
     override fun eventOccurred(e: Event) {
         when {
             e is GroupAddedEvent && e.group.clientId == ForumManager.CLIENT_ID ->
@@ -90,7 +99,7 @@ class ForumViewModel @Inject constructor(
 
             e is GroupRemovedEvent && e.group.clientId == ForumManager.CLIENT_ID -> {
                 removeItem(e.group.id)
-                if (selectedGroupItem.value?.id == e.group.id) _selectedGroupItem.value = null
+                if (_selectedGroupId.value == e.group.id) _selectedGroupId.value = null
             }
 
             e is ForumPostReceivedEvent -> {
@@ -126,18 +135,19 @@ class ForumViewModel @Inject constructor(
     }
 
     fun selectGroup(groupItem: GroupItem) {
-        _selectedGroupItem.value = groupItem
+        if (_selectedGroupId.value == groupItem.id) return
+        _selectedGroupId.value = groupItem.id
         threadViewModel.setGroupItem(groupItem, this::addOwnPost)
     }
 
-    fun isSelected(groupId: GroupId) = _selectedGroupItem.value?.id == groupId
+    fun isSelected(groupId: GroupId) = _selectedGroupId.value == groupId
 
     fun setFilterBy(filter: String) {
         _filterBy.value = filter
     }
 
     private fun addOwnPost(header: ForumPostHeader) {
-        selectedGroupItem.value?.id?.let { id -> updateItem(id) { it.updateOnPostReceived(header) } }
+        selectedGroupId.value?.let { id -> updateItem(id) { it.updateOnPostReceived(header) } }
     }
 
     private fun addItem(forumItem: ForumItem) = _fullForumList.add(forumItem)
diff --git a/briar-desktop/src/main/kotlin/org/briarproject/briar/desktop/forums/PostsState.kt b/briar-desktop/src/main/kotlin/org/briarproject/briar/desktop/forums/PostsState.kt
index 2996d0c2da..67a4355bc1 100644
--- a/briar-desktop/src/main/kotlin/org/briarproject/briar/desktop/forums/PostsState.kt
+++ b/briar-desktop/src/main/kotlin/org/briarproject/briar/desktop/forums/PostsState.kt
@@ -28,7 +28,7 @@ class Loaded(
     val messageTree: MessageTreeImpl<ThreadItem>,
     val scrollTo: MessageId? = null,
 ) : PostsState() {
-    val posts: MutableList<ThreadItem> = messageTree.depthFirstOrder()
+    val posts: List<ThreadItem> = messageTree.depthFirstOrder()
 
     @UiExecutor
     fun unreadBeforeIndex(index: Int): UnreadPostInfo {
diff --git a/briar-desktop/src/main/kotlin/org/briarproject/briar/desktop/forums/ThreadedConversationViewModel.kt b/briar-desktop/src/main/kotlin/org/briarproject/briar/desktop/forums/ThreadedConversationViewModel.kt
index b196a0232a..4b62526064 100644
--- a/briar-desktop/src/main/kotlin/org/briarproject/briar/desktop/forums/ThreadedConversationViewModel.kt
+++ b/briar-desktop/src/main/kotlin/org/briarproject/briar/desktop/forums/ThreadedConversationViewModel.kt
@@ -24,6 +24,7 @@ import kotlinx.coroutines.DelicateCoroutinesApi
 import kotlinx.coroutines.GlobalScope
 import kotlinx.coroutines.launch
 import kotlinx.coroutines.withContext
+import mu.KotlinLogging
 import org.briarproject.bramble.api.crypto.CryptoExecutor
 import org.briarproject.bramble.api.db.TransactionManager
 import org.briarproject.bramble.api.event.Event
@@ -43,7 +44,6 @@ import org.briarproject.briar.desktop.threading.BriarExecutors
 import org.briarproject.briar.desktop.threading.UiExecutor
 import org.briarproject.briar.desktop.viewmodel.EventListenerDbViewModel
 import org.briarproject.briar.desktop.viewmodel.asState
-import org.slf4j.LoggerFactory.getLogger
 import java.lang.Long.max
 import javax.inject.Inject
 
@@ -59,7 +59,7 @@ class ThreadedConversationViewModel @Inject constructor(
 ) : EventListenerDbViewModel(briarExecutors, lifecycleManager, db, eventBus) {
 
     companion object {
-        private val LOG = getLogger(ThreadedConversationViewModel::class.java)
+        private val LOG = KotlinLogging.logger {}
     }
 
     lateinit var groupItem: GroupItem
-- 
GitLab