1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-11-26 23:09:21 +00:00
comunicmobile/lib/ui/widgets/posts_list_widget.dart

276 lines
6.9 KiB
Dart
Raw Normal View History

2020-04-18 14:35:53 +00:00
import 'package:comunic/helpers/events_helper.dart';
2019-06-10 07:47:02 +00:00
import 'package:comunic/helpers/groups_helper.dart';
2020-04-18 14:07:56 +00:00
import 'package:comunic/helpers/posts_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';
2020-04-18 14:35:53 +00:00
import 'package:comunic/models/comment.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';
2020-04-18 14:07:56 +00:00
import 'package:comunic/ui/widgets/safe_state.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
2020-04-16 07:07:23 +00:00
State<StatefulWidget> createState() => PostsListWidgetState();
2019-05-11 13:35:07 +00:00
}
2020-04-18 14:07:56 +00:00
class PostsListWidgetState extends SafeState<PostsListWidget> {
2019-05-11 13:35:07 +00:00
// 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
2020-04-18 14:07:56 +00:00
final _registeredPosts = Set<int>();
2019-05-11 13:35:07 +00:00
set error(ErrorLevel err) => setState(() => _error = err);
@override
void initState() {
super.initState();
2020-04-16 07:07:23 +00:00
_scrollController = ScrollWatcher(onReachBottom: reachedPostsBottom);
2020-04-18 14:35:53 +00:00
// Register to events
this.listen<NewCommentEvent>((ev) => this._addComment(ev.comment));
2020-04-18 14:46:55 +00:00
this.listenChangeState<UpdatedCommentEvent>(
(ev) => this._updateComment(ev.comment));
2020-04-18 14:57:00 +00:00
this.listenChangeState<DeletedCommentEvent>(
(ev) => this._removeComment(ev.commentID));
}
2019-05-11 13:35:07 +00:00
2020-04-18 14:07:56 +00:00
@override
void dispose() {
super.dispose();
_unregisterAllPosts();
}
@override
void setState(fn) {
if (!mounted) return;
super.setState(fn);
// Register for posts update
_registerRequiredPosts();
}
2019-05-11 13:35:07 +00:00
@override
void didChangeDependencies() {
super.didChangeDependencies();
2020-04-17 08:37:02 +00:00
loadPostsList();
2019-05-11 13:35:07 +00:00
}
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
2020-04-17 08:37:02 +00:00
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-11-02 19:11:06 +00:00
if (!mounted) return;
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
}
2020-04-18 14:07:56 +00:00
/// Register for potential new posts
void _registerRequiredPosts() async {
if (_list == null) return;
final missing = _list
.where((f) => !_registeredPosts.contains(f.id))
.map((f) => f.id)
.toSet();
_registeredPosts.addAll(missing);
for (final postID in missing) {
await PostsHelper().registerPostEvents(postID);
}
}
/// Unregister from all posts
///
/// This method should be called only once
void _unregisterAllPosts() async {
for (final postID in _registeredPosts) {
// We put the try - catch here because we must absolutely unregister ALL
// POSTS event if one post fails to remove
try {
await PostsHelper().unregisterPostEvents(postID);
} catch (e, stack) {
print("Could not unregister post! $e $stack");
}
}
}
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,
),
2020-04-17 08:37:02 +00:00
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));
2020-04-16 07:07:23 +00:00
void reachedPostsBottom() {
2020-04-17 08:37:02 +00:00
if (widget.getOlder != null) loadPostsList(getOlder: true);
}
2020-04-18 14:35:53 +00:00
/// Add new comment
void _addComment(Comment c) async {
if (_list == null) return;
try {
final p = _list.singleWhere((p) => p.id == c.postID, orElse: () => null);
if (p == null) return;
if (!_users.hasUser(c.userID))
_users.add(await UsersHelper().getSingleWithThrow(c.userID));
setState(() {
p.comments.add(c);
});
} catch (e, stack) {
print("$e\n$stack");
}
}
2020-04-18 14:46:55 +00:00
/// Update a comment
void _updateComment(Comment c) {
if (_list == null) return;
try {
final p = _list.singleWhere((p) => p.id == c.postID, orElse: () => null);
if (p == null) return;
final index = p.comments.indexWhere((d) => d.id == c.id);
if (index > -1) p.comments[index] = c;
} catch (e, stack) {
print("$e\n$stack");
}
}
2020-04-18 14:57:00 +00:00
/// Remove a comment from the list
void _removeComment(int commentID) {
if (_list == null) return;
_list.forEach((p) => p.comments.removeWhere((c) => c.id == commentID));
}
2019-05-11 13:35:07 +00:00
}