From 80b153ca34ba157e61fe6ed036f2ba8f56a594ba Mon Sep 17 00:00:00 2001 From: mubeen Date: Sun, 6 Oct 2024 17:45:53 +0500 Subject: [PATCH] fix crash on android 14 device --- .../poopjournal/fucksgiven/MainActivity.kt | 2 +- .../component/Horizontal Pager.kt | 49 ++++++++++++------- .../presentation/navigation/NavGraph.kt | 5 +- .../presentation/screens/AboutScreen.kt | 2 +- .../presentation/screens/StatsScreen.kt | 33 ++++++++++--- 5 files changed, 64 insertions(+), 27 deletions(-) diff --git a/android/app/src/main/java/rocks/poopjournal/fucksgiven/MainActivity.kt b/android/app/src/main/java/rocks/poopjournal/fucksgiven/MainActivity.kt index 5647e38..aafde13 100644 --- a/android/app/src/main/java/rocks/poopjournal/fucksgiven/MainActivity.kt +++ b/android/app/src/main/java/rocks/poopjournal/fucksgiven/MainActivity.kt @@ -39,7 +39,7 @@ class MainActivity : ComponentActivity() { AppTheme.DARK -> true } FucksGivenTheme(darkTheme = useDarkColors) { - NavGraph(navController = rememberNavController(), themeSetting = themeSetting) + NavGraph(navController = rememberNavController(), themeSetting = themeSetting, context = this) } } } diff --git a/android/app/src/main/java/rocks/poopjournal/fucksgiven/presentation/component/Horizontal Pager.kt b/android/app/src/main/java/rocks/poopjournal/fucksgiven/presentation/component/Horizontal Pager.kt index 6281629..e8a9bba 100644 --- a/android/app/src/main/java/rocks/poopjournal/fucksgiven/presentation/component/Horizontal Pager.kt +++ b/android/app/src/main/java/rocks/poopjournal/fucksgiven/presentation/component/Horizontal Pager.kt @@ -1,5 +1,6 @@ package rocks.poopjournal.fucksgiven.presentation.component +import android.content.Context import android.util.Log import androidx.compose.foundation.ExperimentalFoundationApi import androidx.compose.foundation.layout.Column @@ -18,6 +19,7 @@ import androidx.compose.ui.Modifier import androidx.compose.ui.res.stringResource import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.unit.dp +import androidx.glance.LocalContext import rocks.poopjournal.fucksgiven.R import rocks.poopjournal.fucksgiven.presentation.ui.utils.ThemeSetting import rocks.poopjournal.fucksgiven.presentation.viewmodel.StatsViewModel @@ -30,14 +32,14 @@ import java.util.Locale fun HorizontalPagerView( pagerState: PagerState, viewModel: StatsViewModel, - themeSetting: ThemeSetting + themeSetting: ThemeSetting, + context: Context +) { - ) { val weeklyData by viewModel.weeklyData.observeAsState(emptyList()) val monthlyData by viewModel.monthlyData.observeAsState(emptyList()) val yearlyData by viewModel.yearlyData.observeAsState(emptyList()) - //weekly data processing val weeklyXValues = listOf( stringResource(id = R.string.m), @@ -50,13 +52,19 @@ fun HorizontalPagerView( ) // Create a list of LineDataPoint objects val weeklyLineDataPoints = weeklyXValues.map { day -> - val dayOfWeek = getDayOfWeek(day) - val count = weeklyData.count { data -> - val calendar = Calendar.getInstance() - calendar.timeInMillis = data.date - calendar.get(Calendar.DAY_OF_WEEK) == dayOfWeek + try { + val dayOfWeek = getDayOfWeek(day, context) + val count = weeklyData.count { data -> + val calendar = Calendar.getInstance() + calendar.timeInMillis = data.date + calendar.get(Calendar.DAY_OF_WEEK) == dayOfWeek + } + LineDataPoint(day, count) + } catch (e: Exception) { + // Log if any exceptions occur during processing + Log.e("HorizontalPagerView", "Error processing day: $day", e) + LineDataPoint(day, 0) // Fallback to avoid crash } - LineDataPoint(day, count) } val total = weeklyLineDataPoints.sumOf { it.yValue } @@ -276,15 +284,22 @@ fun HorizontalPagerView( } } -fun getDayOfWeek(day: String): Int { +fun getDayOfWeek(day: String,context: Context): Int { + val monday = context.getString(R.string.m) + val tuesday = context.getString(R.string.t) + val wednesday = context.getString(R.string.w) + val thursday = context.getString(R.string.th) + val friday = context.getString(R.string.f) + val saturday = context.getString(R.string.s) + val sunday = context.getString(R.string.su) return when (day) { - "M" -> Calendar.MONDAY - "T" -> Calendar.TUESDAY - "W" -> Calendar.WEDNESDAY - "TH" -> Calendar.THURSDAY - "F" -> Calendar.FRIDAY - "S" -> Calendar.SATURDAY - "SU" -> Calendar.SUNDAY + monday -> Calendar.MONDAY + tuesday -> Calendar.TUESDAY + wednesday -> Calendar.WEDNESDAY + thursday -> Calendar.THURSDAY + friday -> Calendar.FRIDAY + saturday -> Calendar.SATURDAY + sunday -> Calendar.SUNDAY else -> throw IllegalArgumentException("Invalid day of the week") } } diff --git a/android/app/src/main/java/rocks/poopjournal/fucksgiven/presentation/navigation/NavGraph.kt b/android/app/src/main/java/rocks/poopjournal/fucksgiven/presentation/navigation/NavGraph.kt index 42dc87d..af93db2 100644 --- a/android/app/src/main/java/rocks/poopjournal/fucksgiven/presentation/navigation/NavGraph.kt +++ b/android/app/src/main/java/rocks/poopjournal/fucksgiven/presentation/navigation/NavGraph.kt @@ -1,5 +1,6 @@ package rocks.poopjournal.fucksgiven.presentation.navigation +import android.content.Context import android.os.Build import androidx.annotation.RequiresApi import androidx.compose.runtime.Composable @@ -18,7 +19,7 @@ import rocks.poopjournal.fucksgiven.presentation.viewmodel.StatsViewModel @RequiresApi(Build.VERSION_CODES.P) @Composable -fun NavGraph(navController: NavHostController,themeSetting: ThemeSetting){ +fun NavGraph(navController: NavHostController,themeSetting: ThemeSetting,context: Context){ val viewModel : HomeViewModel = hiltViewModel() val statsViewModel : StatsViewModel = hiltViewModel() val settingsViewModel : SettingsViewModel = hiltViewModel() @@ -28,7 +29,7 @@ fun NavGraph(navController: NavHostController,themeSetting: ThemeSetting){ HomeScreen(navController,viewModel) } composable(route = STATS_SCREEN){ - StatsScreen(navController = navController, viewModel = statsViewModel,themeSetting) + StatsScreen(navController = navController, viewModel = statsViewModel, themeSetting = themeSetting, context = context) } composable(route = ABOUT_SCREEN){ diff --git a/android/app/src/main/java/rocks/poopjournal/fucksgiven/presentation/screens/AboutScreen.kt b/android/app/src/main/java/rocks/poopjournal/fucksgiven/presentation/screens/AboutScreen.kt index aea65cf..928e238 100644 --- a/android/app/src/main/java/rocks/poopjournal/fucksgiven/presentation/screens/AboutScreen.kt +++ b/android/app/src/main/java/rocks/poopjournal/fucksgiven/presentation/screens/AboutScreen.kt @@ -112,7 +112,7 @@ fun AboutScreen(navController: NavHostController) { style = MaterialTheme.typography.bodyLarge, ) val stylizedPoetry = buildAnnotatedString { - withStyle(style = SpanStyle(fontSize = 14.sp, fontWeight = FontWeight.W400)) { + withStyle(style = SpanStyle(fontSize = 14.sp, fontWeight = FontWeight.W400, color = MaterialTheme.colorScheme.primary)) { append("v${packageInfo.longVersionCode}") append("—") withStyle(style = SpanStyle(color = MaterialTheme.colorScheme.primary)) { diff --git a/android/app/src/main/java/rocks/poopjournal/fucksgiven/presentation/screens/StatsScreen.kt b/android/app/src/main/java/rocks/poopjournal/fucksgiven/presentation/screens/StatsScreen.kt index d8bd71c..d23bdfb 100644 --- a/android/app/src/main/java/rocks/poopjournal/fucksgiven/presentation/screens/StatsScreen.kt +++ b/android/app/src/main/java/rocks/poopjournal/fucksgiven/presentation/screens/StatsScreen.kt @@ -1,5 +1,6 @@ package rocks.poopjournal.fucksgiven.presentation.screens +import android.content.Context import androidx.compose.foundation.BorderStroke import androidx.compose.foundation.ExperimentalFoundationApi import androidx.compose.foundation.background @@ -41,14 +42,24 @@ import rocks.poopjournal.fucksgiven.presentation.viewmodel.StatsViewModel @OptIn(ExperimentalFoundationApi::class) @Composable -fun StatsScreen(navController: NavHostController, viewModel: StatsViewModel,themeSetting: ThemeSetting) { +fun StatsScreen( + navController: NavHostController, + viewModel: StatsViewModel, + themeSetting: ThemeSetting, + context: Context +) { val scope = rememberCoroutineScope() val pager = rememberPagerState(pageCount = { 3 }) var selectedPage by remember { mutableIntStateOf(0) } // Track selected page - Scaffold(topBar = { AppBar(title = stringResource(id = R.string.stats), navigate = navController) }, + Scaffold(topBar = { + AppBar( + title = stringResource(id = R.string.stats), + navigate = navController + ) + }, bottomBar = { BottomNavBar(navHostController = navController, items = BottomBar.getMenuBottomItems()) }) { @@ -89,7 +100,10 @@ fun StatsScreen(navController: NavHostController, viewModel: StatsViewModel,them contentColor = MaterialTheme.colorScheme.primary ) ) { - Text(stringResource(id = R.string.weekly), style = MaterialTheme.typography.bodySmall) + Text( + stringResource(id = R.string.weekly), + style = MaterialTheme.typography.bodySmall + ) } OutlinedButton( onClick = { @@ -111,7 +125,10 @@ fun StatsScreen(navController: NavHostController, viewModel: StatsViewModel,them ) ) { - Text(stringResource(id = R.string.monthly), style = MaterialTheme.typography.bodySmall) + Text( + stringResource(id = R.string.monthly), + style = MaterialTheme.typography.bodySmall + ) } OutlinedButton( onClick = { @@ -133,14 +150,18 @@ fun StatsScreen(navController: NavHostController, viewModel: StatsViewModel,them ) ) { - Text(stringResource(id = R.string.yearly), style = MaterialTheme.typography.bodySmall) + Text( + stringResource(id = R.string.yearly), + style = MaterialTheme.typography.bodySmall + ) } } HorizontalPagerView( pagerState = pager, viewModel = viewModel, - themeSetting = themeSetting + themeSetting = themeSetting, + context = context ) } }