1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-11-29 08:16:28 +00:00
comunicmobile/lib/ui/widgets/status_widget.dart

60 lines
1.7 KiB
Dart
Raw Normal View History

2021-04-24 07:46:53 +00:00
import 'package:comunic/helpers/events_helper.dart';
import 'package:comunic/helpers/notifications_helper.dart';
import 'package:comunic/ui/widgets/safe_state.dart';
import 'package:comunic/utils/log_utils.dart';
import 'package:flutter/material.dart';
/// Status widget
///
/// Store for its children information about the number of unread conversations
/// & the number of unread conversations
///
/// @author Pierre Hubert
class StatusWidget extends StatefulWidget {
final Widget Function(BuildContext) child;
const StatusWidget({
Key? key,
required this.child,
2022-03-11 15:40:56 +00:00
}) : super(key: key);
2021-04-24 07:46:53 +00:00
@override
StatusWidgetState createState() => StatusWidgetState();
}
class StatusWidgetState extends SafeState<StatusWidget> {
int? unreadNotifications = 0;
int? unreadConversations = 0;
2021-04-24 07:46:53 +00:00
Future<void> init() async {
try {
final res = await NotificationsHelper().countUnread();
unreadNotifications = res.notifications;
unreadConversations = res.conversations;
2021-04-24 08:35:12 +00:00
EventsHelper.emit(NewNumberNotifsEvent(unreadNotifications));
EventsHelper.emit(NewNumberUnreadConversations(unreadConversations));
2021-04-24 07:46:53 +00:00
} catch (e, s) {
logError(e, s);
print("Failed to initialize StatusWidget!");
}
}
@override
void initState() {
super.initState();
2021-04-24 07:58:02 +00:00
init();
2021-04-24 07:46:53 +00:00
2021-04-24 08:35:12 +00:00
listen<NewNumberNotifsEvent>((e) => unreadNotifications = e.newNum);
listen<NewNumberUnreadConversations>((e) => unreadConversations = e.newNum);
2021-04-24 07:46:53 +00:00
}
/// Find an ancestor of this object
static StatusWidgetState? of(BuildContext c) =>
2021-04-24 07:46:53 +00:00
c.findAncestorStateOfType<StatusWidgetState>();
@override
2021-04-24 08:35:12 +00:00
Widget build(BuildContext context) => widget.child(context);
2021-04-24 07:46:53 +00:00
}