mirror of
				https://gitlab.com/comunic/comunicmobile
				synced 2025-11-04 04:04:18 +00:00 
			
		
		
		
	Use Websocket to update number of unread notifications
This commit is contained in:
		@@ -1,3 +1,4 @@
 | 
			
		||||
import 'package:comunic/helpers/events_helper.dart';
 | 
			
		||||
import 'package:comunic/helpers/notifications_helper.dart';
 | 
			
		||||
import 'package:comunic/models/count_unread_notifications.dart';
 | 
			
		||||
import 'package:comunic/ui/widgets/safe_state.dart';
 | 
			
		||||
@@ -113,8 +114,7 @@ class ComunicAppBar extends StatefulWidget implements PreferredSizeWidget {
 | 
			
		||||
  final OnSelectMenuAction onTap;
 | 
			
		||||
  final BarCallbackActions selectedAction;
 | 
			
		||||
 | 
			
		||||
  const ComunicAppBar(
 | 
			
		||||
      {Key key, @required this.onTap, @required this.selectedAction})
 | 
			
		||||
  const ComunicAppBar({Key key, @required this.onTap, @required this.selectedAction})
 | 
			
		||||
      : assert(onTap != null),
 | 
			
		||||
        super(key: key);
 | 
			
		||||
 | 
			
		||||
@@ -126,12 +126,17 @@ class ComunicAppBar extends StatefulWidget implements PreferredSizeWidget {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
class _ComunicAppBarState extends SafeState<ComunicAppBar> {
 | 
			
		||||
  CountUnreadNotifications _unreadNotifications;
 | 
			
		||||
  var _unreadNotifications =
 | 
			
		||||
  CountUnreadNotifications(notifications: 0, conversations: 0);
 | 
			
		||||
 | 
			
		||||
  @override
 | 
			
		||||
  void initState() {
 | 
			
		||||
    _refreshCountUnread();
 | 
			
		||||
    super.initState();
 | 
			
		||||
 | 
			
		||||
    // Listen to notifications number update
 | 
			
		||||
    this.listenChangeState<NewNumberNotifsEvent>(
 | 
			
		||||
            (d) => _unreadNotifications.notifications = d.newNum);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  void _refreshCountUnread() async {
 | 
			
		||||
@@ -171,7 +176,7 @@ class _ComunicAppBarState extends SafeState<ComunicAppBar> {
 | 
			
		||||
        crossAxisAlignment: CrossAxisAlignment.stretch,
 | 
			
		||||
        children: List.generate(
 | 
			
		||||
          _menuItems.length,
 | 
			
		||||
          (i) => _MenuItemWidget(
 | 
			
		||||
              (i) => _MenuItemWidget(
 | 
			
		||||
            item: _menuItems[i],
 | 
			
		||||
            onTap: widget.onTap,
 | 
			
		||||
            isSelected: _menuItems[i].action == widget.selectedAction,
 | 
			
		||||
@@ -212,9 +217,9 @@ class _MenuItemWidget extends StatelessWidget {
 | 
			
		||||
        color: isSelected ? _secondaryColor() : _primaryColor(),
 | 
			
		||||
        child: !item.isMenu
 | 
			
		||||
            ? InkWell(
 | 
			
		||||
                child: _buildIconContainer(),
 | 
			
		||||
                onTap: () => onTap(item.action),
 | 
			
		||||
              )
 | 
			
		||||
          child: _buildIconContainer(),
 | 
			
		||||
          onTap: () => onTap(item.action),
 | 
			
		||||
        )
 | 
			
		||||
            : _buildContextMenuPopupButton(),
 | 
			
		||||
      ),
 | 
			
		||||
    );
 | 
			
		||||
@@ -235,15 +240,15 @@ class _MenuItemWidget extends StatelessWidget {
 | 
			
		||||
        newNotice == 0
 | 
			
		||||
            ? Container()
 | 
			
		||||
            : Material(
 | 
			
		||||
                color: Colors.red,
 | 
			
		||||
                child: Padding(
 | 
			
		||||
                  padding: const EdgeInsets.all(2.0),
 | 
			
		||||
                  child: Text(" $newNotice ",
 | 
			
		||||
                      style: TextStyle(color: Colors.white)),
 | 
			
		||||
                ),
 | 
			
		||||
                borderRadius: BorderRadius.all(
 | 
			
		||||
                  Radius.circular(50.0),
 | 
			
		||||
                )),
 | 
			
		||||
            color: Colors.red,
 | 
			
		||||
            child: Padding(
 | 
			
		||||
              padding: const EdgeInsets.all(2.0),
 | 
			
		||||
              child: Text(" $newNotice ",
 | 
			
		||||
                  style: TextStyle(color: Colors.white)),
 | 
			
		||||
            ),
 | 
			
		||||
            borderRadius: BorderRadius.all(
 | 
			
		||||
              Radius.circular(50.0),
 | 
			
		||||
            )),
 | 
			
		||||
        Spacer(flex: 2),
 | 
			
		||||
      ],
 | 
			
		||||
    );
 | 
			
		||||
@@ -255,9 +260,9 @@ class _MenuItemWidget extends StatelessWidget {
 | 
			
		||||
      child: _buildIconContainer(),
 | 
			
		||||
      itemBuilder: (i) => _menuActionsItem
 | 
			
		||||
          .map((f) => PopupMenuItem(
 | 
			
		||||
                child: Text(f.label),
 | 
			
		||||
                value: f.action,
 | 
			
		||||
              ))
 | 
			
		||||
        child: Text(f.label),
 | 
			
		||||
        value: f.action,
 | 
			
		||||
      ))
 | 
			
		||||
          .toList(),
 | 
			
		||||
      onSelected: onTap,
 | 
			
		||||
    );
 | 
			
		||||
 
 | 
			
		||||
@@ -8,7 +8,6 @@ import 'package:flutter/material.dart';
 | 
			
		||||
/// @author Pierre HUBERT
 | 
			
		||||
 | 
			
		||||
abstract class SafeState<T extends StatefulWidget> extends State<T> {
 | 
			
		||||
 | 
			
		||||
  final _subscriptions = List<StreamSubscription>();
 | 
			
		||||
 | 
			
		||||
  @override
 | 
			
		||||
@@ -21,8 +20,7 @@ abstract class SafeState<T extends StatefulWidget> extends State<T> {
 | 
			
		||||
 | 
			
		||||
  @override
 | 
			
		||||
  void setState(fn) {
 | 
			
		||||
    if(mounted)
 | 
			
		||||
      super.setState(fn);
 | 
			
		||||
    if (mounted) super.setState(fn);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /// Register to a new subscription
 | 
			
		||||
@@ -30,4 +28,14 @@ abstract class SafeState<T extends StatefulWidget> extends State<T> {
 | 
			
		||||
  void listen<T>(void onEvent(T event)) {
 | 
			
		||||
    _subscriptions.add(EventsHelper.on<T>(onEvent));
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
  /// Register to a new subscription
 | 
			
		||||
  ///
 | 
			
		||||
  /// Callback will we called inside of setState
 | 
			
		||||
  @protected
 | 
			
		||||
  void listenChangeState<T>(void onEvent(T event)) {
 | 
			
		||||
    _subscriptions.add(EventsHelper.on<T>((d) {
 | 
			
		||||
      setState(() => onEvent(d));
 | 
			
		||||
    }));
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user