Skip to content

Commit

Permalink
- handle cases when fab is pressed after all categories have been del…
Browse files Browse the repository at this point in the history
…eted
  • Loading branch information
pablo03v committed Apr 14, 2024
1 parent 8d67f5a commit 9599ac5
Show file tree
Hide file tree
Showing 13 changed files with 196 additions and 112 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package cloud.pablos.overload.data.category

import androidx.compose.material.icons.filled.Category
import androidx.compose.ui.graphics.Color

data class CategoryState(
Expand Down
22 changes: 19 additions & 3 deletions app/src/main/java/cloud/pablos/overload/data/item/ItemDao.kt
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ fun fabPress(
val categories = categoryState.categories

if (categories.isNotEmpty()) {
startOrStop(categoryState, itemState, itemEvent)
startOrStop(categoryState, categoryEvent, itemState, itemEvent)
} else if (itemState.items.isNotEmpty()) {
categoryEvent(CategoryEvent.SetId(1))
categoryEvent(CategoryEvent.SetName("Default"))
Expand All @@ -53,19 +53,35 @@ fun fabPress(
categoryEvent(CategoryEvent.SetIsDefault(true))
categoryEvent(CategoryEvent.SaveCategory)

startOrStop(categoryState, itemState, itemEvent)
startOrStop(categoryState, categoryEvent, itemState, itemEvent)
} else {
categoryEvent(CategoryEvent.SetIsCreateCategoryDialogOpenHome(true))
}
}

fun startOrStop(
categoryState: CategoryState,
categoryEvent: (CategoryEvent) -> Unit,
itemState: ItemState,
itemEvent: (ItemEvent) -> Unit,
) {
val date = LocalDate.now()
val selectedCategoryId = categoryState.selectedCategory
val selectedCategory = categoryState.categories.find { it.id == categoryState.selectedCategory }
var selectedCategoryId: Int?

if (selectedCategory != null) {
selectedCategoryId = selectedCategory.id
} else if (categoryState.categories.isEmpty()) {
categoryEvent(CategoryEvent.SetIsCreateCategoryDialogOpenHome(true))
return
} else {
selectedCategoryId = categoryState.categories.first().id
categoryEvent(CategoryEvent.SetSelectedCategory(selectedCategoryId))

if (categoryState.categories.count() > 1) {
return
}
}

val itemsForToday = getItemsOfDay(date, categoryState, itemState)
val isFirstToday = itemsForToday.isEmpty()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ fun OverloadTopAppBar(
if (topBarState == currentTopBarState) {
when (topBarState) {
TopBarState.Home -> {
HomeTabTopAppBar()
HomeTabTopAppBar(categoryState, categoryEvent)
}

TopBarState.Calendar -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.RoundedCornerShape
Expand Down Expand Up @@ -230,25 +231,28 @@ fun CategoryScreen(
ConfigurationsTabItem(title = stringResource(id = R.string.goals))
}

// Work Goal
// Goal 1
item {
val itemLabel =
stringResource(id = R.string.work) + ": " + stringResource(id = R.string.work_goal_descr)
val itemLabel = stringResource(id = R.string.work) + ": " + stringResource(id = R.string.work_goal_descr)
Row(
verticalAlignment = Alignment.CenterVertically,
modifier =
Modifier
.padding(bottom = 16.dp)
.clip(shape = RoundedCornerShape(15.dp))
.clickable {
goalDialogState.value = true
pauseGoalDialogState.value = true
}
.clearAndSetSemantics {
contentDescription = itemLabel
},
) {
Text(
selectedCategory.emoji,
modifier = Modifier.padding(horizontal = 8.dp),
modifier =
Modifier
.width(40.dp)
.padding(horizontal = 8.dp),
)
Row(
verticalAlignment = Alignment.CenterVertically,
Expand All @@ -263,10 +267,9 @@ fun CategoryScreen(
}
}

// Pause Goal
// Goal 2
item {
val itemLabel =
stringResource(id = R.string.pause) + ": " + stringResource(id = R.string.pause_goal_descr)
val itemLabel = stringResource(id = R.string.pause) + ": " + stringResource(id = R.string.pause_goal_descr)

Row(
verticalAlignment = Alignment.CenterVertically,
Expand All @@ -285,7 +288,10 @@ fun CategoryScreen(
imageVector = Icons.Filled.DarkMode,
contentDescription = stringResource(id = R.string.pause),
tint = MaterialTheme.colorScheme.primary,
modifier = Modifier.padding(horizontal = 8.dp),
modifier =
Modifier
.width(40.dp)
.padding(horizontal = 8.dp),
)
Row(
verticalAlignment = Alignment.CenterVertically,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,24 @@
package cloud.pablos.overload.ui.tabs.calendar

import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import android.os.Build
import androidx.annotation.RequiresApi
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.NavigationBarDefaults
import androidx.compose.material3.Surface
import androidx.compose.material3.TopAppBar
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
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.ChangeCategoryButton
import cloud.pablos.overload.ui.views.ChangeYearButton
import cloud.pablos.overload.ui.views.TextView
import java.time.LocalDate

@RequiresApi(Build.VERSION_CODES.S)
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun CalendarTabTopAppBar(
Expand All @@ -33,20 +27,6 @@ fun CalendarTabTopAppBar(
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,
color = MaterialTheme.colorScheme.background,
Expand All @@ -59,44 +39,8 @@ 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 },
modifier = Modifier.padding(horizontal = 8.dp),
) {
TextView(itemState.selectedYearCalendar.toString())
}
if (yearDialogState.value) {
CalendarTabYearDialog(
firstYear = firstYear,
itemEvent = itemEvent,
onClose = { yearDialogState.value = false },
)
}
}
ChangeYearButton(itemState, itemEvent)
ChangeCategoryButton(categoryState, categoryEvent)
},
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ 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.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
Expand Down Expand Up @@ -81,9 +80,7 @@ private fun YearListContent(
itemEvent: (ItemEvent) -> Unit,
onClose: () -> Unit,
) {
LazyColumn(
modifier = Modifier.fillMaxHeight(),
) {
LazyColumn {
val currentYear = LocalDate.now().year
items((currentYear downTo firstYear).toList()) { year ->
YearRow(year = year, itemEvent = itemEvent, onClose = onClose)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.focus.FocusRequester
import androidx.compose.ui.focus.focusRequester
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
Expand Down Expand Up @@ -139,6 +141,12 @@ fun ConfigurationsTabCreateCategoryDialog(

var nameError by remember { mutableStateOf(false) }

val focusRequester = remember { FocusRequester() }

LaunchedEffect(Unit) {
focusRequester.requestFocus()
}

AlertDialog(
onDismissRequest = onClose,
title = {
Expand All @@ -157,6 +165,7 @@ fun ConfigurationsTabCreateCategoryDialog(
) {
Column(
verticalArrangement = Arrangement.spacedBy(8.dp),
modifier = Modifier.fillMaxWidth(),
) {
TextView(
"Name",
Expand All @@ -176,11 +185,13 @@ fun ConfigurationsTabCreateCategoryDialog(
placeholder = { Text(text = "Name") },
isError = nameError,
keyboardOptions = KeyboardOptions(capitalization = KeyboardCapitalization.Sentences),
modifier = Modifier.fillMaxWidth().focusRequester(focusRequester),
)
}

Column(
verticalArrangement = Arrangement.spacedBy(8.dp),
modifier = Modifier.fillMaxWidth(),
) {
TextView(
"Color",
Expand All @@ -204,6 +215,7 @@ fun ConfigurationsTabCreateCategoryDialog(

Column(
verticalArrangement = Arrangement.spacedBy(8.dp),
modifier = Modifier.fillMaxWidth(),
) {
TextView(
"Emoji",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package cloud.pablos.overload.ui.tabs.home

import android.os.Build
import androidx.annotation.RequiresApi
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.NavigationBarDefaults
Expand All @@ -9,11 +11,18 @@ import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.runtime.Composable
import androidx.compose.ui.res.stringResource
import cloud.pablos.overload.R
import cloud.pablos.overload.data.category.CategoryEvent
import cloud.pablos.overload.data.category.CategoryState
import cloud.pablos.overload.ui.views.ChangeCategoryButton
import cloud.pablos.overload.ui.views.TextView

@RequiresApi(Build.VERSION_CODES.S)
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun HomeTabTopAppBar() {
fun HomeTabTopAppBar(
categoryState: CategoryState,
categoryEvent: (CategoryEvent) -> Unit,
) {
Surface(
tonalElevation = NavigationBarDefaults.Elevation,
color = MaterialTheme.colorScheme.background,
Expand All @@ -30,6 +39,9 @@ fun HomeTabTopAppBar() {
containerColor = MaterialTheme.colorScheme.background,
titleContentColor = MaterialTheme.colorScheme.onBackground,
),
actions = {
ChangeCategoryButton(categoryState, categoryEvent)
},
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package cloud.pablos.overload.ui.views

import android.os.Build
import androidx.annotation.RequiresApi
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import cloud.pablos.overload.data.Helpers
import cloud.pablos.overload.data.category.CategoryEvent
import cloud.pablos.overload.data.category.CategoryState
import cloud.pablos.overload.ui.tabs.calendar.CalendarTabCategoryDialog

@RequiresApi(Build.VERSION_CODES.S)
@Composable
fun ChangeCategoryButton(
categoryState: CategoryState,
categoryEvent: (CategoryEvent) -> Unit,
) {
val categoryDialogState = remember { mutableStateOf(false) }

val categoriesCount = categoryState.categories.count()
val selectedCategory = categoryState.categories.find { it.id == categoryState.selectedCategory }

if (categoriesCount > 1 && selectedCategory != null) {
val backgroundColor = Helpers.decideBackground(categoryState)
val foregroundColor = Helpers.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 },
)
}
}
}
Loading

0 comments on commit 9599ac5

Please sign in to comment.