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,
|
|
|
|
}) : assert(child != null),
|
|
|
|
super(key: key);
|
|
|
|
|
|
|
|
@override
|
|
|
|
StatusWidgetState createState() => StatusWidgetState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class StatusWidgetState extends SafeState<StatusWidget> {
|
|
|
|
int unreadNotifications = 0;
|
|
|
|
int unreadConversations = 0;
|
|
|
|
|
|
|
|
Future<void> init() async {
|
|
|
|
try {
|
|
|
|
final res = await NotificationsHelper().countUnread();
|
|
|
|
unreadNotifications = res.notifications;
|
|
|
|
unreadConversations = res.conversations;
|
|
|
|
|
|
|
|
setState(() {});
|
|
|
|
} 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
|
|
|
|
|
|
|
listenChangeState<NewNumberNotifsEvent>(
|
|
|
|
(e) => unreadNotifications = e.newNum);
|
|
|
|
|
|
|
|
listenChangeState<NewNumberUnreadConversations>(
|
|
|
|
(e) => unreadConversations = e.newNum);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Find an ancestor of this object
|
|
|
|
static StatusWidgetState of(BuildContext c) =>
|
|
|
|
c.findAncestorStateOfType<StatusWidgetState>();
|
|
|
|
|
|
|
|
@override
|
2021-04-24 07:58:02 +00:00
|
|
|
Widget build(BuildContext context) => Container(
|
|
|
|
key: Key("$unreadNotifications-$unreadConversations"),
|
|
|
|
child: widget.child(context),
|
|
|
|
);
|
2021-04-24 07:46:53 +00:00
|
|
|
}
|