Fixed flickering when (un)staging many files due to file watcher updating

Commiting show progress bar
This commit is contained in:
Abdelilah El Aissaoui 2021-12-14 20:06:18 +01:00
parent 3f31cd2ac0
commit 7f6cc7c32a
2 changed files with 50 additions and 30 deletions

View File

@ -39,9 +39,7 @@ class FileChangesWatcher @Inject constructor() {
var key: WatchKey var key: WatchKey
while (watchService.take().also { key = it } != null) { while (watchService.take().also { key = it } != null) {
this.emit(Unit) this.emit(Unit)
for (event: WatchEvent<*> in key.pollEvents()) { key.pollEvents()
println("Event kind: ${event.kind()}. File affected: ${event.context()}.")
}
key.reset() key.reset()
} }
} }

View File

@ -62,9 +62,13 @@ class GitManager @Inject constructor(
val cloneStatus: StateFlow<CloneStatus> = remoteOperationsManager.cloneStatus val cloneStatus: StateFlow<CloneStatus> = remoteOperationsManager.cloneStatus
val remotes: StateFlow<List<RemoteInfo>> = remotesManager.remotes val remotes: StateFlow<List<RemoteInfo>> = remotesManager.remotes
private var git: Git? = null private var git: Git? = null
/**
* Property that indicates if a git operation is running
*/
@set:Synchronized private var operationRunning = false
private val safeGit: Git private val safeGit: Git
get() { get() {
val git = this.git val git = this.git
@ -125,7 +129,7 @@ class GitManager @Inject constructor(
pathStr = safeGit.repository.directory.parent, pathStr = safeGit.repository.directory.parent,
ignoredDirsPath = ignored, ignoredDirsPath = ignored,
).collect { ).collect {
if (!_processing.value) { // Only update if there isn't any process running if (!operationRunning) { // Only update if there isn't any process running
safeProcessing(showError = false) { safeProcessing(showError = false) {
println("Changes detected, loading status") println("Changes detected, loading status")
statusManager.loadHasUncommitedChanges(safeGit) statusManager.loadHasUncommitedChanges(safeGit)
@ -156,16 +160,22 @@ class GitManager @Inject constructor(
} }
fun stage(diffEntry: DiffEntry) = managerScope.launch { fun stage(diffEntry: DiffEntry) = managerScope.launch {
statusManager.stage(safeGit, diffEntry) runOperation {
statusManager.stage(safeGit, diffEntry)
}
} }
fun unstage(diffEntry: DiffEntry) = managerScope.launch { fun unstage(diffEntry: DiffEntry) = managerScope.launch {
statusManager.unstage(safeGit, diffEntry) runOperation {
statusManager.unstage(safeGit, diffEntry)
}
} }
fun commit(message: String) = managerScope.launch { fun commit(message: String) = managerScope.launch {
statusManager.commit(safeGit, message) safeProcessing {
refreshRepositoryInfo() statusManager.commit(safeGit, message)
refreshRepositoryInfo()
}
} }
val hasUncommitedChanges: StateFlow<Boolean> val hasUncommitedChanges: StateFlow<Boolean>
@ -251,10 +261,6 @@ class GitManager @Inject constructor(
loadLog() loadLog()
} }
fun statusShouldBeUpdated() {
_lastTimeChecked.value = System.currentTimeMillis()
}
fun credentialsDenied() { fun credentialsDenied() {
credentialsStateManager.updateState(CredentialsState.CredentialsDenied) credentialsStateManager.updateState(CredentialsState.CredentialsDenied)
} }
@ -272,11 +278,15 @@ class GitManager @Inject constructor(
} }
fun unstageAll() = managerScope.launch { fun unstageAll() = managerScope.launch {
statusManager.unstageAll(safeGit) safeProcessing {
statusManager.unstageAll(safeGit)
}
} }
fun stageAll() = managerScope.launch { fun stageAll() = managerScope.launch {
statusManager.stageAll(safeGit) safeProcessing {
statusManager.stageAll(safeGit)
}
} }
fun checkoutCommit(revCommit: RevCommit) = managerScope.launch { fun checkoutCommit(revCommit: RevCommit) = managerScope.launch {
@ -316,21 +326,6 @@ class GitManager @Inject constructor(
var onRepositoryChanged: (path: String?) -> Unit = {} var onRepositoryChanged: (path: String?) -> Unit = {}
@Synchronized
private suspend fun safeProcessing(showError: Boolean = true, callback: suspend () -> Unit) {
_processing.value = true
try {
callback()
} catch (ex: Exception) {
ex.printStackTrace()
if (showError)
errorsManager.addError(newErrorNow(ex, ex.localizedMessage))
} finally {
_processing.value = false
}
}
fun checkoutRef(ref: Ref) = managerScope.launch { fun checkoutRef(ref: Ref) = managerScope.launch {
safeProcessing { safeProcessing {
logManager.checkoutRef(safeGit, ref) logManager.checkoutRef(safeGit, ref)
@ -356,6 +351,33 @@ class GitManager @Inject constructor(
fun findCommit(objectId: ObjectId): RevCommit { fun findCommit(objectId: ObjectId): RevCommit {
return safeGit.repository.parseCommit(objectId) return safeGit.repository.parseCommit(objectId)
} }
@Synchronized
private suspend fun safeProcessing(showError: Boolean = true, callback: suspend () -> Unit) {
_processing.value = true
operationRunning = true
try {
callback()
} catch (ex: Exception) {
ex.printStackTrace()
if (showError)
errorsManager.addError(newErrorNow(ex, ex.localizedMessage))
} finally {
_processing.value = false
operationRunning = false
}
}
private inline fun runOperation(block: () -> Unit) {
operationRunning = true
try {
block()
} finally {
operationRunning = false
}
}
} }