mirror of
https://github.com/openflocon/Flocon.git
synced 2026-05-19 10:14:55 +00:00
refact: [DATABASE] allow multiple times the same tab open (#326)
Co-authored-by: Florent Champigny <florent@bere.al>
This commit is contained in:
parent
0b60acfde1
commit
46160e4457
2 changed files with 21 additions and 9 deletions
|
|
@ -82,11 +82,13 @@ class DatabaseViewModel(
|
|||
|
||||
fun onTableDoubleClicked(databaseId: DeviceDataBaseId, table: TableUiModel) {
|
||||
viewModelScope.launch(dispatcherProvider.viewModel) {
|
||||
val generatedName = table.name
|
||||
createTab(
|
||||
DatabaseTabState(
|
||||
databaseId = databaseId,
|
||||
tableName = table.name,
|
||||
displayName = table.name,
|
||||
displayName = generatedName,
|
||||
generatedName = generatedName,
|
||||
)
|
||||
)
|
||||
}
|
||||
|
|
@ -109,11 +111,13 @@ class DatabaseViewModel(
|
|||
|
||||
private fun createTabForDatabase(database: DeviceDataBaseUiModel) {
|
||||
viewModelScope.launch(dispatcherProvider.viewModel) {
|
||||
val generatedName = "${database.name}.db"
|
||||
createTab(
|
||||
DatabaseTabState(
|
||||
databaseId = database.id,
|
||||
tableName = null,
|
||||
displayName = "${database.name}.db"
|
||||
displayName = generatedName,
|
||||
generatedName = generatedName,
|
||||
)
|
||||
)
|
||||
}
|
||||
|
|
@ -148,16 +152,23 @@ class DatabaseViewModel(
|
|||
|
||||
private suspend fun createTab(tab: DatabaseTabState) {
|
||||
val deviceIdAndPackageName = getCurrentDeviceIdAndPackageNameUseCase() ?: return
|
||||
_selectedTab.update { it + (deviceIdAndPackageName to tab) }
|
||||
lateinit var addedTab : DatabaseTabState
|
||||
_tabs.update {
|
||||
val list = it[deviceIdAndPackageName] ?: emptyList()
|
||||
if (list.contains(tab)) it // nothing
|
||||
else {
|
||||
val newList = list.toMutableList().apply {
|
||||
add(tab)
|
||||
}
|
||||
it + (deviceIdAndPackageName to newList)
|
||||
// if we have already a tab with the same "generatedName", it creates a new one with (number) after
|
||||
val withSameName = list.count { it.generatedName == tab.generatedName }
|
||||
|
||||
addedTab = if (withSameName != 0) {
|
||||
tab.copy(displayName = "${tab.displayName} (${withSameName})")
|
||||
} else {
|
||||
tab
|
||||
}
|
||||
|
||||
val newList = list.toMutableList().apply {
|
||||
add(addedTab)
|
||||
}
|
||||
it + (deviceIdAndPackageName to newList)
|
||||
}
|
||||
_selectedTab.update { it + (deviceIdAndPackageName to addedTab) }
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,5 +6,6 @@ import androidx.compose.runtime.Immutable
|
|||
data class DatabaseTabState(
|
||||
val databaseId: String,
|
||||
val tableName: String?,
|
||||
val generatedName: String,
|
||||
val displayName: String,
|
||||
)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue