desktop: prevent corruption of settings file (#4243)

* desktop: prevent corruption of settings file

* check for existence

* unneeded

* simplify

* unused
This commit is contained in:
Stanislav Dmitrenko
2024-05-29 03:41:22 +07:00
committed by GitHub
parent 1b04423745
commit cad3bc048f
2 changed files with 30 additions and 12 deletions

View File

@@ -84,20 +84,25 @@ private fun ApplicationScope.AppWindow(closedByError: MutableState<Boolean>) {
position = WindowPosition(state.x.dp, state.y.dp)
)
val storingJob: MutableState<Job> = remember { mutableStateOf(Job()) }
LaunchedEffect(
windowState.position.x.value,
windowState.position.y.value,
windowState.size.width.value,
windowState.size.height.value
) {
storeWindowState(
WindowPositionSize(
x = windowState.position.x.value.toInt(),
y = windowState.position.y.value.toInt(),
width = windowState.size.width.value.toInt(),
height = windowState.size.height.value.toInt()
storingJob.value.cancel()
storingJob.value = launch {
delay(1000L)
storeWindowState(
WindowPositionSize(
x = windowState.position.x.value.toInt(),
y = windowState.position.y.value.toInt(),
width = windowState.size.width.value.toInt(),
height = windowState.size.height.value.toInt()
)
)
)
}
}
simplexWindowState.windowState = windowState

View File

@@ -8,7 +8,7 @@ import androidx.compose.ui.text.font.FontStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.*
import chat.simplex.common.simplexWindowState
import chat.simplex.common.ui.theme.reactOnDarkThemeChanges
import chat.simplex.common.views.helpers.*
import com.jthemedetecor.OsThemeDetector
import com.russhwolf.settings.*
import dev.icerock.moko.resources.ImageResource
@@ -39,15 +39,28 @@ private val settingsFile =
private val settingsThemesFile =
File(desktopPlatform.configPath + File.separator + "themes.properties")
.also { it.parentFile.mkdirs() }
private val settingsProps =
Properties()
.also { try { it.load(settingsFile.reader()) } catch (e: Exception) { Properties() } }
.also { props ->
if (!settingsFile.exists()) return@also
try {
settingsFile.reader().use {
// Force exception to happen
//it.close()
props.load(it)
}
} catch (e: Exception) {
Log.e(TAG, "Error reading settings file: ${e.stackTraceToString()}")
}
}
private val settingsThemesProps =
Properties()
.also { try { it.load(settingsThemesFile.reader()) } catch (e: Exception) { Properties() } }
.also { props -> try { settingsThemesFile.reader().use { props.load(it) } } catch (e: Exception) { /**/ } }
actual val settings: Settings = PropertiesSettings(settingsProps) { settingsProps.store(settingsFile.writer(), "") }
actual val settingsThemes: Settings = PropertiesSettings(settingsThemesProps) { settingsThemesProps.store(settingsThemesFile.writer(), "") }
actual val settings: Settings = PropertiesSettings(settingsProps) { withApi { settingsFile.writer().use { settingsProps.store(it, "") } } }
actual val settingsThemes: Settings = PropertiesSettings(settingsThemesProps) { withApi { settingsThemesFile.writer().use { settingsThemesProps.store(it, "") } } }
actual fun windowOrientation(): WindowOrientation =
if (simplexWindowState.windowState.size.width > simplexWindowState.windowState.size.height) {