Code cleanup and fix typos

This commit is contained in:
Abdelilah El Aissaoui 2023-12-05 14:35:19 +01:00
parent e91bfcf146
commit c2e69d00f3
No known key found for this signature in database
GPG Key ID: 7587FC860F594869
13 changed files with 52 additions and 68 deletions

View File

@ -74,7 +74,6 @@ class TabState @Inject constructor(
@Synchronized
fun safeProcessing(
showError: Boolean = true,
refreshType: RefreshType,
// TODO Eventually the title and subtitles should be mandatory but for now the default it's empty to slowly
// migrate the code that uses this function
@ -144,7 +143,7 @@ class TabState @Inject constructor(
val containsCancellation = exceptionContainsCancellation(ex)
if (showError && !containsCancellation)
if (!containsCancellation)
errorsManager.addError(newErrorNow(ex, null, ex.message.orEmpty()))
printError(TAG, ex.message.orEmpty(), ex)
@ -174,7 +173,6 @@ class TabState @Inject constructor(
}
fun safeProcessingWithoutGit(
showError: Boolean = true,
// TODO Eventually the title and subtitles should be mandatory but for now the default it's empty to slowly
// migrate the code that uses this function
title: String = "",
@ -193,7 +191,7 @@ class TabState @Inject constructor(
val containsCancellation = exceptionContainsCancellation(ex)
if (showError && !containsCancellation)
if (!containsCancellation)
errorsManager.addError(newErrorNow(ex, null, ex.localizedMessage))
printError(TAG, ex.message.orEmpty(), ex)
@ -318,8 +316,8 @@ enum class RefreshType {
ONLY_LOG,
STASHES,
SUBMODULES,
UNCOMMITED_CHANGES,
UNCOMMITED_CHANGES_AND_LOG,
UNCOMMITTED_CHANGES,
UNCOMMITTED_CHANGES_AND_LOG,
REMOTES,
REBASE_INTERACTIVE_STATE,
}

View File

@ -477,8 +477,8 @@ fun MessagesList(
}
}
// Setting a key makes the graph preserve the scroll position when a new line has been added on top (uncommited changes)
// Therefore, after popping a stash, the uncommited changes wouldn't be visible and requires the user scrolling.
// Setting a key makes the graph preserve the scroll position when a new line has been added on top (uncommitted changes)
// Therefore, after popping a stash, the uncommitted changes wouldn't be visible and requires the user scrolling.
items(items = commitList) { graphNode ->
CommitLine(
graphWidth = graphWidth,

View File

@ -74,8 +74,8 @@ class DiffViewModel @Inject constructor(
tabScope.launch {
tabState.refreshFlowFiltered(
RefreshType.UNCOMMITED_CHANGES,
RefreshType.UNCOMMITED_CHANGES_AND_LOG,
RefreshType.UNCOMMITTED_CHANGES,
RefreshType.UNCOMMITTED_CHANGES_AND_LOG,
) {
val diffResultValue = diffResult.value
if (diffResultValue is ViewDiffResult.Loaded) {
@ -143,7 +143,7 @@ class DiffViewModel @Inject constructor(
}
} catch (ex: Exception) {
if (ex is MissingDiffEntryException) {
tabState.refreshData(refreshType = RefreshType.UNCOMMITED_CHANGES)
tabState.refreshData(refreshType = RefreshType.UNCOMMITTED_CHANGES)
_diffResult.value = ViewDiffResult.DiffNotFound
} else
ex.printStackTrace()
@ -152,33 +152,33 @@ class DiffViewModel @Inject constructor(
}
fun stageHunk(diffEntry: DiffEntry, hunk: Hunk) = tabState.runOperation(
refreshType = RefreshType.UNCOMMITED_CHANGES,
refreshType = RefreshType.UNCOMMITTED_CHANGES,
) { git ->
stageHunkUseCase(git, diffEntry, hunk)
}
fun resetHunk(diffEntry: DiffEntry, hunk: Hunk) = tabState.runOperation(
refreshType = RefreshType.UNCOMMITED_CHANGES,
refreshType = RefreshType.UNCOMMITTED_CHANGES,
showError = true,
) { git ->
resetHunkUseCase(git, diffEntry, hunk)
}
fun unstageHunk(diffEntry: DiffEntry, hunk: Hunk) = tabState.runOperation(
refreshType = RefreshType.UNCOMMITED_CHANGES,
refreshType = RefreshType.UNCOMMITTED_CHANGES,
) { git ->
unstageHunkUseCase(git, diffEntry, hunk)
}
fun stageFile(statusEntry: StatusEntry) = tabState.runOperation(
refreshType = RefreshType.UNCOMMITED_CHANGES,
refreshType = RefreshType.UNCOMMITTED_CHANGES,
showError = true,
) { git ->
stageEntryUseCase(git, statusEntry)
}
fun unstageFile(statusEntry: StatusEntry) = tabState.runOperation(
refreshType = RefreshType.UNCOMMITED_CHANGES,
refreshType = RefreshType.UNCOMMITTED_CHANGES,
showError = true,
) { git ->
unstageEntryUseCase(git, statusEntry)
@ -197,14 +197,14 @@ class DiffViewModel @Inject constructor(
}
fun stageHunkLine(entry: DiffEntry, hunk: Hunk, line: Line) = tabState.runOperation(
refreshType = RefreshType.UNCOMMITED_CHANGES,
refreshType = RefreshType.UNCOMMITTED_CHANGES,
showError = true,
) { git ->
stageHunkLineUseCase(git, entry, hunk, line)
}
fun unstageHunkLine(entry: DiffEntry, hunk: Hunk, line: Line) = tabState.runOperation(
refreshType = RefreshType.UNCOMMITED_CHANGES,
refreshType = RefreshType.UNCOMMITTED_CHANGES,
showError = true,
) { git ->
unstageHunkLineUseCase(git, entry, hunk, line)
@ -215,7 +215,7 @@ class DiffViewModel @Inject constructor(
}
fun discardHunkLine(entry: DiffEntry, hunk: Hunk, line: Line) = tabState.runOperation(
refreshType = RefreshType.UNCOMMITED_CHANGES,
refreshType = RefreshType.UNCOMMITTED_CHANGES,
showError = true,
) { git ->
discardUnstagedHunkLineUseCase(git, entry, hunk, line)

View File

@ -119,7 +119,7 @@ class HistoryViewModel @Inject constructor(
_viewDiffResult.value = ViewDiffResult.Loaded(diffEntryType, formattedDiffResult)
} catch (ex: Exception) {
if (ex is MissingDiffEntryException) {
tabState.refreshData(refreshType = RefreshType.UNCOMMITED_CHANGES)
tabState.refreshData(refreshType = RefreshType.UNCOMMITTED_CHANGES)
_viewDiffResult.value = ViewDiffResult.DiffNotFound
} else
ex.printStackTrace()

View File

@ -17,9 +17,6 @@ import com.jetpackduba.gitnuro.git.rebase.StartRebaseInteractiveUseCase
import com.jetpackduba.gitnuro.git.remote_operations.DeleteRemoteBranchUseCase
import com.jetpackduba.gitnuro.git.remote_operations.PullFromSpecificBranchUseCase
import com.jetpackduba.gitnuro.git.remote_operations.PushToSpecificBranchUseCase
import com.jetpackduba.gitnuro.git.stash.ApplyStashUseCase
import com.jetpackduba.gitnuro.git.stash.DeleteStashUseCase
import com.jetpackduba.gitnuro.git.stash.PopStashUseCase
import com.jetpackduba.gitnuro.git.tags.CreateTagOnCommitUseCase
import com.jetpackduba.gitnuro.git.tags.DeleteTagUseCase
import com.jetpackduba.gitnuro.git.workspace.CheckHasUncommitedChangesUseCase
@ -53,7 +50,7 @@ private const val LOG_MIN_TIME_IN_MS_TO_SHOW_LOAD = 500L
class LogViewModel @Inject constructor(
private val getLogUseCase: GetLogUseCase,
private val getStatusSummaryUseCase: GetStatusSummaryUseCase,
private val checkHasUncommitedChangesUseCase: CheckHasUncommitedChangesUseCase,
private val checkHasUncommittedChangesUseCase: CheckHasUncommitedChangesUseCase,
private val getCurrentBranchUseCase: GetCurrentBranchUseCase,
private val checkoutRefUseCase: CheckoutRefUseCase,
private val createBranchOnCommitUseCase: CreateBranchOnCommitUseCase,
@ -70,13 +67,10 @@ class LogViewModel @Inject constructor(
private val deleteTagUseCase: DeleteTagUseCase,
private val rebaseBranchUseCase: RebaseBranchUseCase,
private val startRebaseInteractiveUseCase: StartRebaseInteractiveUseCase,
private val applyStashUseCase: ApplyStashUseCase,
private val popStashUseCase: PopStashUseCase,
private val deleteStashUseCase: DeleteStashUseCase,
private val tabState: TabState,
private val appSettings: AppSettings,
private val tabScope: CoroutineScope,
sharedStashViewModel: SharedStashViewModel,
private val sharedStashViewModel: SharedStashViewModel,
) : ViewModel, ISharedStashViewModel by sharedStashViewModel {
private val _logStatus = MutableStateFlow<LogStatus>(LogStatus.Loading)
@ -125,10 +119,10 @@ class LogViewModel @Inject constructor(
tabState.refreshFlowFiltered(
RefreshType.ALL_DATA,
RefreshType.ONLY_LOG,
RefreshType.UNCOMMITED_CHANGES,
RefreshType.UNCOMMITED_CHANGES_AND_LOG,
RefreshType.UNCOMMITTED_CHANGES,
RefreshType.UNCOMMITTED_CHANGES_AND_LOG,
) { refreshType ->
if (refreshType == RefreshType.UNCOMMITED_CHANGES) {
if (refreshType == RefreshType.UNCOMMITTED_CHANGES) {
uncommitedChangesLoadLog(tabState.git)
} else
refresh(tabState.git)
@ -205,9 +199,9 @@ class LogViewModel @Inject constructor(
fun revertCommit(revCommit: RevCommit) = tabState.safeProcessing(
refreshType = RefreshType.ALL_DATA,
refreshEvenIfCrashes = true,
title = "Commit revert",
subtitle = "Reverting commit ${revCommit.name}",
refreshEvenIfCrashes = true,
) { git ->
revertCommitUseCase(git, revCommit)
}
@ -229,7 +223,7 @@ class LogViewModel @Inject constructor(
}
fun cherrypickCommit(revCommit: RevCommit) = tabState.safeProcessing(
refreshType = RefreshType.UNCOMMITED_CHANGES_AND_LOG,
refreshType = RefreshType.UNCOMMITTED_CHANGES_AND_LOG,
title = "Cherry-pick",
subtitle = "Cherry-picking commit ${revCommit.shortName}",
) { git ->
@ -238,9 +232,9 @@ class LogViewModel @Inject constructor(
fun createBranchOnCommit(branch: String, revCommit: RevCommit) = tabState.safeProcessing(
refreshType = RefreshType.ALL_DATA,
refreshEvenIfCrashesInteractive = { it is CheckoutConflictException },
title = "New branch",
subtitle = "Creating new branch \"$branch\" on commit ${revCommit.shortName}",
refreshEvenIfCrashesInteractive = { it is CheckoutConflictException },
) { git ->
createBranchOnCommitUseCase(git, branch, revCommit)
}
@ -279,7 +273,7 @@ class LogViewModel @Inject constructor(
private suspend fun uncommitedChangesLoadLog(git: Git) {
val currentBranch = getCurrentBranchUseCase(git)
val hasUncommitedChanges = checkHasUncommitedChangesUseCase(git)
val hasUncommitedChanges = checkHasUncommittedChangesUseCase(git)
val statsSummary = if (hasUncommitedChanges) {
getStatusSummaryUseCase(

View File

@ -28,41 +28,41 @@ class MenuViewModel @Inject constructor(
fun pull(pullType: PullType) = tabState.safeProcessing(
refreshType = RefreshType.ALL_DATA,
refreshEvenIfCrashes = true,
title = "Pulling",
subtitle = "Pulling changes from the remote branch to the current branch"
subtitle = "Pulling changes from the remote branch to the current branch",
refreshEvenIfCrashes = true
) { git ->
pullBranchUseCase(git, pullType)
}
fun fetchAll() = tabState.safeProcessing(
refreshType = RefreshType.ALL_DATA,
refreshEvenIfCrashes = true,
title = "Fetching",
subtitle = "Updating references from the remote repositories...",
isCancellable = false
isCancellable = false,
refreshEvenIfCrashes = true
) { git ->
fetchAllBranchesUseCase(git)
}
fun push(force: Boolean = false, pushTags: Boolean = false) = tabState.safeProcessing(
refreshType = RefreshType.ALL_DATA,
refreshEvenIfCrashes = true,
title = "Push",
subtitle = "Pushing current branch to the remote repository",
isCancellable = false,
refreshEvenIfCrashes = true,
) { git ->
pushBranchUseCase(git, force, pushTags)
}
fun stash() = tabState.safeProcessing(
refreshType = RefreshType.UNCOMMITED_CHANGES_AND_LOG,
refreshType = RefreshType.UNCOMMITTED_CHANGES_AND_LOG,
) { git ->
stashChangesUseCase(git, null)
}
fun popStash() = tabState.safeProcessing(
refreshType = RefreshType.UNCOMMITED_CHANGES_AND_LOG,
refreshType = RefreshType.UNCOMMITTED_CHANGES_AND_LOG,
refreshEvenIfCrashes = true,
) { git ->
popLastStashUseCase(git)

View File

@ -25,14 +25,14 @@ class SharedStashViewModel @Inject constructor(
private val tabState: TabState,
) : ISharedStashViewModel {
override fun applyStash(stashInfo: RevCommit) = tabState.safeProcessing(
refreshType = RefreshType.UNCOMMITED_CHANGES_AND_LOG,
refreshType = RefreshType.UNCOMMITTED_CHANGES_AND_LOG,
refreshEvenIfCrashes = true,
) { git ->
applyStashUseCase(git, stashInfo)
}
override fun popStash(stash: RevCommit) = tabState.safeProcessing(
refreshType = RefreshType.UNCOMMITED_CHANGES_AND_LOG,
refreshType = RefreshType.UNCOMMITTED_CHANGES_AND_LOG,
refreshEvenIfCrashes = true,
) { git ->
popStashUseCase(git, stash)

View File

@ -153,8 +153,8 @@ class StatusViewModel @Inject constructor(
tabScope.launch {
tabState.refreshFlowFiltered(
RefreshType.ALL_DATA,
RefreshType.UNCOMMITED_CHANGES,
RefreshType.UNCOMMITED_CHANGES_AND_LOG,
RefreshType.UNCOMMITTED_CHANGES,
RefreshType.UNCOMMITTED_CHANGES_AND_LOG,
) {
refresh(tabState.git)
}
@ -177,14 +177,14 @@ class StatusViewModel @Inject constructor(
}
fun stage(statusEntry: StatusEntry) = tabState.runOperation(
refreshType = RefreshType.UNCOMMITED_CHANGES,
refreshType = RefreshType.UNCOMMITTED_CHANGES,
showError = true,
) { git ->
stageEntryUseCase(git, statusEntry)
}
fun unstage(statusEntry: StatusEntry) = tabState.runOperation(
refreshType = RefreshType.UNCOMMITED_CHANGES,
refreshType = RefreshType.UNCOMMITTED_CHANGES,
showError = true,
) { git ->
unstageEntryUseCase(git, statusEntry)
@ -192,25 +192,25 @@ class StatusViewModel @Inject constructor(
fun unstageAll() = tabState.safeProcessing(
refreshType = RefreshType.UNCOMMITED_CHANGES,
refreshType = RefreshType.UNCOMMITTED_CHANGES,
) { git ->
unstageAllUseCase(git)
}
fun stageAll() = tabState.safeProcessing(
refreshType = RefreshType.UNCOMMITED_CHANGES,
refreshType = RefreshType.UNCOMMITTED_CHANGES,
) { git ->
stageAllUseCase(git)
}
fun resetStaged(statusEntry: StatusEntry) = tabState.runOperation(
refreshType = RefreshType.UNCOMMITED_CHANGES_AND_LOG,
refreshType = RefreshType.UNCOMMITTED_CHANGES_AND_LOG,
) { git ->
resetEntryUseCase(git, statusEntry, staged = true)
}
fun resetUnstaged(statusEntry: StatusEntry) = tabState.runOperation(
refreshType = RefreshType.UNCOMMITED_CHANGES_AND_LOG,
refreshType = RefreshType.UNCOMMITTED_CHANGES_AND_LOG,
) { git ->
resetEntryUseCase(git, statusEntry, staged = false)
}
@ -406,7 +406,7 @@ class StatusViewModel @Inject constructor(
}
fun deleteFile(statusEntry: StatusEntry) = tabState.runOperation(
refreshType = RefreshType.UNCOMMITED_CHANGES,
refreshType = RefreshType.UNCOMMITTED_CHANGES,
) { git ->
val path = statusEntry.filePath

View File

@ -280,7 +280,7 @@ class TabViewModel @Inject constructor(
refreshType = RefreshType.NONE,
) {
updateDiffEntry()
tabState.refreshData(RefreshType.UNCOMMITED_CHANGES_AND_LOG)
tabState.refreshData(RefreshType.UNCOMMITTED_CHANGES_AND_LOG)
}
private suspend fun refreshRepositoryInfo() {
@ -327,9 +327,7 @@ class TabViewModel @Inject constructor(
return openFilePickerUseCase(PickerType.DIRECTORIES, latestDirectoryOpened)
}
fun initLocalRepository(dir: String) = tabState.safeProcessingWithoutGit(
showError = true,
) {
fun initLocalRepository(dir: String) = tabState.safeProcessingWithoutGit {
val repoDir = File(dir)
initLocalRepositoryUseCase(repoDir)
openRepository(repoDir)
@ -409,7 +407,7 @@ class TabViewModel @Inject constructor(
}
fun stashWithMessage(message: String) = tabState.safeProcessing(
refreshType = RefreshType.UNCOMMITED_CHANGES_AND_LOG,
refreshType = RefreshType.UNCOMMITTED_CHANGES_AND_LOG,
) { git ->
stageUntrackedFileUseCase(git)
stashChangesUseCase(git, message)

View File

@ -9,7 +9,6 @@ import com.jetpackduba.gitnuro.git.rebase.RebaseBranchUseCase
import com.jetpackduba.gitnuro.git.remote_operations.PullFromSpecificBranchUseCase
import com.jetpackduba.gitnuro.git.remote_operations.PushToSpecificBranchUseCase
import com.jetpackduba.gitnuro.preferences.AppSettings
import com.jetpackduba.gitnuro.ui.SelectedItem
import dagger.assisted.Assisted
import dagger.assisted.AssistedInject
import kotlinx.coroutines.CoroutineScope

View File

@ -125,7 +125,6 @@ class RemotesViewModel @AssistedInject constructor(
fun deleteRemote(remoteName: String, isNew: Boolean) = tabState.safeProcessing(
refreshType = if (isNew) RefreshType.REMOTES else RefreshType.ALL_DATA,
showError = true,
) { git ->
deleteRemoteUseCase(git, remoteName)

View File

@ -3,11 +3,7 @@ package com.jetpackduba.gitnuro.viewmodels.sidepanel
import com.jetpackduba.gitnuro.extensions.lowercaseContains
import com.jetpackduba.gitnuro.git.RefreshType
import com.jetpackduba.gitnuro.git.TabState
import com.jetpackduba.gitnuro.git.stash.ApplyStashUseCase
import com.jetpackduba.gitnuro.git.stash.DeleteStashUseCase
import com.jetpackduba.gitnuro.git.stash.GetStashListUseCase
import com.jetpackduba.gitnuro.git.stash.PopStashUseCase
import com.jetpackduba.gitnuro.ui.SelectedItem
import com.jetpackduba.gitnuro.viewmodels.ISharedStashViewModel
import com.jetpackduba.gitnuro.viewmodels.SharedStashViewModel
import dagger.assisted.Assisted
@ -27,6 +23,7 @@ class StashesViewModel @AssistedInject constructor(
private val filter: StateFlow<String>,
sharedStashViewModel: SharedStashViewModel,
) : SidePanelChildViewModel(true), ISharedStashViewModel by sharedStashViewModel {
private val stashes = MutableStateFlow<List<RevCommit>>(emptyList())
val stashesState: StateFlow<StashesState> = combine(stashes, isExpanded, filter) { stashes, isExpanded, filter ->
@ -45,14 +42,14 @@ class StashesViewModel @AssistedInject constructor(
tabState.refreshFlowFiltered(
RefreshType.ALL_DATA,
RefreshType.STASHES,
RefreshType.UNCOMMITED_CHANGES_AND_LOG
RefreshType.UNCOMMITTED_CHANGES_AND_LOG
) {
refresh(tabState.git)
}
}
}
suspend fun loadStashes(git: Git) {
private suspend fun loadStashes(git: Git) {
val stashList = getStashListUseCase(git)
stashes.value = stashList
}

View File

@ -43,7 +43,7 @@ class SubmodulesViewModel @AssistedInject constructor(
init {
tabScope.launch {
tabState.refreshFlowFiltered(RefreshType.ALL_DATA, RefreshType.UNCOMMITED_CHANGES, RefreshType.SUBMODULES) {
tabState.refreshFlowFiltered(RefreshType.ALL_DATA, RefreshType.UNCOMMITTED_CHANGES, RefreshType.SUBMODULES) {
refresh(tabState.git)
}
}
@ -54,7 +54,6 @@ class SubmodulesViewModel @AssistedInject constructor(
}
fun initializeSubmodule(path: String) = tabState.safeProcessing(
showError = true,
refreshType = RefreshType.SUBMODULES,
) { git ->
initializeSubmoduleUseCase(git, path)