import 'package:comunic/helpers/groups_helper.dart'; import 'package:comunic/helpers/notifications_helper.dart'; import 'package:comunic/helpers/users_helper.dart'; import 'package:comunic/lists/groups_list.dart'; import 'package:comunic/lists/notifications_list.dart'; import 'package:comunic/lists/users_list.dart'; import 'package:comunic/utils/intl_utils.dart'; import 'package:comunic/utils/ui_utils.dart'; import 'package:flutter/material.dart'; /// Notifications screen /// /// @author Pierre HUBERT enum _Status { LOADING, ERROR, NONE } class NotificationsScreen extends StatefulWidget { @override _NotificationsScreenState createState() => _NotificationsScreenState(); } class _NotificationsScreenState extends State { NotificationsList _list; UsersList _users; GroupsList _groups; _Status _status = _Status.LOADING; void setStatus(_Status s) => setState(() => _status = s); Future _loadList() async { setStatus(_Status.LOADING); try { final list = await NotificationsHelper().getUnread(); final users = await UsersHelper().getListWithThrow(list.usersIds); final groups = await GroupsHelper().getListOrThrow(list.groupsIds); setState(() { _list = list; _users = users; _groups = groups; }); setStatus(_Status.NONE); } on Exception catch (e) { print("Exception while getting the list of notifications!"); print(e); } on Error catch (e) { print("Error while getting the list of notifications!"); print(e.stackTrace); } } @override void didChangeDependencies() { super.didChangeDependencies(); _loadList(); } @override Widget build(BuildContext context) { // Loading status if (_list == null || _status == _Status.LOADING) return Center( child: CircularProgressIndicator(), ); return Column( children: [ Expanded( child: RefreshIndicator( child: SingleChildScrollView( physics: AlwaysScrollableScrollPhysics(), child: _buildBody(), ), onRefresh: _loadList), ) ], ); } /// Build body Widget _buildBody() { // In case of error if (_status == _Status.ERROR) return buildErrorCard(tr("Could not get the list of notifications!"), actions: [ MaterialButton( onPressed: () => _loadList(), child: Text(tr("Try again".toUpperCase())), ) ]); // When there is no notification if (_list.length == 0) return Padding( padding: const EdgeInsets.all(8.0), child: Center( child: Text(tr("You do not have any notification now.")), ), ); } }