143 lines
5.1 KiB
Dart
143 lines
5.1 KiB
Dart
import 'package:firebase_messaging/firebase_messaging.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
|
import 'package:flutter_split_view/flutter_split_view.dart';
|
|
|
|
import '../../../state/app/infrastructure/loadableState/loadable_state.dart';
|
|
import '../../../state/app/infrastructure/loadableState/view/loadable_state_consumer.dart';
|
|
import '../../../state/app/infrastructure/utilityWidgets/bloc_module.dart';
|
|
import '../../../state/app/modules/chatList/bloc/chat_list_bloc.dart';
|
|
import '../../../state/app/modules/chatList/bloc/chat_list_state.dart';
|
|
import '../../../notification/notifyUpdater.dart';
|
|
import '../../../state/app/modules/settings/bloc/settings_cubit.dart';
|
|
import '../../../widget/confirmDialog.dart';
|
|
import 'components/chatTile.dart';
|
|
import 'components/splitViewPlaceholder.dart';
|
|
import 'joinChat.dart';
|
|
import 'searchChat.dart';
|
|
|
|
class ChatList extends StatelessWidget {
|
|
const ChatList({super.key});
|
|
|
|
@override
|
|
Widget build(BuildContext context) => BlocModule<ChatListBloc, LoadableState<ChatListState>>(
|
|
create: (_) => ChatListBloc(),
|
|
child: (context, bloc, _) => const _ChatListView(),
|
|
);
|
|
}
|
|
|
|
class _ChatListView extends StatefulWidget {
|
|
const _ChatListView();
|
|
|
|
@override
|
|
State<_ChatListView> createState() => _ChatListViewState();
|
|
}
|
|
|
|
class _ChatListViewState extends State<_ChatListView> {
|
|
late final SettingsCubit _settings;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_settings = context.read<SettingsCubit>();
|
|
|
|
WidgetsBinding.instance.addPostFrameCallback((_) => _maybeAskForNotificationPermission());
|
|
}
|
|
|
|
void _maybeAskForNotificationPermission() {
|
|
final notificationSettings = _settings.val().notificationSettings;
|
|
if (notificationSettings.enabled || notificationSettings.askUsageDismissed) return;
|
|
|
|
_settings.val(write: true).notificationSettings.askUsageDismissed = true;
|
|
ConfirmDialog(
|
|
icon: Icons.notifications_active_outlined,
|
|
title: 'Benachrichtigungen aktivieren',
|
|
content: 'Auf wunsch kannst du Push-Benachrichtigungen aktivieren. Deine Einstellungen kannst du jederzeit ändern.',
|
|
confirmButton: 'Weiter',
|
|
onConfirm: () {
|
|
FirebaseMessaging.instance.requestPermission(provisional: false).then((value) {
|
|
if (!mounted) return;
|
|
switch (value.authorizationStatus) {
|
|
case AuthorizationStatus.authorized:
|
|
NotifyUpdater.enableAfterDisclaimer(_settings).asDialog(context);
|
|
break;
|
|
case AuthorizationStatus.denied:
|
|
showDialog(
|
|
context: context,
|
|
builder: (_) => const AlertDialog(
|
|
content: Text('Du kannst die Benachrichtigungen später jederzeit in den App-Einstellungen aktivieren.'),
|
|
),
|
|
);
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
});
|
|
},
|
|
).asDialog(context);
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final bloc = context.read<ChatListBloc>();
|
|
return SplitView.material(
|
|
placeholder: const SplitViewPlaceholder(),
|
|
breakpoint: 1000,
|
|
child: Scaffold(
|
|
appBar: AppBar(
|
|
title: const Text('Talk'),
|
|
actions: [
|
|
IconButton(
|
|
icon: const Icon(Icons.search),
|
|
onPressed: () {
|
|
final rooms = bloc.state.data?.rooms;
|
|
if (rooms == null) return;
|
|
showSearch(context: context, delegate: SearchChat(rooms.data.toList()));
|
|
},
|
|
),
|
|
],
|
|
),
|
|
floatingActionButton: FloatingActionButton(
|
|
heroTag: 'createChat',
|
|
backgroundColor: Theme.of(context).primaryColor,
|
|
onPressed: () {
|
|
showSearch(context: context, delegate: JoinChat()).then((username) {
|
|
if (username == null || !context.mounted) return;
|
|
ConfirmDialog(
|
|
title: 'Chat starten',
|
|
content: "Möchtest du einen Chat mit Nutzer '$username' starten?",
|
|
confirmButton: 'Chat starten',
|
|
onConfirm: () {
|
|
bloc.createDirectChat(username);
|
|
},
|
|
).asDialog(context);
|
|
});
|
|
},
|
|
child: const Icon(Icons.add_comment_outlined),
|
|
),
|
|
body: LoadableStateConsumer<ChatListBloc, ChatListState>(
|
|
child: (state, _) {
|
|
final rooms = state.rooms;
|
|
if (rooms == null) return const SizedBox.shrink();
|
|
|
|
final talkSettings = context.watch<SettingsCubit>().val().talkSettings;
|
|
final sorted = rooms.sortBy(
|
|
lastActivity: true,
|
|
favoritesToTop: talkSettings.sortFavoritesToTop,
|
|
unreadToTop: talkSettings.sortUnreadToTop,
|
|
);
|
|
|
|
return ListView(
|
|
padding: EdgeInsets.zero,
|
|
children: sorted.map((room) {
|
|
final hasDraft = _settings.val().talkSettings.drafts.containsKey(room.token);
|
|
return ChatTile(data: room, hasDraft: hasDraft);
|
|
}).toList(),
|
|
);
|
|
},
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|