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() => {
"time_zone": timeZone,
"mon": mon?.toJson(),
"tue": tue?.toJson(),
"wed": wed?.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';
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 {
final BlockedServicesSchedule blockedServicesSchedule;
final void Function(BlockedServicesSchedule) setBlockedServicesSchedule;
@ -19,11 +33,38 @@ class BlockingSchedule extends StatelessWidget {
@override
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() {
showDialog(
context: context,
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(
weekday: AppLocalizations.of(context)!.monday,
schedule: "${formatTime(blockedServicesSchedule.mon!.start!)} - ${formatTime(blockedServicesSchedule.mon!.end!)}",
onEdit: () => {},
onEdit: () => openEditScheduleModal("mon"),
onDelete: () => {}
),
if (blockedServicesSchedule.tue != null) _ScheduleTile(
weekday: AppLocalizations.of(context)!.tuesday,
schedule: "${formatTime(blockedServicesSchedule.tue!.start!)} - ${formatTime(blockedServicesSchedule.tue!.end!)}",
onEdit: () => {},
onEdit: () => openEditScheduleModal("tue"),
onDelete: () => {}
),
if (blockedServicesSchedule.wed != null) _ScheduleTile(
weekday: AppLocalizations.of(context)!.wednesday,
schedule: "${formatTime(blockedServicesSchedule.wed!.start!)} - ${formatTime(blockedServicesSchedule.wed!.end!)}",
onEdit: () => {},
onEdit: () => openEditScheduleModal("wed"),
onDelete: () => {}
),
if (blockedServicesSchedule.thu != null) _ScheduleTile(
weekday: AppLocalizations.of(context)!.thursday,
schedule: "${formatTime(blockedServicesSchedule.thu!.start!)} - ${formatTime(blockedServicesSchedule.thu!.end!)}",
onEdit: () => {},
onEdit: () => openEditScheduleModal("thu"),
onDelete: () => {}
),
if (blockedServicesSchedule.fri != null) _ScheduleTile(
weekday: AppLocalizations.of(context)!.friday,
schedule: "${formatTime(blockedServicesSchedule.fri!.start!)} - ${formatTime(blockedServicesSchedule.fri!.end!)}",
onEdit: () => {},
onEdit: () => openEditScheduleModal("fri"),
onDelete: () => {}
),
if (blockedServicesSchedule.sat != null) _ScheduleTile(
weekday: AppLocalizations.of(context)!.saturday,
schedule: "${formatTime(blockedServicesSchedule.sat!.start!)} - ${formatTime(blockedServicesSchedule.sat!.end!)}",
onEdit: () => {},
onEdit: () => openEditScheduleModal("sat"),
onDelete: () => {}
),
if (blockedServicesSchedule.sun != null) _ScheduleTile(
weekday: AppLocalizations.of(context)!.sunday,
schedule: "${formatTime(blockedServicesSchedule.sun!.start!)} - ${formatTime(blockedServicesSchedule.sun!.end!)}",
onEdit: () => {},
onEdit: () => openEditScheduleModal("sun"),
onDelete: () => {}
),
],

View file

@ -3,13 +3,15 @@ import 'package:timezone/timezone.dart' as tz;
import 'package:timezone/data/latest.dart' as tz;
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 {
final void Function(BlockedServicesSchedule) onConfirm;
final EditBlockingSchedule? schedule;
final void Function(EditBlockingSchedule) onConfirm;
const BlockingScheduleModal({
super.key,
this.schedule,
required this.onConfirm,
});
@ -50,13 +52,26 @@ class _BlockingScheduleModalState extends State<BlockingScheduleModal> {
hours: timeOfDay.hour,
minutes: timeOfDay.minute,
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
void initState() {
tz.initializeTimeZones();
_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();
}
@ -73,17 +88,14 @@ class _BlockingScheduleModalState extends State<BlockingScheduleModal> {
void onConfirm() {
widget.onConfirm(
BlockedServicesSchedule(
timeZone: _timezone,
mon: _weekdays.contains("mon") ? BlockedServicesScheduleDay(start: _timeOfDayToInt(_from!), end: _timeOfDayToInt(_to!)) : null,
tue: _weekdays.contains("tue") ? BlockedServicesScheduleDay(start: _timeOfDayToInt(_from!), end: _timeOfDayToInt(_to!)) : null,
wed: _weekdays.contains("wed") ? BlockedServicesScheduleDay(start: _timeOfDayToInt(_from!), end: _timeOfDayToInt(_to!)) : null,
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,
EditBlockingSchedule(
timezone: _timezone!,
weekday: _weekdays,
start: _timeOfDayToInt(_from!),
end: _timeOfDayToInt(_to!)
)
);
Navigator.pop(context);
}
final valid = _validate();
@ -109,7 +121,9 @@ class _BlockingScheduleModalState extends State<BlockingScheduleModal> {
),
const SizedBox(height: 16),
Text(
AppLocalizations.of(context)!.newSchedule,
widget.schedule != null
? AppLocalizations.of(context)!.editSchedule
: AppLocalizations.of(context)!.newSchedule,
textAlign: TextAlign.center,
style: const TextStyle(
fontSize: 24,