Completed tabs refactor

This commit is contained in:
Abdelilah El Aissaoui 2023-04-06 01:18:42 +02:00
parent a55dd755d7
commit f54dfbd67b
No known key found for this signature in database
GPG Key ID: 7587FC860F594869
5 changed files with 30 additions and 39 deletions

View File

@ -17,10 +17,6 @@ class AppStateManager @Inject constructor(
) {
private val mutex = Mutex()
private val _openRepositoriesPaths = mutableMapOf<Int, String>()
val openRepositoriesPathsTabs: Map<Int, String>
get() = _openRepositoriesPaths
private val _latestOpenedRepositoriesPaths = mutableListOf<String>()
val latestOpenedRepositoriesPaths: List<String>
get() = _latestOpenedRepositoriesPaths
@ -28,13 +24,9 @@ class AppStateManager @Inject constructor(
val latestOpenedRepositoryPath: String
get() = _latestOpenedRepositoriesPaths.firstOrNull() ?: ""
fun repositoryTabChanged(key: Int, path: String) = appScope.launch(Dispatchers.IO) {
fun repositoryTabChanged(path: String) = appScope.launch(Dispatchers.IO) {
mutex.lock()
try {
// Do not save already saved repos
if (!_openRepositoriesPaths.containsValue(path))
_openRepositoriesPaths[key] = path
// Remove any previously existing path
_latestOpenedRepositoriesPaths.removeIf { it == path }
@ -44,22 +36,12 @@ class AppStateManager @Inject constructor(
if (_latestOpenedRepositoriesPaths.count() > 10)
_latestOpenedRepositoriesPaths.removeLast()
updateSavedRepositoryTabs()
updateLatestRepositoryTabs()
appSettings.latestOpenedRepositoriesPath = Json.encodeToString(_latestOpenedRepositoriesPaths)
} finally {
mutex.unlock()
}
}
private suspend fun updateSavedRepositoryTabs() = withContext(Dispatchers.IO) {
val tabsList = _openRepositoriesPaths.map { it.value }
appSettings.latestTabsOpened = Json.encodeToString(tabsList)
}
private suspend fun updateLatestRepositoryTabs() = withContext(Dispatchers.IO) {
appSettings.latestOpenedRepositoriesPath = Json.encodeToString(_latestOpenedRepositoriesPaths)
}
fun loadRepositoriesTabs() {
val repositoriesPathsSaved = appSettings.latestOpenedRepositoriesPath
if (repositoriesPathsSaved.isNotEmpty()) {
@ -69,6 +51,6 @@ class AppStateManager @Inject constructor(
}
fun cancelCoroutines() {
appScope.cancel("Closing com.jetpackduba.gitnuro.app")
appScope.cancel("Closing app")
}
}

View File

@ -57,7 +57,7 @@ class TabState @Inject constructor(
@set:Synchronized
var operationRunning = false
var currentJob: Job? = null
private var currentJob: Job? = null
private val _processing = MutableStateFlow<ProcessingState>(ProcessingState.None)
val processing: StateFlow<ProcessingState> = _processing

View File

@ -72,6 +72,7 @@ class TabsManager @Inject constructor(
if (currentTab.value == tab) {
val index = tabsList.indexOf(tab)
if (tabsList.count() == 1) {
newCurrentTab = newAppTab()
} else if (index > 0) {
@ -81,7 +82,7 @@ class TabsManager @Inject constructor(
}
}
tab.tabViewModel.dispose()
tab.dispose()
tabsList.remove(tab)
if (newCurrentTab != null) {
@ -99,8 +100,10 @@ class TabsManager @Inject constructor(
}
private fun updatePersistedTabs() {
val tabs = tabsFlow.value
appSettings.latestTabsOpened = Json.encodeToString(tabs)
val tabsPaths = tabsFlow.value
.mapNotNull { it.path }
appSettings.latestTabsOpened = Json.encodeToString(tabsPaths)
}
fun newTab() {
@ -121,7 +124,8 @@ class TabsManager @Inject constructor(
): TabInformation {
return TabInformation(
tabName = tabName,
path = path,
initialPath = path,
onTabPathChanged = { updatePersistedTabs() },
appComponent = appComponent,
)
}

View File

@ -197,7 +197,8 @@ fun Tab(title: MutableState<String>, isSelected: Boolean, onClick: () -> Unit, o
class TabInformation(
val tabName: MutableState<String>,
val path: String?,
val initialPath: String?,
val onTabPathChanged: () -> Unit,
appComponent: AppComponent?,
) {
private val tabComponent: TabComponent = DaggerTabComponent.builder()
@ -213,23 +214,29 @@ class TabInformation(
@Inject
lateinit var tabViewModelsHolder: TabViewModelsHolder
var path = initialPath
private set
init {
tabComponent.inject(this)
tabViewModel.onRepositoryChanged = { path ->
if (path == null) {
// appStateManager.repositoryTabRemoved(key)
} else {
tabName.value = Path(path).name
// appStateManager.repositoryTabChanged(path)
}
this.path = path
tabName.value = Path(path).name
appStateManager.repositoryTabChanged(path)
onTabPathChanged()
}
if (path != null)
tabViewModel.openRepository(path)
if (initialPath != null)
tabViewModel.openRepository(initialPath)
}
fun dispose() {
tabViewModel.dispose()
}
}
fun emptyTabInformation() = TabInformation(mutableStateOf(""), "", null)
fun emptyTabInformation() = TabInformation(mutableStateOf(""), "", {}, null)
@Composable
inline fun <reified T> gitnuroViewModel(): T {

View File

@ -193,7 +193,6 @@ class TabViewModel @Inject constructor(
watchRepositoryChanges(git)
} catch (ex: Exception) {
ex.printStackTrace()
onRepositoryChanged(null)
errorsManager.addError(newErrorNow(ex, ex.localizedMessage))
_repositorySelectionStatus.value = RepositorySelectionStatus.None
}
@ -325,8 +324,7 @@ class TabViewModel @Inject constructor(
credentialsStateManager.updateState(CredentialsAccepted.SshCredentialsAccepted(password))
}
var onRepositoryChanged: (path: String?) -> Unit = {}
var onRepositoryChanged: (path: String) -> Unit = {}
fun dispose() {
tabScope.cancel()