From a93910f8cb8492923a1b18fd3b768a70122f4dd7 Mon Sep 17 00:00:00 2001
From: greenart7c3 <greenart7c3@proton.me>
Date: Sat, 7 Dec 2024 11:17:38 -0300
Subject: [PATCH] Move relay dialog to a route

---
 .../ui/actions/relays/AllRelayListView.kt     | 297 +++++++++---------
 .../amethyst/ui/navigation/AppNavigation.kt   |  11 +
 .../amethyst/ui/navigation/DrawerContent.kt   |   7 +-
 .../amethyst/ui/navigation/Routes.kt          |   7 +
 .../amethyst/ui/note/types/RelayList.kt       |  19 +-
 .../screen/loggedIn/profile/RelayFeedView.kt  |  21 +-
 6 files changed, 187 insertions(+), 175 deletions(-)

diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/actions/relays/AllRelayListView.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/actions/relays/AllRelayListView.kt
index c3dce2b17..08bae79e3 100644
--- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/actions/relays/AllRelayListView.kt
+++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/actions/relays/AllRelayListView.kt
@@ -37,6 +37,7 @@ import androidx.compose.material3.Text
 import androidx.compose.material3.TopAppBar
 import androidx.compose.material3.TopAppBarDefaults
 import androidx.compose.runtime.Composable
+import androidx.compose.runtime.DisposableEffect
 import androidx.compose.runtime.LaunchedEffect
 import androidx.compose.runtime.getValue
 import androidx.compose.runtime.setValue
@@ -45,8 +46,6 @@ import androidx.compose.ui.Modifier
 import androidx.compose.ui.text.style.TextAlign
 import androidx.compose.ui.text.style.TextOverflow
 import androidx.compose.ui.unit.dp
-import androidx.compose.ui.window.Dialog
-import androidx.compose.ui.window.DialogProperties
 import androidx.lifecycle.compose.collectAsStateWithLifecycle
 import androidx.lifecycle.viewmodel.compose.viewModel
 import com.vitorpamplona.amethyst.R
@@ -66,20 +65,25 @@ import com.vitorpamplona.amethyst.ui.theme.grayText
 import com.vitorpamplona.ammolite.relays.Constants
 import com.vitorpamplona.ammolite.relays.RelayStat
 
