Skip to content
This repository has been archived by the owner on Mar 18, 2024. It is now read-only.

Commit

Permalink
Fix first week of day in calendar screen
Browse files Browse the repository at this point in the history
  • Loading branch information
AnkitSuda committed Nov 12, 2022
1 parent 652ff46 commit 8ce764a
Show file tree
Hide file tree
Showing 8 changed files with 213 additions and 118 deletions.
3 changes: 3 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ android {
}
}
compileOptions {
coreLibraryDesugaringEnabled true

sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
Expand Down Expand Up @@ -168,4 +170,5 @@ dependencies {
api Deps.Dagger.hilt
kapt Deps.Dagger.hiltCompiler

coreLibraryDesugaring Deps.Android.desugaring
}
63 changes: 34 additions & 29 deletions app/src/main/java/com/ankitsuda/rebound/ui/main/MainScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ import com.google.accompanist.navigation.material.rememberBottomSheetNavigator
import kotlinx.coroutines.launch
import timber.log.Timber
import com.ankitsuda.common.compose.R
import java.time.DayOfWeek

/**
* Root screen of the app
Expand Down Expand Up @@ -103,6 +104,9 @@ private fun MainLayout(


val currentWorkoutId by viewModel.currentWorkoutId.collectAsState(initial = NONE_WORKOUT_ID)
val appSettings by viewModel.appSettings.collectAsState(
initial = AppSettings.defValues()
)

val panelHidden = currentWorkoutId == NONE_WORKOUT_ID

Expand Down Expand Up @@ -136,6 +140,7 @@ private fun MainLayout(
CompositionLocalProvider(
LocalDialog provides dialog,
LocalPanel provides mainPanel,
LocalAppSettings provides appSettings
) {
Box() {
/**
Expand Down Expand Up @@ -243,35 +248,35 @@ private fun BottomBar(
navController: NavHostController,
) {
val bottomNavigationItems = listOf(
BottomNavigationScreens(
TabRootScreen.HomeTab.route,
stringResource(id = R.string.home),
Icons.Outlined.Home,
Icons.Filled.Home
),
BottomNavigationScreens(
TabRootScreen.HistoryTab.route,
stringResource(id = R.string.history),
Icons.Outlined.WatchLater,
Icons.Filled.WatchLater
),
BottomNavigationScreens(
TabRootScreen.WorkoutTab.route,
stringResource(id = R.string.workout),
Icons.Outlined.PlayArrow,
Icons.Filled.PlayArrow
),
BottomNavigationScreens(
TabRootScreen.ExercisesTab.route,
stringResource(id = R.string.exercises),
Icons.Outlined.FitnessCenter, Icons.Filled.FitnessCenter
),
BottomNavigationScreens(
TabRootScreen.MoreTab.route,
stringResource(id = R.string.more),
Icons.Outlined.Menu,
Icons.Filled.Menu
)
BottomNavigationScreens(
TabRootScreen.HomeTab.route,
stringResource(id = R.string.home),
Icons.Outlined.Home,
Icons.Filled.Home
),
BottomNavigationScreens(
TabRootScreen.HistoryTab.route,
stringResource(id = R.string.history),
Icons.Outlined.WatchLater,
Icons.Filled.WatchLater
),
BottomNavigationScreens(
TabRootScreen.WorkoutTab.route,
stringResource(id = R.string.workout),
Icons.Outlined.PlayArrow,
Icons.Filled.PlayArrow
),
BottomNavigationScreens(
TabRootScreen.ExercisesTab.route,
stringResource(id = R.string.exercises),
Icons.Outlined.FitnessCenter, Icons.Filled.FitnessCenter
),
BottomNavigationScreens(
TabRootScreen.MoreTab.route,
stringResource(id = R.string.more),
Icons.Outlined.Menu,
Icons.Filled.Menu
)
)

val theme = LocalThemeState.current
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import androidx.core.os.HandlerCompat.postDelayed
import androidx.lifecycle.LiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.map
import androidx.lifecycle.viewModelScope
import com.ankitsuda.common.compose.AppSettings
import com.ankitsuda.rebound.R
import com.ankitsuda.rebound.data.datastore.PrefStorage
import com.ankitsuda.rebound.data.stopwatch.*
Expand All @@ -28,7 +30,9 @@ import com.ankitsuda.rebound.resttimer.TimerState
import com.ankitsuda.rebound.resttimer.getFormattedStopWatchTime
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.launch
import timber.log.Timber
import java.time.DayOfWeek
import java.util.*
import java.util.logging.Handler
import javax.inject.Inject
Expand All @@ -37,8 +41,8 @@ import kotlin.math.max

@HiltViewModel
class MainScreenViewModel @Inject constructor(
prefStorage: PrefStorage,
private val restTimerRepository: RestTimerRepository
restTimerRepository: RestTimerRepository,
private val prefStorage: PrefStorage
) : ViewModel() {
val currentWorkoutId = prefStorage.currentWorkoutId

Expand All @@ -62,4 +66,21 @@ class MainScreenViewModel @Inject constructor(
null
}

private var _appSettings = MutableStateFlow<AppSettings>(AppSettings.defValues())
val appSettings = _appSettings.asStateFlow()

init {
observeSettings()
}

private fun observeSettings() {
viewModelScope.launch {
prefStorage.firstDayOfWeek.collect {
_appSettings.value = AppSettings(
firstDayOfWeek = DayOfWeek.of(it)
)
}
}
}

}
4 changes: 4 additions & 0 deletions modules/common-ui-compose/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ android {


compileOptions {
coreLibraryDesugaringEnabled true

sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
Expand Down Expand Up @@ -85,6 +87,8 @@ dependencies {
api Deps.Android.Accompanist.navigationFlowlayout
api Deps.Android.Accompanist.navigationAnimation

coreLibraryDesugaring Deps.Android.desugaring

// 3rd party
// api Deps.Android.Compose.coil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright (c) 2022 Ankit Suda.
*
* Licensed under the GNU General Public License v3
*
* This is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*/

package com.ankitsuda.common.compose

import androidx.compose.runtime.staticCompositionLocalOf
import java.time.DayOfWeek

data class AppSettings(
val firstDayOfWeek: DayOfWeek
) {
companion object {
fun defValues(): AppSettings = AppSettings(
firstDayOfWeek = DayOfWeek.SUNDAY
)

}
}

val LocalAppSettings = staticCompositionLocalOf<AppSettings> {
error("No AppSettings given")
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
package com.ankitsuda.rebound.ui.calendar

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.lazy.rememberLazyListState
Expand All @@ -28,24 +28,18 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavController
import com.ankitsuda.rebound.ui.components.TopBar
import com.ankitsuda.rebound.ui.components.TopBarIconButton
import me.onebone.toolbar.CollapsingToolbarScaffold
import me.onebone.toolbar.rememberCollapsingToolbarScaffoldState
import com.ankitsuda.base.utils.toEpochMillis
import com.ankitsuda.base.utils.toLocalDate
import com.ankitsuda.navigation.DATE_KEY
import com.ankitsuda.navigation.SELECTED_DATE_KEY
import com.ankitsuda.rebound.ui.calendar.components.CalendarMonthItem
import com.ankitsuda.rebound.ui.components.TopBar
import com.ankitsuda.rebound.ui.components.TopBarIconButton
import kotlinx.coroutines.launch
import me.onebone.toolbar.CollapsingToolbarScaffold
import me.onebone.toolbar.ScrollStrategy
import timber.log.Timber
import java.lang.Exception
import java.text.SimpleDateFormat
import java.time.Instant
import me.onebone.toolbar.rememberCollapsingToolbarScaffoldState
import java.time.LocalDate
import java.time.ZoneId
import java.util.*

@Composable
fun CalendarScreen(
Expand All @@ -60,74 +54,81 @@ fun CalendarScreen(
val collapsingState = rememberCollapsingToolbarScaffoldState()
val scrollState = rememberLazyListState()

val calendar = viewModel.calendar
val mCalendar by viewModel.calendar.collectAsState()
val countsWithDate by viewModel.workoutsCountOnDates.collectAsState()
val today = LocalDate.now()

val coroutine = rememberCoroutineScope()

LaunchedEffect(key1 = Unit) {
if (calendar.isEmpty()) {
viewModel.getCalendar()
try {
scrollState.scrollToItem(calendar.indexOf((calendar.filter {
try {
it.month == selectedDate.month.value && it.year == selectedDate.year
} catch (e1: Exception) {
it.month == today.month.value && today.year == today.year
}
}[0])))
} catch (e: Exception) {
e.printStackTrace()
mCalendar?.let { calendar ->
LaunchedEffect(key1 = Unit) {
if (calendar.isNotEmpty()) {
try {
scrollState.scrollToItem(calendar.indexOf((calendar.filter {
try {
it.month == selectedDate.month.value && it.year == selectedDate.year
} catch (e1: Exception) {
it.month == today.month.value && today.year == today.year
}
}[0])))
} catch (e: Exception) {
e.printStackTrace()
}
}
}
}

CollapsingToolbarScaffold(
scrollStrategy = ScrollStrategy.EnterAlwaysCollapsed,
state = collapsingState,
toolbar = {
TopBar(title = stringResource(id = R.string.calendar), strictLeftIconAlignToStart = true, leftIconBtn = {
TopBarIconButton(icon = Icons.Outlined.Close, title = stringResource(id = R.string.close_calendar)) {
navController.popBackStack()
}
}, rightIconBtn = {
TopBarIconButton(
icon = Icons.Outlined.Today,
title = stringResource(id = R.string.jump_to_today),
onClick = {
coroutine.launch {
scrollState.animateScrollToItem(calendar.indexOf((calendar.filter {
it.month == today.month.value && it.year == today.year
}[0])))
CollapsingToolbarScaffold(
scrollStrategy = ScrollStrategy.EnterAlwaysCollapsed,
state = collapsingState,
toolbar = {
TopBar(
title = stringResource(id = R.string.calendar),
strictLeftIconAlignToStart = true,
leftIconBtn = {
TopBarIconButton(
icon = Icons.Outlined.Close,
title = stringResource(id = R.string.close_calendar)
) {
navController.popBackStack()
}
},
rightIconBtn = {
TopBarIconButton(
icon = Icons.Outlined.Today,
title = stringResource(id = R.string.jump_to_today),
onClick = {
coroutine.launch {
scrollState.animateScrollToItem(calendar.indexOf((calendar.filter {
it.month == today.month.value && it.year == today.year
}[0])))
}
})
})
})
},
modifier = Modifier.background(MaterialTheme.colors.background)
) {
LazyColumn(
state = scrollState, modifier = Modifier
.fillMaxSize()
.background(MaterialTheme.colors.background)
},
modifier = Modifier.background(MaterialTheme.colors.background)
) {
items(calendar, key = { "${it.month}_${it.year}" }) {
val month = it
CalendarMonthItem(
month = month,
selectedDate = selectedDate,
countsWithDate = countsWithDate ?: emptyList(),
onClickOnDay = { dateItem ->
navController.previousBackStackEntry?.savedStateHandle?.set(
DATE_KEY,
// dateItem.date.date.time
dateItem.date.toEpochMillis()
LazyColumn(
state = scrollState, modifier = Modifier
.fillMaxSize()
.background(MaterialTheme.colors.background)
) {
items(calendar, key = { "${it.month}_${it.year}" }) {
val month = it
CalendarMonthItem(
month = month,
selectedDate = selectedDate,
countsWithDate = countsWithDate ?: emptyList(),
onClickOnDay = { dateItem ->
navController.previousBackStackEntry?.savedStateHandle?.set(
DATE_KEY,
dateItem.date.toEpochMillis()

)
navController.popBackStack()
})
)
navController.popBackStack()
})
}
}
}

}
}
}
Loading

0 comments on commit 8ce764a

Please sign in to comment.