1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-11-22 21:09:21 +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/conversations_helper.dart';
import 'package:comunic/helpers/users_helper.dart'; import 'package:comunic/helpers/users_helper.dart';
import 'package:comunic/lists/conversation_messages_list.dart'; import 'package:comunic/lists/conversation_messages_list.dart';
@ -39,6 +41,7 @@ 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;
@override @override
void didChangeDependencies() { void didChangeDependencies() {
@ -46,6 +49,12 @@ class _ConversationScreenState extends State<ConversationScreen> {
_initializeLoading(); _initializeLoading();
} }
@override
void deactivate() {
super.deactivate();
_refreshTime.cancel();
}
void _setError(ErrorLevel err) => setState(() => _error = err); void _setError(ErrorLevel err) => setState(() => _error = err);
void _setSending(bool sending) => setState(() => _isSendingMessage = sending); void _setSending(bool sending) => setState(() => _isSendingMessage = sending);
@ -59,6 +68,11 @@ class _ConversationScreenState extends State<ConversationScreen> {
Future<void> _initializeLoading() async { Future<void> _initializeLoading() async {
await _loadMessages(false); await _loadMessages(false);
await _loadMessages(true); 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 /// 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 // In case we are offline and we did not get any message we do not do
// anything (we wait for the online request) // anything (we wait for the online request)
if(messages.length == 0 && !online) if (messages.length == 0 && !online) return;
return;
//Then get information about users //Then get information about users
final usersToGet = final usersToGet =
@ -92,12 +105,17 @@ class _ConversationScreenState extends State<ConversationScreen> {
else else
_messages.addAll(messages); _messages.addAll(messages);
//Reverse the order of the messages //Reverse the order of the messages (if required)
if (messages.length > 0) {
_messages.sort(); _messages.sort();
final reverse = _messages.reversed; final reverse = _messages.reversed;
_messages = ConversationMessagesList(); _messages = ConversationMessagesList();
_messages.addAll(reverse); _messages.addAll(reverse);
}
}); });
// Remove previous errors
_setError(ErrorLevel.NONE);
} }
/// Pick and send an image /// Pick and send an image