Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sort planner by weekday relative to the current day #647

Merged
merged 3 commits into from
Feb 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions kitchenowl/lib/l10n/app_de.arb
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,8 @@
"@themeLight": {},
"@themeMode": {},
"@themeSystem": {},
"@today": {},
"@tomorrow": {},
"@total": {},
"@totalTime": {},
"@tutorialItemDescription1": {},
Expand Down Expand Up @@ -624,6 +626,8 @@
"themeLight": "Hell",
"themeMode": "Farbschema",
"themeSystem": "System",
"today": "Heute",
"tomorrow": "Morgen",
"total": "Gesamt",
"totalTime": "Gesamtzeit",
"tutorialItemDescription1": "Bei der Suche nach Artikeln kannst du mit einer Menge anfangen, die dann als Beschreibung hinzugefügt wird.",
Expand Down
4 changes: 4 additions & 0 deletions kitchenowl/lib/l10n/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,8 @@
"@themeLight": {},
"@themeMode": {},
"@themeSystem": {},
"@today": {},
"@tomorrow": {},
"@total": {},
"@totalTime": {},
"@tutorialItemDescription1": {},
Expand Down Expand Up @@ -626,6 +628,8 @@
"themeLight": "Light",
"themeMode": "Theme",
"themeSystem": "System",
"today": "Today",
"tomorrow": "Tomorrow",
"total": "Total",
"totalTime": "Total time",
"tutorialItemDescription1": "When searching for items, you can add an amount at the start of your query, and it will be added as a description.",
Expand Down
4 changes: 4 additions & 0 deletions kitchenowl/lib/l10n/app_es.arb
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,8 @@
"@themeLight": {},
"@themeMode": {},
"@themeSystem": {},
"@today": {},
"@tomorrow": {},
"@total": {},
"@totalTime": {},
"@tutorialItemDescription1": {},
Expand Down Expand Up @@ -624,6 +626,8 @@
"themeLight": "Claro",
"themeMode": "Tema",
"themeSystem": "Sistema",
"today": "Hoy",
"tomorrow": "Mañana",
"total": "Total",
"totalTime": "Tiempo total",
"tutorialItemDescription1": "Al buscar artículos, puede añadir una cantidad al principio de la consulta, y se añadirá como descripción.",
Expand Down
71 changes: 39 additions & 32 deletions kitchenowl/lib/pages/household_page/planner.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,31 @@ import 'package:kitchenowl/widgets/recipe_card.dart';
import 'package:responsive_builder/responsive_builder.dart';
import 'package:tuple/tuple.dart';

int db_weekday(int shift) {
// subtract 1 because DateTime.weekday goes from 1 to 7. Kitchenowl-db from 0 to 6
return DateTime.now().add(Duration(days: shift)).weekday - 1;
}

String formatDateAsWeekday(DateTime date, BuildContext context,
{String default_format = 'EEEE'}) {
DateTime today = DateTime.now();
DateTime tomorrow = today.add(Duration(days: 1));

// Check if the date is today or tomorrow
if (date.year == today.year &&
date.month == today.month &&
date.day == today.day) {
return AppLocalizations.of(context)!.today;
} else if (date.year == tomorrow.year &&
date.month == tomorrow.month &&
date.day == tomorrow.day) {
return AppLocalizations.of(context)!.tomorrow;
} else {
// Return the weekday name
return DateFormat(default_format).format(date);
}
}