+object RelayToAdd {
+    var relayToAdd: String = ""
+}
+
 @Composable
 fun AllRelayListView(
-    onClose: () -> Unit,
-    relayToAdd: String = "",
     accountViewModel: AccountViewModel,
     nav: INav,
 ) {
-    MappedAllRelayListView(onClose, relayToAdd, accountViewModel, rememberExtendedNav(nav, onClose))
+    DisposableEffect(Unit) {
+        onDispose { RelayToAdd.relayToAdd = "" }
+    }
+
+    MappedAllRelayListView(RelayToAdd.relayToAdd, accountViewModel, rememberExtendedNav(nav, {}))
 }
 
 @OptIn(ExperimentalMaterial3Api::class)
 @Composable
 fun MappedAllRelayListView(
-    onClose: () -> Unit,
     relayToAdd: String = "",
     accountViewModel: AccountViewModel,
     newNav: INav,
@@ -113,154 +117,149 @@ fun MappedAllRelayListView(
         privateOutboxViewModel.load(accountViewModel.account)
     }
 
-    Dialog(
-        onDismissRequest = onClose,
-        properties = DialogProperties(usePlatformDefaultWidth = false),
-    ) {
-        Scaffold(
-            topBar = {
-                TopAppBar(
-                    title = {
-                        Row(
-                            modifier = Modifier.fillMaxWidth(),
-                            horizontalArrangement = Arrangement.SpaceBetween,
-                            verticalAlignment = Alignment.CenterVertically,
-                        ) {
-                            Spacer(modifier = MinHorzSpacer)
+    Scaffold(
+        topBar = {
+            TopAppBar(
+                title = {
+                    Row(
+                        modifier = Modifier.fillMaxWidth(),
+                        horizontalArrangement = Arrangement.SpaceBetween,
+                        verticalAlignment = Alignment.CenterVertically,
+                    ) {
+                        Spacer(modifier = MinHorzSpacer)
 
-                            Text(
-                                text = stringRes(R.string.relay_settings),
-                                modifier = Modifier.weight(1f),
-                                textAlign = TextAlign.Center,
-                                style = MaterialTheme.typography.titleLarge,
-                                overflow = TextOverflow.Ellipsis,
-                                maxLines = 1,
-                            )
+                        Text(
+                            text = stringRes(R.string.relay_settings),
+                            modifier = Modifier.weight(1f),
+                            textAlign = TextAlign.Center,
+                            style = MaterialTheme.typography.titleLarge,
+                            overflow = TextOverflow.Ellipsis,
+                            maxLines = 1,
+                        )
 
-                            SaveButton(
-                                onPost = {
-                                    kind3ViewModel.create()
-                                    dmViewModel.create()
-                                    nip65ViewModel.create()
-                                    searchViewModel.create()
-                                    localViewModel.create()
-                                    privateOutboxViewModel.create()
-                                    onClose()
-                                },
-                                true,
-                            )
-                        }
-                    },
-                    navigationIcon = {
-                        Row {
-                            Spacer(modifier = StdHorzSpacer)
-                            CloseButton(
-                                onPress = {
-                                    kind3ViewModel.clear()
-                                    dmViewModel.clear()
-                                    nip65ViewModel.clear()
-                                    searchViewModel.clear()
-                                    localViewModel.clear()
-                                    privateOutboxViewModel.clear()
-                                    onClose()
-                                },
-                            )
-                        }
-                    },
-                    colors =
-                        TopAppBarDefaults.topAppBarColors(
-                            containerColor = MaterialTheme.colorScheme.surface,
-                        ),
-                )
-            },
-        ) { pad ->
-            LazyColumn(
-                contentPadding = FeedPadding,
-                modifier =
-                    Modifier
-                        .fillMaxSize()
-                        .padding(
-                            start = 10.dp,
-                            end = 10.dp,
-                            top = pad.calculateTopPadding(),
-                            bottom = pad.calculateBottomPadding(),
-                        ).consumeWindowInsets(pad),
-            ) {
-                item {
-                    SettingsCategory(
-                        stringRes(R.string.public_home_section),
-                        stringRes(R.string.public_home_section_explainer),
-                        Modifier.padding(bottom = 8.dp),
-                    )
-                }
-                renderNip65HomeItems(homeFeedState, nip65ViewModel, accountViewModel, newNav)
-
-                item {
-                    SettingsCategory(
-                        stringRes(R.string.public_notif_section),
-                        stringRes(R.string.public_notif_section_explainer),
-                    )
-                }
-                renderNip65NotifItems(notifFeedState, nip65ViewModel, accountViewModel, newNav)
-
-                item {
-                    SettingsCategoryWithButton(
-                        stringRes(R.string.private_inbox_section),
-                        stringRes(R.string.private_inbox_section_explainer),
-                        action = {
-                            ResetDMRelays(dmViewModel)
-                        },
-                    )
-                }
-                renderDMItems(dmFeedState, dmViewModel, accountViewModel, newNav)
-
-                item {
-                    SettingsCategory(
-                        stringRes(R.string.private_outbox_section),
-                        stringRes(R.string.private_outbox_section_explainer),
-                    )
-                }
-                renderPrivateOutboxItems(privateOutboxFeedState, privateOutboxViewModel, accountViewModel, newNav)
-
-                item {
-                    SettingsCategoryWithButton(
-                        stringRes(R.string.search_section),
-                        stringRes(R.string.search_section_explainer),
-                        action = {
-                            ResetSearchRelays(searchViewModel)
-                        },
-                    )
-                }
-                renderSearchItems(searchFeedState, searchViewModel, accountViewModel, newNav)
-
-                item {
-                    SettingsCategory(
-                        stringRes(R.string.local_section),
-                        stringRes(R.string.local_section_explainer),
-                    )
-                }
-                renderLocalItems(localFeedState, localViewModel, accountViewModel, newNav)
-
-                item {
-                    SettingsCategoryWithButton(
-                        stringRes(R.string.kind_3_section),
-                        stringRes(R.string.kind_3_section_description),
-                        action = {
-                            ResetKind3Relays(kind3ViewModel)
-                        },
-                    )
-                }
-                renderKind3Items(kind3FeedState, kind3ViewModel, accountViewModel, newNav, relayToAdd)
-
-                if (kind3Proposals.isNotEmpty()) {
-                    item {
-                        SettingsCategory(
-                            stringRes(R.string.kind_3_recommended_section),
-                            stringRes(R.string.kind_3_recommended_section_description),
+                        SaveButton(
+                            onPost = {
+                                kind3ViewModel.create()
+                                dmViewModel.create()
+                                nip65ViewModel.create()
+                                searchViewModel.create()
+                                localViewModel.create()
+                                privateOutboxViewModel.create()
+                                newNav.popBack()
+                            },
+                            true,
                         )
                     }
-                    renderKind3ProposalItems(kind3Proposals, kind3ViewModel, accountViewModel, newNav)
+                },
+                navigationIcon = {
+                    Row {
+                        Spacer(modifier = StdHorzSpacer)
+                        CloseButton(
+                            onPress = {
+                                kind3ViewModel.clear()
+                                dmViewModel.clear()
+                                nip65ViewModel.clear()
+                                searchViewModel.clear()
+                                localViewModel.clear()
+                                privateOutboxViewModel.clear()
+                                newNav.popBack()
+                            },
+                        )
+                    }
+                },
+                colors =
+                    TopAppBarDefaults.topAppBarColors(
+                        containerColor = MaterialTheme.colorScheme.surface,
+                    ),
+            )
+        },
+    ) { pad ->
+        LazyColumn(
+            contentPadding = FeedPadding,
+            modifier =
+                Modifier
+                    .fillMaxSize()
+                    .padding(
+                        start = 10.dp,
+                        end = 10.dp,
+                        top = pad.calculateTopPadding(),
+                        bottom = pad.calculateBottomPadding(),
+                    ).consumeWindowInsets(pad),
+        ) {
+            item {
+                SettingsCategory(
+                    stringRes(R.string.public_home_section),
+                    stringRes(R.string.public_home_section_explainer),
+                    Modifier.padding(bottom = 8.dp),
+                )
+            }
+            renderNip65HomeItems(homeFeedState, nip65ViewModel, accountViewModel, newNav)
+
+            item {
+                SettingsCategory(
+                    stringRes(R.string.public_notif_section),
+                    stringRes(R.string.public_notif_section_explainer),
+                )
+            }
+            renderNip65NotifItems(notifFeedState, nip65ViewModel, accountViewModel, newNav)
+
+            item {
+                SettingsCategoryWithButton(
+                    stringRes(R.string.private_inbox_section),
+                    stringRes(R.string.private_inbox_section_explainer),
+                    action = {
+                        ResetDMRelays(dmViewModel)
+                    },
+                )
+            }
+            renderDMItems(dmFeedState, dmViewModel, accountViewModel, newNav)
+
+            item {
+                SettingsCategory(
+                    stringRes(R.string.private_outbox_section),
+                    stringRes(R.string.private_outbox_section_explainer),
+                )
+            }
+            renderPrivateOutboxItems(privateOutboxFeedState, privateOutboxViewModel, accountViewModel, newNav)
+
+            item {
+                SettingsCategoryWithButton(
+                    stringRes(R.string.search_section),
+                    stringRes(R.string.search_section_explainer),
+                    action = {
+                        ResetSearchRelays(searchViewModel)
+                    },
+                )
+            }
+            renderSearchItems(searchFeedState, searchViewModel, accountViewModel, newNav)
+
+            item {
+                SettingsCategory(
+                    stringRes(R.string.local_section),
+                    stringRes(R.string.local_section_explainer),
+                )
+            }
+            renderLocalItems(localFeedState, localViewModel, accountViewModel, newNav)
+
+            item {
+                SettingsCategoryWithButton(
+                    stringRes(R.string.kind_3_section),
+                    stringRes(R.string.kind_3_section_description),
+                    action = {
+                        ResetKind3Relays(kind3ViewModel)
+                    },
+                )
+            }
+            renderKind3Items(kind3FeedState, kind3ViewModel, accountViewModel, newNav, relayToAdd)
+
+            if (kind3Proposals.isNotEmpty()) {
+                item {
+                    SettingsCategory(
+                        stringRes(R.string.kind_3_recommended_section),
+                        stringRes(R.string.kind_3_recommended_section_description),
+                    )
                 }
+                renderKind3ProposalItems(kind3Proposals, kind3ViewModel, accountViewModel, newNav)
             }
         }
     }
diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/navigation/AppNavigation.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/navigation/AppNavigation.kt
index 4d2335b93..c1c43350b 100644
--- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/navigation/AppNavigation.kt
+++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/navigation/AppNavigation.kt
@@ -49,6 +49,7 @@ import androidx.navigation.compose.NavHost
 import androidx.navigation.compose.composable
 import com.vitorpamplona.amethyst.R
 import com.vitorpamplona.amethyst.ui.MainActivity
+import com.vitorpamplona.amethyst.ui.actions.relays.AllRelayListView
 import com.vitorpamplona.amethyst.ui.components.DisplayErrorMessages
 import com.vitorpamplona.amethyst.ui.components.DisplayNotifyMessages
 import com.vitorpamplona.amethyst.ui.screen.AccountStateViewModel
@@ -320,6 +321,16 @@ fun AppNavigation(
                     nav = nav,
                 )
             }
+
+            composable(
+                Route.EditRelays.route,
+                content = {
+                    AllRelayListView(
+                        accountViewModel = accountViewModel,
+                        nav = nav,
+                    )
+                },
+            )
         }
     }
 
diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/navigation/DrawerContent.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/navigation/DrawerContent.kt
index e72deae52..5ba30f191 100644
--- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/navigation/DrawerContent.kt
+++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/navigation/DrawerContent.kt
@@ -89,7 +89,6 @@ import com.vitorpamplona.amethyst.model.Account
 import com.vitorpamplona.amethyst.model.FeatureSetType
 import com.vitorpamplona.amethyst.model.User
 import com.vitorpamplona.amethyst.ui.actions.mediaServers.MediaServersListView
-import com.vitorpamplona.amethyst.ui.actions.relays.AllRelayListView
 import com.vitorpamplona.amethyst.ui.components.ClickableText
 import com.vitorpamplona.amethyst.ui.components.CreateTextWithEmoji
 import com.vitorpamplona.amethyst.ui.components.RobohashFallbackAsyncImage
@@ -437,7 +436,6 @@ fun ListContent(
 ) {
     val route = remember(accountViewModel) { "User/${accountViewModel.userProfile().pubkeyHex}" }
 
-    var wantsToEditRelays by remember { mutableStateOf(false) }
     var editMediaServers by remember { mutableStateOf(false) }
 
     var backupDialogOpen by remember { mutableStateOf(false) }
@@ -475,7 +473,7 @@ fun ListContent(
             accountViewModel = accountViewModel,
             onClick = {
                 nav.closeDrawer()
-                wantsToEditRelays = true
+                nav.nav(Route.EditRelays.route)
             },
         )
 
@@ -537,9 +535,6 @@ fun ListContent(
         )
     }
 
-    if (wantsToEditRelays) {
-        AllRelayListView({ wantsToEditRelays = false }, accountViewModel = accountViewModel, nav = nav)
-    }
     if (editMediaServers) {
         MediaServersListView({ editMediaServers = false }, accountViewModel = accountViewModel, nav = nav)
     }
diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/navigation/Routes.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/navigation/Routes.kt
index cbc01fc9d..6745ebe51 100644
--- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/navigation/Routes.kt
+++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/navigation/Routes.kt
@@ -68,6 +68,13 @@ sealed class Route(
             contentDescriptor = R.string.route_global,
         )
 
+    object EditRelays :
+        Route(
+            route = "EditRelays",
+            icon = R.drawable.ic_globe,
+            contentDescriptor = R.string.relays,
+        )
+
     object Search :
         Route(
             route = "Search",
diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/note/types/RelayList.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/note/types/RelayList.kt
index dea594279..647be1191 100644
--- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/note/types/RelayList.kt
+++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/note/types/RelayList.kt
@@ -45,9 +45,10 @@ import androidx.compose.ui.text.style.TextOverflow
 import androidx.compose.ui.unit.dp
 import com.vitorpamplona.amethyst.R
 import com.vitorpamplona.amethyst.model.Note
-import com.vitorpamplona.amethyst.ui.actions.relays.AllRelayListView
+import com.vitorpamplona.amethyst.ui.actions.relays.RelayToAdd
 import com.vitorpamplona.amethyst.ui.components.ShowMoreButton
 import com.vitorpamplona.amethyst.ui.navigation.INav
+import com.vitorpamplona.amethyst.ui.navigation.Route
 import com.vitorpamplona.amethyst.ui.note.AddRelayButton
 import com.vitorpamplona.amethyst.ui.note.RemoveRelayButton
 import com.vitorpamplona.amethyst.ui.note.getGradient
@@ -289,15 +290,15 @@ private fun RelayOptionsAction(
             }
         }
 
-    var wantsToAddRelay by remember { mutableStateOf("") }
-
-    if (wantsToAddRelay.isNotEmpty()) {
-        AllRelayListView({ wantsToAddRelay = "" }, wantsToAddRelay, accountViewModel, nav = nav)
-    }
-
     if (isCurrentlyOnTheUsersList) {
-        AddRelayButton { wantsToAddRelay = relay }
+        AddRelayButton {
+            RelayToAdd.relayToAdd = relay
+            nav.nav(Route.EditRelays.route)
+        }
     } else {
-        RemoveRelayButton { wantsToAddRelay = relay }
+        RemoveRelayButton {
+            RelayToAdd.relayToAdd = relay
+            nav.nav(Route.EditRelays.route)
+        }
     }
 }
diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/profile/RelayFeedView.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/profile/RelayFeedView.kt
index 60614719d..6a410e564 100644
--- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/profile/RelayFeedView.kt
+++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/profile/RelayFeedView.kt
@@ -26,13 +26,12 @@ import androidx.compose.foundation.lazy.rememberLazyListState
 import androidx.compose.material3.HorizontalDivider
 import androidx.compose.runtime.Composable
 import androidx.compose.runtime.getValue
-import androidx.compose.runtime.mutableStateOf
-import androidx.compose.runtime.remember
 import androidx.compose.runtime.setValue
 import androidx.lifecycle.compose.collectAsStateWithLifecycle
-import com.vitorpamplona.amethyst.ui.actions.relays.AllRelayListView
+import com.vitorpamplona.amethyst.ui.actions.relays.RelayToAdd
 import com.vitorpamplona.amethyst.ui.feeds.RefresheableBox
 import com.vitorpamplona.amethyst.ui.navigation.INav
+import com.vitorpamplona.amethyst.ui.navigation.Route
 import com.vitorpamplona.amethyst.ui.note.RelayCompose
 import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel
 import com.vitorpamplona.amethyst.ui.theme.DividerThickness
@@ -47,12 +46,6 @@ fun RelayFeedView(
 ) {
     val feedState by viewModel.feedContent.collectAsStateWithLifecycle()
 
-    var wantsToAddRelay by remember { mutableStateOf("") }
-
-    if (wantsToAddRelay.isNotEmpty()) {
-        AllRelayListView({ wantsToAddRelay = "" }, wantsToAddRelay, accountViewModel, nav = nav)
-    }
-
     RefresheableBox(viewModel, enablePullRefresh) {
         val listState = rememberLazyListState()
 
@@ -64,8 +57,14 @@ fun RelayFeedView(
                 RelayCompose(
                     item,
                     accountViewModel = accountViewModel,
-                    onAddRelay = { wantsToAddRelay = item.url },
-                    onRemoveRelay = { wantsToAddRelay = item.url },
+                    onAddRelay = {
+                        RelayToAdd.relayToAdd = item.url
+                        nav.nav(Route.EditRelays.route)
+                    },
+                    onRemoveRelay = {
+                        RelayToAdd.relayToAdd = item.url
+                        nav.nav(Route.EditRelays.route)
+                    },
                 )
                 HorizontalDivider(
                     thickness = DividerThickness,