Skip to content

Commit f94b198

Browse files
committed
refactor(desktop): Unify application data directory naming and optimize WebView initialization
1 parent 5baa876 commit f94b198

File tree

4 files changed

+61
-37
lines changed

4 files changed

+61
-37
lines changed

composeApp/src/jvmMain/kotlin/com/jankinwu/fntv/client/main.kt

Lines changed: 55 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,18 @@ import kotlinx.coroutines.flow.MutableStateFlow
7272
import kotlinx.coroutines.flow.debounce
7373
import kotlinx.coroutines.isActive
7474
import kotlinx.coroutines.launch
75+
import kotlinx.coroutines.withContext
7576
import org.jetbrains.compose.resources.painterResource
7677
import org.koin.compose.KoinApplication
7778
import org.koin.compose.viewmodel.koinViewModel
7879
import org.openani.mediamp.PlaybackState
7980
import org.openani.mediamp.compose.rememberMediampPlayer
81+
import java.awt.EventQueue
8082
import java.awt.Dimension
8183
import java.io.File
8284
import java.io.RandomAccessFile
8385
import java.util.concurrent.atomic.AtomicBoolean
86+
import kotlin.coroutines.CoroutineContext
8487

8588
private object WindowsDisplaySleepBlocker {
8689
private const val ES_SYSTEM_REQUIRED = 0x00000001
@@ -113,12 +116,6 @@ fun main() {
113116
Logger.setLogWriters(ConsoleLogWriter(), FileLogWriter(logDir))
114117
Logger.withTag("main").i { "Application started. Logs directory: ${logDir.absolutePath}" }
115118

116-
WebViewBootstrap.start(
117-
installDir = kcefInstallDir(),
118-
cacheDir = kcefCacheDir(),
119-
logDir = logDir
120-
)
121-
122119
application {
123120
DisposableEffect(Unit) {
124121
ProxyManager.start()
@@ -127,6 +124,14 @@ fun main() {
127124
}
128125
}
129126

127+
LaunchedEffect(Unit) {
128+
WebViewBootstrap.start(
129+
installDir = kcefInstallDir(),
130+
cacheDir = kcefCacheDir(),
131+
logDir = logDir
132+
)
133+
}
134+
130135
val webViewInitialized by WebViewBootstrap.initialized.collectAsState()
131136
val webViewRestartRequired by WebViewBootstrap.restartRequired.collectAsState()
132137
val webViewInitError by WebViewBootstrap.initError.collectAsState()
@@ -467,7 +472,16 @@ fun main() {
467472

468473
private object WebViewBootstrap {
469474
private val started = AtomicBoolean(false)
470-
private val scope = CoroutineScope(SupervisorJob() + Dispatchers.IO)
475+
private val scope = CoroutineScope(SupervisorJob() + Dispatchers.Default)
476+
private val awtDispatcher = object : kotlinx.coroutines.CoroutineDispatcher() {
477+
override fun dispatch(context: CoroutineContext, block: Runnable) {
478+
if (EventQueue.isDispatchThread()) {
479+
block.run()
480+
} else {
481+
EventQueue.invokeLater(block)
482+
}
483+
}
484+
}
471485

472486
val initialized = MutableStateFlow(false)
473487
val restartRequired = MutableStateFlow(false)
@@ -488,27 +502,35 @@ private object WebViewBootstrap {
488502
if (!installDir.exists()) installDir.mkdirs()
489503
if (!cacheDir.exists()) cacheDir.mkdirs()
490504

491-
KCEF.init(
492-
builder = {
493-
installDir(installDir)
494-
settings {
495-
cachePath = cacheDir.absolutePath
496-
logFile = kcefLog.absolutePath
497-
// logSeverity = KCEFBuilder.Settings.LogSeverity.INFO
498-
}
499-
progress {
500-
onInitialized {
501-
initialized.value = true
505+
val initFailure = runCatching {
506+
withContext(awtDispatcher) {
507+
KCEF.init(
508+
builder = {
509+
installDir(installDir)
510+
settings {
511+
cachePath = cacheDir.absolutePath
512+
logFile = kcefLog.absolutePath
513+
// logSeverity = KCEFBuilder.Settings.LogSeverity.INFO
502514
}
515+
progress {
516+
onInitialized {
517+
initialized.value = true
518+
}
519+
}
520+
},
521+
onError = { throwable ->
522+
initError.value = throwable
523+
},
524+
onRestartRequired = {
525+
restartRequired.value = true
503526
}
504-
},
505-
onError = { throwable ->
506-
initError.value = throwable
507-
},
508-
onRestartRequired = {
509-
restartRequired.value = true
527+
)
510528
}
511-
)
529+
}.exceptionOrNull()
530+
531+
if (initFailure != null) {
532+
initError.value = initFailure
533+
}
512534
} catch (t: Throwable) {
513535
initError.value = t
514536
}
@@ -567,27 +589,29 @@ private object WebViewBootstrap {
567589
private fun kcefInstallDir(): File {
568590
val platform = currentPlatformDesktop()
569591
val baseDir = when (platform) {
570-
is Platform.Linux -> File(System.getProperty("user.home"), ".local/share/fn-media")
571-
is Platform.MacOS -> File(System.getProperty("user.home"), "Library/Application Support/fn-media")
592+
is Platform.Linux -> File(System.getProperty("user.home"), ".local/share/fly-narwhal")
593+
is Platform.MacOS -> File(System.getProperty("user.home"), "Library/Application Support/fly-narwhal")
572594
is Platform.Windows -> {
573595
val localAppData = System.getenv("LOCALAPPDATA")?.takeIf { it.isNotBlank() }
574596
File(localAppData ?: System.getProperty("user.home"), "FlyNarwhal")
575597
}
576598
}
577-
return File(baseDir, "kcef-bundle")
599+
val version = BuildConfig.VERSION_NAME.replace(Regex("[^A-Za-z0-9._-]"), "_")
600+
return File(baseDir, "kcef-bundle-$version")
578601
}
579602

580603
private fun kcefCacheDir(): File {
581604
val platform = currentPlatformDesktop()
582605
val baseDir = when (platform) {
583-
is Platform.Linux -> File(System.getProperty("user.home"), ".local/share/fn-media")
584-
is Platform.MacOS -> File(System.getProperty("user.home"), "Library/Application Support/fn-media")
606+
is Platform.Linux -> File(System.getProperty("user.home"), ".local/share/fly-narwhal")
607+
is Platform.MacOS -> File(System.getProperty("user.home"), "Library/Application Support/fly-narwhal")
585608
is Platform.Windows -> {
586609
val localAppData = System.getenv("LOCALAPPDATA")?.takeIf { it.isNotBlank() }
587610
File(localAppData ?: System.getProperty("user.home"), "FlyNarwhal")
588611
}
589612
}
590-
return File(baseDir, "kcef-cache")
613+
val version = BuildConfig.VERSION_NAME.replace(Regex("[^A-Za-z0-9._-]"), "_")
614+
return File(baseDir, "kcef-cache-$version")
591615
}
592616

593617
/**

composeApp/src/jvmMain/kotlin/com/jankinwu/fntv/client/manager/DesktopUpdateManager.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -332,8 +332,8 @@ class DesktopUpdateManager : UpdateManager {
332332
// Linux/macOS: Use a user-writable directory
333333
val userHome = System.getProperty("user.home")
334334
val appDataDir = when {
335-
osName.contains("mac") -> File(userHome, "Library/Caches/FlyNarwhal/updates")
336-
else -> File(userHome, ".cache/FlyNarwhal/updates") // Linux/Unix
335+
osName.contains("mac") -> File(userHome, "Library/Caches/fly-narwhal/updates")
336+
else -> File(userHome, ".cache/fly-narwhal/updates") // Linux/Unix
337337
}
338338
if (!appDataDir.exists()) {
339339
appDataDir.mkdirs()

composeApp/src/jvmMain/kotlin/com/jankinwu/fntv/client/manager/ProxyManager.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,8 @@ object ProxyManager {
8484
File(exeDir, "app/resources/fntv-proxy")
8585
} else {
8686
val appDataDir = when {
87-
osName.contains("mac") -> File(userHome, "Library/Application Support/FlyNarwhal")
88-
else -> File(userHome, ".local/share/fn-media") // Linux/Unix
87+
osName.contains("mac") -> File(userHome, "Library/Application Support/fly-narwhal")
88+
else -> File(userHome, ".local/share/fly-narwhal") // Linux/Unix
8989
}
9090
File(appDataDir, "proxy")
9191
}

composeApp/src/jvmMain/kotlin/com/jankinwu/fntv/client/utils/DesktopUpdateInstaller.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,9 @@ interface DesktopUpdateInstaller : UpdateInstaller {
6969

7070
val userHome = System.getProperty("user.home")
7171
val appDataDir = if (osName.contains("mac")) {
72-
File(userHome, "Library/Application Support/FlyNarwhal")
72+
File(userHome, "Library/Application Support/fly-narwhal")
7373
} else {
74-
File(userHome, ".local/share/fn-media")
74+
File(userHome, ".local/share/fly-narwhal")
7575
}
7676

7777
val proxyDir = File(appDataDir, "proxy")

0 commit comments

Comments
 (0)