Skip to content

Commit

Permalink
fix crash on android 14 device
Browse files Browse the repository at this point in the history
  • Loading branch information
mubeen1519 committed Oct 6, 2024
1 parent c80d227 commit 80b153c
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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),
Expand All @@ -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 }
Expand Down Expand Up @@ -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")
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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()
Expand All @@ -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){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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())
}) {
Expand Down Expand Up @@ -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 = {
Expand All @@ -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 = {
Expand All @@ -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
)
}
}
Expand Down

0 comments on commit 80b153c

Please sign in to comment.