mirror of
https://gitlab.com/comunic/comunicmobile
synced 2024-12-26 04:48:51 +00:00
Register to post events
This commit is contained in:
parent
469e1e1f92
commit
02034acbbe
@ -4,6 +4,7 @@ import 'package:comunic/enums/post_visibility_level.dart';
|
||||
import 'package:comunic/enums/user_access_levels.dart';
|
||||
import 'package:comunic/helpers/comments_helper.dart';
|
||||
import 'package:comunic/helpers/survey_helper.dart';
|
||||
import 'package:comunic/helpers/websocket_helper.dart';
|
||||
import 'package:comunic/lists/comments_list.dart';
|
||||
import 'package:comunic/lists/posts_list.dart';
|
||||
import 'package:comunic/models/api_request.dart';
|
||||
@ -46,11 +47,17 @@ const _APIPostsTargetKindsMap = {
|
||||
};
|
||||
|
||||
class PostsHelper {
|
||||
/// Stores the list of posts we are registered to
|
||||
///
|
||||
/// First int = post ID
|
||||
/// Second int = number of registered people
|
||||
static final _registeredPosts = Map<int, int>();
|
||||
|
||||
/// Get the list of latest posts. Return the list of posts or null in case of
|
||||
/// failure
|
||||
Future<PostsList> getLatest({int from = 0}) async {
|
||||
final response =
|
||||
await APIRequest(uri: "posts/get_latest", needLogin: true, args: {
|
||||
await APIRequest(uri: "posts/get_latest", needLogin: true, args: {
|
||||
"include_groups": true.toString(),
|
||||
"startFrom": from.toString(),
|
||||
}).exec();
|
||||
@ -69,8 +76,7 @@ class PostsHelper {
|
||||
/// Get the list of posts of a user
|
||||
Future<PostsList> getUserPosts(int userID, {int from = 0}) async {
|
||||
final response = await (APIRequest(uri: "posts/get_user", needLogin: true)
|
||||
..addInt("userID", userID)
|
||||
..addInt("startFrom", from == 0 ? 0 : from - 1))
|
||||
..addInt("userID", userID)..addInt("startFrom", from == 0 ? 0 : from - 1))
|
||||
.exec();
|
||||
|
||||
if (response.code != 200) return null;
|
||||
@ -87,8 +93,7 @@ class PostsHelper {
|
||||
/// Get the list of posts of a group
|
||||
Future<PostsList> getGroupPosts(int groupID, {int from = 0}) async {
|
||||
final response = await (APIRequest(uri: "posts/get_group", needLogin: true)
|
||||
..addInt("groupID", groupID)
|
||||
..addInt("startFrom", from == 0 ? 0 : from - 1))
|
||||
..addInt("groupID", groupID)..addInt("startFrom", from == 0 ? 0 : from - 1))
|
||||
.exec();
|
||||
|
||||
if (response.code != 200) return null;
|
||||
@ -121,11 +126,11 @@ class PostsHelper {
|
||||
/// This function crash in case of error
|
||||
Future<void> createPost(NewPost post) async {
|
||||
APIRequest request =
|
||||
APIRequest(uri: "posts/create", needLogin: true, args: {
|
||||
APIRequest(uri: "posts/create", needLogin: true, args: {
|
||||
"kind-page": _APIPostsTargetKindsMap[post.target],
|
||||
"kind-id": post.targetID.toString(),
|
||||
"visibility": _APIPostsVisibilityLevelMap.map(
|
||||
(s, v) => MapEntry(v, s))[post.visibility],
|
||||
(s, v) => MapEntry(v, s))[post.visibility],
|
||||
"kind": _APIPostsKindsMap.map((s, k) => MapEntry(k, s))[post.kind],
|
||||
"content": post.content
|
||||
});
|
||||
@ -169,7 +174,7 @@ class PostsHelper {
|
||||
args: {
|
||||
"postID": id.toString(),
|
||||
"new_level":
|
||||
_APIPostsVisibilityLevelMap.map((k, v) => MapEntry(v, k))[level]
|
||||
_APIPostsVisibilityLevelMap.map((k, v) => MapEntry(v, k))[level]
|
||||
},
|
||||
).exec())
|
||||
.isOK;
|
||||
@ -185,6 +190,28 @@ class PostsHelper {
|
||||
.isOK;
|
||||
}
|
||||
|
||||
/// Register to a post events
|
||||
Future<void> registerPostEvents(int id) async {
|
||||
if (_registeredPosts.containsKey(id))
|
||||
_registeredPosts[id]++;
|
||||
else {
|
||||
_registeredPosts[id] = 1;
|
||||
await ws("\$main/register_post", {"postID": id});
|
||||
}
|
||||
}
|
||||
|
||||
/// Un-register to post events
|
||||
Future<void> unregisterPostEvents(int id) async {
|
||||
if (!_registeredPosts.containsKey(id)) return;
|
||||
|
||||
_registeredPosts[id]--;
|
||||
|
||||
if (_registeredPosts[id] <= 0) {
|
||||
_registeredPosts.remove(id);
|
||||
await ws("\$main/unregister_post", {"postID": id});
|
||||
}
|
||||
}
|
||||
|
||||
/// Turn an API entry into a [Post] object
|
||||
Post _apiToPost(Map<String, dynamic> map) {
|
||||
final postKind = _APIPostsKindsMap[map["kind"]];
|
||||
|
@ -591,7 +591,8 @@ class _PostTileState extends State<PostTile> {
|
||||
context: context,
|
||||
title: tr("Update post content"),
|
||||
message: tr("Please enter message content: "),
|
||||
defaultValue: widget.post.content.isNull == null ? "" : widget.post.content.content,
|
||||
defaultValue:
|
||||
widget.post.content.isNull == null ? "" : widget.post.content.content,
|
||||
hint: tr("Post content"),
|
||||
);
|
||||
|
||||
|
@ -1,4 +1,5 @@
|
||||
import 'package:comunic/helpers/groups_helper.dart';
|
||||
import 'package:comunic/helpers/posts_helper.dart';
|
||||
import 'package:comunic/helpers/users_helper.dart';
|
||||
import 'package:comunic/lists/groups_list.dart';
|
||||
import 'package:comunic/lists/posts_list.dart';
|
||||
@ -6,6 +7,7 @@ import 'package:comunic/lists/users_list.dart';
|
||||
import 'package:comunic/models/post.dart';
|
||||
import 'package:comunic/ui/screens/conversation_screen.dart';
|
||||
import 'package:comunic/ui/tiles/post_tile.dart';
|
||||
import 'package:comunic/ui/widgets/safe_state.dart';
|
||||
import 'package:comunic/ui/widgets/scroll_watcher.dart';
|
||||
import 'package:comunic/utils/intl_utils.dart';
|
||||
import 'package:comunic/utils/ui_utils.dart';
|
||||
@ -41,7 +43,7 @@ class PostsListWidget extends StatefulWidget {
|
||||
State<StatefulWidget> createState() => PostsListWidgetState();
|
||||
}
|
||||
|
||||
class PostsListWidgetState extends State<PostsListWidget> {
|
||||
class PostsListWidgetState extends SafeState<PostsListWidget> {
|
||||
// Helpers
|
||||
final UsersHelper _usersHelper = UsersHelper();
|
||||
final GroupsHelper _groupsHelper = GroupsHelper();
|
||||
@ -54,6 +56,8 @@ class PostsListWidgetState extends State<PostsListWidget> {
|
||||
ErrorLevel _error = ErrorLevel.NONE;
|
||||
bool _loading = false;
|
||||
|
||||
final _registeredPosts = Set<int>();
|
||||
|
||||
set error(ErrorLevel err) => setState(() => _error = err);
|
||||
|
||||
@override
|
||||
@ -63,6 +67,22 @@ class PostsListWidgetState extends State<PostsListWidget> {
|
||||
_scrollController = ScrollWatcher(onReachBottom: reachedPostsBottom);
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
super.dispose();
|
||||
|
||||
_unregisterAllPosts();
|
||||
}
|
||||
|
||||
@override
|
||||
void setState(fn) {
|
||||
if (!mounted) return;
|
||||
super.setState(fn);
|
||||
|
||||
// Register for posts update
|
||||
_registerRequiredPosts();
|
||||
}
|
||||
|
||||
@override
|
||||
void didChangeDependencies() {
|
||||
super.didChangeDependencies();
|
||||
@ -111,6 +131,37 @@ class PostsListWidgetState extends State<PostsListWidget> {
|
||||
_loading = false;
|
||||
}
|
||||
|
||||
/// 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");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Widget _buildErrorCard() {
|
||||
return buildErrorCard(tr("Could not get the list of posts !"));
|
||||
}
|
||||
|
@ -1,5 +1,4 @@
|
||||
import 'package:comunic/models/displayed_content.dart';
|
||||
import 'package:comunic/utils/bbcode_parser.dart';
|
||||
import 'package:comunic/utils/input_utils.dart';
|
||||
import 'package:comunic/utils/navigation_utils.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
@ -37,11 +36,11 @@ class TextWidget extends StatelessWidget {
|
||||
var content = this.content.parsedString;
|
||||
|
||||
// Parse BBcode
|
||||
if (parseBBcode)
|
||||
/*if (parseBBcode)
|
||||
return BBCodeParsedWidget(
|
||||
text: content,
|
||||
parseCallback: (style, text) => _parseLinks(context, text, style),
|
||||
);
|
||||
);*/
|
||||
|
||||
// Just parse link
|
||||
return RichText(
|
||||
|
Loading…
Reference in New Issue
Block a user