From c806ff2e3346590906dfa27d74806fd3bab3a879 Mon Sep 17 00:00:00 2001
From: Koen <koen@pop-os.localdomain>
Date: Wed, 29 Nov 2023 13:54:26 +0100
Subject: [PATCH] Added support for comment deletion.

---
 .../mainactivity/main/CommentsFragment.kt     | 50 ++++++++++---------
 .../views/segments/CommentsList.kt            | 50 ++++++++++---------
 2 files changed, 52 insertions(+), 48 deletions(-)

diff --git a/app/src/main/java/com/futo/platformplayer/fragment/mainactivity/main/CommentsFragment.kt b/app/src/main/java/com/futo/platformplayer/fragment/mainactivity/main/CommentsFragment.kt
index 97d5200e..eb1bb34b 100644
--- a/app/src/main/java/com/futo/platformplayer/fragment/mainactivity/main/CommentsFragment.kt
+++ b/app/src/main/java/com/futo/platformplayer/fragment/mainactivity/main/CommentsFragment.kt
@@ -154,33 +154,35 @@ class CommentsFragment : MainFragment() {
         }
 
         private fun onDelete(comment: IPlatformComment) {
-            val processHandle = StatePolycentric.instance.processHandle ?: return
-            if (comment !is PolycentricPlatformComment) {
-                return
-            }
-
-            val index = _comments.indexOf(comment)
-            if (index != -1) {
-                _comments.removeAt(index)
-                _adapterComments.notifyItemRemoved(_adapterComments.childToParentPosition(index))
-
-                StateApp.instance.scopeOrNull?.launch(Dispatchers.IO) {
-                    try {
-                        processHandle.delete(comment.eventPointer.process, comment.eventPointer.logicalClock)
-                    } catch (e: Throwable) {
-                        Logger.e(TAG, "Failed to delete event.", e);
-                        return@launch;
-                    }
+            UIDialogs.showConfirmationDialog(context, "Are you sure you want to delete this comment?", {
+                val processHandle = StatePolycentric.instance.processHandle ?: return@showConfirmationDialog
+                if (comment !is PolycentricPlatformComment) {
+                    return@showConfirmationDialog
+                }
 
-                    try {
-                        Logger.i(TAG, "Started backfill");
-                        processHandle.fullyBackfillServersAnnounceExceptions();
-                        Logger.i(TAG, "Finished backfill");
-                    } catch (e: Throwable) {
-                        Logger.e(TAG, "Failed to fully backfill servers.", e);
+                val index = _comments.indexOf(comment)
+                if (index != -1) {
+                    _comments.removeAt(index)
+                    _adapterComments.notifyItemRemoved(_adapterComments.childToParentPosition(index))
+
+                    StateApp.instance.scopeOrNull?.launch(Dispatchers.IO) {
+                        try {
+                            processHandle.delete(comment.eventPointer.process, comment.eventPointer.logicalClock)
+                        } catch (e: Throwable) {
+                            Logger.e(TAG, "Failed to delete event.", e);
+                            return@launch
+                        }
+
+                        try {
+                            Logger.i(TAG, "Started backfill");
+                            processHandle.fullyBackfillServersAnnounceExceptions();
+                            Logger.i(TAG, "Finished backfill");
+                        } catch (e: Throwable) {
+                            Logger.e(TAG, "Failed to fully backfill servers.", e);
+                        }
                     }
                 }
-            }
+            })
         }
 
         fun onBackPressed(): Boolean {
diff --git a/app/src/main/java/com/futo/platformplayer/views/segments/CommentsList.kt b/app/src/main/java/com/futo/platformplayer/views/segments/CommentsList.kt
index 29adc859..e377d81d 100644
--- a/app/src/main/java/com/futo/platformplayer/views/segments/CommentsList.kt
+++ b/app/src/main/java/com/futo/platformplayer/views/segments/CommentsList.kt
@@ -111,33 +111,35 @@ class CommentsList : ConstraintLayout {
     }
 
     private fun onDelete(comment: IPlatformComment) {
-        val processHandle = StatePolycentric.instance.processHandle ?: return
-        if (comment !is PolycentricPlatformComment) {
-            return
-        }
-
-        val index = _comments.indexOf(comment)
-        if (index != -1) {
-            _comments.removeAt(index)
-            _adapterComments.notifyItemRemoved(_adapterComments.childToParentPosition(index))
-
-            StateApp.instance.scopeOrNull?.launch(Dispatchers.IO) {
-                try {
-                    processHandle.delete(comment.eventPointer.process, comment.eventPointer.logicalClock)
-                } catch (e: Throwable) {
-                    Logger.e(TAG, "Failed to delete event.", e);
-                    return@launch;
-                }
+        UIDialogs.showConfirmationDialog(context, "Are you sure you want to delete this comment?", {
+            val processHandle = StatePolycentric.instance.processHandle ?: return@showConfirmationDialog
+            if (comment !is PolycentricPlatformComment) {
+                return@showConfirmationDialog
+            }
 
-                try {
-                    Logger.i(TAG, "Started backfill");
-                    processHandle.fullyBackfillServersAnnounceExceptions();
-                    Logger.i(TAG, "Finished backfill");
-                } catch (e: Throwable) {
-                    Logger.e(TAG, "Failed to fully backfill servers.", e);
+            val index = _comments.indexOf(comment)
+            if (index != -1) {
+                _comments.removeAt(index)
+                _adapterComments.notifyItemRemoved(_adapterComments.childToParentPosition(index))
+
+                StateApp.instance.scopeOrNull?.launch(Dispatchers.IO) {
+                    try {
+                        processHandle.delete(comment.eventPointer.process, comment.eventPointer.logicalClock)
+                    } catch (e: Throwable) {
+                        Logger.e(TAG, "Failed to delete event.", e);
+                        return@launch;
+                    }
+
+                    try {
+                        Logger.i(TAG, "Started backfill");
+                        processHandle.fullyBackfillServersAnnounceExceptions();
+                        Logger.i(TAG, "Finished backfill");
+                    } catch (e: Throwable) {
+                        Logger.e(TAG, "Failed to fully backfill servers.", e);
+                    }
                 }
             }
-        }
+        })
     }
 
     private fun onScrolled() {
-- 
GitLab