From 2fed65db8dcb65603535ccba5553118fa604f436 Mon Sep 17 00:00:00 2001 From: greenart7c3 <115044884+greenart7c3@users.noreply.github.com> Date: Mon, 17 Jul 2023 10:41:47 -0300 Subject: [PATCH] created RelaySelectionDialog file --- .../amethyst/ui/actions/NewPostView.kt | 115 ++------------ .../ui/actions/RelaySelectionDialog.kt | 150 ++++++++++++++++++ 2 files changed, 162 insertions(+), 103 deletions(-) create mode 100644 app/src/main/java/com/vitorpamplona/amethyst/ui/actions/RelaySelectionDialog.kt diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/NewPostView.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/NewPostView.kt index f2a9407d8..3c08850d7 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/NewPostView.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/NewPostView.kt @@ -9,7 +9,6 @@ import android.widget.Toast import androidx.compose.foundation.BorderStroke import androidx.compose.foundation.Image import androidx.compose.foundation.border -import androidx.compose.foundation.clickable import androidx.compose.foundation.layout.* import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.foundation.lazy.itemsIndexed @@ -72,7 +71,6 @@ import com.vitorpamplona.amethyst.model.Note import com.vitorpamplona.amethyst.model.User import com.vitorpamplona.amethyst.service.NostrSearchEventOrUserDataSource import com.vitorpamplona.amethyst.service.noProtocolUrlValidator -import com.vitorpamplona.amethyst.service.relays.Relay import com.vitorpamplona.amethyst.ui.components.* import com.vitorpamplona.amethyst.ui.note.CancelIcon import com.vitorpamplona.amethyst.ui.note.CloseIcon @@ -97,11 +95,6 @@ import kotlinx.coroutines.delay import kotlinx.coroutines.launch import kotlinx.coroutines.withContext -data class RelayList( - val relay: Relay, - val isSelected: Boolean -) - @OptIn(ExperimentalComposeUiApi::class) @Composable fun NewPostView(onClose: () -> Unit, baseReplyTo: Note? = null, quote: Note? = null, accountViewModel: AccountViewModel, nav: (String) -> Unit) { @@ -120,7 +113,7 @@ fun NewPostView(onClose: () -> Unit, baseReplyTo: Note? = null, quote: Note? = n var showRelaysDialog by remember { mutableStateOf(false) } - val relayList = account.activeRelays()?.filter { + var relayList = account.activeRelays()?.filter { it.write }?.map { it @@ -128,17 +121,6 @@ fun NewPostView(onClose: () -> Unit, baseReplyTo: Note? = null, quote: Note? = n it.write } - var relays by remember { - mutableStateOf( - relayList.map { - RelayList( - it, - true - ) - } - ) - } - LaunchedEffect(Unit) { postViewModel.load(account, baseReplyTo, quote) delay(100) @@ -173,87 +155,16 @@ fun NewPostView(onClose: () -> Unit, baseReplyTo: Note? = null, quote: Note? = n .fillMaxHeight() ) { if (showRelaysDialog) { - Dialog( - onDismissRequest = { showRelaysDialog = false }, - properties = DialogProperties( - usePlatformDefaultWidth = false, - dismissOnClickOutside = false, - decorFitsSystemWindows = false - ) - ) { - Surface( - modifier = Modifier - .fillMaxWidth() - .fillMaxHeight() - ) { - Column( - modifier = Modifier - .fillMaxWidth() - .fillMaxHeight() - .padding(start = 10.dp, end = 10.dp, top = 10.dp) - - ) { - Row( - modifier = Modifier.fillMaxWidth(), - horizontalArrangement = Arrangement.SpaceBetween, - verticalAlignment = Alignment.CenterVertically - ) { - CloseButton( - onCancel = { - showRelaysDialog = false - } - ) - - PostButton( - onPost = { - scope.launch(Dispatchers.IO) { - showRelaysDialog = false - } - }, - isActive = true - ) - } - - LazyColumn( - contentPadding = PaddingValues( - top = 10.dp, - bottom = 10.dp - ) - ) { - itemsIndexed( - relays, - key = { _, item -> item.relay.url } - ) { index, item -> - Row( - horizontalArrangement = Arrangement.SpaceBetween, - verticalAlignment = Alignment.CenterVertically, - modifier = Modifier - .fillMaxWidth() - .clickable { - relays = relays.mapIndexed { j, item -> - if (index == j) { - item.copy(isSelected = !item.isSelected) - } else { item } - } - } - ) { - Text(text = item.relay.url) - Switch( - checked = item.isSelected, - onCheckedChange = { - relays = relays.mapIndexed { j, item -> - if (index == j) { - item.copy(isSelected = !item.isSelected) - } else { item } - } - } - ) - } - } - } - } - } - } + RelaySelectionDialog( + list = relayList, + onClose = { + showRelaysDialog = false + }, + onPost = { + relayList = it + }, + account = account + ) } Column( @@ -294,10 +205,8 @@ fun NewPostView(onClose: () -> Unit, baseReplyTo: Note? = null, quote: Note? = n } PostButton( onPost = { - val list = relays.filter { it.isSelected }.map { it.relay } - scope.launch(Dispatchers.IO) { - postViewModel.sendPost(relayList = list) + postViewModel.sendPost(relayList = relayList) onClose() } }, diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/RelaySelectionDialog.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/RelaySelectionDialog.kt new file mode 100644 index 000000000..9d0bc2ee5 --- /dev/null +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/RelaySelectionDialog.kt @@ -0,0 +1,150 @@ +package com.vitorpamplona.amethyst.ui.actions + +import android.widget.Toast +import androidx.compose.foundation.clickable +import androidx.compose.foundation.layout.Arrangement +import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.PaddingValues +import androidx.compose.foundation.layout.Row +import androidx.compose.foundation.layout.fillMaxHeight +import androidx.compose.foundation.layout.fillMaxWidth +import androidx.compose.foundation.layout.padding +import androidx.compose.foundation.lazy.LazyColumn +import androidx.compose.foundation.lazy.itemsIndexed +import androidx.compose.material.Surface +import androidx.compose.material.Switch +import androidx.compose.material.Text +import androidx.compose.runtime.Composable +import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.remember +import androidx.compose.runtime.rememberCoroutineScope +import androidx.compose.runtime.setValue +import androidx.compose.ui.Alignment +import androidx.compose.ui.Modifier +import androidx.compose.ui.platform.LocalContext +import androidx.compose.ui.unit.dp +import androidx.compose.ui.window.Dialog +import androidx.compose.ui.window.DialogProperties +import com.vitorpamplona.amethyst.model.Account +import com.vitorpamplona.amethyst.service.relays.Relay +import kotlinx.coroutines.launch + +data class RelayList( + val relay: Relay, + val isSelected: Boolean +) + +@Composable +fun RelaySelectionDialog(list: List, onClose: () -> Unit, onPost: (list: List) -> Unit, account: Account) { + val scope = rememberCoroutineScope() + val context = LocalContext.current + val relayList = account.activeRelays()?.filter { + it.write + }?.map { + it + } ?: account.convertLocalRelays().filter { + it.write + } + + var relays by remember { + mutableStateOf( + relayList.map { + RelayList( + it, + list.any { relay -> it.url == relay.url } + ) + } + ) + } + + Dialog( + onDismissRequest = { onClose() }, + properties = DialogProperties( + usePlatformDefaultWidth = false, + dismissOnClickOutside = false, + decorFitsSystemWindows = false + ) + ) { + Surface( + modifier = Modifier + .fillMaxWidth() + .fillMaxHeight() + ) { + Column( + modifier = Modifier + .fillMaxWidth() + .fillMaxHeight() + .padding(start = 10.dp, end = 10.dp, top = 10.dp) + + ) { + Row( + modifier = Modifier.fillMaxWidth(), + horizontalArrangement = Arrangement.SpaceBetween, + verticalAlignment = Alignment.CenterVertically + ) { + CloseButton( + onCancel = { + onClose() + } + ) + + PostButton( + onPost = { + val selectedRelays = relays.filter { it.isSelected } + if (selectedRelays.isEmpty()) { + scope.launch { + Toast.makeText(context, "Select a relay to continue", Toast.LENGTH_SHORT).show() + return@launch + } + } + onPost(selectedRelays.map { it.relay }) + onClose() + }, + isActive = true + ) + } + + LazyColumn( + contentPadding = PaddingValues( + top = 10.dp, + bottom = 10.dp + ) + ) { + itemsIndexed( + relays, + key = { _, item -> item.relay.url } + ) { index, item -> + Row( + horizontalArrangement = Arrangement.SpaceBetween, + verticalAlignment = Alignment.CenterVertically, + modifier = Modifier + .fillMaxWidth() + .clickable { + relays = relays.mapIndexed { j, item -> + if (index == j) { + item.copy(isSelected = !item.isSelected) + } else { + item + } + } + } + ) { + Text(text = item.relay.url) + Switch( + checked = item.isSelected, + onCheckedChange = { + relays = relays.mapIndexed { j, item -> + if (index == j) { + item.copy(isSelected = !item.isSelected) + } else { item } + } + } + ) + } + } + } + } + } + } +}