Commit message is now preserved even after disposing the composable (stored in he VM)

This commit is contained in:
Abdelilah El Aissaoui 2022-01-03 23:04:04 +01:00
parent e6619cbd4b
commit 620fd1022b
3 changed files with 12 additions and 11 deletions

View File

@ -85,8 +85,6 @@ class StatusManager @Inject constructor(
dirCacheEditor.commit()
completedWithErrors = false
// loadStatus(git)
} finally {
if (completedWithErrors)
dirCache.unlock()

View File

@ -51,8 +51,9 @@ fun UncommitedChanges(
onUnstagedDiffEntrySelected: (DiffEntry) -> Unit,
) {
val stageStatusState = statusViewModel.stageStatus.collectAsState()
val stageStatus = stageStatusState.value
val commitMessage by statusViewModel.commitMessage.collectAsState()
val stageStatus = stageStatusState.value
val staged: List<StatusEntry>
val unstaged: List<StatusEntry>
if (stageStatus is StageStatus.Loaded) {
@ -74,11 +75,10 @@ fun UncommitedChanges(
unstaged = listOf<StatusEntry>() // return empty lists if still loading
}
var commitMessage by remember { mutableStateOf("") }
val doCommit = {
statusViewModel.commit(commitMessage)
onStagedDiffEntrySelected(null)
commitMessage = ""
statusViewModel.newCommitMessage = ""
}
val canCommit = commitMessage.isNotEmpty() && staged.isNotEmpty()
@ -158,7 +158,7 @@ fun UncommitedChanges(
false
},
value = commitMessage,
onValueChange = { commitMessage = it },
onValueChange = { statusViewModel.newCommitMessage = it },
label = { Text("Write your commit message here", fontSize = 14.sp) },
colors = TextFieldDefaults.textFieldColors(backgroundColor = MaterialTheme.colors.background),
textStyle = TextStyle.Default.copy(fontSize = 14.sp),

View File

@ -4,7 +4,6 @@ import app.git.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.eclipse.jgit.api.Git
import org.eclipse.jgit.diff.DiffEntry
@ -19,6 +18,13 @@ class StatusViewModel @Inject constructor(
private val _stageStatus = MutableStateFlow<StageStatus>(StageStatus.Loaded(listOf(), listOf()))
val stageStatus: StateFlow<StageStatus> = _stageStatus
private val _commitMessage = MutableStateFlow("")
val commitMessage: StateFlow<String> = _commitMessage
var newCommitMessage: String
get() = commitMessage.value
set(value) {
_commitMessage.value = value
}
private val _hasUncommitedChanges = MutableStateFlow<Boolean>(false)
val hasUncommitedChanges: StateFlow<Boolean>
@ -37,7 +43,6 @@ class StatusViewModel @Inject constructor(
}
fun unstageAll() = tabState.safeProcessing { git ->
statusManager.unstageAll(git)
@ -51,14 +56,13 @@ class StatusViewModel @Inject constructor(
}
fun resetStaged(diffEntry: DiffEntry) = tabState.runOperation { git ->
statusManager.reset(git, diffEntry, staged = true)
return@runOperation RefreshType.UNCOMMITED_CHANGES
}
fun resetUnstaged(diffEntry: DiffEntry) =tabState.runOperation { git ->
fun resetUnstaged(diffEntry: DiffEntry) = tabState.runOperation { git ->
statusManager.reset(git, diffEntry, staged = false)
return@runOperation RefreshType.UNCOMMITED_CHANGES
@ -92,7 +96,6 @@ class StatusViewModel @Inject constructor(
}
suspend fun refresh(git: Git) = withContext(Dispatchers.IO) {
loadStatus(git)
loadHasUncommitedChanges(git)