From d4b2a1c57fcd279c13ba164ddb5451860db97b31 Mon Sep 17 00:00:00 2001 From: Abdelilah El Aissaoui Date: Sat, 1 Jan 2022 18:32:35 +0100 Subject: [PATCH] Refactored tabs management --- src/main/kotlin/app/App.kt | 40 ++++++++++++++----- .../app/ui/components/RepositoriesTabPanel.kt | 24 ++++------- 2 files changed, 37 insertions(+), 27 deletions(-) diff --git a/src/main/kotlin/app/App.kt b/src/main/kotlin/app/App.kt index 184859f..eb8fd4a 100644 --- a/src/main/kotlin/app/App.kt +++ b/src/main/kotlin/app/App.kt @@ -10,6 +10,7 @@ import androidx.compose.material.Icon import androidx.compose.material.IconButton import androidx.compose.material.MaterialTheme import androidx.compose.runtime.* +import androidx.compose.runtime.snapshots.SnapshotStateMap import androidx.compose.ui.Alignment import androidx.compose.ui.ExperimentalComposeUiApi import androidx.compose.ui.Modifier @@ -66,10 +67,12 @@ class Main { icon = painterResource("logo.svg"), ) { var showSettingsDialog by remember { mutableStateOf(false) } + val tabs = mutableStateMapOf() AppTheme(theme = theme) { Box(modifier = Modifier.background(MaterialTheme.colors.background)) { AppTabs( + tabs = tabs, onOpenSettings = { showSettingsDialog = true } @@ -90,9 +93,15 @@ class Main { @Composable fun AppTabs( + tabs: SnapshotStateMap, onOpenSettings: () -> Unit, ) { - val tabs = remember { + + val tabsInformationList = tabs.map { it.value }.sortedBy { it.key } + + println("Tabs count ${tabs.count()}") + + LaunchedEffect(Unit) { val repositoriesSavedTabs = appStateManager.openRepositoriesPathsTabs var repoTabs = repositoriesSavedTabs.map { repositoryTab -> newAppTab( @@ -107,29 +116,37 @@ class Main { ) } - mutableStateOf(repoTabs) + repoTabs.forEach { + tabs[it.key] = it + } // Store list of tabs in the map + + println("After reading prefs, got ${tabs.count()} tabs") } val selectedTabKey = remember { mutableStateOf(0) } + println("Selected tab key: ${selectedTabKey.value}") + Column( modifier = Modifier.background(MaterialTheme.colors.background) ) { Tabs( tabs = tabs, + tabsInformationList = tabsInformationList, selectedTabKey = selectedTabKey, onOpenSettings = onOpenSettings ) - TabsContent(tabs.value, selectedTabKey.value) + TabsContent(tabsInformationList, selectedTabKey.value) } } @Composable fun Tabs( - tabs: MutableState>, + tabs: SnapshotStateMap, selectedTabKey: MutableState, onOpenSettings: () -> Unit, + tabsInformationList: List, ) { Row( modifier = Modifier @@ -140,21 +157,24 @@ class Main { RepositoriesTabPanel( modifier = Modifier .weight(1f), - tabs = tabs.value, + tabs = tabsInformationList, selectedTabKey = selectedTabKey.value, onTabSelected = { newSelectedTabKey -> + println("New selected tab key $newSelectedTabKey") selectedTabKey.value = newSelectedTabKey }, newTabContent = { key -> - newAppTab( + val newAppTab = newAppTab( key = key ) - }, - onTabsUpdated = { tabInformationList -> - tabs.value = tabInformationList + + tabs[key] = newAppTab + + newAppTab }, onTabClosed = { key -> appStateManager.repositoryTabRemoved(key) + tabs.remove(key) } ) IconButton( @@ -226,4 +246,4 @@ private fun TabsContent(tabs: List, selectedTabKey: Int) { @Composable fun LoadingRepository() { Box { } -} +} \ No newline at end of file diff --git a/src/main/kotlin/app/ui/components/RepositoriesTabPanel.kt b/src/main/kotlin/app/ui/components/RepositoriesTabPanel.kt index 67817da..7bae14a 100644 --- a/src/main/kotlin/app/ui/components/RepositoriesTabPanel.kt +++ b/src/main/kotlin/app/ui/components/RepositoriesTabPanel.kt @@ -30,7 +30,6 @@ fun RepositoriesTabPanel( tabs: List, selectedTabKey: Int, onTabSelected: (Int) -> Unit, - onTabsUpdated: (List) -> Unit, onTabClosed: (Int) -> Unit, newTabContent: (key: Int) -> TabInformation, ) { @@ -41,14 +40,10 @@ fun RepositoriesTabPanel( TabPanel( modifier = modifier, onNewTabClicked = { - val tabsCopy = tabs.toMutableList() - tabsIdentifier++ - tabsCopy.add(newTabContent(tabsIdentifier)) - + newTabContent(tabsIdentifier) onTabSelected(tabsIdentifier) - onTabsUpdated(tabsCopy) } ) { items(items = tabs) { tab -> @@ -62,17 +57,16 @@ fun RepositoriesTabPanel( val isTabSelected = selectedTabKey == tab.key val index = tabs.indexOf(tab) val nextIndex = if (index == 0 && tabs.count() >= 2) { - 0 - } else if (tabs.count() >= 2) + 1 + } else if (index == tabs.count() -1 && tabs.count() >= 2) index - 1 + else if (tabs.count() >= 2) + index else -1 - val tabsCopy = tabs.toMutableList() - tabsCopy.remove(tab) - val nextKey = if (nextIndex >= 0) - tabsCopy[nextIndex].key + tabs[nextIndex].key else -1 @@ -82,16 +76,12 @@ fun RepositoriesTabPanel( } else { tabsIdentifier++ - tabsCopy.add( - newTabContent(tabsIdentifier) - ) - onTabSelected(tabsIdentifier) + newTabContent(tabsIdentifier) } } onTabClosed(tab.key) - onTabsUpdated(tabsCopy) } ) }