diff --git a/src/main/kotlin/com/jetpackduba/gitnuro/AppStateManager.kt b/src/main/kotlin/com/jetpackduba/gitnuro/AppStateManager.kt index f15abc9..f91583f 100644 --- a/src/main/kotlin/com/jetpackduba/gitnuro/AppStateManager.kt +++ b/src/main/kotlin/com/jetpackduba/gitnuro/AppStateManager.kt @@ -17,10 +17,6 @@ class AppStateManager @Inject constructor( ) { private val mutex = Mutex() - private val _openRepositoriesPaths = mutableMapOf() - val openRepositoriesPathsTabs: Map - get() = _openRepositoriesPaths - private val _latestOpenedRepositoriesPaths = mutableListOf() val latestOpenedRepositoriesPaths: List 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") } } \ No newline at end of file diff --git a/src/main/kotlin/com/jetpackduba/gitnuro/git/TabState.kt b/src/main/kotlin/com/jetpackduba/gitnuro/git/TabState.kt index 11741ca..1d6082b 100644 --- a/src/main/kotlin/com/jetpackduba/gitnuro/git/TabState.kt +++ b/src/main/kotlin/com/jetpackduba/gitnuro/git/TabState.kt @@ -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.None) val processing: StateFlow = _processing diff --git a/src/main/kotlin/com/jetpackduba/gitnuro/ui/TabsManager.kt b/src/main/kotlin/com/jetpackduba/gitnuro/ui/TabsManager.kt index a934711..f787fc6 100644 --- a/src/main/kotlin/com/jetpackduba/gitnuro/ui/TabsManager.kt +++ b/src/main/kotlin/com/jetpackduba/gitnuro/ui/TabsManager.kt @@ -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, ) } diff --git a/src/main/kotlin/com/jetpackduba/gitnuro/ui/components/RepositoriesTabPanel.kt b/src/main/kotlin/com/jetpackduba/gitnuro/ui/components/RepositoriesTabPanel.kt index 5bbe928..802086e 100644 --- a/src/main/kotlin/com/jetpackduba/gitnuro/ui/components/RepositoriesTabPanel.kt +++ b/src/main/kotlin/com/jetpackduba/gitnuro/ui/components/RepositoriesTabPanel.kt @@ -197,7 +197,8 @@ fun Tab(title: MutableState, isSelected: Boolean, onClick: () -> Unit, o class TabInformation( val tabName: MutableState, - 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 gitnuroViewModel(): T { diff --git a/src/main/kotlin/com/jetpackduba/gitnuro/viewmodels/TabViewModel.kt b/src/main/kotlin/com/jetpackduba/gitnuro/viewmodels/TabViewModel.kt index e95815a..2942a27 100644 --- a/src/main/kotlin/com/jetpackduba/gitnuro/viewmodels/TabViewModel.kt +++ b/src/main/kotlin/com/jetpackduba/gitnuro/viewmodels/TabViewModel.kt @@ -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()