mirror of
https://gitlab.com/comunic/comunicmobile
synced 2024-11-22 21:09:21 +00:00
Use the conversation messages cache before going online
This commit is contained in:
parent
eaac6b1afa
commit
0579397ba7
@ -176,9 +176,16 @@ class ConversationsHelper {
|
|||||||
/// the conversation.
|
/// the conversation.
|
||||||
/// Otherwise [lastMessageID] contains the ID of the last known message
|
/// Otherwise [lastMessageID] contains the ID of the last known message
|
||||||
Future<ConversationMessagesList> getNewMessages(
|
Future<ConversationMessagesList> getNewMessages(
|
||||||
{@required int conversationID, int lastMessageID = 0}) async {
|
{@required int conversationID,
|
||||||
|
int lastMessageID = 0,
|
||||||
|
bool online = true}) async {
|
||||||
|
if (online)
|
||||||
return await _downloadNewMessagesSingle(conversationID,
|
return await _downloadNewMessagesSingle(conversationID,
|
||||||
lastMessageID: lastMessageID);
|
lastMessageID: lastMessageID);
|
||||||
|
else
|
||||||
|
return await _conversationMessagesDatabaseHelper
|
||||||
|
.getAllMessagesConversations(conversationID,
|
||||||
|
lastMessageID: lastMessageID);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Send a new message to the server
|
/// Send a new message to the server
|
||||||
|
@ -1,13 +1,14 @@
|
|||||||
import 'package:comunic/helpers/database/database_contract.dart';
|
import 'package:comunic/helpers/database/database_contract.dart';
|
||||||
import 'package:comunic/helpers/database/model_database_helper.dart';
|
import 'package:comunic/helpers/database/model_database_helper.dart';
|
||||||
|
import 'package:comunic/lists/conversation_messages_list.dart';
|
||||||
import 'package:comunic/models/conversation_message.dart';
|
import 'package:comunic/models/conversation_message.dart';
|
||||||
|
|
||||||
/// Conversation messages database helper
|
/// Conversation messages database helper
|
||||||
///
|
///
|
||||||
/// @author Pierre HUBERT
|
/// @author Pierre HUBERT
|
||||||
|
|
||||||
class ConversationMessagesDatabaseHelper extends ModelDatabaseHelper<ConversationMessage> {
|
class ConversationMessagesDatabaseHelper
|
||||||
|
extends ModelDatabaseHelper<ConversationMessage> {
|
||||||
@override
|
@override
|
||||||
ConversationMessage initializeFromMap(Map<String, dynamic> map) {
|
ConversationMessage initializeFromMap(Map<String, dynamic> map) {
|
||||||
return ConversationMessage.fromMap(map);
|
return ConversationMessage.fromMap(map);
|
||||||
@ -18,4 +19,19 @@ class ConversationMessagesDatabaseHelper extends ModelDatabaseHelper<Conversatio
|
|||||||
return ConversationsMessagesTableContract.TABLE_NAME;
|
return ConversationsMessagesTableContract.TABLE_NAME;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Get all the message cached for a given conversation
|
||||||
|
Future<ConversationMessagesList> getAllMessagesConversations(
|
||||||
|
int conversationID,
|
||||||
|
{int lastMessageID = 0}) async {
|
||||||
|
final list = await getMultiple(
|
||||||
|
where: "${ConversationsMessagesTableContract.C_CONVERSATION_ID} = ? "
|
||||||
|
"AND ${BaseTableContract.C_ID} > ?",
|
||||||
|
whereArgs: [conversationID, lastMessageID],
|
||||||
|
);
|
||||||
|
|
||||||
|
// Turn the list into a conversation messages list
|
||||||
|
ConversationMessagesList finalList = ConversationMessagesList();
|
||||||
|
finalList.addAll(list);
|
||||||
|
return finalList;
|
||||||
|
}
|
||||||
}
|
}
|
@ -48,7 +48,33 @@ abstract class ModelDatabaseHelper<T extends CacheModel> {
|
|||||||
|
|
||||||
/// Get all the entries from the table
|
/// Get all the entries from the table
|
||||||
Future<List<T>> getAll() async {
|
Future<List<T>> getAll() async {
|
||||||
List<Map> maps = await(await DatabaseHelper.get()).query(tableName());
|
List<Map> maps = await (await DatabaseHelper.get()).query(tableName());
|
||||||
|
return maps.map((f) => initializeFromMap(f)).toList();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Get some entries from the table based on some conditions
|
||||||
|
Future<List<T>> getMultiple(
|
||||||
|
{bool distinct,
|
||||||
|
List<String> columns,
|
||||||
|
String where,
|
||||||
|
List<dynamic> whereArgs,
|
||||||
|
String groupBy,
|
||||||
|
String having,
|
||||||
|
String orderBy,
|
||||||
|
int limit,
|
||||||
|
int offset}) async {
|
||||||
|
List<Map> maps = await (await DatabaseHelper.get()).query(
|
||||||
|
tableName(),
|
||||||
|
distinct: distinct,
|
||||||
|
columns: columns,
|
||||||
|
where: where,
|
||||||
|
whereArgs: whereArgs,
|
||||||
|
groupBy: groupBy,
|
||||||
|
having: having,
|
||||||
|
orderBy: orderBy,
|
||||||
|
limit: limit,
|
||||||
|
offset: offset,
|
||||||
|
);
|
||||||
return maps.map((f) => initializeFromMap(f)).toList();
|
return maps.map((f) => initializeFromMap(f)).toList();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -43,7 +43,7 @@ class _ConversationScreenState extends State<ConversationScreen> {
|
|||||||
@override
|
@override
|
||||||
void didChangeDependencies() {
|
void didChangeDependencies() {
|
||||||
super.didChangeDependencies();
|
super.didChangeDependencies();
|
||||||
_loadMessages();
|
_initializeLoading();
|
||||||
}
|
}
|
||||||
|
|
||||||
void _setError(ErrorLevel err) => setState(() => _error = err);
|
void _setError(ErrorLevel err) => setState(() => _error = err);
|
||||||
@ -55,15 +55,27 @@ class _ConversationScreenState extends State<ConversationScreen> {
|
|||||||
_setError(_messages == null ? ErrorLevel.MAJOR : ErrorLevel.MINOR);
|
_setError(_messages == null ? ErrorLevel.MAJOR : ErrorLevel.MINOR);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Load the first conversations
|
||||||
|
Future<void> _initializeLoading() async {
|
||||||
|
await _loadMessages(false);
|
||||||
|
await _loadMessages(true);
|
||||||
|
}
|
||||||
|
|
||||||
/// Load a list of messages
|
/// Load a list of messages
|
||||||
Future<void> _loadMessages() async {
|
Future<void> _loadMessages(bool online) async {
|
||||||
//First, get the messages
|
//First, get the messages
|
||||||
final messages = await _conversationsHelper.getNewMessages(
|
final messages = await _conversationsHelper.getNewMessages(
|
||||||
conversationID: widget.conversationID,
|
conversationID: widget.conversationID,
|
||||||
lastMessageID: _messages == null ? 0 : _messages.lastMessageID);
|
lastMessageID: _messages == null ? 0 : _messages.lastMessageID,
|
||||||
|
online: online);
|
||||||
|
|
||||||
if (messages == null) return _errorLoading();
|
if (messages == null) return _errorLoading();
|
||||||
|
|
||||||
|
// 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;
|
||||||
|
|
||||||
//Then get information about users
|
//Then get information about users
|
||||||
final usersToGet =
|
final usersToGet =
|
||||||
findMissingFromList(_usersInfo.usersID, messages.getUsersID());
|
findMissingFromList(_usersInfo.usersID, messages.getUsersID());
|
||||||
@ -254,6 +266,9 @@ class _ConversationScreenState extends State<ConversationScreen> {
|
|||||||
|
|
||||||
return Column(
|
return Column(
|
||||||
children: <Widget>[
|
children: <Widget>[
|
||||||
|
Container(
|
||||||
|
child: _error == ErrorLevel.MINOR ? _buildError() : null,
|
||||||
|
),
|
||||||
_buildMessagesList(),
|
_buildMessagesList(),
|
||||||
Divider(),
|
Divider(),
|
||||||
_buildSendMessageForm()
|
_buildSendMessageForm()
|
||||||
|
Loading…
Reference in New Issue
Block a user