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() dirCacheEditor.commit()
completedWithErrors = false completedWithErrors = false
// loadStatus(git)
} finally { } finally {
if (completedWithErrors) if (completedWithErrors)
dirCache.unlock() dirCache.unlock()

View File

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

View File

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