1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-12-29 06:18:51 +00:00
comunicmobile/lib/ui/widgets/posts_list_widget.dart

170 lines
4.3 KiB
Dart
Raw Normal View History

2019-06-10 07:47:02 +00:00
import 'package:comunic/helpers/groups_helper.dart';
2019-05-11 13:35:07 +00:00
import 'package:comunic/helpers/users_helper.dart';
2019-06-10 07:47:02 +00:00
import 'package:comunic/lists/groups_list.dart';
2019-05-11 13:35:07 +00:00
import 'package:comunic/lists/posts_list.dart';
import 'package:comunic/lists/users_list.dart';
2019-05-19 12:54:09 +00:00
import 'package:comunic/models/post.dart';
2019-05-11 13:35:07 +00:00
import 'package:comunic/ui/screens/conversation_screen.dart';
import 'package:comunic/ui/tiles/post_tile.dart';
import 'package:comunic/ui/widgets/scroll_watcher.dart';
2019-05-11 13:35:07 +00:00
import 'package:comunic/utils/intl_utils.dart';
import 'package:comunic/utils/ui_utils.dart';
import 'package:flutter/material.dart';
/// Posts list widget
///
/// Displays a list of posts
///
/// @author Pierre HUBERT
class PostsListWidget extends StatefulWidget {
final Future<PostsList> Function() getPostsList;
final Future<PostsList> Function(int from) getOlder;
2019-05-23 16:37:56 +00:00
final bool showPostsTarget;
2019-06-10 12:47:27 +00:00
final bool buildListView;
final bool userNamesClickable;
2019-05-11 13:35:07 +00:00
const PostsListWidget({
Key key,
@required this.getPostsList,
2019-05-23 16:37:56 +00:00
@required this.showPostsTarget,
this.userNamesClickable = true,
2019-06-10 12:47:27 +00:00
this.buildListView = true,
this.getOlder,
2019-05-11 13:35:07 +00:00
}) : assert(getPostsList != null),
2019-05-23 16:37:56 +00:00
assert(showPostsTarget != null),
2019-06-10 12:47:27 +00:00
assert(buildListView != null),
assert(userNamesClickable != null),
2019-05-11 13:35:07 +00:00
super(key: key);
@override
State<StatefulWidget> createState() => _PostsListWidgetState();
}
class _PostsListWidgetState extends State<PostsListWidget> {
// Helpers
final UsersHelper _usersHelper = UsersHelper();
2019-06-10 07:47:02 +00:00
final GroupsHelper _groupsHelper = GroupsHelper();
2019-05-11 13:35:07 +00:00
// Class members
PostsList _list;
UsersList _users;
2019-06-10 07:47:02 +00:00
GroupsList _groups;
ScrollWatcher _scrollController;
ErrorLevel _error = ErrorLevel.NONE;
2019-06-15 14:29:07 +00:00
bool _loading = false;
2019-05-11 13:35:07 +00:00
set error(ErrorLevel err) => setState(() => _error = err);
@override
void initState() {
super.initState();
_scrollController = ScrollWatcher(onReachBottom: _reachedPostsBottom);
}
2019-05-11 13:35:07 +00:00
@override
void didChangeDependencies() {
super.didChangeDependencies();
_loadPostsList();
}
2019-06-15 14:29:07 +00:00
void _loadError() {
error = _list == null ? ErrorLevel.MAJOR : ErrorLevel.MINOR;
_loading = false;
}
2019-05-11 13:35:07 +00:00
/// Load the list of posts
Future<void> _loadPostsList({bool getOlder = false}) async {
2019-11-02 11:46:17 +00:00
if (_loading) return;
2019-06-15 14:29:07 +00:00
_loading = true;
final list = !getOlder
? await widget.getPostsList()
: await widget.getOlder(_list.oldestID);
2019-05-11 13:35:07 +00:00
if (list == null) return _loadError();
final users = await _usersHelper.getList(list.usersID);
if (users == null) return _loadError();
2019-06-10 07:47:02 +00:00
final groups = await _groupsHelper.getList(list.groupsID);
2019-06-10 12:47:27 +00:00
if (groups == null) return _loadError();
2019-06-10 07:47:02 +00:00
2019-05-11 13:35:07 +00:00
setState(() {
if (!getOlder) {
_list = list;
_users = users;
_groups = groups;
} else {
_list.addAll(list);
_users.addAll(users);
_groups.addAll(groups);
}
2019-05-11 13:35:07 +00:00
});
2019-06-15 14:29:07 +00:00
_loading = false;
2019-05-11 13:35:07 +00:00
}
Widget _buildErrorCard() {
return buildErrorCard(tr("Could not get the list of posts !"));
}
Widget _buildNoPostNotice() {
return Center(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Text(tr("There is no post to display here yet.")),
),
);
}
2019-05-11 13:35:07 +00:00
Widget _buildListView() {
2019-06-15 14:26:42 +00:00
return RefreshIndicator(
child: ListView.builder(
itemCount: _list.length,
itemBuilder: _buildItem,
controller: _scrollController,
),
onRefresh: () => _loadPostsList(),
2019-06-10 12:47:27 +00:00
);
}
Widget _buildColumn() {
return Column(
children: List.generate(
_list.length,
(i) => _buildItem(null, i),
),
);
}
Widget _buildItem(BuildContext context, int index) {
return PostTile(
post: _list[index],
usersInfo: _users,
groupsInfo: _groups,
onDeletedPost: _removePost,
showPostTarget: widget.showPostsTarget,
userNamesClickable: widget.userNamesClickable,
2019-05-11 13:35:07 +00:00
);
}
@override
Widget build(BuildContext context) {
if (_error == ErrorLevel.MAJOR) return _buildErrorCard();
if (_list == null) return buildCenteredProgressBar();
if (_list.length == 0) return _buildNoPostNotice();
2019-06-10 12:47:27 +00:00
return widget.buildListView ? _buildListView() : _buildColumn();
2019-05-11 13:35:07 +00:00
}
2019-05-19 12:54:09 +00:00
void _removePost(Post post) => setState(() => _list.remove(post));
void _reachedPostsBottom() {
if (widget.getOlder != null) _loadPostsList(getOlder: true);
}
2019-05-11 13:35:07 +00:00
}