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
while (watchService.take().also { key = it } != null) {
this.emit(Unit)
for (event: WatchEvent<*> in key.pollEvents()) {
println("Event kind: ${event.kind()}. File affected: ${event.context()}.")
}
key.pollEvents()
key.reset()
}
}

View File

@ -62,9 +62,13 @@ class GitManager @Inject constructor(
val cloneStatus: StateFlow<CloneStatus> = remoteOperationsManager.cloneStatus
val remotes: StateFlow<List<RemoteInfo>> = remotesManager.remotes
private var git: Git? = null
/**
* Property that indicates if a git operation is running
*/
@set:Synchronized private var operationRunning = false
private val safeGit: Git
get() {
val git = this.git
@ -125,7 +129,7 @@ class GitManager @Inject constructor(
pathStr = safeGit.repository.directory.parent,
ignoredDirsPath = ignored,
).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) {
println("Changes detected, loading status")
statusManager.loadHasUncommitedChanges(safeGit)
@ -156,17 +160,23 @@ class GitManager @Inject constructor(
}
fun stage(diffEntry: DiffEntry) = managerScope.launch {
runOperation {
statusManager.stage(safeGit, diffEntry)
}
}
fun unstage(diffEntry: DiffEntry) = managerScope.launch {
runOperation {
statusManager.unstage(safeGit, diffEntry)
}
}
fun commit(message: String) = managerScope.launch {
safeProcessing {
statusManager.commit(safeGit, message)
refreshRepositoryInfo()
}
}
val hasUncommitedChanges: StateFlow<Boolean>
get() = statusManager.hasUncommitedChanges
@ -251,10 +261,6 @@ class GitManager @Inject constructor(
loadLog()
}
fun statusShouldBeUpdated() {
_lastTimeChecked.value = System.currentTimeMillis()
}
fun credentialsDenied() {
credentialsStateManager.updateState(CredentialsState.CredentialsDenied)
}
@ -272,12 +278,16 @@ class GitManager @Inject constructor(
}
fun unstageAll() = managerScope.launch {
safeProcessing {
statusManager.unstageAll(safeGit)
}
}
fun stageAll() = managerScope.launch {
safeProcessing {
statusManager.stageAll(safeGit)
}
}
fun checkoutCommit(revCommit: RevCommit) = managerScope.launch {
safeProcessing {
@ -316,21 +326,6 @@ class GitManager @Inject constructor(
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 {
safeProcessing {
logManager.checkoutRef(safeGit, ref)
@ -356,6 +351,33 @@ class GitManager @Inject constructor(
fun findCommit(objectId: ObjectId): RevCommit {
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
}
}
}