Skip to content

Commit 9599ac5

Browse files
committed
- handle cases when fab is pressed after all categories have been deleted
1 parent 8d67f5a commit 9599ac5

File tree

13 files changed

+196
-112
lines changed

13 files changed

+196
-112
lines changed

app/src/main/java/cloud/pablos/overload/data/category/CategoryState.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package cloud.pablos.overload.data.category
22

3-
import androidx.compose.material.icons.filled.Category
43
import androidx.compose.ui.graphics.Color
54

65
data class CategoryState(

app/src/main/java/cloud/pablos/overload/data/item/ItemDao.kt

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ fun fabPress(
4242
val categories = categoryState.categories
4343

4444
if (categories.isNotEmpty()) {
45-
startOrStop(categoryState, itemState, itemEvent)
45+
startOrStop(categoryState, categoryEvent, itemState, itemEvent)
4646
} else if (itemState.items.isNotEmpty()) {
4747
categoryEvent(CategoryEvent.SetId(1))
4848
categoryEvent(CategoryEvent.SetName("Default"))
@@ -53,19 +53,35 @@ fun fabPress(
5353
categoryEvent(CategoryEvent.SetIsDefault(true))
5454
categoryEvent(CategoryEvent.SaveCategory)
5555

56-
startOrStop(categoryState, itemState, itemEvent)
56+
startOrStop(categoryState, categoryEvent, itemState, itemEvent)
5757
} else {
5858
categoryEvent(CategoryEvent.SetIsCreateCategoryDialogOpenHome(true))
5959
}
6060
}
6161

6262
fun startOrStop(
6363
categoryState: CategoryState,
64+
categoryEvent: (CategoryEvent) -> Unit,
6465
itemState: ItemState,
6566
itemEvent: (ItemEvent) -> Unit,
6667
) {
6768
val date = LocalDate.now()
68-
val selectedCategoryId = categoryState.selectedCategory
69+
val selectedCategory = categoryState.categories.find { it.id == categoryState.selectedCategory }
70+
var selectedCategoryId: Int?
71+
72+
if (selectedCategory != null) {
73+
selectedCategoryId = selectedCategory.id
74+
} else if (categoryState.categories.isEmpty()) {
75+
categoryEvent(CategoryEvent.SetIsCreateCategoryDialogOpenHome(true))
76+
return
77+
} else {
78+
selectedCategoryId = categoryState.categories.first().id
79+
categoryEvent(CategoryEvent.SetSelectedCategory(selectedCategoryId))
80+
81+
if (categoryState.categories.count() > 1) {
82+
return
83+
}
84+
}
6985

7086
val itemsForToday = getItemsOfDay(date, categoryState, itemState)
7187
val isFirstToday = itemsForToday.isEmpty()

app/src/main/java/cloud/pablos/overload/ui/navigation/OverloadNavigationComponents.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ fun OverloadTopAppBar(
402402
if (topBarState == currentTopBarState) {
403403
when (topBarState) {
404404
TopBarState.Home -> {
405-
HomeTabTopAppBar()
405+
HomeTabTopAppBar(categoryState, categoryEvent)
406406
}
407407

408408
TopBarState.Calendar -> {

app/src/main/java/cloud/pablos/overload/ui/screens/category/CategoryScreen.kt

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import androidx.compose.foundation.layout.Row
1111
import androidx.compose.foundation.layout.fillMaxSize
1212
import androidx.compose.foundation.layout.fillMaxWidth
1313
import androidx.compose.foundation.layout.padding
14+
import androidx.compose.foundation.layout.width
1415
import androidx.compose.foundation.lazy.LazyColumn
1516
import androidx.compose.foundation.rememberScrollState
1617
import androidx.compose.foundation.shape.RoundedCornerShape
@@ -230,25 +231,28 @@ fun CategoryScreen(
230231
ConfigurationsTabItem(title = stringResource(id = R.string.goals))
231232
}
232233

233-
// Work Goal
234+
// Goal 1
234235
item {
235-
val itemLabel =
236-
stringResource(id = R.string.work) + ": " + stringResource(id = R.string.work_goal_descr)
236+
val itemLabel = stringResource(id = R.string.work) + ": " + stringResource(id = R.string.work_goal_descr)
237237
Row(
238238
verticalAlignment = Alignment.CenterVertically,
239239
modifier =
240240
Modifier
241+
.padding(bottom = 16.dp)
241242
.clip(shape = RoundedCornerShape(15.dp))
242243
.clickable {
243-
goalDialogState.value = true
244+
pauseGoalDialogState.value = true
244245
}
245246
.clearAndSetSemantics {
246247
contentDescription = itemLabel
247248
},
248249
) {
249250
Text(
250251
selectedCategory.emoji,
251-
modifier = Modifier.padding(horizontal = 8.dp),
252+
modifier =
253+
Modifier
254+
.width(40.dp)
255+
.padding(horizontal = 8.dp),
252256
)
253257
Row(
254258
verticalAlignment = Alignment.CenterVertically,
@@ -263,10 +267,9 @@ fun CategoryScreen(
263267
}
264268
}
265269

266-
// Pause Goal
270+
// Goal 2
267271
item {
268-
val itemLabel =
269-
stringResource(id = R.string.pause) + ": " + stringResource(id = R.string.pause_goal_descr)
272+
val itemLabel = stringResource(id = R.string.pause) + ": " + stringResource(id = R.string.pause_goal_descr)
270273

271274
Row(
272275
verticalAlignment = Alignment.CenterVertically,
@@ -285,7 +288,10 @@ fun CategoryScreen(
285288
imageVector = Icons.Filled.DarkMode,
286289
contentDescription = stringResource(id = R.string.pause),
287290
tint = MaterialTheme.colorScheme.primary,
288-
modifier = Modifier.padding(horizontal = 8.dp),
291+
modifier =
292+
Modifier
293+
.width(40.dp)
294+
.padding(horizontal = 8.dp),
289295
)
290296
Row(
291297
verticalAlignment = Alignment.CenterVertically,

app/src/main/java/cloud/pablos/overload/ui/tabs/calendar/CalendarTabTopAppBar.kt

Lines changed: 7 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,24 @@
11
package cloud.pablos.overload.ui.tabs.calendar
22

3-
import androidx.compose.foundation.layout.padding
4-
import androidx.compose.material3.Button
5-
import androidx.compose.material3.ButtonDefaults
3+
import android.os.Build
4+
import androidx.annotation.RequiresApi
65
import androidx.compose.material3.ExperimentalMaterial3Api
76
import androidx.compose.material3.MaterialTheme
87
import androidx.compose.material3.NavigationBarDefaults
98
import androidx.compose.material3.Surface
109
import androidx.compose.material3.TopAppBar
1110
import androidx.compose.runtime.Composable
12-
import androidx.compose.runtime.mutableStateOf
13-
import androidx.compose.runtime.remember
14-
import androidx.compose.ui.Modifier
1511
import androidx.compose.ui.res.stringResource
16-
import androidx.compose.ui.unit.dp
1712
import cloud.pablos.overload.R
18-
import cloud.pablos.overload.data.Converters.Companion.convertStringToLocalDateTime
19-
import cloud.pablos.overload.data.Helpers.Companion.decideBackground
20-
import cloud.pablos.overload.data.Helpers.Companion.decideForeground
2113
import cloud.pablos.overload.data.category.CategoryEvent
2214
import cloud.pablos.overload.data.category.CategoryState
2315
import cloud.pablos.overload.data.item.ItemEvent
2416
import cloud.pablos.overload.data.item.ItemState
17+
import cloud.pablos.overload.ui.views.ChangeCategoryButton
18+
import cloud.pablos.overload.ui.views.ChangeYearButton
2519
import cloud.pablos.overload.ui.views.TextView
26-
import java.time.LocalDate
2720

21+
@RequiresApi(Build.VERSION_CODES.S)
2822
@OptIn(ExperimentalMaterial3Api::class)
2923
@Composable
3024
fun CalendarTabTopAppBar(
@@ -33,20 +27,6 @@ fun CalendarTabTopAppBar(
3327
itemState: ItemState,
3428
itemEvent: (ItemEvent) -> Unit,
3529
) {
36-
val yearDialogState = remember { mutableStateOf(false) }
37-
val categoryDialogState = remember { mutableStateOf(false) }
38-
39-
val firstYear =
40-
if (itemState.items.isEmpty()) {
41-
LocalDate.now().year
42-
} else {
43-
itemState.items.minByOrNull { it.startTime }?.let { convertStringToLocalDateTime(it.startTime).year } ?: LocalDate.now().year
44-
}
45-
46-
val yearsCount = LocalDate.now().year - firstYear
47-
val categoriesCount = categoryState.categories.count()
48-
val selectedCategory = categoryState.categories.find { it.id == categoryState.selectedCategory }
49-
5030
Surface(
5131
tonalElevation = NavigationBarDefaults.Elevation,
5232
color = MaterialTheme.colorScheme.background,
@@ -59,44 +39,8 @@ fun CalendarTabTopAppBar(
5939
)
6040
},
6141
actions = {
62-
if (categoriesCount > 1 && selectedCategory != null) {
63-
val backgroundColor = decideBackground(categoryState)
64-
val foregroundColor = decideForeground(backgroundColor)
65-
66-
Button(
67-
onClick = { categoryDialogState.value = true },
68-
modifier = Modifier.padding(horizontal = 8.dp),
69-
colors =
70-
ButtonDefaults.buttonColors(
71-
containerColor = backgroundColor,
72-
contentColor = foregroundColor,
73-
),
74-
) {
75-
TextView(selectedCategory.emoji)
76-
}
77-
if (categoryDialogState.value) {
78-
CalendarTabCategoryDialog(
79-
categoryState,
80-
categoryEvent,
81-
onClose = { categoryDialogState.value = false },
82-
)
83-
}
84-
}
85-
if (yearsCount > 0) {
86-
Button(
87-
onClick = { yearDialogState.value = true },
88-
modifier = Modifier.padding(horizontal = 8.dp),
89-
) {
90-
TextView(itemState.selectedYearCalendar.toString())
91-
}
92-
if (yearDialogState.value) {
93-
CalendarTabYearDialog(
94-
firstYear = firstYear,
95-
itemEvent = itemEvent,
96-
onClose = { yearDialogState.value = false },
97-
)
98-
}
99-
}
42+
ChangeYearButton(itemState, itemEvent)
43+
ChangeCategoryButton(categoryState, categoryEvent)
10044
},
10145
)
10246
}

app/src/main/java/cloud/pablos/overload/ui/tabs/calendar/CalendarTabYearDialog.kt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import androidx.compose.foundation.clickable
44
import androidx.compose.foundation.layout.Arrangement
55
import androidx.compose.foundation.layout.Column
66
import androidx.compose.foundation.layout.Row
7-
import androidx.compose.foundation.layout.fillMaxHeight
87
import androidx.compose.foundation.layout.fillMaxWidth
98
import androidx.compose.foundation.layout.padding
109
import androidx.compose.foundation.lazy.LazyColumn
@@ -81,9 +80,7 @@ private fun YearListContent(
8180
itemEvent: (ItemEvent) -> Unit,
8281
onClose: () -> Unit,
8382
) {
84-
LazyColumn(
85-
modifier = Modifier.fillMaxHeight(),
86-
) {
83+
LazyColumn {
8784
val currentYear = LocalDate.now().year
8885
items((currentYear downTo firstYear).toList()) { year ->
8986
YearRow(year = year, itemEvent = itemEvent, onClose = onClose)

app/src/main/java/cloud/pablos/overload/ui/tabs/configurations/ConfigurationsTabCreateCategoryDialog.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ import androidx.compose.runtime.setValue
5656
import androidx.compose.ui.Alignment
5757
import androidx.compose.ui.Modifier
5858
import androidx.compose.ui.draw.clip
59+
import androidx.compose.ui.focus.FocusRequester
60+
import androidx.compose.ui.focus.focusRequester
5961
import androidx.compose.ui.graphics.Color
6062
import androidx.compose.ui.res.stringResource
6163
import androidx.compose.ui.text.font.FontWeight
@@ -139,6 +141,12 @@ fun ConfigurationsTabCreateCategoryDialog(
139141

140142
var nameError by remember { mutableStateOf(false) }
141143

144+
val focusRequester = remember { FocusRequester() }
145+
146+
LaunchedEffect(Unit) {
147+
focusRequester.requestFocus()
148+
}
149+
142150
AlertDialog(
143151
onDismissRequest = onClose,
144152
title = {
@@ -157,6 +165,7 @@ fun ConfigurationsTabCreateCategoryDialog(
157165
) {
158166
Column(
159167
verticalArrangement = Arrangement.spacedBy(8.dp),
168+
modifier = Modifier.fillMaxWidth(),
160169
) {
161170
TextView(
162171
"Name",
@@ -176,11 +185,13 @@ fun ConfigurationsTabCreateCategoryDialog(
176185
placeholder = { Text(text = "Name") },
177186
isError = nameError,
178187
keyboardOptions = KeyboardOptions(capitalization = KeyboardCapitalization.Sentences),
188+
modifier = Modifier.fillMaxWidth().focusRequester(focusRequester),
179189
)
180190
}
181191

182192
Column(
183193
verticalArrangement = Arrangement.spacedBy(8.dp),
194+
modifier = Modifier.fillMaxWidth(),
184195
) {
185196
TextView(
186197
"Color",
@@ -204,6 +215,7 @@ fun ConfigurationsTabCreateCategoryDialog(
204215

205216
Column(
206217
verticalArrangement = Arrangement.spacedBy(8.dp),
218+
modifier = Modifier.fillMaxWidth(),
207219
) {
208220
TextView(
209221
"Emoji",

app/src/main/java/cloud/pablos/overload/ui/tabs/home/HomeTabTopAppBar.kt

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package cloud.pablos.overload.ui.tabs.home
22

3+
import android.os.Build
4+
import androidx.annotation.RequiresApi
35
import androidx.compose.material3.ExperimentalMaterial3Api
46
import androidx.compose.material3.MaterialTheme
57
import androidx.compose.material3.NavigationBarDefaults
@@ -9,11 +11,18 @@ import androidx.compose.material3.TopAppBarDefaults
911
import androidx.compose.runtime.Composable
1012
import androidx.compose.ui.res.stringResource
1113
import cloud.pablos.overload.R
14+
import cloud.pablos.overload.data.category.CategoryEvent
15+
import cloud.pablos.overload.data.category.CategoryState
16+
import cloud.pablos.overload.ui.views.ChangeCategoryButton
1217
import cloud.pablos.overload.ui.views.TextView
1318

19+
@RequiresApi(Build.VERSION_CODES.S)
1420
@OptIn(ExperimentalMaterial3Api::class)
1521
@Composable
16-
fun HomeTabTopAppBar() {
22+
fun HomeTabTopAppBar(
23+
categoryState: CategoryState,
24+
categoryEvent: (CategoryEvent) -> Unit,
25+
) {
1726
Surface(
1827
tonalElevation = NavigationBarDefaults.Elevation,
1928
color = MaterialTheme.colorScheme.background,
@@ -30,6 +39,9 @@ fun HomeTabTopAppBar() {
3039
containerColor = MaterialTheme.colorScheme.background,
3140
titleContentColor = MaterialTheme.colorScheme.onBackground,
3241
),
42+
actions = {
43+
ChangeCategoryButton(categoryState, categoryEvent)
44+
},
3345
)
3446
}
3547
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package cloud.pablos.overload.ui.views
2+
3+
import android.os.Build
4+
import androidx.annotation.RequiresApi
5+
import androidx.compose.foundation.layout.padding
6+
import androidx.compose.material3.Button
7+
import androidx.compose.material3.ButtonDefaults
8+
import androidx.compose.runtime.Composable
9+
import androidx.compose.runtime.mutableStateOf
10+
import androidx.compose.runtime.remember
11+
import androidx.compose.ui.Modifier
12+
import androidx.compose.ui.unit.dp
13+
import cloud.pablos.overload.data.Helpers
14+
import cloud.pablos.overload.data.category.CategoryEvent
15+
import cloud.pablos.overload.data.category.CategoryState
16+
import cloud.pablos.overload.ui.tabs.calendar.CalendarTabCategoryDialog
17+
18+
@RequiresApi(Build.VERSION_CODES.S)
19+
@Composable
20+
fun ChangeCategoryButton(
21+
categoryState: CategoryState,
22+
categoryEvent: (CategoryEvent) -> Unit,
23+
) {
24+
val categoryDialogState = remember { mutableStateOf(false) }
25+
26+
val categoriesCount = categoryState.categories.count()
27+
val selectedCategory = categoryState.categories.find { it.id == categoryState.selectedCategory }
28+
29+
if (categoriesCount > 1 && selectedCategory != null) {
30+
val backgroundColor = Helpers.decideBackground(categoryState)
31+
val foregroundColor = Helpers.decideForeground(backgroundColor)
32+
33+
Button(
34+
onClick = { categoryDialogState.value = true },
35+
modifier = Modifier.padding(horizontal = 8.dp),
36+
colors =
37+
ButtonDefaults.buttonColors(
38+
containerColor = backgroundColor,
39+
contentColor = foregroundColor,
40+
),
41+
) {
42+
TextView(selectedCategory.emoji)
43+
}
44+
if (categoryDialogState.value) {
45+
CalendarTabCategoryDialog(
46+
categoryState,
47+
categoryEvent,
48+
onClose = { categoryDialogState.value = false },
49+
)
50+
}
51+
}
52+
}

0 commit comments

Comments
 (0)