diff --git a/src/main/kotlin/app/git/FileChangesWatcher.kt b/src/main/kotlin/app/git/FileChangesWatcher.kt index 3cc046c..c286132 100644 --- a/src/main/kotlin/app/git/FileChangesWatcher.kt +++ b/src/main/kotlin/app/git/FileChangesWatcher.kt @@ -1,9 +1,12 @@ package app.git import app.extensions.systemSeparator -import kotlinx.coroutines.* +import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.flow.MutableSharedFlow import kotlinx.coroutines.flow.SharedFlow +import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext +import java.io.File import java.io.IOException import java.nio.file.* import java.nio.file.StandardWatchEventKinds.* @@ -47,7 +50,7 @@ class FileChangesWatcher @Inject constructor() { var key: WatchKey while (watchService.take().also { key = it } != null) { - key.pollEvents() + val events = key.pollEvents() println("Polled events on dir ${keys[key]}") @@ -59,6 +62,27 @@ class FileChangesWatcher @Inject constructor() { _changesNotifier.emit(hasGitDirectoryChanged) + // Check if new directories have been added to add them to the watchService + launch(Dispatchers.IO) { + for (event in events) { + if (event.kind() == ENTRY_CREATE) { + try { + val eventFile = File(dir.toAbsolutePath().toString() + systemSeparator + event.context()) + + if (eventFile.isDirectory) { + val eventPath = eventFile.toPath() + println("New directory $eventFile detected, adding it to watchService") + val watchKey = + eventPath.register(watchService, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY) + keys[watchKey] = eventPath + } + } catch (ex: Exception) { + ex.printStackTrace() + } + } + } + } + key.reset() } } diff --git a/src/main/kotlin/app/viewmodels/TabViewModel.kt b/src/main/kotlin/app/viewmodels/TabViewModel.kt index b0255f9..677a3fd 100644 --- a/src/main/kotlin/app/viewmodels/TabViewModel.kt +++ b/src/main/kotlin/app/viewmodels/TabViewModel.kt @@ -152,6 +152,8 @@ class TabViewModel @Inject constructor( launch { fileChangesWatcher.changesNotifier.collect { latestUpdateChangedGitDir -> if (!tabState.operationRunning) { // Only update if there isn't any process running + println("Detected changes in the repository's directory") + if(latestUpdateChangedGitDir) { hasGitDirChanged = true } @@ -181,6 +183,8 @@ class TabViewModel @Inject constructor( } lastNotify = currentTimeMillis + } else { + println("Ignoring changed occurred during operation running...") } } }