import 'package:collection/collection.dart'; import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:flutter_linkify/flutter_linkify.dart'; import '../../../api/marianumconnect/queries/get_capabilities/guardian_child.dart'; import '../../../api/marianumconnect/queries/parent_letters/parent_letter_models.dart'; import '../../../extensions/date_time.dart'; import '../../../notification/notification_tasks.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/capabilities/bloc/capabilities_cubit.dart'; import '../../../state/app/modules/parent_letters/bloc/parent_letter_bloc.dart'; import '../../../state/app/modules/parent_letters/bloc/parent_letter_state.dart'; import '../../../state/app/modules/parent_letters/bloc/parent_letters_bloc.dart'; import '../../../utils/url_opener.dart'; import 'parent_letter_form_policy.dart'; import 'widgets/parent_letter_attachments.dart'; import 'widgets/parent_letter_response_card.dart'; import 'widgets/parent_letter_thread.dart'; class ParentLetterView extends StatelessWidget { final String id; const ParentLetterView({required this.id, super.key}); @override Widget build( BuildContext context, ) => BlocModule>( create: (context) => ParentLetterBloc(id, inbox: context.read()), autoRebuild: true, onInitialisation: (_, _) => NotificationTasks.clearParentLetterNotification(id), child: (context, bloc, state) { final letter = state.data?.letter; return Scaffold( appBar: AppBar(title: const Text('Elternbrief')), body: Column( children: [ Expanded( child: LoadableStateConsumer( isReady: (state) => state.letter != null, child: (state, loading) => _LetterBody( letter: state.letter!, children: context.watch().state.children, bloc: bloc, ), ), ), if (letter != null && letter.content.thread.enabled) ParentLetterThreadInput( recipientName: letter.summary.sender.displayName, onSend: bloc.sendThreadMessage, ), ], ), ); }, ); } class _LetterBody extends StatelessWidget { final ParentLetterDetail letter; final List children; final ParentLetterBloc bloc; const _LetterBody({ required this.letter, required this.children, required this.bloc, }); String _childName(String childId) => children.firstWhereOrNull((child) => child.id == childId)?.displayName ?? 'dein Kind'; @override Widget build(BuildContext context) { final theme = Theme.of(context); final summary = letter.summary; final content = letter.content; final editedAt = summary.editedAt; return ListView( physics: const AlwaysScrollableScrollPhysics(), padding: const EdgeInsets.only(bottom: 16), children: [ Padding( padding: const EdgeInsets.fromLTRB(16, 16, 16, 0), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Text(summary.subject, style: theme.textTheme.titleLarge), const SizedBox(height: 4), Text( [ summary.sender.displayName, summary.sentAt.formatDateTime(), if (editedAt != null) 'bearbeitet am ${editedAt.formatDateTime()}', ].join(' ยท '), style: theme.textTheme.bodySmall, ), if (children.length > 1) Text( 'Betrifft: ${summary.childIds.map(_childName).join(', ')}', style: theme.textTheme.bodySmall, ), const SizedBox(height: 16), SelectableLinkify( text: content.body, onOpen: UrlOpener.onOpen, options: const LinkifyOptions(humanize: false), style: theme.textTheme.bodyLarge, ), ], ), ), if (content.attachments.isNotEmpty) ...[ const SizedBox(height: 8), ParentLetterAttachments( letterId: summary.id, attachments: content.attachments, load: bloc.loadAttachment, ), ], const SizedBox(height: 8), for (final child in content.children) if (ParentLetterFormPolicy.resolve( request: content.request, child: child, ) case final policy?) ParentLetterResponseCard( key: ValueKey((child.childId, child.response)), childName: _childName(child.childId), child: child, policy: policy, deadline: content.request?.deadline, onSubmit: (answers, signaturePng) => bloc.submitResponse( childId: child.childId, answers: answers, signaturePng: signaturePng, ), ), if (content.thread.messages.isNotEmpty) ...[ const Padding( padding: EdgeInsets.fromLTRB(16, 12, 16, 4), child: Text( 'Nachrichten', style: TextStyle(fontWeight: FontWeight.w600), ), ), ParentLetterThreadMessages(content.thread.messages), ], ], ); } }