Skip to content

Commit

Permalink
Recent repositories now are unlimited, can be removed and searched
Browse files Browse the repository at this point in the history
  • Loading branch information
JetpackDuba committed Jun 16, 2024
1 parent ef41e9a commit 127f215
Show file tree
Hide file tree
Showing 8 changed files with 281 additions and 178 deletions.
40 changes: 29 additions & 11 deletions src/main/kotlin/com/jetpackduba/gitnuro/managers/AppStateManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import com.jetpackduba.gitnuro.repositories.AppSettingsRepository
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.cancel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch
import kotlinx.coroutines.sync.Mutex
import kotlinx.serialization.decodeFromString
Expand All @@ -20,26 +22,25 @@ class AppStateManager @Inject constructor(
) {
private val mutex = Mutex()

private val _latestOpenedRepositoriesPaths = mutableListOf<String>()
val latestOpenedRepositoriesPaths: List<String>
get() = _latestOpenedRepositoriesPaths
private val _latestOpenedRepositoriesPaths = MutableStateFlow<List<String>>(emptyList())
val latestOpenedRepositoriesPaths = _latestOpenedRepositoriesPaths.asStateFlow()

val latestOpenedRepositoryPath: String
get() = _latestOpenedRepositoriesPaths.firstOrNull() ?: ""
get() = _latestOpenedRepositoriesPaths.value.firstOrNull() ?: ""

fun repositoryTabChanged(path: String) = appScope.launch(Dispatchers.IO) {
mutex.lock()
try {
val repoPaths = _latestOpenedRepositoriesPaths.value.toMutableList()

// Remove any previously existing path
_latestOpenedRepositoriesPaths.removeIf { it == path }
repoPaths.removeIf { it == path }

// Add the latest one to the beginning
_latestOpenedRepositoriesPaths.add(0, path)

if (_latestOpenedRepositoriesPaths.count() > 10)
_latestOpenedRepositoriesPaths.removeLast()
repoPaths.add(0, path)

appSettingsRepository.latestOpenedRepositoriesPath = Json.encodeToString(_latestOpenedRepositoriesPaths)
appSettingsRepository.latestOpenedRepositoriesPath = Json.encodeToString(repoPaths)
_latestOpenedRepositoriesPaths.value = repoPaths
} finally {
mutex.unlock()
}
Expand All @@ -49,11 +50,28 @@ class AppStateManager @Inject constructor(
val repositoriesPathsSaved = appSettingsRepository.latestOpenedRepositoriesPath
if (repositoriesPathsSaved.isNotEmpty()) {
val repositories = Json.decodeFromString<List<String>>(repositoriesPathsSaved)
_latestOpenedRepositoriesPaths.addAll(repositories)
val repoPaths = _latestOpenedRepositoriesPaths.value.toMutableList()

repoPaths.addAll(repositories)

_latestOpenedRepositoriesPaths.value = repoPaths
}
}

fun cancelCoroutines() {
appScope.cancel("Closing app")
}

fun removeRepositoryFromRecent(path: String) = appScope.launch {
mutex.lock()
try {
val repoPaths = _latestOpenedRepositoriesPaths.value.toMutableList()
repoPaths.removeIf { it == path }

appSettingsRepository.latestOpenedRepositoriesPath = Json.encodeToString(repoPaths)
_latestOpenedRepositoriesPaths.value = repoPaths
} finally {
mutex.unlock()
}
}
}
26 changes: 20 additions & 6 deletions src/main/kotlin/com/jetpackduba/gitnuro/ui/RepositoryOpen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ import com.jetpackduba.gitnuro.git.DiffEntryType
import com.jetpackduba.gitnuro.git.rebase.RebaseInteractiveState
import com.jetpackduba.gitnuro.keybindings.KeybindingOption
import com.jetpackduba.gitnuro.keybindings.matchesBinding
import com.jetpackduba.gitnuro.models.AuthorInfoSimple
import com.jetpackduba.gitnuro.ui.components.SecondaryButton
import com.jetpackduba.gitnuro.ui.components.TripleVerticalSplitPanel
import com.jetpackduba.gitnuro.ui.dialogs.*
import com.jetpackduba.gitnuro.ui.diff.Diff
import com.jetpackduba.gitnuro.ui.log.Log
import com.jetpackduba.gitnuro.updates.Update
import com.jetpackduba.gitnuro.viewmodels.BlameState
import com.jetpackduba.gitnuro.viewmodels.TabViewModel
import org.eclipse.jgit.lib.RepositoryState
Expand Down Expand Up @@ -160,14 +162,26 @@ fun RepositoryOpenPage(
.background(MaterialTheme.colors.primaryVariant.copy(alpha = 0.2f))
)

BottomInfoBar(tabViewModel)

val userInfo by tabViewModel.authorInfoSimple.collectAsState()
val newUpdate = tabViewModel.update.collectAsState().value

BottomInfoBar(
userInfo,
newUpdate,
onOpenUrlInBrowser = { tabViewModel.openUrlInBrowser(it) },
onShowAuthorInfoDialog = { tabViewModel.showAuthorInfoDialog() },
)
}
}

@Composable
private fun BottomInfoBar(tabViewModel: TabViewModel) {
val userInfo by tabViewModel.authorInfoSimple.collectAsState()
val newUpdate = tabViewModel.hasUpdates.collectAsState().value
private fun BottomInfoBar(
userInfo: AuthorInfoSimple,
newUpdate: Update?,
onOpenUrlInBrowser: (String) -> Unit,
onShowAuthorInfoDialog: () -> Unit,
) {

Row(
modifier = Modifier
Expand All @@ -180,7 +194,7 @@ private fun BottomInfoBar(tabViewModel: TabViewModel) {
Box(
modifier = Modifier
.fillMaxHeight()
.handMouseClickable { tabViewModel.showAuthorInfoDialog() },
.handMouseClickable { onShowAuthorInfoDialog() },
contentAlignment = Alignment.Center,
) {
Text(
Expand All @@ -194,7 +208,7 @@ private fun BottomInfoBar(tabViewModel: TabViewModel) {
if (newUpdate != null) {
SecondaryButton(
text = "Update ${newUpdate.appVersion} available",
onClick = { tabViewModel.openUrlInBrowser(newUpdate.downloadUrl) },
onClick = { onOpenUrlInBrowser(newUpdate.downloadUrl) },
backgroundButton = MaterialTheme.colors.primary,
modifier = Modifier.padding(end = 16.dp)
)
Expand Down
Loading

0 comments on commit 127f215

Please sign in to comment.