Improved edit schedule

This commit is contained in:
Juan Gilsanz Polo 2024-01-26 21:18:04 +01:00
parent d73ad93180
commit 283d4e5c41
3 changed files with 80 additions and 21 deletions

View file

@ -187,9 +187,13 @@ class BlockedServicesSchedule {
Map<String, dynamic> toJson() => { Map<String, dynamic> toJson() => {
"time_zone": timeZone, "time_zone": timeZone,
"mon": mon?.toJson(),
"tue": tue?.toJson(), "tue": tue?.toJson(),
"wed": wed?.toJson(), "wed": wed?.toJson(),
"thu": thu?.toJson(), "thu": thu?.toJson(),
"fri": fri?.toJson(),
"sat": sat?.toJson(),
"sun": sun?.toJson(),
}; };
} }

View file

@ -7,6 +7,20 @@ import 'package:adguard_home_manager/widgets/section_label.dart';
import 'package:adguard_home_manager/models/clients.dart'; import 'package:adguard_home_manager/models/clients.dart';
class EditBlockingSchedule {
final String timezone;
final List<String> weekday;
final int start;
final int end;
const EditBlockingSchedule({
required this.timezone,
required this.weekday,
required this.start,
required this.end,
});
}
class BlockingSchedule extends StatelessWidget { class BlockingSchedule extends StatelessWidget {
final BlockedServicesSchedule blockedServicesSchedule; final BlockedServicesSchedule blockedServicesSchedule;
final void Function(BlockedServicesSchedule) setBlockedServicesSchedule; final void Function(BlockedServicesSchedule) setBlockedServicesSchedule;
@ -19,11 +33,38 @@ class BlockingSchedule extends StatelessWidget {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
void updateSchedule(EditBlockingSchedule v) {
final scheduleJson = blockedServicesSchedule.toJson();
for (var weekday in v.weekday) {
scheduleJson[weekday] = {
"start": v.start,
"end": v.end
};
}
scheduleJson["time_zone"] = v.timezone;
setBlockedServicesSchedule(BlockedServicesSchedule.fromJson(scheduleJson));
}
void openAddScheduleModal() { void openAddScheduleModal() {
showDialog( showDialog(
context: context, context: context,
builder: (context) => BlockingScheduleModal( builder: (context) => BlockingScheduleModal(
onConfirm: (v) => {}, onConfirm: updateSchedule,
),
);
}
void openEditScheduleModal(String weekday) {
showDialog(
context: context,
builder: (context) => BlockingScheduleModal(
schedule: EditBlockingSchedule(
timezone: blockedServicesSchedule.timeZone!,
weekday: [weekday],
start: blockedServicesSchedule.toJson()[weekday]['start'],
end: blockedServicesSchedule.toJson()[weekday]['end'],
),
onConfirm: updateSchedule,
), ),
); );
} }
@ -54,43 +95,43 @@ class BlockingSchedule extends StatelessWidget {
if (blockedServicesSchedule.mon != null) _ScheduleTile( if (blockedServicesSchedule.mon != null) _ScheduleTile(
weekday: AppLocalizations.of(context)!.monday, weekday: AppLocalizations.of(context)!.monday,
schedule: "${formatTime(blockedServicesSchedule.mon!.start!)} - ${formatTime(blockedServicesSchedule.mon!.end!)}", schedule: "${formatTime(blockedServicesSchedule.mon!.start!)} - ${formatTime(blockedServicesSchedule.mon!.end!)}",
onEdit: () => {}, onEdit: () => openEditScheduleModal("mon"),
onDelete: () => {} onDelete: () => {}
), ),
if (blockedServicesSchedule.tue != null) _ScheduleTile( if (blockedServicesSchedule.tue != null) _ScheduleTile(
weekday: AppLocalizations.of(context)!.tuesday, weekday: AppLocalizations.of(context)!.tuesday,
schedule: "${formatTime(blockedServicesSchedule.tue!.start!)} - ${formatTime(blockedServicesSchedule.tue!.end!)}", schedule: "${formatTime(blockedServicesSchedule.tue!.start!)} - ${formatTime(blockedServicesSchedule.tue!.end!)}",
onEdit: () => {}, onEdit: () => openEditScheduleModal("tue"),
onDelete: () => {} onDelete: () => {}
), ),
if (blockedServicesSchedule.wed != null) _ScheduleTile( if (blockedServicesSchedule.wed != null) _ScheduleTile(
weekday: AppLocalizations.of(context)!.wednesday, weekday: AppLocalizations.of(context)!.wednesday,
schedule: "${formatTime(blockedServicesSchedule.wed!.start!)} - ${formatTime(blockedServicesSchedule.wed!.end!)}", schedule: "${formatTime(blockedServicesSchedule.wed!.start!)} - ${formatTime(blockedServicesSchedule.wed!.end!)}",
onEdit: () => {}, onEdit: () => openEditScheduleModal("wed"),
onDelete: () => {} onDelete: () => {}
), ),
if (blockedServicesSchedule.thu != null) _ScheduleTile( if (blockedServicesSchedule.thu != null) _ScheduleTile(
weekday: AppLocalizations.of(context)!.thursday, weekday: AppLocalizations.of(context)!.thursday,
schedule: "${formatTime(blockedServicesSchedule.thu!.start!)} - ${formatTime(blockedServicesSchedule.thu!.end!)}", schedule: "${formatTime(blockedServicesSchedule.thu!.start!)} - ${formatTime(blockedServicesSchedule.thu!.end!)}",
onEdit: () => {}, onEdit: () => openEditScheduleModal("thu"),
onDelete: () => {} onDelete: () => {}
), ),
if (blockedServicesSchedule.fri != null) _ScheduleTile( if (blockedServicesSchedule.fri != null) _ScheduleTile(
weekday: AppLocalizations.of(context)!.friday, weekday: AppLocalizations.of(context)!.friday,
schedule: "${formatTime(blockedServicesSchedule.fri!.start!)} - ${formatTime(blockedServicesSchedule.fri!.end!)}", schedule: "${formatTime(blockedServicesSchedule.fri!.start!)} - ${formatTime(blockedServicesSchedule.fri!.end!)}",
onEdit: () => {}, onEdit: () => openEditScheduleModal("fri"),
onDelete: () => {} onDelete: () => {}
), ),
if (blockedServicesSchedule.sat != null) _ScheduleTile( if (blockedServicesSchedule.sat != null) _ScheduleTile(
weekday: AppLocalizations.of(context)!.saturday, weekday: AppLocalizations.of(context)!.saturday,
schedule: "${formatTime(blockedServicesSchedule.sat!.start!)} - ${formatTime(blockedServicesSchedule.sat!.end!)}", schedule: "${formatTime(blockedServicesSchedule.sat!.start!)} - ${formatTime(blockedServicesSchedule.sat!.end!)}",
onEdit: () => {}, onEdit: () => openEditScheduleModal("sat"),
onDelete: () => {} onDelete: () => {}
), ),
if (blockedServicesSchedule.sun != null) _ScheduleTile( if (blockedServicesSchedule.sun != null) _ScheduleTile(
weekday: AppLocalizations.of(context)!.sunday, weekday: AppLocalizations.of(context)!.sunday,
schedule: "${formatTime(blockedServicesSchedule.sun!.start!)} - ${formatTime(blockedServicesSchedule.sun!.end!)}", schedule: "${formatTime(blockedServicesSchedule.sun!.start!)} - ${formatTime(blockedServicesSchedule.sun!.end!)}",
onEdit: () => {}, onEdit: () => openEditScheduleModal("sun"),
onDelete: () => {} onDelete: () => {}
), ),
], ],

View file

@ -3,13 +3,15 @@ import 'package:timezone/timezone.dart' as tz;
import 'package:timezone/data/latest.dart' as tz; import 'package:timezone/data/latest.dart' as tz;
import 'package:flutter_gen/gen_l10n/app_localizations.dart'; import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:adguard_home_manager/models/clients.dart'; import 'package:adguard_home_manager/screens/clients/client/blocking_schedule.dart';
class BlockingScheduleModal extends StatefulWidget { class BlockingScheduleModal extends StatefulWidget {
final void Function(BlockedServicesSchedule) onConfirm; final EditBlockingSchedule? schedule;
final void Function(EditBlockingSchedule) onConfirm;
const BlockingScheduleModal({ const BlockingScheduleModal({
super.key, super.key,
this.schedule,
required this.onConfirm, required this.onConfirm,
}); });
@ -50,13 +52,26 @@ class _BlockingScheduleModalState extends State<BlockingScheduleModal> {
hours: timeOfDay.hour, hours: timeOfDay.hour,
minutes: timeOfDay.minute, minutes: timeOfDay.minute,
seconds: 0 seconds: 0
).inMinutes; ).inMilliseconds;
}
TimeOfDay _intToTimeOfDay(int value) {
final duration = Duration(milliseconds: value);
final minutes = duration.inMinutes - duration.inHours*60;
return TimeOfDay(hour: duration.inHours, minute: minutes);
} }
@override @override
void initState() { void initState() {
tz.initializeTimeZones(); tz.initializeTimeZones();
_timezone = tz.local.name; _timezone = tz.local.name;
if (widget.schedule != null) {
_timezone = widget.schedule!.timezone;
_weekdays = widget.schedule!.weekday;
_from = _intToTimeOfDay(widget.schedule!.start);
_to = _intToTimeOfDay(widget.schedule!.end);
}
super.initState(); super.initState();
} }
@ -73,17 +88,14 @@ class _BlockingScheduleModalState extends State<BlockingScheduleModal> {
void onConfirm() { void onConfirm() {
widget.onConfirm( widget.onConfirm(
BlockedServicesSchedule( EditBlockingSchedule(
timeZone: _timezone, timezone: _timezone!,
mon: _weekdays.contains("mon") ? BlockedServicesScheduleDay(start: _timeOfDayToInt(_from!), end: _timeOfDayToInt(_to!)) : null, weekday: _weekdays,
tue: _weekdays.contains("tue") ? BlockedServicesScheduleDay(start: _timeOfDayToInt(_from!), end: _timeOfDayToInt(_to!)) : null, start: _timeOfDayToInt(_from!),
wed: _weekdays.contains("wed") ? BlockedServicesScheduleDay(start: _timeOfDayToInt(_from!), end: _timeOfDayToInt(_to!)) : null, end: _timeOfDayToInt(_to!)
thu: _weekdays.contains("thu") ? BlockedServicesScheduleDay(start: _timeOfDayToInt(_from!), end: _timeOfDayToInt(_to!)) : null,
fri: _weekdays.contains("fri") ? BlockedServicesScheduleDay(start: _timeOfDayToInt(_from!), end: _timeOfDayToInt(_to!)) : null,
sat: _weekdays.contains("sat") ? BlockedServicesScheduleDay(start: _timeOfDayToInt(_from!), end: _timeOfDayToInt(_to!)) : null,
sun: _weekdays.contains("sun") ? BlockedServicesScheduleDay(start: _timeOfDayToInt(_from!), end: _timeOfDayToInt(_to!)) : null,
) )
); );
Navigator.pop(context);
} }
final valid = _validate(); final valid = _validate();
@ -109,7 +121,9 @@ class _BlockingScheduleModalState extends State<BlockingScheduleModal> {
), ),
const SizedBox(height: 16), const SizedBox(height: 16),
Text( Text(
AppLocalizations.of(context)!.newSchedule, widget.schedule != null
? AppLocalizations.of(context)!.editSchedule
: AppLocalizations.of(context)!.newSchedule,
textAlign: TextAlign.center, textAlign: TextAlign.center,
style: const TextStyle( style: const TextStyle(
fontSize: 24, fontSize: 24,