mirror of
https://gitlab.com/comunic/comunicmobile
synced 2024-11-26 14:59:22 +00:00
Register to conversation updates
This commit is contained in:
parent
dab4e7bde1
commit
b0be889833
@ -1,6 +1,7 @@
|
|||||||
import 'package:comunic/helpers/database/conversation_messages_database_helper.dart';
|
import 'package:comunic/helpers/database/conversation_messages_database_helper.dart';
|
||||||
import 'package:comunic/helpers/database/conversations_database_helper.dart';
|
import 'package:comunic/helpers/database/conversations_database_helper.dart';
|
||||||
import 'package:comunic/helpers/users_helper.dart';
|
import 'package:comunic/helpers/users_helper.dart';
|
||||||
|
import 'package:comunic/helpers/websocket_helper.dart';
|
||||||
import 'package:comunic/lists/conversation_messages_list.dart';
|
import 'package:comunic/lists/conversation_messages_list.dart';
|
||||||
import 'package:comunic/lists/conversations_list.dart';
|
import 'package:comunic/lists/conversations_list.dart';
|
||||||
import 'package:comunic/lists/users_list.dart';
|
import 'package:comunic/lists/users_list.dart';
|
||||||
@ -20,6 +21,8 @@ import 'package:meta/meta.dart';
|
|||||||
enum SendMessageResult { SUCCESS, MESSAGE_REJECTED, FAILED }
|
enum SendMessageResult { SUCCESS, MESSAGE_REJECTED, FAILED }
|
||||||
|
|
||||||
class ConversationsHelper {
|
class ConversationsHelper {
|
||||||
|
static final _registeredConversations = Map<int, int>();
|
||||||
|
|
||||||
final ConversationsDatabaseHelper _conversationsDatabaseHelper =
|
final ConversationsDatabaseHelper _conversationsDatabaseHelper =
|
||||||
ConversationsDatabaseHelper();
|
ConversationsDatabaseHelper();
|
||||||
final ConversationMessagesDatabaseHelper _conversationMessagesDatabaseHelper =
|
final ConversationMessagesDatabaseHelper _conversationMessagesDatabaseHelper =
|
||||||
@ -356,6 +359,29 @@ class ConversationsHelper {
|
|||||||
return await _conversationMessagesDatabaseHelper.delete(id);
|
return await _conversationMessagesDatabaseHelper.delete(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Register a conversation : ask the server to notify about updates to the
|
||||||
|
/// conversation through WebSocket
|
||||||
|
Future<void> registerConversationEvents(int id) async {
|
||||||
|
if (_registeredConversations.containsKey(id))
|
||||||
|
_registeredConversations[id]++;
|
||||||
|
else {
|
||||||
|
_registeredConversations[id] = 1;
|
||||||
|
await ws("\$main/register_conv", {"convID": id});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Un-register to conversation update events
|
||||||
|
Future<void> unregisterConversationEvents(int id) async {
|
||||||
|
if (!_registeredConversations.containsKey(id)) return;
|
||||||
|
|
||||||
|
_registeredConversations[id]--;
|
||||||
|
|
||||||
|
if (_registeredConversations[id] <= 0) {
|
||||||
|
_registeredConversations.remove(id);
|
||||||
|
await ws("\$main/unregister_conv", {"convID": id});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Turn an API response into a ConversationMessage object
|
/// Turn an API response into a ConversationMessage object
|
||||||
ConversationMessage _apiToConversationMessage({
|
ConversationMessage _apiToConversationMessage({
|
||||||
@required int conversationID,
|
@required int conversationID,
|
||||||
|
@ -7,6 +7,7 @@ import 'package:comunic/lists/users_list.dart';
|
|||||||
import 'package:comunic/models/conversation_message.dart';
|
import 'package:comunic/models/conversation_message.dart';
|
||||||
import 'package:comunic/models/new_conversation_message.dart';
|
import 'package:comunic/models/new_conversation_message.dart';
|
||||||
import 'package:comunic/ui/tiles/conversation_message_tile.dart';
|
import 'package:comunic/ui/tiles/conversation_message_tile.dart';
|
||||||
|
import 'package:comunic/ui/widgets/safe_state.dart';
|
||||||
import 'package:comunic/ui/widgets/scroll_watcher.dart';
|
import 'package:comunic/ui/widgets/scroll_watcher.dart';
|
||||||
import 'package:comunic/utils/files_utils.dart';
|
import 'package:comunic/utils/files_utils.dart';
|
||||||
import 'package:comunic/utils/intl_utils.dart';
|
import 'package:comunic/utils/intl_utils.dart';
|
||||||
@ -32,7 +33,7 @@ class ConversationScreen extends StatefulWidget {
|
|||||||
State<StatefulWidget> createState() => _ConversationScreenState();
|
State<StatefulWidget> createState() => _ConversationScreenState();
|
||||||
}
|
}
|
||||||
|
|
||||||
class _ConversationScreenState extends State<ConversationScreen> {
|
class _ConversationScreenState extends SafeState<ConversationScreen> {
|
||||||
//Helpers
|
//Helpers
|
||||||
final ConversationsHelper _conversationsHelper = ConversationsHelper();
|
final ConversationsHelper _conversationsHelper = ConversationsHelper();
|
||||||
final UsersHelper _usersHelper = UsersHelper();
|
final UsersHelper _usersHelper = UsersHelper();
|
||||||
@ -44,27 +45,19 @@ class _ConversationScreenState extends State<ConversationScreen> {
|
|||||||
bool _isMessageValid = false;
|
bool _isMessageValid = false;
|
||||||
bool _isSendingMessage = false;
|
bool _isSendingMessage = false;
|
||||||
TextEditingController _textEditingController = TextEditingController();
|
TextEditingController _textEditingController = TextEditingController();
|
||||||
Timer _refreshTime;
|
|
||||||
ScrollWatcher _scrollController;
|
ScrollWatcher _scrollController;
|
||||||
_OlderMessagesLevel _loadingOlderMessages = _OlderMessagesLevel.NONE;
|
_OlderMessagesLevel _loadingOlderMessages = _OlderMessagesLevel.NONE;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
super.initState();
|
super.initState();
|
||||||
_scrollController = ScrollWatcher(onReachBottom: _loadOlderMessages);
|
_init();
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void didChangeDependencies() {
|
void dispose() {
|
||||||
super.didChangeDependencies();
|
super.dispose();
|
||||||
_initializeLoading();
|
_deallocate();
|
||||||
}
|
|
||||||
|
|
||||||
@override
|
|
||||||
void deactivate() {
|
|
||||||
super.deactivate();
|
|
||||||
|
|
||||||
if (_refreshTime != null) _refreshTime.cancel();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void _setError(ErrorLevel err) => setState(() => _error = err);
|
void _setError(ErrorLevel err) => setState(() => _error = err);
|
||||||
@ -81,14 +74,20 @@ class _ConversationScreenState extends State<ConversationScreen> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Load the first conversations
|
/// Load the first conversations
|
||||||
Future<void> _initializeLoading() async {
|
Future<void> _init() async {
|
||||||
|
_scrollController = ScrollWatcher(onReachBottom: _loadOlderMessages);
|
||||||
|
|
||||||
|
// Fetch latest messages
|
||||||
await _loadMessages(false);
|
await _loadMessages(false);
|
||||||
await _loadMessages(true);
|
await _loadMessages(true);
|
||||||
|
|
||||||
// Set a timer to regularly update conversation messages
|
await _conversationsHelper
|
||||||
if (_refreshTime == null || !_refreshTime.isActive)
|
.registerConversationEvents(widget.conversationID);
|
||||||
_refreshTime = Timer.periodic(
|
}
|
||||||
Duration(milliseconds: 1500), (t) => _loadMessages(true));
|
|
||||||
|
/// Free resources when this conversation widget is no longer required
|
||||||
|
void _deallocate() {
|
||||||
|
_conversationsHelper.unregisterConversationEvents(widget.conversationID);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Load a list of messages
|
/// Load a list of messages
|
||||||
|
Loading…
Reference in New Issue
Block a user