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