diff --git a/src/main/kotlin/com/jetpackduba/gitnuro/repositories/AppSettingsRepository.kt b/src/main/kotlin/com/jetpackduba/gitnuro/repositories/AppSettingsRepository.kt index 773dc24..9f619a4 100644 --- a/src/main/kotlin/com/jetpackduba/gitnuro/repositories/AppSettingsRepository.kt +++ b/src/main/kotlin/com/jetpackduba/gitnuro/repositories/AppSettingsRepository.kt @@ -131,10 +131,10 @@ class AppSettingsRepository @Inject constructor() { preferences.put(PREF_LATEST_REPOSITORIES_TABS_OPENED, value) } - var latestRepositoryTabSelected: String - get() = preferences.get(PREF_LATEST_REPOSITORY_TAB_SELECTED, "") + var latestRepositoryTabSelected: Int + get() = preferences.getInt(PREF_LATEST_REPOSITORY_TAB_SELECTED, -1) set(value) { - preferences.put(PREF_LATEST_REPOSITORY_TAB_SELECTED, value) + preferences.putInt(PREF_LATEST_REPOSITORY_TAB_SELECTED, value) } var latestOpenedRepositoriesPath: String diff --git a/src/main/kotlin/com/jetpackduba/gitnuro/ui/TabsManager.kt b/src/main/kotlin/com/jetpackduba/gitnuro/ui/TabsManager.kt index bf475c2..d9615bc 100644 --- a/src/main/kotlin/com/jetpackduba/gitnuro/ui/TabsManager.kt +++ b/src/main/kotlin/com/jetpackduba/gitnuro/ui/TabsManager.kt @@ -40,11 +40,12 @@ class TabsManager @Inject constructor( _tabsFlow.value = repositoriesList.ifEmpty { listOf(newAppTab()) } - val latestSelectedTabPath = appSettingsRepository.latestRepositoryTabSelected + val latestSelectedTabIndex = appSettingsRepository.latestRepositoryTabSelected - val latestSelectedTab = repositoriesList.firstOrNull { it.path == latestSelectedTabPath } - - _currentTab.value = latestSelectedTab ?: _tabsFlow.value.first() + _currentTab.value = when(latestSelectedTabIndex < 0) { + true -> _tabsFlow.value.first() + false -> tabsFlow.value[latestSelectedTabIndex] + } } fun addNewTabFromPath(path: String, selectTab: Boolean, tabToBeReplacedPath: String? = null) { @@ -79,7 +80,7 @@ class TabsManager @Inject constructor( } private fun persistTabSelected(tab: TabInformation) { - appSettingsRepository.latestRepositoryTabSelected = tab.path.orEmpty() + appSettingsRepository.latestRepositoryTabSelected = tabsFlow.value.indexOf(tab) } fun closeTab(tab: TabInformation) { @@ -116,10 +117,10 @@ class TabsManager @Inject constructor( } private fun updatePersistedTabs() { - val tabsPaths = tabsFlow.value - .mapNotNull { it.path } + val tabs = tabsFlow.value.filter { it.path != null } - appSettingsRepository.latestTabsOpened = Json.encodeToString(tabsPaths) + appSettingsRepository.latestTabsOpened = Json.encodeToString(tabs.map { it.path }) + appSettingsRepository.latestRepositoryTabSelected = tabs.indexOf(currentTab.value) } fun addNewEmptyTab() {