diff --git a/gradle.properties b/gradle.properties index fff01031..aba84e19 100644 --- a/gradle.properties +++ b/gradle.properties @@ -2,4 +2,4 @@ kotlin.code.style=official kotlin.version=1.9.23 agp.version=7.1.3 compose.version=1.6.2 -app.version=1.4.0-beta1 +app.version=1.4.0-beta2 diff --git a/src/jvmMain/kotlin/com/sdercolin/vlabeler/util/FileChangeDetection.kt b/src/jvmMain/kotlin/com/sdercolin/vlabeler/util/FileChangeDetection.kt index a10abde4..e3e28ef8 100644 --- a/src/jvmMain/kotlin/com/sdercolin/vlabeler/util/FileChangeDetection.kt +++ b/src/jvmMain/kotlin/com/sdercolin/vlabeler/util/FileChangeDetection.kt @@ -28,38 +28,46 @@ class FileChangeDetection( return } job = coroutineScope.launch(Dispatchers.IO) { - val watchService = FileSystems.getDefault().newWatchService() - val directoryPath = Paths.get(directory.absolutePath) - directoryPath.register(watchService, ENTRY_CREATE, ENTRY_MODIFY) - while (isActive) { - val key = watchService.take() - yield() - val new = mutableListOf() - val changed = mutableListOf() - key.pollEvents().forEach { event -> - val kind = event.kind() - val path = event.context() as Path - val file = directory.resolve(path.toString()) - if (filter(file).not()) { - return@forEach - } - when (kind) { - ENTRY_CREATE -> new.add(file) - ENTRY_MODIFY -> changed.add(file) - } - Log.debug("File change detected: new=$new, changed=$changed") - if (callbackJob?.isActive == true) { - Log.debug("File change detection callback is already running, skipping") - } else { - callbackJob = coroutineScope.launch { - callback.onFileChanged(changed, new) + runCatching { + if (directory.isDirectory.not()) { + Log.info("Creating directory $directory during file change detection") + directory.mkdirs() + } + val watchService = FileSystems.getDefault().newWatchService() + val directoryPath = Paths.get(directory.absolutePath) + directoryPath.register(watchService, ENTRY_CREATE, ENTRY_MODIFY) + while (isActive) { + val key = watchService.take() + yield() + val new = mutableListOf() + val changed = mutableListOf() + key.pollEvents().forEach { event -> + val kind = event.kind() + val path = event.context() as Path + val file = directory.resolve(path.toString()) + if (filter(file).not()) { + return@forEach + } + when (kind) { + ENTRY_CREATE -> new.add(file) + ENTRY_MODIFY -> changed.add(file) + } + Log.debug("File change detected: new=$new, changed=$changed") + if (callbackJob?.isActive == true) { + Log.debug("File change detection callback is already running, skipping") + } else { + callbackJob = coroutineScope.launch { + callback.onFileChanged(changed, new) + } } } + val valid = key.reset() + if (!valid) { + break + } } - val valid = key.reset() - if (!valid) { - break - } + }.onFailure { + Log.error(it) } } }