class PlannerPage extends StatefulWidget {
const PlannerPage({super.key});

Expand All @@ -43,16 +68,6 @@ class _PlannerPageState extends State<PlannerPage> {
final cubit = BlocProvider.of<PlannerCubit>(context);
final household = BlocProvider.of<HouseholdCubit>(context).state.household;

final weekdayMapping = {
0: DateTime.monday,
1: DateTime.tuesday,
2: DateTime.wednesday,
3: DateTime.thursday,
4: DateTime.friday,
5: DateTime.saturday,
6: DateTime.sunday,
};

return SafeArea(
child: Scrollbar(
child: RefreshIndicator(
Expand Down Expand Up @@ -172,8 +187,9 @@ class _PlannerPageState extends State<PlannerPage> {
),
),
),
for (int day = 0; day < 7; day++)
for (final plan in state.getPlannedOfDay(day))
for (int i = 0; i < 7; i++)
for (final plan
in state.getPlannedOfDay(db_weekday(i)))
KitchenOwlFractionallySizedBox(
widthFactor: (1 /
DynamicStyling.itemCrossAxisCount(
Expand All @@ -188,12 +204,14 @@ class _PlannerPageState extends State<PlannerPage> {
crossAxisAlignment:
CrossAxisAlignment.stretch,
children: [
if (plan == state.getPlannedOfDay(day)[0])
if (plan ==
state.getPlannedOfDay(
db_weekday(i))[0])
Padding(
padding:
const EdgeInsets.only(top: 5),
child: Text(
'${DateFormat.E().dateSymbols.STANDALONEWEEKDAYS[weekdayMapping[day]! % 7]}:',
'${formatDateAsWeekday(DateTime.now().add(Duration(days: i)), context, default_format: 'E')}',
style: Theme.of(context)
.textTheme
.bodyLarge,
Expand All @@ -211,7 +229,7 @@ class _PlannerPageState extends State<PlannerPage> {
onPressed: () {
cubit.remove(
plan.recipe,
day,
db_weekday(i),
);
},
onLongPressed: () => _openRecipePage(
Expand Down Expand Up @@ -394,28 +412,17 @@ class _PlannerPageState extends State<PlannerPage> {
PlannerCubit cubit,
Recipe recipe,
) async {
final weekdayMapping = {
0: DateTime.monday,
1: DateTime.tuesday,
2: DateTime.wednesday,
3: DateTime.thursday,
4: DateTime.friday,
5: DateTime.saturday,
6: DateTime.sunday,
};
int? day = await showDialog<int>(
context: context,
builder: (context) => SelectDialog(
title: AppLocalizations.of(context)!.addRecipeToPlannerShort,
cancelText: AppLocalizations.of(context)!.cancel,
options: weekdayMapping.entries
.map(
(e) => SelectDialogOption(
e.key,
DateFormat.E().dateSymbols.STANDALONEWEEKDAYS[e.value % 7],
),
)
.toList(),
options: List.generate(7, (index) {
return SelectDialogOption(
db_weekday(index),
formatDateAsWeekday(
DateTime.now().add(Duration(days: index)), context));
}),
),
);
if (day != null) {
Expand Down
53 changes: 33 additions & 20 deletions kitchenowl/lib/pages/recipe_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,31 @@ import 'package:responsive_builder/responsive_builder.dart';
import 'package:sliver_tools/sliver_tools.dart';
import 'package:tuple/tuple.dart';

int db_weekday(int shift) {
// subtract 1 because DateTime.weekday goes from 1 to 7. Kitchenowl-db from 0 to 6
return DateTime.now().add(Duration(days: shift)).weekday - 1;
}

String formatDateAsWeekday(DateTime date, BuildContext context,
{String default_format = 'EEEE'}) {
DateTime today = DateTime.now();
DateTime tomorrow = today.add(Duration(days: 1));

// Check if the date is today or tomorrow
if (date.year == today.year &&
date.month == today.month &&
date.day == today.day) {
return AppLocalizations.of(context)!.today;
} else if (date.year == tomorrow.year &&
date.month == tomorrow.month &&
date.day == tomorrow.day) {
return AppLocalizations.of(context)!.tomorrow;
} else {
// Return the weekday name
return DateFormat(default_format).format(date);
}
}

class RecipePage extends StatefulWidget {
final Household? household;
final Recipe recipe;
Expand Down Expand Up @@ -339,33 +364,21 @@ class _RecipePageState extends State<RecipePage> {
LoadingElevatedButton(
child: const Icon(Icons.calendar_month_rounded),
onPressed: () async {
final weekdayMapping = {
0: DateTime.monday,
1: DateTime.tuesday,
2: DateTime.wednesday,
3: DateTime.thursday,
4: DateTime.friday,
5: DateTime.saturday,
6: DateTime.sunday,
};
int? day = await showDialog<int>(
context: context,
builder: (context) => SelectDialog(
title: AppLocalizations.of(context)!
.addRecipeToPlannerShort,
cancelText:
AppLocalizations.of(context)!.cancel,
options: weekdayMapping.entries
.map(
(e) => SelectDialogOption(
e.key,
DateFormat.E()
.dateSymbols
.STANDALONEWEEKDAYS[
e.value % 7],
),
)
.toList(),
options: List.generate(7, (index) {
return SelectDialogOption(
db_weekday(index),
formatDateAsWeekday(
DateTime.now()
.add(Duration(days: index)),
context));
}),
),
);
if (day != null) {
Expand Down
Loading