diff options
Diffstat (limited to 'lib/widgets/agenda_week_item.dart')
| -rw-r--r-- | lib/widgets/agenda_week_item.dart | 225 |
1 files changed, 225 insertions, 0 deletions
diff --git a/lib/widgets/agenda_week_item.dart b/lib/widgets/agenda_week_item.dart new file mode 100644 index 0000000..ba70083 --- /dev/null +++ b/lib/widgets/agenda_week_item.dart @@ -0,0 +1,225 @@ +import 'dart:async'; + +import 'package:flutter/material.dart'; +import 'package:flutter/services.dart'; +import 'package:flutter_local_notifications/flutter_local_notifications.dart'; +import 'package:training_planner/main.dart'; +import 'package:training_planner/shift.dart'; +import 'package:training_planner/utils/date.dart'; +import '../style/style.dart'; + +class AgendaWeekItem extends StatefulWidget { + final Shift shift; + const AgendaWeekItem({ + Key? key, + required this.shift, + }) : super(key: key); + + @override + _ExerciseEntryState createState() => _ExerciseEntryState(); +} + +class _ExerciseEntryState extends State<AgendaWeekItem> { + String shiftTypeName = ''; + String shiftTime = ''; + String shiftTimeEnd = ''; + bool canUseLocalAuth = false; + + final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = + FlutterLocalNotificationsPlugin(); + + Future<void> _showOngoingNotification() async { + if (widget.shift.isDone()) return; + + AndroidNotificationDetails androidPlatformChannelSpecifics = + AndroidNotificationDetails('channel_1', 'Actieve Sessie', + channelDescription: '3:45 => \$50', + importance: Importance.max, + priority: Priority.high, + ongoing: true, + icon: 'dhl', + showProgress: true, + onlyAlertOnce: true, + maxProgress: 100, + channelAction: AndroidNotificationChannelAction.update, + progress: widget.shift.getPercentage(), + color: Style.background, + autoCancel: false); + NotificationDetails platformChannelSpecifics = + NotificationDetails(android: androidPlatformChannelSpecifics); + + String elapsedTime = + "${widget.shift.getElapsedSessionTime().inHours.toString().padLeft(2, '0')}:${(widget.shift.getElapsedSessionTime().inMinutes % 60).toString().padLeft(2, '0')}"; + + await flutterLocalNotificationsPlugin.show( + 0, + 'Sessie actief', + '⏱ ' + + elapsedTime + + ' => €' + + widget.shift.getMoneyForActiveSession().toStringAsFixed(2), + platformChannelSpecifics); + } + + void initState() { + super.initState(); + + auth.canCheckBiometrics.then((bio) => { + auth + .isDeviceSupported() + .then((supported) => {canUseLocalAuth = bio && supported}) + }); + + setState(() { + switch (widget.shift.type) { + case ShiftType.Avondrit: + shiftTypeName = 'Avondrit'; + break; + case ShiftType.Dagrit: + shiftTypeName = 'Dagrit'; + break; + case ShiftType.Terugscannen: + shiftTypeName = 'Terugscannen'; + break; + } + + setStartAndEndTime(); + }); + } + + void setStartAndEndTime() { + shiftTime = + "${widget.shift.start.hour.toString().padLeft(2, '0')}:${widget.shift.start.minute.toString().padLeft(2, '0')}"; + + DateTime? expectedEndTime = widget.shift.expectedEndTime(); + if (widget.shift.isDone()) { + expectedEndTime = widget.shift.end; + } + + shiftTimeEnd = ' - ' + + "${expectedEndTime?.hour.toString().padLeft(2, '0')}:${expectedEndTime?.minute.toString().padLeft(2, '0')}"; + } + + Timer? updateNotificationTimer; + void showNotificationForActiveSession() { + _showOngoingNotification(); + updateNotificationTimer = Timer.periodic( + Duration(seconds: 10), (Timer t) => _showOngoingNotification()); + } + + void stopNotificationForActiveSession() { + updateNotificationTimer?.cancel(); + flutterLocalNotificationsPlugin.cancelAll(); + } + + @override + Widget build(BuildContext context) { + Widget startShiftWidget = Padding(padding: const EdgeInsets.all(0)); + TextStyle endDateTextStyle = TextStyle(color: Colors.black); + + if (!widget.shift.getIsActive() && widget.shift.canStart()) { + startShiftWidget = TextButton( + onPressed: () => { + setState(() { + showNotificationForActiveSession(); + widget.shift.setIsActive(true); + shiftProvider.updateShift(widget.shift); + setStartAndEndTime(); + }) + }, + child: Text('Begin')); + } else if (widget.shift.getIsActive()) { + startShiftWidget = TextButton( + onPressed: () { + auth + .authenticate( + localizedReason: 'Weet je zeker dat je wilt eindigen?') + .then((value) => { + if (value) + { + setState(() { + widget.shift.setIsActive(false); + shiftProvider.updateShift(widget.shift); + setStartAndEndTime(); + stopNotificationForActiveSession(); + }) + } + }) + .catchError((f) => {}); + }, + child: Text('Einde')); + } + + if (!widget.shift.isDone()) { + endDateTextStyle = TextStyle(color: Color.fromARGB(80, 0, 0, 0)); + } + + return Padding( + padding: const EdgeInsets.only(bottom: 8, left: 10, right: 10), + child: Container( + decoration: const BoxDecoration( + color: Style.listEntryBackground, + borderRadius: BorderRadius.all(Radius.circular(8))), + child: Padding( + padding: const EdgeInsets.all(0), + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Container( + width: double.infinity, + decoration: const BoxDecoration( + borderRadius: BorderRadius.all(Radius.circular(4))), + padding: EdgeInsets.all(0), + child: Padding( + padding: EdgeInsets.only(right: 5), + child: Row( + children: [ + Container( + decoration: BoxDecoration( + color: widget.shift.getStatusColor(), + borderRadius: BorderRadius.only( + topLeft: Radius.circular(8), + bottomLeft: Radius.circular(8))), + height: 48.0, + width: 32.0, + child: widget.shift.getStatusIcon(), + ), + Container( + padding: const EdgeInsets.only(left: 10), + child: Text( + DateHelper.getWeekdayName(widget.shift.start.weekday), + style: Style.listItemTitletextBold, + ), + width: 35, + ), + Container( + child: RichText( + text: TextSpan( + style: TextStyle(color: Colors.black), + children: [ + TextSpan(text: ' | ' + shiftTime), + TextSpan( + text: shiftTimeEnd, style: endDateTextStyle) + ], + ), + ), + width: 95, + ), + Container( + child: Text( + '| ' + shiftTypeName, + ), + width: 100, + ), + startShiftWidget, + ], + ), + ), + ), + ], + ), + ), + ), + ); + } +} |
