diff --git a/src/main/kotlin/app/git/StashManager.kt b/src/main/kotlin/app/git/StashManager.kt deleted file mode 100644 index 1fc906f..0000000 --- a/src/main/kotlin/app/git/StashManager.kt +++ /dev/null @@ -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() - } -} \ No newline at end of file diff --git a/src/main/kotlin/app/git/stash/ApplyStashUseCase.kt b/src/main/kotlin/app/git/stash/ApplyStashUseCase.kt new file mode 100644 index 0000000..58cb65c --- /dev/null +++ b/src/main/kotlin/app/git/stash/ApplyStashUseCase.kt @@ -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() + } +} \ No newline at end of file diff --git a/src/main/kotlin/app/git/stash/DeleteStashUseCase.kt b/src/main/kotlin/app/git/stash/DeleteStashUseCase.kt new file mode 100644 index 0000000..36cc5c1 --- /dev/null +++ b/src/main/kotlin/app/git/stash/DeleteStashUseCase.kt @@ -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() + } +} \ No newline at end of file diff --git a/src/main/kotlin/app/git/stash/GetStashListUseCase.kt b/src/main/kotlin/app/git/stash/GetStashListUseCase.kt new file mode 100644 index 0000000..bb47b04 --- /dev/null +++ b/src/main/kotlin/app/git/stash/GetStashListUseCase.kt @@ -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 = withContext(Dispatchers.IO) { + return@withContext git + .stashList() + .call() + .toList() + } +} \ No newline at end of file diff --git a/src/main/kotlin/app/git/stash/PopLastStashUseCase.kt b/src/main/kotlin/app/git/stash/PopLastStashUseCase.kt new file mode 100644 index 0000000..b9ea4b1 --- /dev/null +++ b/src/main/kotlin/app/git/stash/PopLastStashUseCase.kt @@ -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() + } +} \ No newline at end of file diff --git a/src/main/kotlin/app/git/stash/PopStashUseCase.kt b/src/main/kotlin/app/git/stash/PopStashUseCase.kt new file mode 100644 index 0000000..7475205 --- /dev/null +++ b/src/main/kotlin/app/git/stash/PopStashUseCase.kt @@ -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) + } +} \ No newline at end of file diff --git a/src/main/kotlin/app/git/stash/StashChangesUseCase.kt b/src/main/kotlin/app/git/stash/StashChangesUseCase.kt new file mode 100644 index 0000000..cb626e8 --- /dev/null +++ b/src/main/kotlin/app/git/stash/StashChangesUseCase.kt @@ -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() + } +} \ No newline at end of file diff --git a/src/main/kotlin/app/viewmodels/MenuViewModel.kt b/src/main/kotlin/app/viewmodels/MenuViewModel.kt index dab9adb..a048f0c 100644 --- a/src/main/kotlin/app/viewmodels/MenuViewModel.kt +++ b/src/main/kotlin/app/viewmodels/MenuViewModel.kt @@ -5,6 +5,8 @@ import app.git.remote_operations.DeleteRemoteBranchUseCase import app.git.remote_operations.FetchAllBranchesUseCase import app.git.remote_operations.PullBranchUseCase import app.git.remote_operations.PushBranchUseCase +import app.git.stash.PopLastStashUseCase +import app.git.stash.StashChangesUseCase import app.git.workspace.StageUntrackedFileUseCase import java.awt.Desktop import javax.inject.Inject @@ -14,7 +16,8 @@ class MenuViewModel @Inject constructor( private val pullBranchUseCase: PullBranchUseCase, private val pushBranchUseCase: PushBranchUseCase, private val fetchAllBranchesUseCase: FetchAllBranchesUseCase, - private val stashManager: StashManager, + private val stashChangesUseCase: StashChangesUseCase, + private val popLastStashUseCase: PopLastStashUseCase, private val stageUntrackedFileUseCase: StageUntrackedFileUseCase, ) { fun pull(rebase: Boolean = false) = tabState.safeProcessing( @@ -42,21 +45,21 @@ class MenuViewModel @Inject constructor( refreshType = RefreshType.UNCOMMITED_CHANGES_AND_LOG, ) { git -> stageUntrackedFileUseCase(git) - stashManager.stash(git, null) + stashChangesUseCase(git, null) } fun stashWithMessage(message: String) = tabState.safeProcessing( refreshType = RefreshType.UNCOMMITED_CHANGES_AND_LOG, ) { git -> stageUntrackedFileUseCase(git) - stashManager.stash(git, message) + stashChangesUseCase(git, message) } fun popStash() = tabState.safeProcessing( refreshType = RefreshType.UNCOMMITED_CHANGES_AND_LOG, refreshEvenIfCrashes = true, ) { git -> - stashManager.popStash(git) + popLastStashUseCase(git) } fun openFolderInFileExplorer() = tabState.runOperation( diff --git a/src/main/kotlin/app/viewmodels/StashesViewModel.kt b/src/main/kotlin/app/viewmodels/StashesViewModel.kt index 069fa26..d9cbe89 100644 --- a/src/main/kotlin/app/viewmodels/StashesViewModel.kt +++ b/src/main/kotlin/app/viewmodels/StashesViewModel.kt @@ -1,8 +1,11 @@ package app.viewmodels import app.git.RefreshType -import app.git.StashManager 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 kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.StateFlow @@ -11,16 +14,20 @@ import org.eclipse.jgit.revwalk.RevCommit import javax.inject.Inject 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, ) : ExpandableViewModel(true) { private val _stashStatus = MutableStateFlow(StashStatus.Loaded(listOf())) + val stashStatus: StateFlow get() = _stashStatus suspend fun loadStashes(git: Git) { _stashStatus.value = StashStatus.Loading - val stashList = stashManager.getStashList(git) + val stashList = getStashListUseCase(git) _stashStatus.value = StashStatus.Loaded(stashList.toList()) } @@ -32,14 +39,14 @@ class StashesViewModel @Inject constructor( refreshType = RefreshType.UNCOMMITED_CHANGES_AND_LOG, refreshEvenIfCrashes = true, ) { git -> - stashManager.applyStash(git, stashInfo) + applyStashUseCase(git, stashInfo) } fun popStash(stash: RevCommit) = tabState.safeProcessing( refreshType = RefreshType.UNCOMMITED_CHANGES_AND_LOG, refreshEvenIfCrashes = true, ) { git -> - stashManager.popStash(git, stash) + popStashUseCase(git, stash) stashDropped(stash) } @@ -47,7 +54,7 @@ class StashesViewModel @Inject constructor( fun deleteStash(stash: RevCommit) = tabState.safeProcessing( refreshType = RefreshType.STASHES, ) { git -> - stashManager.deleteStash(git, stash) + deleteStashUseCase(git, stash) stashDropped(stash) }