desktop: unsaved changes popup for network and servers when clicking middle lane (#5230)

* Revert "Revert "handle click when have unsaved changes""

This reverts commit ba53cc63c6.

* fix in children view

* unsaved changes for network and children

* don't close all modals when pressing back

* explicit param

---------

Co-authored-by: spaced4ndy <8711996+spaced4ndy@users.noreply.github.com>
This commit is contained in:
Diogo
2024-11-23 14:42:25 +00:00
committed by GitHub
parent 30a24df9c0
commit 909edac64f
3 changed files with 34 additions and 14 deletions
@@ -431,8 +431,10 @@ fun DesktopScreen(userPickerState: MutableStateFlow<AnimatedViewState>) {
.fillMaxSize()
.padding(start = DEFAULT_START_MODAL_WIDTH * fontSizeSqrtMultiplier)
.clickable(interactionSource = remember { MutableInteractionSource() }, indication = null, onClick = {
ModalManager.start.closeModals()
userPickerState.value = AnimatedViewState.HIDING
if (chatModel.centerPanelBackgroundClickHandler == null || chatModel.centerPanelBackgroundClickHandler?.invoke() == false) {
ModalManager.start.closeModals()
userPickerState.value = AnimatedViewState.HIDING
}
})
)
}
@@ -167,6 +167,9 @@ object ChatModel {
val processedCriticalError: ProcessedErrors<AgentErrorType.CRITICAL> = ProcessedErrors(60_000)
val processedInternalError: ProcessedErrors<AgentErrorType.INTERNAL> = ProcessedErrors(20_000)
// return true if you handled the click
var centerPanelBackgroundClickHandler: (() -> Boolean)? = null
fun getUser(userId: Long): User? = if (currentUser.value?.userId == userId) {
currentUser.value
} else {
@@ -39,10 +39,10 @@ import chat.simplex.common.views.onboarding.OnboardingActionButton
import chat.simplex.common.views.onboarding.ReadableText
import chat.simplex.common.views.usersettings.*
import chat.simplex.res.MR
import kotlinx.coroutines.launch
import kotlinx.coroutines.*
@Composable
fun ModalData.NetworkAndServersView(close: () -> Unit) {
fun ModalData.NetworkAndServersView(closeNetworkAndServers: () -> Unit) {
val currentRemoteHost by remember { chatModel.currentRemoteHost }
// It's not a state, just a one-time value. Shouldn't be used in any state-related situations
val netCfg = remember { chatModel.controller.getNetCfg() }
@@ -50,21 +50,36 @@ fun ModalData.NetworkAndServersView(close: () -> Unit) {
val currUserServers = remember { stateGetOrPut("currUserServers") { emptyList<UserOperatorServers>() } }
val userServers = remember { stateGetOrPut("userServers") { emptyList<UserOperatorServers>() } }
val serverErrors = remember { stateGetOrPut("serverErrors") { emptyList<UserServersError>() } }
val scope = rememberCoroutineScope()
val proxyPort = remember { derivedStateOf { appPrefs.networkProxy.state.value.port } }
ModalView(
close = {
if (!serversCanBeSaved(currUserServers.value, userServers.value, serverErrors.value)) {
fun onClose(close: () -> Unit): Boolean = if (!serversCanBeSaved(currUserServers.value, userServers.value, serverErrors.value)) {
chatModel.centerPanelBackgroundClickHandler = null
close()
false
} else {
showUnsavedChangesAlert(
{
CoroutineScope(Dispatchers.Default).launch {
saveServers(currentRemoteHost?.remoteHostId, currUserServers, userServers)
chatModel.centerPanelBackgroundClickHandler = null
close()
}
},
{
chatModel.centerPanelBackgroundClickHandler = null
close()
} else {
showUnsavedChangesAlert(
{ scope.launch { saveServers(currentRemoteHost?.remoteHostId, currUserServers, userServers) }},
close
)
}
)
true
}
LaunchedEffect(Unit) {
// Enables unsaved changes alert on this view and all children views.
chatModel.centerPanelBackgroundClickHandler = {
onClose(close = { ModalManager.start.closeModals() })
}
) {
}
ModalView(close = { onClose(closeNetworkAndServers) }) {
NetworkAndServersLayout(
currentRemoteHost = currentRemoteHost,
networkUseSocksProxy = networkUseSocksProxy,