156 lines
6.4 KiB
Dart
156 lines
6.4 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:jiffy/jiffy.dart';
|
|
|
|
import '../../../state/app/infrastructure/loadable_state/loadable_state.dart';
|
|
import '../../../state/app/infrastructure/loadable_state/view/loadable_state_consumer.dart';
|
|
import '../../../state/app/infrastructure/utility_widgets/bloc_module.dart';
|
|
import '../../../state/app/modules/holidays/bloc/holidays_bloc.dart';
|
|
import '../../../state/app/modules/holidays/bloc/holidays_event.dart';
|
|
import '../../../state/app/modules/holidays/bloc/holidays_state.dart';
|
|
import '../../../widget/animated_time.dart';
|
|
import '../../../widget/centered_leading.dart';
|
|
import '../../../widget/debug/debug_tile.dart';
|
|
import '../../../widget/details_bottom_sheet.dart';
|
|
import '../../../widget/info_dialog.dart';
|
|
import '../../../widget/list_view_util.dart';
|
|
import '../../../widget/string_extensions.dart';
|
|
|
|
class HolidaysView extends StatelessWidget {
|
|
const HolidaysView({super.key});
|
|
|
|
@override
|
|
Widget build(
|
|
BuildContext context,
|
|
) => BlocModule<HolidaysBloc, LoadableState<HolidaysState>>(
|
|
create: (context) => HolidaysBloc(),
|
|
autoRebuild: true,
|
|
child: (context, bloc, state) {
|
|
void showDisclaimer() => InfoDialog.show(
|
|
context,
|
|
'Sämtliche Datumsangaben sind ohne Gewähr.\n'
|
|
'Ich übernehme weder Verantwortung für die Richtigkeit der Daten noch hafte ich für wirtschaftliche Schäden die aus der Verwendung dieser Daten entstehen können.\n\n'
|
|
'Die Daten stammen von https://ferien-api.de/',
|
|
title: 'Richtigkeit und Bereitstellung der Daten',
|
|
);
|
|
|
|
return Scaffold(
|
|
appBar: AppBar(
|
|
title: const Text('Schulferien in Hessen'),
|
|
actions: [
|
|
IconButton(
|
|
icon: const Icon(Icons.info_outline),
|
|
onPressed: showDisclaimer,
|
|
),
|
|
PopupMenuButton<bool>(
|
|
initialValue: bloc.showPastHolidays(),
|
|
icon: const Icon(Icons.history),
|
|
itemBuilder: (context) => [true, false]
|
|
.map(
|
|
(e) => PopupMenuItem<bool>(
|
|
value: e,
|
|
enabled: e != bloc.showPastHolidays(),
|
|
child: Row(
|
|
children: [
|
|
Icon(
|
|
e
|
|
? Icons.history_outlined
|
|
: Icons.history_toggle_off_outlined,
|
|
color: Theme.of(context).colorScheme.onSurface,
|
|
),
|
|
const SizedBox(width: 15),
|
|
Text(e ? 'Alle anzeigen' : 'Nur zukünftige anzeigen'),
|
|
],
|
|
),
|
|
),
|
|
)
|
|
.toList(),
|
|
onSelected: (e) => bloc.add(SetPastHolidaysVisible(e)),
|
|
),
|
|
],
|
|
),
|
|
body: LoadableStateConsumer<HolidaysBloc, HolidaysState>(
|
|
onLoad: (state) {
|
|
if (state.showDisclaimer) showDisclaimer();
|
|
bloc.add(DisclaimerDismissed());
|
|
},
|
|
child: (state, loading) => ListViewUtil.fromList<Holiday>(
|
|
bloc.getHolidays(),
|
|
(holiday) {
|
|
var holidayType = holiday.name.split(' ').first.capitalize();
|
|
String formatDate(String date) =>
|
|
Jiffy.parse(date).format(pattern: 'dd.MM.yyyy');
|
|
String getYear(String date, {String format = 'yyyy'}) =>
|
|
Jiffy.parse(date).format(pattern: format);
|
|
|
|
String getHolidayYear(String startDate, String endDate) =>
|
|
getYear(startDate) == getYear(endDate)
|
|
? getYear(startDate)
|
|
: '${getYear(startDate)}/${getYear(endDate, format: 'yy')}';
|
|
|
|
return ListTile(
|
|
leading: const CenteredLeading(Icon(Icons.calendar_month)),
|
|
title: Text(
|
|
'$holidayType ${getHolidayYear(holiday.start, holiday.end)}',
|
|
),
|
|
subtitle: Text(
|
|
'${formatDate(holiday.start)} - ${formatDate(holiday.end)}',
|
|
),
|
|
onTap: () => showDetailsBottomSheet(
|
|
context,
|
|
header: Padding(
|
|
padding: const EdgeInsets.fromLTRB(16, 4, 16, 12),
|
|
child: Text(
|
|
'$holidayType ${holiday.year} in Hessen',
|
|
style: Theme.of(context).textTheme.titleLarge,
|
|
),
|
|
),
|
|
children: (sheetCtx) => [
|
|
ListTile(
|
|
leading: const CenteredLeading(
|
|
Icon(Icons.signpost_outlined),
|
|
),
|
|
title: Text(holiday.name.capitalize()),
|
|
subtitle: Text(holiday.slug.capitalize()),
|
|
),
|
|
ListTile(
|
|
leading: const Icon(Icons.date_range_outlined),
|
|
title: Text('vom ${formatDate(holiday.start)}'),
|
|
),
|
|
ListTile(
|
|
leading: const Icon(Icons.date_range_outlined),
|
|
title: Text('bis zum ${formatDate(holiday.end)}'),
|
|
),
|
|
if (DateTime.parse(
|
|
holiday.start,
|
|
).difference(DateTime.now()).isNegative)
|
|
ListTile(
|
|
leading: const CenteredLeading(
|
|
Icon(Icons.content_paste_search_outlined),
|
|
),
|
|
title: Text(Jiffy.parse(holiday.start).fromNow()),
|
|
)
|
|
else
|
|
ListTile(
|
|
leading: const CenteredLeading(
|
|
Icon(Icons.timer_outlined),
|
|
),
|
|
title: AnimatedTime(
|
|
callback: () => DateTime.parse(
|
|
holiday.start,
|
|
).difference(DateTime.now()),
|
|
),
|
|
subtitle: Text(Jiffy.parse(holiday.start).fromNow()),
|
|
),
|
|
DebugTile(sheetCtx).jsonData(holiday.toJson()),
|
|
],
|
|
),
|
|
trailing: const Icon(Icons.arrow_right),
|
|
);
|
|
},
|
|
),
|
|
),
|
|
);
|
|
},
|
|
);
|
|
}
|