Skip to content

Commit

Permalink
- adds category switch to calendar
Browse files Browse the repository at this point in the history
  • Loading branch information
pablo03v committed Apr 14, 2024
1 parent 905ab57 commit 8d67f5a
Show file tree
Hide file tree
Showing 11 changed files with 174 additions and 6 deletions.
2 changes: 2 additions & 0 deletions app/src/main/java/cloud/pablos/overload/ui/OverloadApp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,7 @@ private fun OverloadNavHost(
CalendarTab(
contentType = contentType,
categoryState = categoryState,
categoryEvent = categoryEvent,
itemState = itemState,
itemEvent = itemEvent,
onNavigate = { navController.navigate(OverloadRoute.DAY) },
Expand All @@ -400,6 +401,7 @@ private fun OverloadNavHost(
composable(OverloadRoute.DAY) {
DayScreen(
categoryState = categoryState,
categoryEvent = categoryEvent,
itemState = itemState,
itemEvent = itemEvent,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,7 @@ class TopBarState private constructor() {
fun OverloadTopAppBar(
selectedDestination: String,
categoryState: CategoryState,
categoryEvent: (CategoryEvent) -> Unit,
itemState: ItemState,
itemEvent: (ItemEvent) -> Unit,
) {
Expand Down Expand Up @@ -405,7 +406,7 @@ fun OverloadTopAppBar(
}

TopBarState.Calendar -> {
CalendarTabTopAppBar(itemState, itemEvent)
CalendarTabTopAppBar(categoryState, categoryEvent, itemState, itemEvent)
}

TopBarState.Configurations -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ fun CategoryScreen(
OverloadTopAppBar(
selectedDestination = OverloadRoute.CATEGORY,
categoryState = categoryState,
categoryEvent = categoryEvent,
itemState = itemState,
itemEvent = itemEvent,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import cloud.pablos.overload.data.Converters.Companion.convertStringToLocalDateTime
import cloud.pablos.overload.data.category.CategoryEvent
import cloud.pablos.overload.data.category.CategoryState
import cloud.pablos.overload.data.item.ItemEvent
import cloud.pablos.overload.data.item.ItemState
Expand All @@ -32,6 +33,7 @@ import java.time.temporal.ChronoUnit
@Composable
fun DayScreen(
categoryState: CategoryState,
categoryEvent: (CategoryEvent) -> Unit,
itemState: ItemState,
itemEvent: (ItemEvent) -> Unit,
) {
Expand Down Expand Up @@ -82,6 +84,7 @@ fun DayScreen(
OverloadTopAppBar(
selectedDestination = OverloadRoute.DAY,
categoryState = categoryState,
categoryEvent = categoryEvent,
itemState = itemState,
itemEvent = itemEvent,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import cloud.pablos.overload.data.Converters.Companion.convertStringToLocalDateTime
import cloud.pablos.overload.data.category.CategoryEvent
import cloud.pablos.overload.data.category.CategoryState
import cloud.pablos.overload.data.item.ItemEvent
import cloud.pablos.overload.data.item.ItemState
Expand All @@ -49,6 +50,7 @@ import java.time.temporal.ChronoUnit
fun CalendarTab(
contentType: OverloadContentType,
categoryState: CategoryState,
categoryEvent: (CategoryEvent) -> Unit,
itemState: ItemState,
itemEvent: (ItemEvent) -> Unit,
onNavigate: () -> Unit,
Expand All @@ -66,6 +68,7 @@ fun CalendarTab(
OverloadTopAppBar(
selectedDestination = OverloadRoute.CALENDAR,
categoryState = categoryState,
categoryEvent = categoryEvent,
itemState = itemState,
itemEvent = itemEvent,
)
Expand All @@ -87,6 +90,7 @@ fun CalendarTab(
}

YearView(
categoryState = categoryState,
itemEvent = itemEvent,
date = getLocalDate(itemState.selectedDayCalendar),
year = itemState.selectedYearCalendar,
Expand Down Expand Up @@ -185,6 +189,7 @@ fun CalendarTab(
}

YearView(
categoryState = categoryState,
itemEvent = itemEvent,
date = getLocalDate(itemState.selectedDayCalendar),
year = itemState.selectedYearCalendar,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package cloud.pablos.overload.ui.tabs.calendar

import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.itemsIndexed
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.rounded.Category
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.NavigationBarDefaults
import androidx.compose.material3.Surface
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Dialog
import cloud.pablos.overload.R
import cloud.pablos.overload.data.category.Category
import cloud.pablos.overload.data.category.CategoryEvent
import cloud.pablos.overload.data.category.CategoryState
import cloud.pablos.overload.ui.views.TextView

@Composable
fun CalendarTabCategoryDialog(
categoryState: CategoryState,
categoryEvent: (CategoryEvent) -> Unit,
onClose: () -> Unit,
) {
Dialog(
onDismissRequest = onClose,
content = {
Surface(
shape = MaterialTheme.shapes.large,
tonalElevation = NavigationBarDefaults.Elevation,
color = MaterialTheme.colorScheme.background,
modifier = Modifier.fillMaxWidth(),
) {
CategoryDialogContent(categoryState, categoryEvent, onClose)
}
},
)
}

@Composable
private fun CategoryDialogContent(
categoryState: CategoryState,
categoryEvent: (CategoryEvent) -> Unit,
onClose: () -> Unit,
) {
Column(
horizontalAlignment = Alignment.CenterHorizontally,
modifier = Modifier.padding(24.dp),
) {
Icon(
imageVector = Icons.Rounded.Category,
contentDescription = stringResource(id = R.string.select_category),
tint = MaterialTheme.colorScheme.primary,
modifier = Modifier.padding(16.dp),
)

TextView(
text = stringResource(id = R.string.select_category),
fontSize = MaterialTheme.typography.titleLarge.fontSize,
modifier = Modifier.padding(top = 16.dp, bottom = 8.dp),
)

CategoryListContent(categoryState, categoryEvent, onClose)
}
}

@Composable
private fun CategoryListContent(
categoryState: CategoryState,
categoryEvent: (CategoryEvent) -> Unit,
onClose: () -> Unit,
) {
LazyColumn {
itemsIndexed(categoryState.categories) { index, category ->
CategoryRow(category, categoryEvent, onClose)
if (index < categoryState.categories.count() - 1) {
HorizontalDivider()
}
}
}
}

@Composable
private fun CategoryRow(
category: Category,
categoryEvent: (CategoryEvent) -> Unit,
onClose: () -> Unit,
) {
Row(
modifier =
Modifier
.fillMaxWidth()
.clickable {
categoryEvent(CategoryEvent.SetSelectedCategory(category.id))
onClose()
}
.padding(16.dp),
horizontalArrangement = Arrangement.Center,
) {
TextView(text = category.emoji + " " + category.name)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cloud.pablos.overload.ui.tabs.calendar

import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.NavigationBarDefaults
Expand All @@ -15,6 +16,10 @@ import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import cloud.pablos.overload.R
import cloud.pablos.overload.data.Converters.Companion.convertStringToLocalDateTime
import cloud.pablos.overload.data.Helpers.Companion.decideBackground
import cloud.pablos.overload.data.Helpers.Companion.decideForeground
import cloud.pablos.overload.data.category.CategoryEvent
import cloud.pablos.overload.data.category.CategoryState
import cloud.pablos.overload.data.item.ItemEvent
import cloud.pablos.overload.data.item.ItemState
import cloud.pablos.overload.ui.views.TextView
Expand All @@ -23,18 +28,24 @@ import java.time.LocalDate
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun CalendarTabTopAppBar(
categoryState: CategoryState,
categoryEvent: (CategoryEvent) -> Unit,
itemState: ItemState,
itemEvent: (ItemEvent) -> Unit,
) {
val yearDialogState = remember { mutableStateOf(false) }
val categoryDialogState = remember { mutableStateOf(false) }

val firstYear =
if (itemState.items.isEmpty()) {
LocalDate.now().year
} else {
itemState.items.minByOrNull { it.startTime }?.let { convertStringToLocalDateTime(it.startTime).year } ?: LocalDate.now().year
}

val yearsCount = LocalDate.now().year - firstYear
val categoriesCount = categoryState.categories.count()
val selectedCategory = categoryState.categories.find { it.id == categoryState.selectedCategory }

Surface(
tonalElevation = NavigationBarDefaults.Elevation,
Expand All @@ -48,6 +59,29 @@ fun CalendarTabTopAppBar(
)
},
actions = {
if (categoriesCount > 1 && selectedCategory != null) {
val backgroundColor = decideBackground(categoryState)
val foregroundColor = decideForeground(backgroundColor)

Button(
onClick = { categoryDialogState.value = true },
modifier = Modifier.padding(horizontal = 8.dp),
colors =
ButtonDefaults.buttonColors(
containerColor = backgroundColor,
contentColor = foregroundColor,
),
) {
TextView(selectedCategory.emoji)
}
if (categoryDialogState.value) {
CalendarTabCategoryDialog(
categoryState,
categoryEvent,
onClose = { categoryDialogState.value = false },
)
}
}
if (yearsCount > 0) {
Button(
onClick = { yearDialogState.value = true },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ fun ConfigurationsTab(
OverloadTopAppBar(
selectedDestination = OverloadRoute.CONFIGURATIONS,
categoryState = categoryState,
categoryEvent = categoryEvent,
itemState = itemState,
itemEvent = itemEvent,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ fun HomeTab(
OverloadTopAppBar(
selectedDestination = OverloadRoute.HOME,
categoryState = categoryState,
categoryEvent = categoryEvent,
itemState = itemState,
itemEvent = itemEvent,
)
Expand Down
16 changes: 11 additions & 5 deletions app/src/main/java/cloud/pablos/overload/ui/views/YearView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import cloud.pablos.overload.data.Helpers.Companion.decideBackground
import cloud.pablos.overload.data.category.CategoryState
import cloud.pablos.overload.data.item.ItemEvent
import cloud.pablos.overload.ui.tabs.home.getFormattedDate
import java.time.LocalDate
Expand All @@ -39,6 +41,7 @@ import java.util.Locale
fun YearView(
date: LocalDate,
year: Int,
categoryState: CategoryState,
itemEvent: (ItemEvent) -> Unit,
bottomPadding: Dp = 0.dp,
onNavigate: () -> Unit = {},
Expand Down Expand Up @@ -80,7 +83,7 @@ fun YearView(
if (isLastWeekInLastMonth) bottomPadding else 0.dp,
),
) {
WeekRow(firstDayOfMonth, weekOfMonth, date, highlightSelectedDay, itemEvent, onNavigate)
WeekRow(firstDayOfMonth, weekOfMonth, date, highlightSelectedDay, categoryState, itemEvent, onNavigate)
}
}
}
Expand All @@ -107,6 +110,7 @@ fun WeekRow(
weekOfMonth: Int,
date: LocalDate,
highlightSelectedDay: Boolean = false,
categoryState: CategoryState,
itemEvent: (ItemEvent) -> Unit,
onNavigate: () -> Unit,
) {
Expand Down Expand Up @@ -143,6 +147,7 @@ fun WeekRow(
while (iterationDate < endDayOfWeek) {
val (backgroundColor, borderColor) =
getColorOfDay(
categoryState = categoryState,
date = iterationDate,
firstDayOfMonth = firstDayOfMonth,
selected = date == iterationDate,
Expand Down Expand Up @@ -198,7 +203,7 @@ fun DayCell(
interactionSource = remember { MutableInteractionSource() },
)
.clip(CircleShape)
.border(2.dp, borderColor, CircleShape),
.border(3.dp, borderColor, CircleShape),
contentAlignment = Alignment.Center,
) {
TextView(
Expand All @@ -217,12 +222,13 @@ fun EmptyDayCell() {
.requiredSize(36.dp)
.background(Color.Transparent, shape = CircleShape)
.clip(CircleShape)
.border(2.dp, Color.Transparent, CircleShape),
.border(3.dp, Color.Transparent, CircleShape),
)
}

@Composable
fun getColorOfDay(
categoryState: CategoryState,
date: LocalDate,
firstDayOfMonth: LocalDate,
selected: Boolean,
Expand All @@ -242,7 +248,7 @@ fun getColorOfDay(
when (highlightSelectedDay) {
true -> {
if (selected) {
MaterialTheme.colorScheme.primary
decideBackground(categoryState)
} else {
Color.Transparent
}
Expand All @@ -252,7 +258,7 @@ fun getColorOfDay(
if (
date == LocalDate.now()
) {
MaterialTheme.colorScheme.primary
decideBackground(categoryState)
} else {
Color.Transparent
}
Expand Down
Loading

0 comments on commit 8d67f5a

Please sign in to comment.