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';
|
2019-06-15 14:25:06 +00:00
|
|
|
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;
|
2019-06-15 14:25:06 +00:00
|
|
|
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;
|
2019-06-15 06:27:20 +00:00
|
|
|
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,
|
2019-06-15 06:27:20 +00:00
|
|
|
this.userNamesClickable = true,
|
2019-06-10 12:47:27 +00:00
|
|
|
this.buildListView = true,
|
2019-06-15 14:25:06 +00:00
|
|
|
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),
|
2019-06-15 06:27:20 +00:00
|
|
|
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;
|
2019-06-15 14:25:06 +00:00
|
|
|
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);
|
|
|
|
|
2019-06-15 14:25:06 +00:00
|
|
|
@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
|
2019-06-15 14:25:06 +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;
|
|
|
|
|
2019-06-15 14:25:06 +00:00
|
|
|
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(() {
|
2019-06-15 14:25:06 +00:00
|
|
|
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 !"));
|
|
|
|
}
|
|
|
|
|
2019-06-15 14:09:49 +00:00
|
|
|
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,
|
2019-06-15 06:27:20 +00:00
|
|
|
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();
|
2019-06-15 14:09:49 +00:00
|
|
|
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));
|
2019-06-15 14:25:06 +00:00
|
|
|
|
|
|
|
void _reachedPostsBottom() {
|
|
|
|
if (widget.getOlder != null) _loadPostsList(getOlder: true);
|
|
|
|
}
|
2019-05-11 13:35:07 +00:00
|
|
|
}
|