Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ui/src/main/composeResources/values-en-rGB/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
<string name="setup_startup_low_memory_message">This device has less than 8 GB of memory. Server List Explorer is expected to use at least 300 MB while idle in the system tray.</string>
<string name="setup_startup_low_memory_icon_content_description">Low memory warning</string>
<string name="setup_startup_save_failed">Couldn't update startup preference. Please try again.</string>
<string name="setup_locale_save_failed">Couldn't update language. Please try again.</string>
<string name="settings_section_startup">Start-up</string>
<string name="startup_when_computer_starts_dropdown_title">When my computer starts</string>
<string name="startup_when_computer_starts_dropdown_description">Choose how Server List Explorer starts when your computer starts.</string>
Expand Down
1 change: 1 addition & 0 deletions ui/src/main/composeResources/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
<string name="setup_startup_low_memory_message">This device has less than 8 GB of memory. Server List Explorer is expected to use at least 300 MB while idle in the system tray.</string>
<string name="setup_startup_low_memory_icon_content_description">Low memory warning</string>
<string name="setup_startup_save_failed">Couldn't update startup preference. Please try again.</string>
<string name="setup_locale_save_failed">Couldn't update language. Please try again.</string>
<string name="settings_section_startup">Startup</string>
<string name="startup_when_computer_starts_dropdown_title">When my computer starts</string>
<string name="startup_when_computer_starts_dropdown_description">Choose how Server List Explorer starts when your computer starts.</string>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
import server_list_explorer.ui.generated.resources.button_back
import server_list_explorer.ui.generated.resources.button_finish
import server_list_explorer.ui.generated.resources.button_next
import server_list_explorer.ui.generated.resources.setup_locale_save_failed
import server_list_explorer.ui.generated.resources.setup_wizard_finish_failed
import server_list_explorer.ui.generated.resources.setup_wizard_step_counter
import java.nio.file.Path
Expand Down Expand Up @@ -160,156 +161,190 @@
}

