Removed stash manager in favor of use casess

This commit is contained in:
Abdelilah El Aissaoui 2022-08-26 05:46:03 +02:00
parent 3b1486efb6
commit 7b1ce8b17a
9 changed files with 124 additions and 65 deletions

View File

@ -1,55 +0,0 @@
package app.git
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import org.eclipse.jgit.api.Git
import org.eclipse.jgit.revwalk.RevCommit
import javax.inject.Inject
class StashManager @Inject constructor() {
suspend fun stash(git: Git, message: String?) = withContext(Dispatchers.IO) {
git
.stashCreate()
.setIncludeUntracked(true)
.apply {
if (message != null)
setWorkingDirectoryMessage(message)
}
.call()
}
suspend fun popStash(git: Git) = withContext(Dispatchers.IO) {
git
.stashApply()
.call()
git.stashDrop()
.call()
}
suspend fun popStash(git: Git, stash: RevCommit) = withContext(Dispatchers.IO) {
applyStash(git, stash)
deleteStash(git, stash)
}
suspend fun getStashList(git: Git) = withContext(Dispatchers.IO) {
return@withContext git
.stashList()
.call()
}
suspend fun applyStash(git: Git, stashInfo: RevCommit) = withContext(Dispatchers.IO) {
git.stashApply()
.setStashRef(stashInfo.name)
.call()
}
suspend fun deleteStash(git: Git, stashInfo: RevCommit) = withContext(Dispatchers.IO) {
val stashList = getStashList(git)
val indexOfStashToDelete = stashList.indexOf(stashInfo)
git.stashDrop()
.setStashRef(indexOfStashToDelete)
.call()
}
}

View File

@ -0,0 +1,15 @@
package app.git.stash
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import org.eclipse.jgit.api.Git
import org.eclipse.jgit.revwalk.RevCommit
import javax.inject.Inject
class ApplyStashUseCase @Inject constructor() {
suspend operator fun invoke(git: Git, stashInfo: RevCommit): Unit = withContext(Dispatchers.IO) {
git.stashApply()
.setStashRef(stashInfo.name)
.call()
}
}

View File

@ -0,0 +1,20 @@
package app.git.stash
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import org.eclipse.jgit.api.Git
import org.eclipse.jgit.revwalk.RevCommit
import javax.inject.Inject
class DeleteStashUseCase @Inject constructor(
private val getStashListUseCase: GetStashListUseCase,
) {
suspend operator fun invoke(git: Git, stashInfo: RevCommit): Unit = withContext(Dispatchers.IO) {
val stashList = getStashListUseCase(git)
val indexOfStashToDelete = stashList.indexOf(stashInfo)
git.stashDrop()
.setStashRef(indexOfStashToDelete)
.call()
}
}

View File

@ -0,0 +1,16 @@
package app.git.stash
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import org.eclipse.jgit.api.Git
import org.eclipse.jgit.revwalk.RevCommit
import javax.inject.Inject
class GetStashListUseCase @Inject constructor() {
suspend operator fun invoke(git: Git): List<RevCommit> = withContext(Dispatchers.IO) {
return@withContext git
.stashList()
.call()
.toList()
}
}

View File

@ -0,0 +1,17 @@
package app.git.stash
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import org.eclipse.jgit.api.Git
import javax.inject.Inject
class PopLastStashUseCase @Inject constructor() {
suspend operator fun invoke(git: Git): Unit = withContext(Dispatchers.IO) {
git
.stashApply()
.call()
git.stashDrop()
.call()
}
}

View File

@ -0,0 +1,17 @@
package app.git.stash
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import org.eclipse.jgit.api.Git
import org.eclipse.jgit.revwalk.RevCommit
import javax.inject.Inject
class PopStashUseCase @Inject constructor(
private val applyStashUseCase: ApplyStashUseCase,
private val deleteStashUseCase: DeleteStashUseCase,
) {
suspend operator fun invoke(git: Git, stash: RevCommit) = withContext(Dispatchers.IO) {
applyStashUseCase(git, stash)
deleteStashUseCase(git, stash)
}
}

View File

@ -0,0 +1,19 @@
package app.git.stash
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import org.eclipse.jgit.api.Git
import javax.inject.Inject
class StashChangesUseCase @Inject constructor() {
suspend operator fun invoke(git: Git, message: String?): Unit = withContext(Dispatchers.IO) {
git
.stashCreate()
.setIncludeUntracked(true)
.apply {
if (message != null)
setWorkingDirectoryMessage(message)
}
.call()
}
}

View File

