From f5e90dfe87d737c359eba8f96097398cbb313523 Mon Sep 17 00:00:00 2001 From: SpoilerRules Date: Tue, 13 Aug 2024 18:55:35 +0200 Subject: [PATCH] fix file picker error --- .../explorer/StartupCoordinator.kt | 36 ++++++------ .../ui/dialogs/MapleServerFilePickerDialog.kt | 57 +++++++++---------- .../ui/dialogs/dialog/MapleDialogBase.kt | 1 - 3 files changed, 47 insertions(+), 47 deletions(-) diff --git a/src/main/kotlin/com/spoiligaming/explorer/StartupCoordinator.kt b/src/main/kotlin/com/spoiligaming/explorer/StartupCoordinator.kt index a638ad0..5c3b526 100644 --- a/src/main/kotlin/com/spoiligaming/explorer/StartupCoordinator.kt +++ b/src/main/kotlin/com/spoiligaming/explorer/StartupCoordinator.kt @@ -8,6 +8,7 @@ import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateListOf import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.remember import androidx.compose.runtime.setValue import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier @@ -23,7 +24,6 @@ import com.spoiligaming.explorer.ui.state.DialogController import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch -import kotlinx.coroutines.withContext object StartupCoordinator { private var isServerDataLoaded by mutableStateOf(false) @@ -43,7 +43,9 @@ object StartupCoordinator { @Composable private fun Content() { - LaunchedEffect(isServerDataLoaded) { loadServerData() } + LaunchedEffect(Unit) { + verifyServerFile() + } if (isServerDataLoaded) { DisplayMainContent() @@ -56,26 +58,28 @@ object StartupCoordinator { @Composable private fun DisplayMainContent() { + val names = remember { ServerFileHandler.loadNames() } + val addresses = remember { ServerFileHandler.loadAddresses() } + ContemporaryServerEntryListData.initialize( - mutableStateListOf(*ServerFileHandler.loadNames().toTypedArray()), - mutableStateListOf(*ServerFileHandler.loadAddresses().toTypedArray()), + mutableStateListOf(*names.toTypedArray()), + mutableStateListOf(*addresses.toTypedArray()), ) + NavigationComponent() NavigationController.navigateTo(Screen.Main) } - private suspend fun loadServerData() = - withContext(Dispatchers.IO) { - isServerDataLoaded = - ( - ServerFileHandler.initializeServerFileLocation() == - ServerFileValidationResult.VALID - ) + // TODO: fix this getting invoked twice when called by `retryLoad` function + private fun verifyServerFile() { + isServerDataLoaded = ServerFileHandler.initializeServerFileLocation() == ServerFileValidationResult.VALID - if (!isServerDataLoaded) { - shouldShowFilePickerDialog = true - } - } + shouldShowFilePickerDialog = !isServerDataLoaded + } - fun retryLoad() = CoroutineScope(Dispatchers.IO).launch { isServerDataLoaded = false } + fun retryLoad() = + CoroutineScope(Dispatchers.IO).launch { + isServerDataLoaded = false + verifyServerFile() + } } diff --git a/src/main/kotlin/com/spoiligaming/explorer/ui/dialogs/MapleServerFilePickerDialog.kt b/src/main/kotlin/com/spoiligaming/explorer/ui/dialogs/MapleServerFilePickerDialog.kt index 9ea8b13..c58a901 100644 --- a/src/main/kotlin/com/spoiligaming/explorer/ui/dialogs/MapleServerFilePickerDialog.kt +++ b/src/main/kotlin/com/spoiligaming/explorer/ui/dialogs/MapleServerFilePickerDialog.kt @@ -40,41 +40,40 @@ fun MapleServerFilePickerDialog( isUserRequested: Boolean, onDismiss: () -> Unit, ) { - MapleDialogBase( - isFullPopup = true, - heightType = 0, - isCloseable = isUserRequested, - onDismiss = onDismiss, - ) { - var hasFailedOnce by remember { mutableStateOf(false) } - - val filePicker = - rememberFilePickerLauncher( - type = PickerType.File(extensions = listOf("dat")), - mode = PickerMode.Single, - title = "Select server list file", - ) { file -> - if (file != null) { - val validationResult = - ServerFileHandler.validateFile( - file.path?.let { File(it) }, - ) + var hasFailedOnce by remember { mutableStateOf(false) } + val filePicker = + rememberFilePickerLauncher( + type = PickerType.File(extensions = listOf("dat")), + mode = PickerMode.Single, + title = "Select server list file", + ) { file -> + if (file != null) { + val validationResult = + ServerFileHandler.validateFile( + file.path?.let { File(it) }, + ) - if (validationResult == ServerFileValidationResult.VALID) { - hasFailedOnce = false - ConfigurationHandler.updateValue { - generalSettings.serverFilePath = file.path.toString() - } - onDismiss() - StartupCoordinator.retryLoad() - } else { - hasFailedOnce = true + if (validationResult == ServerFileValidationResult.VALID) { + hasFailedOnce = false + ConfigurationHandler.updateValue { + generalSettings.serverFilePath = file.path.toString() } + onDismiss() + StartupCoordinator.retryLoad() } else { hasFailedOnce = true } + } else { + hasFailedOnce = true } + } + MapleDialogBase( + isFullPopup = true, + heightType = 0, + isCloseable = isUserRequested, + onDismiss = onDismiss, + ) { Column(verticalArrangement = Arrangement.spacedBy(20.dp)) { Text( text = @@ -127,8 +126,6 @@ fun MapleServerFilePickerDialog( .fillMaxWidth(), contentAlignment = Alignment.Center, ) { - // Error thrown by this will be fixed by JetBrains in next compose MP update: - // https://youtrack.jetbrains.com/issue/COMPT-5064/Clicking-a-clickable-component-inside-another-disabled-clickable-component-throws-IllegalStateException MapleButton( Modifier.fillMaxWidth(), width = null, diff --git a/src/main/kotlin/com/spoiligaming/explorer/ui/dialogs/dialog/MapleDialogBase.kt b/src/main/kotlin/com/spoiligaming/explorer/ui/dialogs/dialog/MapleDialogBase.kt index 761a6a7..b398788 100644 --- a/src/main/kotlin/com/spoiligaming/explorer/ui/dialogs/dialog/MapleDialogBase.kt +++ b/src/main/kotlin/com/spoiligaming/explorer/ui/dialogs/dialog/MapleDialogBase.kt @@ -81,7 +81,6 @@ fun MapleDialogBase( } } .clickable( - enabled = isCloseable, onClick = { if (!isHovered && isCloseable) onDismiss() }, indication = null, interactionSource = remember { MutableInteractionSource() },