@Composable
internal fun SetupWizard(
onFinished: () -> Unit,
intOffsetAnimationSpec: FiniteAnimationSpec<IntOffset>,
floatAnimationSpec: FiniteAnimationSpec<Float>,
) {
val prefs = LocalPrefs.current
val sp = LocalSingleplayerSettings.current
val startupSettings = LocalStartupSettings.current
val scope = rememberCoroutineScope()
val activeServerListFilePath by ServerListFileBookmarksManager.activePath.collectAsState()
val setupWizardFinishFailedMessage = t(Res.string.setup_wizard_finish_failed)
val setupLocaleSaveFailedMessage = t(Res.string.setup_locale_save_failed)
val supportsStartupRegistration =
(OSUtils.isWindows || OSUtils.isDebian) && !OSUtils.isRunningOnBareJvm && !AppStoragePaths.isPortableInstall

LaunchedEffect(Unit) {
ServerListFileBookmarksManager.load()
}

val state =
remember {
SetupUiState(
initialLocale = prefs.locale,
initialStartupSettings =
resolveInitialSetupStartupSettings(
startupSettings = startupSettings,
supportsStartupRegistration = supportsStartupRegistration,
),
initialWorldSavesPath = sp.savesDirectory,
initialServerFilePath = activeServerListFilePath,
)
}

LaunchedEffect(activeServerListFilePath) {
if (state.serverFilePath == null && activeServerListFilePath != null) {
state.serverFilePath = activeServerListFilePath
}
}

suspend fun persistLocaleSetting(locale: Locale) =
persistSettingsUpdate(
manager = preferenceSettingsManager,
context = "locale setting",
update = { it.copy(locale = locale) },
isPersisted = { it.locale == locale },
)

fun applyLocaleSelection(locale: Locale) {
if (state.locale == locale) {
return
}

state.locale = locale
scope.launch {
runCatching {
persistLocaleSetting(locale)
}.onFailure { e ->
logger.error(e) {
"Failed to apply setup wizard locale selection immediately. locale=$locale"
}
if (state.locale == locale) {
state.locale = preferenceSettingsManager.settingsFlow.value.locale
SnackbarController.sendEvent(
SnackbarEvent(
message = setupLocaleSaveFailedMessage,
duration = SnackbarDuration.Short,
),
)
}
}
}
}

suspend fun persistSetupState() =
runCatching {
if (prefs.locale != state.locale) {
persistSettingsUpdate(
manager = preferenceSettingsManager,
context = "locale setting",
update = { it.copy(locale = state.locale) },
isPersisted = { it.locale == state.locale },
)
persistLocaleSetting(state.locale)
}

if (sp.savesDirectory != state.worldSavesPath) {
persistSettingsUpdate(
manager = singleplayerSettingsManager,
context = "singleplayer saves directory setting",
update = { it.copy(savesDirectory = state.worldSavesPath) },
isPersisted = { it.savesDirectory == state.worldSavesPath },
)
}

if (startupSettings.computerStartupBehavior != state.startupSettings.computerStartupBehavior) {
ComputerStartupRegistrationManager
.applyBehavior(state.startupSettings.computerStartupBehavior)
.getOrThrow()
}
if (startupSettings != state.startupSettings) {
persistSettingsUpdate(
manager = startupSettingsManager,
context = "startup settings",
update = { state.startupSettings },
isPersisted = { it == state.startupSettings },
)
}

val selectedServerFilePath = state.serverFilePath
if (selectedServerFilePath != null && selectedServerFilePath != activeServerListFilePath) {
ServerListFileBookmarksManager.setActivePath(selectedServerFilePath)
}
}.onFailure { e ->
logger.error(e) { "Failed to finalize setup wizard settings." }
SnackbarController.sendEvent(
SnackbarEvent(
message = setupWizardFinishFailedMessage,
duration = SnackbarDuration.Short,
),
)
}.isSuccess

fun finalizeSetup() {
if (state.isFinishing) {
return
}
state.isFinishing = true
scope.launch {
try {
if (persistSetupState()) {
onFinished()
}
} finally {
state.isFinishing = false
}
}
}

Box(Modifier.fillMaxSize()) {
Column(
modifier = Modifier.fillMaxSize(),
verticalArrangement = Arrangement.spacedBy(VerticalSpacing),
) {
Box(
modifier =
Modifier
.weight(1f)
.fillMaxWidth(),
contentAlignment = Alignment.Center,
) {
AnimatedContent(
targetState = state.currentStep,
transitionSpec = {
val isMovingForward = targetState.order > initialState.order

if (isMovingForward) {
slideInHorizontally(animationSpec = intOffsetAnimationSpec) { it } +
fadeIn(animationSpec = floatAnimationSpec) togetherWith
slideOutHorizontally(animationSpec = intOffsetAnimationSpec) { -it } +
fadeOut(animationSpec = floatAnimationSpec)
} else {
slideInHorizontally(animationSpec = intOffsetAnimationSpec) { -it } +
fadeIn(animationSpec = floatAnimationSpec) togetherWith
slideOutHorizontally(animationSpec = intOffsetAnimationSpec) { it } +
fadeOut(animationSpec = floatAnimationSpec)
}
},
) { targetStep ->
when (targetStep) {
SetupStep.LANGUAGE_SELECTION -> LanguageSelectionStep(state = state)
SetupStep.LANGUAGE_SELECTION ->
LanguageSelectionStep(
state = state,
onLocaleSelected = ::applyLocaleSelection,
)
SetupStep.STARTUP_CONFIGURATION -> StartupStep(state = state)
SetupStep.PATH_CONFIGURATION -> PathStep(state = state)
}
}
}

SetupProgressBar(state, floatAnimationSpec)
NavigationControls(
state = state,
onFinished = ::finalizeSetup,
)
}
}
}

@Composable

Check notice on line 347 in ui/src/main/kotlin/com/spoiligaming/explorer/ui/screens/setup/SetupScreen.kt

View check run for this annotation

codefactor.io / CodeFactor

ui/src/main/kotlin/com/spoiligaming/explorer/ui/screens/setup/SetupScreen.kt#L164-L347

Complex Method
private fun SetupProgressBar(
state: SetupUiState,
floatAnimationSpec: FiniteAnimationSpec<Float>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,13 @@ import com.spoiligaming.explorer.ui.widgets.LanguagePickerDropdownMenu
import server_list_explorer.ui.generated.resources.Res
import server_list_explorer.ui.generated.resources.preferred_language_label
import server_list_explorer.ui.generated.resources.setup_step_title_localization
import java.util.Locale

@Composable
internal fun LanguageSelectionStep(state: SetupUiState) {
internal fun LanguageSelectionStep(
state: SetupUiState,
onLocaleSelected: (Locale) -> Unit,
) {
SetupStepContainer(title = t(Res.string.setup_step_title_localization)) {
Column(
verticalArrangement = Arrangement.spacedBy(LanguageStepItemSpacing),
Expand All @@ -49,7 +53,7 @@ internal fun LanguageSelectionStep(state: SetupUiState) {
LanguagePickerDropdownMenu(
selectedLocale = state.locale,
onLocaleSelected = { locale ->
state.locale = locale
onLocaleSelected(locale)
},
)
}
Expand Down
Loading