@ -5,6 +5,8 @@ import app.git.remote_operations.DeleteRemoteBranchUseCase
import app.git.remote_operations.FetchAllBranchesUseCase import app.git.remote_operations.FetchAllBranchesUseCase
import app.git.remote_operations.PullBranchUseCase import app.git.remote_operations.PullBranchUseCase
import app.git.remote_operations.PushBranchUseCase import app.git.remote_operations.PushBranchUseCase
import app.git.stash.PopLastStashUseCase
import app.git.stash.StashChangesUseCase
import app.git.workspace.StageUntrackedFileUseCase import app.git.workspace.StageUntrackedFileUseCase
import java.awt.Desktop import java.awt.Desktop
import javax.inject.Inject import javax.inject.Inject
@ -14,7 +16,8 @@ class MenuViewModel @Inject constructor(
private val pullBranchUseCase: PullBranchUseCase, private val pullBranchUseCase: PullBranchUseCase,
private val pushBranchUseCase: PushBranchUseCase, private val pushBranchUseCase: PushBranchUseCase,
private val fetchAllBranchesUseCase: FetchAllBranchesUseCase, private val fetchAllBranchesUseCase: FetchAllBranchesUseCase,
private val stashManager: StashManager, private val stashChangesUseCase: StashChangesUseCase,
private val popLastStashUseCase: PopLastStashUseCase,
private val stageUntrackedFileUseCase: StageUntrackedFileUseCase, private val stageUntrackedFileUseCase: StageUntrackedFileUseCase,
) { ) {
fun pull(rebase: Boolean = false) = tabState.safeProcessing( fun pull(rebase: Boolean = false) = tabState.safeProcessing(
@ -42,21 +45,21 @@ class MenuViewModel @Inject constructor(
refreshType = RefreshType.UNCOMMITED_CHANGES_AND_LOG, refreshType = RefreshType.UNCOMMITED_CHANGES_AND_LOG,
) { git -> ) { git ->
stageUntrackedFileUseCase(git) stageUntrackedFileUseCase(git)
stashManager.stash(git, null) stashChangesUseCase(git, null)
} }
fun stashWithMessage(message: String) = tabState.safeProcessing( fun stashWithMessage(message: String) = tabState.safeProcessing(
refreshType = RefreshType.UNCOMMITED_CHANGES_AND_LOG, refreshType = RefreshType.UNCOMMITED_CHANGES_AND_LOG,
) { git -> ) { git ->
stageUntrackedFileUseCase(git) stageUntrackedFileUseCase(git)
stashManager.stash(git, message) stashChangesUseCase(git, message)
} }
fun popStash() = tabState.safeProcessing( fun popStash() = tabState.safeProcessing(
refreshType = RefreshType.UNCOMMITED_CHANGES_AND_LOG, refreshType = RefreshType.UNCOMMITED_CHANGES_AND_LOG,
refreshEvenIfCrashes = true, refreshEvenIfCrashes = true,
) { git -> ) { git ->
stashManager.popStash(git) popLastStashUseCase(git)
} }
fun openFolderInFileExplorer() = tabState.runOperation( fun openFolderInFileExplorer() = tabState.runOperation(

View File

@ -1,8 +1,11 @@
package app.viewmodels package app.viewmodels
import app.git.RefreshType import app.git.RefreshType
import app.git.StashManager
import app.git.TabState import app.git.TabState
import app.git.stash.ApplyStashUseCase
import app.git.stash.DeleteStashUseCase
import app.git.stash.GetStashListUseCase
import app.git.stash.PopStashUseCase
import app.ui.SelectedItem import app.ui.SelectedItem
import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.StateFlow
@ -11,16 +14,20 @@ import org.eclipse.jgit.revwalk.RevCommit
import javax.inject.Inject import javax.inject.Inject
class StashesViewModel @Inject constructor( class StashesViewModel @Inject constructor(
private val stashManager: StashManager, private val getStashListUseCase: GetStashListUseCase,
private val applyStashUseCase: ApplyStashUseCase,
private val popStashUseCase: PopStashUseCase,
private val deleteStashUseCase: DeleteStashUseCase,
private val tabState: TabState, private val tabState: TabState,
) : ExpandableViewModel(true) { ) : ExpandableViewModel(true) {
private val _stashStatus = MutableStateFlow<StashStatus>(StashStatus.Loaded(listOf())) private val _stashStatus = MutableStateFlow<StashStatus>(StashStatus.Loaded(listOf()))
val stashStatus: StateFlow<StashStatus> val stashStatus: StateFlow<StashStatus>
get() = _stashStatus get() = _stashStatus
suspend fun loadStashes(git: Git) { suspend fun loadStashes(git: Git) {
_stashStatus.value = StashStatus.Loading _stashStatus.value = StashStatus.Loading
val stashList = stashManager.getStashList(git) val stashList = getStashListUseCase(git)
_stashStatus.value = StashStatus.Loaded(stashList.toList()) _stashStatus.value = StashStatus.Loaded(stashList.toList())
} }
@ -32,14 +39,14 @@ class StashesViewModel @Inject constructor(
refreshType = RefreshType.UNCOMMITED_CHANGES_AND_LOG, refreshType = RefreshType.UNCOMMITED_CHANGES_AND_LOG,
refreshEvenIfCrashes = true, refreshEvenIfCrashes = true,
) { git -> ) { git ->
stashManager.applyStash(git, stashInfo) applyStashUseCase(git, stashInfo)
} }
fun popStash(stash: RevCommit) = tabState.safeProcessing( fun popStash(stash: RevCommit) = tabState.safeProcessing(
refreshType = RefreshType.UNCOMMITED_CHANGES_AND_LOG, refreshType = RefreshType.UNCOMMITED_CHANGES_AND_LOG,
refreshEvenIfCrashes = true, refreshEvenIfCrashes = true,
) { git -> ) { git ->
stashManager.popStash(git, stash) popStashUseCase(git, stash)
stashDropped(stash) stashDropped(stash)
} }
@ -47,7 +54,7 @@ class StashesViewModel @Inject constructor(
fun deleteStash(stash: RevCommit) = tabState.safeProcessing( fun deleteStash(stash: RevCommit) = tabState.safeProcessing(
refreshType = RefreshType.STASHES, refreshType = RefreshType.STASHES,
) { git -> ) { git ->
stashManager.deleteStash(git, stash) deleteStashUseCase(git, stash)
stashDropped(stash) stashDropped(stash)
} }