Refactored tabs management
This commit is contained in:
parent
6ddb77bf60
commit
d4b2a1c57f
@ -10,6 +10,7 @@ import androidx.compose.material.Icon
|
|||||||
import androidx.compose.material.IconButton
|
import androidx.compose.material.IconButton
|
||||||
import androidx.compose.material.MaterialTheme
|
import androidx.compose.material.MaterialTheme
|
||||||
import androidx.compose.runtime.*
|
import androidx.compose.runtime.*
|
||||||
|
import androidx.compose.runtime.snapshots.SnapshotStateMap
|
||||||
import androidx.compose.ui.Alignment
|
import androidx.compose.ui.Alignment
|
||||||
import androidx.compose.ui.ExperimentalComposeUiApi
|
import androidx.compose.ui.ExperimentalComposeUiApi
|
||||||
import androidx.compose.ui.Modifier
|
import androidx.compose.ui.Modifier
|
||||||
@ -66,10 +67,12 @@ class Main {
|
|||||||
icon = painterResource("logo.svg"),
|
icon = painterResource("logo.svg"),
|
||||||
) {
|
) {
|
||||||
var showSettingsDialog by remember { mutableStateOf(false) }
|
var showSettingsDialog by remember { mutableStateOf(false) }
|
||||||
|
val tabs = mutableStateMapOf<Int, TabInformation>()
|
||||||
|
|
||||||
AppTheme(theme = theme) {
|
AppTheme(theme = theme) {
|
||||||
Box(modifier = Modifier.background(MaterialTheme.colors.background)) {
|
Box(modifier = Modifier.background(MaterialTheme.colors.background)) {
|
||||||
AppTabs(
|
AppTabs(
|
||||||
|
tabs = tabs,
|
||||||
onOpenSettings = {
|
onOpenSettings = {
|
||||||
showSettingsDialog = true
|
showSettingsDialog = true
|
||||||
}
|
}
|
||||||
@ -90,9 +93,15 @@ class Main {
|
|||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
fun AppTabs(
|
fun AppTabs(
|
||||||
|
tabs: SnapshotStateMap<Int, TabInformation>,
|
||||||
onOpenSettings: () -> Unit,
|
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
|
val repositoriesSavedTabs = appStateManager.openRepositoriesPathsTabs
|
||||||
var repoTabs = repositoriesSavedTabs.map { repositoryTab ->
|
var repoTabs = repositoriesSavedTabs.map { repositoryTab ->
|
||||||
newAppTab(
|
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) }
|
val selectedTabKey = remember { mutableStateOf(0) }
|
||||||
|
|
||||||
|
println("Selected tab key: ${selectedTabKey.value}")
|
||||||
|
|
||||||
Column(
|
Column(
|
||||||
modifier = Modifier.background(MaterialTheme.colors.background)
|
modifier = Modifier.background(MaterialTheme.colors.background)
|
||||||
) {
|
) {
|
||||||
Tabs(
|
Tabs(
|
||||||
tabs = tabs,
|
tabs = tabs,
|
||||||
|
tabsInformationList = tabsInformationList,
|
||||||
selectedTabKey = selectedTabKey,
|
selectedTabKey = selectedTabKey,
|
||||||
onOpenSettings = onOpenSettings
|
onOpenSettings = onOpenSettings
|
||||||
)
|
)
|
||||||
|
|
||||||
TabsContent(tabs.value, selectedTabKey.value)
|
TabsContent(tabsInformationList, selectedTabKey.value)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
fun Tabs(
|
fun Tabs(
|
||||||
tabs: MutableState<List<TabInformation>>,
|
tabs: SnapshotStateMap<Int, TabInformation>,
|
||||||
selectedTabKey: MutableState<Int>,
|
selectedTabKey: MutableState<Int>,
|
||||||
onOpenSettings: () -> Unit,
|
onOpenSettings: () -> Unit,
|
||||||
|
tabsInformationList: List<TabInformation>,
|
||||||
) {
|
) {
|
||||||
Row(
|
Row(
|
||||||
modifier = Modifier
|
modifier = Modifier
|
||||||
@ -140,21 +157,24 @@ class Main {
|
|||||||
RepositoriesTabPanel(
|
RepositoriesTabPanel(
|
||||||
modifier = Modifier
|
modifier = Modifier
|
||||||
.weight(1f),
|
.weight(1f),
|
||||||
tabs = tabs.value,
|
tabs = tabsInformationList,
|
||||||
selectedTabKey = selectedTabKey.value,
|
selectedTabKey = selectedTabKey.value,
|
||||||
onTabSelected = { newSelectedTabKey ->
|
onTabSelected = { newSelectedTabKey ->
|
||||||
|
println("New selected tab key $newSelectedTabKey")
|
||||||
selectedTabKey.value = newSelectedTabKey
|
selectedTabKey.value = newSelectedTabKey
|
||||||
},
|
},
|
||||||
newTabContent = { key ->
|
newTabContent = { key ->
|
||||||
newAppTab(
|
val newAppTab = newAppTab(
|
||||||
key = key
|
key = key
|
||||||
)
|
)
|
||||||
},
|
|
||||||
onTabsUpdated = { tabInformationList ->
|
tabs[key] = newAppTab
|
||||||
tabs.value = tabInformationList
|
|
||||||
|
newAppTab
|
||||||
},
|
},
|
||||||
onTabClosed = { key ->
|
onTabClosed = { key ->
|
||||||
appStateManager.repositoryTabRemoved(key)
|
appStateManager.repositoryTabRemoved(key)
|
||||||
|
tabs.remove(key)
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
IconButton(
|
IconButton(
|
||||||
|
@ -30,7 +30,6 @@ fun RepositoriesTabPanel(
|
|||||||
tabs: List<TabInformation>,
|
tabs: List<TabInformation>,
|
||||||
selectedTabKey: Int,
|
selectedTabKey: Int,
|
||||||
onTabSelected: (Int) -> Unit,
|
onTabSelected: (Int) -> Unit,
|
||||||
onTabsUpdated: (List<TabInformation>) -> Unit,
|
|
||||||
onTabClosed: (Int) -> Unit,
|
onTabClosed: (Int) -> Unit,
|
||||||
newTabContent: (key: Int) -> TabInformation,
|
newTabContent: (key: Int) -> TabInformation,
|
||||||
) {
|
) {
|
||||||
@ -41,14 +40,10 @@ fun RepositoriesTabPanel(
|
|||||||
TabPanel(
|
TabPanel(
|
||||||
modifier = modifier,
|
modifier = modifier,
|
||||||
onNewTabClicked = {
|
onNewTabClicked = {
|
||||||
val tabsCopy = tabs.toMutableList()
|
|
||||||
|
|
||||||
tabsIdentifier++
|
tabsIdentifier++
|
||||||
|
|
||||||
tabsCopy.add(newTabContent(tabsIdentifier))
|
newTabContent(tabsIdentifier)
|
||||||
|
|
||||||
onTabSelected(tabsIdentifier)
|
onTabSelected(tabsIdentifier)
|
||||||
onTabsUpdated(tabsCopy)
|
|
||||||
}
|
}
|
||||||
) {
|
) {
|
||||||
items(items = tabs) { tab ->
|
items(items = tabs) { tab ->
|
||||||
@ -62,17 +57,16 @@ fun RepositoriesTabPanel(
|
|||||||
val isTabSelected = selectedTabKey == tab.key
|
val isTabSelected = selectedTabKey == tab.key
|
||||||
val index = tabs.indexOf(tab)
|
val index = tabs.indexOf(tab)
|
||||||
val nextIndex = if (index == 0 && tabs.count() >= 2) {
|
val nextIndex = if (index == 0 && tabs.count() >= 2) {
|
||||||
0
|
1
|
||||||
} else if (tabs.count() >= 2)
|
} else if (index == tabs.count() -1 && tabs.count() >= 2)
|
||||||
index - 1
|
index - 1
|
||||||
|
else if (tabs.count() >= 2)
|
||||||
|
index
|
||||||
else
|
else
|
||||||
-1
|
-1
|
||||||
|
|
||||||
val tabsCopy = tabs.toMutableList()
|
|
||||||
tabsCopy.remove(tab)
|
|
||||||
|
|
||||||
val nextKey = if (nextIndex >= 0)
|
val nextKey = if (nextIndex >= 0)
|
||||||
tabsCopy[nextIndex].key
|
tabs[nextIndex].key
|
||||||
else
|
else
|
||||||
-1
|
-1
|
||||||
|
|
||||||
@ -82,16 +76,12 @@ fun RepositoriesTabPanel(
|
|||||||
} else {
|
} else {
|
||||||
tabsIdentifier++
|
tabsIdentifier++
|
||||||
|
|
||||||
tabsCopy.add(
|
|
||||||
newTabContent(tabsIdentifier)
|
|
||||||
)
|
|
||||||
|
|
||||||
onTabSelected(tabsIdentifier)
|
onTabSelected(tabsIdentifier)
|
||||||
|
newTabContent(tabsIdentifier)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
onTabClosed(tab.key)
|
onTabClosed(tab.key)
|
||||||
onTabsUpdated(tabsCopy)
|
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user