Skip to content

Commit

Permalink
fix file picker error
Browse files Browse the repository at this point in the history
  • Loading branch information
SpoilerRules committed Aug 13, 2024
1 parent 96c5b07 commit f5e90df
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 47 deletions.
36 changes: 20 additions & 16 deletions src/main/kotlin/com/spoiligaming/explorer/StartupCoordinator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -43,7 +43,9 @@ object StartupCoordinator {

@Composable
private fun Content() {
LaunchedEffect(isServerDataLoaded) { loadServerData() }
LaunchedEffect(Unit) {
verifyServerFile()
}

if (isServerDataLoaded) {
DisplayMainContent()
Expand All @@ -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()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ fun MapleDialogBase(
}
}
.clickable(
enabled = isCloseable,
onClick = { if (!isHovered && isCloseable) onDismiss() },
indication = null,
interactionSource = remember { MutableInteractionSource() },
Expand Down

0 comments on commit f5e90df

Please sign in to comment.