fix(app): initialization

This commit is contained in:
Diego Beraldin 2023-11-26 12:09:07 +01:00
parent 6d07b36dee
commit a144de3651

View File

@ -82,32 +82,27 @@ fun App() {
val accountRepository = remember { getAccountRepository() }
val settingsRepository = remember { getSettingsRepository() }
val settings by settingsRepository.currentSettings.collectAsState()
var hasBeenInitialized by remember { mutableStateOf(false) }
val apiConfigurationRepository = remember { getApiConfigurationRepository() }
val crashReportSender = remember { getCrashReportSender() }
val crashReportConfiguration = remember { getCrashReportConfiguration() }
val themeRepository = remember { getThemeRepository() }
val defaultLocale = stringResource(MR.strings.lang)
val languageRepository = remember { getLanguageRepository() }
val locale by derivedStateOf { settings.locale }
val defaultTheme = if (isSystemInDarkTheme()) {
UiTheme.Dark.toInt()
} else {
UiTheme.Light.toInt()
}
val defaultLocale = stringResource(MR.strings.lang)
val languageRepository = remember { getLanguageRepository() }
val locale by derivedStateOf { settings.locale }
val currentTheme by themeRepository.uiTheme.collectAsState()
val useDynamicColors by themeRepository.dynamicColors.collectAsState()
val fontScale by themeRepository.contentFontScale.collectAsState()
val uiFontScale by themeRepository.uiFontScale.collectAsState()
val navigationCoordinator = remember { getNavigationCoordinator() }
val scope = rememberCoroutineScope()
val drawerState = rememberDrawerState(initialValue = DrawerValue.Closed)
val drawerCoordinator = remember { getDrawerCoordinator() }
val drawerGestureEnabled by drawerCoordinator.gesturesEnabled.collectAsState()
var isInitialized by remember { mutableStateOf(false) }
languageRepository.currentLanguage.onEach { lang ->
StringDesc.localeType = StringDesc.LocaleType.Custom(lang)
}.launchIn(scope)
LaunchedEffect(Unit) {
val accountId = accountRepository.getActive()?.id
@ -118,12 +113,8 @@ fun App() {
if (lastInstance != null) {
apiConfigurationRepository.changeInstance(lastInstance)
}
launch {
with(crashReportSender) {
initialize()
setEnabled(crashReportConfiguration.isEnabled())
}
}
crashReportSender.initialize()
crashReportSender.setEnabled(crashReportConfiguration.isEnabled())
with(themeRepository) {
changeUiTheme((currentSettings.theme ?: defaultTheme).toUiTheme())
@ -141,11 +132,35 @@ fun App() {
}
}
isInitialized = true
hasBeenInitialized = true
}
LaunchedEffect(locale) {
languageRepository.changeLanguage(locale ?: defaultLocale)
}
val scope = rememberCoroutineScope()
languageRepository.currentLanguage.onEach { lang ->
StringDesc.localeType = StringDesc.LocaleType.Custom(lang)
}.launchIn(scope)
LaunchedEffect(settings) {
with(themeRepository) {
changeUiTheme((settings.theme ?: defaultTheme).toUiTheme())
changeNavItemTitles(settings.navigationTitlesVisible)
changeDynamicColors(settings.dynamicColors)
changeCustomSeedColor(settings.customSeedColor?.let { Color(it) })
changePostLayout(settings.postLayout.toPostLayout())
changeContentFontScale(settings.contentFontScale)
changeUiFontScale(settings.uiFontScale)
changeUiFontFamily(settings.uiFontFamily.toUiFontFamily())
with(themeRepository) {
changeUpvoteColor(settings.upvoteColor?.let { Color(it) })
changeDownvoteColor(settings.downvoteColor?.let { Color(it) })
}
}
}
LaunchedEffect(navigationCoordinator) {
navigationCoordinator.deepLinkUrl.debounce(750).onEach { url ->
val community = getCommunityFromUrl(url)
@ -181,6 +196,7 @@ fun App() {
}
}.launchIn(this)
}
LaunchedEffect(drawerCoordinator) {
drawerCoordinator.toggleEvents.onEach { evt ->
when (evt) {
@ -228,57 +244,55 @@ fun App() {
fontScale = uiFontScale,
),
) {
if (isInitialized) {
BottomSheetNavigator(
sheetShape = RoundedCornerShape(
topStart = CornerSize.xl, topEnd = CornerSize.xl
),
sheetBackgroundColor = MaterialTheme.colorScheme.background,
) { bottomNavigator ->
navigationCoordinator.setBottomNavigator(bottomNavigator)
ModalNavigationDrawer(
drawerState = drawerState,
gesturesEnabled = drawerGestureEnabled,
drawerContent = {
ModalDrawerSheet {
TabNavigator(ModalDrawerContent)
}
},
) {
Navigator(screen = MainScreen, onBackPressed = {
val callback = navigationCoordinator.getCanGoBackCallback()
callback?.let { it() } ?: true
}) { navigator ->
LaunchedEffect(Unit) {
navigationCoordinator.setRootNavigator(navigator)
}
CurrentScreen()
BottomSheetNavigator(
sheetShape = RoundedCornerShape(
topStart = CornerSize.xl, topEnd = CornerSize.xl
),
sheetBackgroundColor = MaterialTheme.colorScheme.background,
) { bottomNavigator ->
navigationCoordinator.setBottomNavigator(bottomNavigator)
ModalNavigationDrawer(
drawerState = drawerState,
gesturesEnabled = drawerGestureEnabled,
drawerContent = {
ModalDrawerSheet {
TabNavigator(ModalDrawerContent)
}
}
}
} else {
Box(
modifier = Modifier
.fillMaxSize()
.background(color = ic_launcher_background),
contentAlignment = Alignment.Center,
},
) {
Column(
modifier = Modifier.padding(top = 24.dp),
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.spacedBy(Spacing.s),
) {
Image(
painter = painterResource(MR.images.icon),
contentDescription = null,
)
CircularProgressIndicator(
color = md_theme_dark_onPrimary,
)
Navigator(screen = MainScreen, onBackPressed = {
val callback = navigationCoordinator.getCanGoBackCallback()
callback?.let { it() } ?: true
}) { navigator ->
LaunchedEffect(Unit) {
navigationCoordinator.setRootNavigator(navigator)
}
if (hasBeenInitialized) {
CurrentScreen()
} else {
Box(
modifier = Modifier
.fillMaxSize()
.background(color = ic_launcher_background),
contentAlignment = Alignment.Center,
) {
Column(
modifier = Modifier.padding(top = 24.dp),
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.spacedBy(Spacing.s),
) {
Image(
painter = painterResource(MR.images.icon),
contentDescription = null,
)
CircularProgressIndicator(
color = md_theme_dark_onPrimary,
)
}
}
}
}
}
}