1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-11-25 22:39:22 +00:00

Automatically refresh conversations

This commit is contained in:
Pierre HUBERT 2019-04-27 09:52:16 +02:00
parent 08669d2632
commit 0e8803c6a0

View File

@ -1,3 +1,5 @@
import 'dart:async';
import 'package:comunic/helpers/conversations_helper.dart';
import 'package:comunic/helpers/users_helper.dart';
import 'package:comunic/lists/conversation_messages_list.dart';
@ -39,6 +41,7 @@ class _ConversationScreenState extends State<ConversationScreen> {
bool _isMessageValid = false;
bool _isSendingMessage = false;
TextEditingController _textEditingController = TextEditingController();
Timer _refreshTime;
@override
void didChangeDependencies() {
@ -46,6 +49,12 @@ class _ConversationScreenState extends State<ConversationScreen> {
_initializeLoading();
}
@override
void deactivate() {
super.deactivate();
_refreshTime.cancel();
}
void _setError(ErrorLevel err) => setState(() => _error = err);
void _setSending(bool sending) => setState(() => _isSendingMessage = sending);
@ -59,6 +68,11 @@ class _ConversationScreenState extends State<ConversationScreen> {
Future<void> _initializeLoading() async {
await _loadMessages(false);
await _loadMessages(true);
// Set a timer to regularly update conversation messages
if (_refreshTime == null || !_refreshTime.isActive)
_refreshTime = Timer.periodic(
Duration(milliseconds: 1500), (t) => _loadMessages(true));
}
/// Load a list of messages
@ -73,8 +87,7 @@ class _ConversationScreenState extends State<ConversationScreen> {
// In case we are offline and we did not get any message we do not do
// anything (we wait for the online request)
if(messages.length == 0 && !online)
return;
if (messages.length == 0 && !online) return;
//Then get information about users
final usersToGet =
@ -92,12 +105,17 @@ class _ConversationScreenState extends State<ConversationScreen> {
else
_messages.addAll(messages);
//Reverse the order of the messages
_messages.sort();
final reverse = _messages.reversed;
_messages = ConversationMessagesList();
_messages.addAll(reverse);
//Reverse the order of the messages (if required)
if (messages.length > 0) {
_messages.sort();
final reverse = _messages.reversed;
_messages = ConversationMessagesList();
_messages.addAll(reverse);
}
});
// Remove previous errors
_setError(ErrorLevel.NONE);
}
/// Pick and send an image