Fixed adding a new tab crashing

This commit is contained in:
Abdelilah El Aissaoui 2022-01-04 17:53:31 +01:00
parent 620fd1022b
commit e7de563b28
5 changed files with 92 additions and 70 deletions

View File

@ -10,7 +10,6 @@ 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
@ -28,9 +27,11 @@ import app.theme.AppTheme
import app.ui.components.RepositoriesTabPanel
import app.ui.components.TabInformation
import app.ui.dialogs.SettingsDialog
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.MutableStateFlow
import javax.inject.Inject
class Main {
class App {
private val appComponent = DaggerAppComponent.create()
@Inject
@ -39,84 +40,91 @@ class Main {
@Inject
lateinit var appPreferences: AppPreferences
private val appScope = CoroutineScope(SupervisorJob() + Dispatchers.IO)
init {
appComponent.inject(this)
println("AppStateManagerReference $appStateManager")
appStateManager.loadRepositoriesTabs()
}
fun start() = application {
var isOpen by remember { mutableStateOf(true) }
val theme by appPreferences.themeState.collectAsState()
if (isOpen) {
Window(
title = "Gitnuro",
onCloseRequest = {
isOpen = false
},
state = rememberWindowState(
placement = WindowPlacement.Maximized,
size = DpSize(1280.dp, 720.dp)
),
icon = painterResource("logo.svg"),
) {
var showSettingsDialog by remember { mutableStateOf(false) }
val tabs = mutableStateMapOf<Int, TabInformation>()
private val tabsFlow = MutableStateFlow<List<TabInformation>>(emptyList())
AppTheme(theme = theme) {
Box(modifier = Modifier.background(MaterialTheme.colors.background)) {
AppTabs(
tabs = tabs,
onOpenSettings = {
showSettingsDialog = true
}
)
}
fun start(){
appStateManager.loadRepositoriesTabs()
loadTabs()
if (showSettingsDialog) {
SettingsDialog(
appPreferences = appPreferences,
onDismiss = { showSettingsDialog = false }
)
application {
var isOpen by remember { mutableStateOf(true) }
val theme by appPreferences.themeState.collectAsState()
if (isOpen) {
Window(
title = "Gitnuro",
onCloseRequest = {
isOpen = false
},
state = rememberWindowState(
placement = WindowPlacement.Maximized,
size = DpSize(1280.dp, 720.dp)
),
icon = painterResource("logo.svg"),
) {
var showSettingsDialog by remember { mutableStateOf(false) }
AppTheme(theme = theme) {
Box(modifier = Modifier.background(MaterialTheme.colors.background)) {
AppTabs(
onOpenSettings = {
showSettingsDialog = true
}
)
}
if (showSettingsDialog) {
SettingsDialog(
appPreferences = appPreferences,
onDismiss = { showSettingsDialog = false }
)
}
}
}
} else {
appScope.cancel("Closing app")
this.exitApplication()
}
}
}
private fun loadTabs() {
val repositoriesSavedTabs = appStateManager.openRepositoriesPathsTabs
var repoTabs = repositoriesSavedTabs.map { repositoryTab ->
newAppTab(
key = repositoryTab.key,
path = repositoryTab.value
)
}
if (repoTabs.isEmpty()) {
repoTabs = listOf(
newAppTab()
)
}
tabsFlow.value = repoTabs
println("After reading prefs, got ${tabsFlow.value.count()} tabs")
}
@Composable
fun AppTabs(
tabs: SnapshotStateMap<Int, TabInformation>,
onOpenSettings: () -> Unit,
) {
val tabsInformationList = tabs.map { it.value }.sortedBy { it.key }
val tabs by tabsFlow.collectAsState()
val tabsInformationList = tabs.sortedBy { it.key }
println("Tabs count ${tabs.count()}")
LaunchedEffect(Unit) {
val repositoriesSavedTabs = appStateManager.openRepositoriesPathsTabs
var repoTabs = repositoriesSavedTabs.map { repositoryTab ->
newAppTab(
key = repositoryTab.key,
path = repositoryTab.value
)
}
if (repoTabs.isEmpty()) {
repoTabs = listOf(
newAppTab()
)
}
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}")
@ -125,22 +133,36 @@ class Main {
modifier = Modifier.background(MaterialTheme.colors.background)
) {
Tabs(
tabs = tabs,
tabsInformationList = tabsInformationList,
selectedTabKey = selectedTabKey,
onOpenSettings = onOpenSettings
onOpenSettings = onOpenSettings,
onAddedTab = { tabInfo ->
addTab(tabs, tabInfo)
},
onRemoveTab = { key ->
removeTab(tabs, key)
}
)
TabsContent(tabsInformationList, selectedTabKey.value)
}
}
private fun removeTab(tabs: List<TabInformation>, key: Int) = appScope.launch(Dispatchers.IO) {
tabsFlow.value = tabs.filter { tab -> tab.key != key }
}
fun addTab(tabsList: List<TabInformation>, tabInformation: TabInformation) = appScope.launch(Dispatchers.IO) {
tabsFlow.value = tabsList.toMutableList().apply { add(tabInformation) }
}
@Composable
fun Tabs(
tabs: SnapshotStateMap<Int, TabInformation>,
selectedTabKey: MutableState<Int>,
onOpenSettings: () -> Unit,
tabsInformationList: List<TabInformation>,
onAddedTab: (TabInformation) -> Unit,
onRemoveTab: (Int) -> Unit,
) {
Row(
modifier = Modifier
@ -162,13 +184,12 @@ class Main {
key = key
)
tabs[key] = newAppTab
onAddedTab(newAppTab)
newAppTab
},
onTabClosed = { key ->
appStateManager.repositoryTabRemoved(key)
tabs.remove(key)
onRemoveTab(key)
}
)
IconButton(

View File

@ -57,7 +57,7 @@ class AppStateManager @Inject constructor(
appPreferences.latestOpenedRepositoriesPath = Json.encodeToString(_latestOpenedRepositoriesPaths)
}
fun loadRepositoriesTabs() = appStateScope.launch(Dispatchers.IO) {
fun loadRepositoriesTabs() {
val repositoriesSaved = appPreferences.latestTabsOpened
if (repositoriesSaved.isNotEmpty()) {

View File

@ -1,13 +1,13 @@
package app.di
import app.AppStateManager
import app.Main
import app.App
import dagger.Component
import javax.inject.Singleton
@Singleton
@Component
interface AppComponent {
fun inject(main: Main)
fun inject(main: App)
fun appStateManager(): AppStateManager
}

View File

@ -179,6 +179,7 @@ class TabInformation(
.build()
tabComponent.inject(this)
//TODO: This shouldn't be here, should be in the parent method
gitManager.onRepositoryChanged = { path ->
if (path == null) {
appStateManager.repositoryTabRemoved(key)

View File

@ -1,8 +1,8 @@
import androidx.compose.ui.ExperimentalComposeUiApi
import app.Main
import app.App
@OptIn(ExperimentalComposeUiApi::class)
fun main() {
val main = Main()
val main = App()
main.start()
}