From 20f7e7245a174a14b6cc6bb3c669003756d3a59c Mon Sep 17 00:00:00 2001
From: ialokim <ialokim@mailbox.org>
Date: Tue, 14 Dec 2021 18:56:34 +0100
Subject: [PATCH] actually trigger introduction in UI

---
 .../briar/desktop/contact/ContactInfoDrawer.kt     |  4 ++--
 .../desktop/conversation/ConversationScreen.kt     | 11 ++++++++++-
 .../desktop/conversation/ConversationViewModel.kt  |  6 +++++-
 .../desktop/introduction/ContactDrawerMakeIntro.kt |  9 ++++++---
 .../desktop/introduction/IntroductionViewModel.kt  | 14 ++++++++++++++
 5 files changed, 37 insertions(+), 7 deletions(-)

diff --git a/src/main/kotlin/org/briarproject/briar/desktop/contact/ContactInfoDrawer.kt b/src/main/kotlin/org/briarproject/briar/desktop/contact/ContactInfoDrawer.kt
index 43462c4da9..c7d7433540 100644
--- a/src/main/kotlin/org/briarproject/briar/desktop/contact/ContactInfoDrawer.kt
+++ b/src/main/kotlin/org/briarproject/briar/desktop/contact/ContactInfoDrawer.kt
@@ -14,10 +14,10 @@ enum class ContactInfoDrawerState {
 @Composable
 fun ContactInfoDrawer(
     contactItem: ContactItem,
-    setInfoDrawer: (Boolean) -> Unit,
+    closeInfoDrawer: () -> Unit,
     drawerState: ContactInfoDrawerState
 ) {
     when (drawerState) {
-        MakeIntro -> ContactDrawerMakeIntro(contactItem, setInfoDrawer)
+        MakeIntro -> ContactDrawerMakeIntro(contactItem, closeInfoDrawer)
     }
 }
diff --git a/src/main/kotlin/org/briarproject/briar/desktop/conversation/ConversationScreen.kt b/src/main/kotlin/org/briarproject/briar/desktop/conversation/ConversationScreen.kt
index a842320ce8..1299e27b82 100644
--- a/src/main/kotlin/org/briarproject/briar/desktop/conversation/ConversationScreen.kt
+++ b/src/main/kotlin/org/briarproject/briar/desktop/conversation/ConversationScreen.kt
@@ -125,7 +125,16 @@ fun ConversationScreen(
                         RoundedCornerShape(topStart = 10.dp, bottomStart = 10.dp)
                     )
             ) {
-                ContactInfoDrawer(contactItem, setInfoDrawer, contactDrawerState)
+                ContactInfoDrawer(
+                    contactItem,
+                    closeInfoDrawer = {
+                        setInfoDrawer(false)
+                        // reload all messages to also show introduction message
+                        // todo: might be better to have an event to react to, also for (all) outgoing messages
+                        viewModel.reloadMessages()
+                    },
+                    contactDrawerState
+                )
             }
         }
     }
diff --git a/src/main/kotlin/org/briarproject/briar/desktop/conversation/ConversationViewModel.kt b/src/main/kotlin/org/briarproject/briar/desktop/conversation/ConversationViewModel.kt
index 70dc749822..adbbb13391 100644
--- a/src/main/kotlin/org/briarproject/briar/desktop/conversation/ConversationViewModel.kt
+++ b/src/main/kotlin/org/briarproject/briar/desktop/conversation/ConversationViewModel.kt
@@ -182,6 +182,10 @@ constructor(
         }
     }
 
+    fun reloadMessages() = runOnDbThreadWithTransaction(true) { txn ->
+        loadMessages(txn, contactItem.value!!)
+    }
+
     private fun loadMessages(txn: Transaction, contact: ContactItem) {
         try {
             var start = LogUtils.now()
@@ -282,7 +286,7 @@ constructor(
                     throw IllegalArgumentException("Only introduction requests are supported for the time being.")
             }
             // reload all messages to also show request response message
-            // todo: might be better to have an event to react to, also for (all) outgoing messages
+            // todo: might be better to have an event to react to, also for (other) outgoing messages
             loadMessages(txn, contactItem.value!!)
         }
     }
diff --git a/src/main/kotlin/org/briarproject/briar/desktop/introduction/ContactDrawerMakeIntro.kt b/src/main/kotlin/org/briarproject/briar/desktop/introduction/ContactDrawerMakeIntro.kt
index e0081588c2..1a471e98ba 100644
--- a/src/main/kotlin/org/briarproject/briar/desktop/introduction/ContactDrawerMakeIntro.kt
+++ b/src/main/kotlin/org/briarproject/briar/desktop/introduction/ContactDrawerMakeIntro.kt
@@ -39,7 +39,7 @@ import java.util.Locale
 @Composable
 fun ContactDrawerMakeIntro(
     contactItem: ContactItem,
-    setInfoDrawer: (Boolean) -> Unit,
+    closeInfoDrawer: () -> Unit,
     viewModel: IntroductionViewModel = viewModel(),
 ) {
     LaunchedEffect(contactItem) {
@@ -50,7 +50,7 @@ fun ContactDrawerMakeIntro(
             Column {
                 Row(Modifier.fillMaxWidth().height(HEADER_SIZE)) {
                     IconButton(
-                        onClick = { setInfoDrawer(false) },
+                        onClick = closeInfoDrawer,
                         Modifier.padding(horizontal = 11.dp).size(32.dp).align(Alignment.CenterVertically)
                     ) {
                         Icon(Icons.Filled.Close, i18n("access.introduction.close"))
@@ -109,7 +109,10 @@ fun ContactDrawerMakeIntro(
             }
             Row(Modifier.padding(8.dp)) {
                 TextButton(
-                    onClick = { setInfoDrawer(false) },
+                    onClick = {
+                        viewModel.makeIntroduction()
+                        closeInfoDrawer()
+                    },
                     Modifier.fillMaxWidth()
                 ) {
                     val text = i18n("introduction.introduce")
diff --git a/src/main/kotlin/org/briarproject/briar/desktop/introduction/IntroductionViewModel.kt b/src/main/kotlin/org/briarproject/briar/desktop/introduction/IntroductionViewModel.kt
index 4b6bb3ef97..7ee9d42f34 100644
--- a/src/main/kotlin/org/briarproject/briar/desktop/introduction/IntroductionViewModel.kt
+++ b/src/main/kotlin/org/briarproject/briar/desktop/introduction/IntroductionViewModel.kt
@@ -7,6 +7,7 @@ import org.briarproject.bramble.api.db.TransactionManager
 import org.briarproject.bramble.api.event.EventBus
 import org.briarproject.bramble.api.lifecycle.LifecycleManager
 import org.briarproject.briar.api.conversation.ConversationManager
+import org.briarproject.briar.api.introduction.IntroductionManager
 import org.briarproject.briar.desktop.contact.BaseContactItem
 import org.briarproject.briar.desktop.contact.ContactItem
 import org.briarproject.briar.desktop.contact.ContactsViewModel
@@ -17,6 +18,7 @@ import javax.inject.Inject
 class IntroductionViewModel
 @Inject
 constructor(
+    private val introductionManager: IntroductionManager,
     contactManager: ContactManager,
     conversationManager: ConversationManager,
     connectionRegistry: ConnectionRegistry,
@@ -63,4 +65,16 @@ constructor(
             _firstContact.value?.idWrapper != contactItem.idWrapper
         } else false
     }
+
+    fun makeIntroduction() {
+        val c1 = requireNotNull(_firstContact.value)
+        val c2 = requireNotNull(_secondContact.value)
+        val msg = _introductionMessage.value
+
+        runOnDbThread {
+            val c1 = contactManager.getContact(c1.idWrapper.contactId)
+            val c2 = contactManager.getContact(c2.idWrapper.contactId)
+            introductionManager.makeIntroduction(c1, c2, msg)
+        }
+    }
 }
-- 
GitLab