1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-11-22 12:59:21 +00:00

Show cached conversations list before getting list from the server

This commit is contained in:
Pierre HUBERT 2019-04-24 15:03:36 +02:00
parent eb34ed5c3d
commit 4be5a1b5a8
5 changed files with 79 additions and 16 deletions

View File

@ -43,6 +43,13 @@ class ConversationsHelper {
} }
} }
/// Get the local list of conversations
Future<ConversationsList> getCachedList() async {
final list = await _conversationsDatabaseHelper.getAll();
list.sort();
return list;
}
/// Get the name of a [conversation]. This requires information about the /// Get the name of a [conversation]. This requires information about the
/// users of this conversation /// users of this conversation
static String getConversationName( static String getConversationName(

View File

@ -1,5 +1,6 @@
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/conversations_list.dart';
import 'package:comunic/models/conversation.dart'; import 'package:comunic/models/conversation.dart';
/// Conversations database helper /// Conversations database helper
@ -17,4 +18,12 @@ class ConversationsDatabaseHelper extends ModelDatabaseHelper<Conversation> {
return ConversationTableContract.TABLE_NAME; return ConversationTableContract.TABLE_NAME;
} }
@override
Future<ConversationsList> getAll() async {
ConversationsList list = ConversationsList();
list.addAll(await super.getAll());
return list;
}
} }

View File

@ -36,8 +36,8 @@ abstract class ModelDatabaseHelper<T extends CacheModel> {
/// Returns null if none found /// Returns null if none found
Future<T> get(int id) async { Future<T> get(int id) async {
List<Map> maps = await (await DatabaseHelper.get()).query( List<Map> maps = await (await DatabaseHelper.get()).query(
UserTableContract.TABLE_NAME, tableName(),
where: '${UserTableContract.C_ID} = ?', where: '${BaseTableContract.C_ID} = ?',
whereArgs: [id], whereArgs: [id],
); );
@ -46,6 +46,12 @@ abstract class ModelDatabaseHelper<T extends CacheModel> {
return null; return null;
} }
/// Get all the entries from the table
Future<List<T>> getAll() async {
List<Map> maps = await(await DatabaseHelper.get()).query(tableName());
return maps.map((f) => initializeFromMap(f)).toList();
}
/// Empty the table /// Empty the table
Future<void> clearTable() async { Future<void> clearTable() async {
await (await DatabaseHelper.get()).execute("DELETE FROM ${tableName()}"); await (await DatabaseHelper.get()).execute("DELETE FROM ${tableName()}");

View File

@ -7,7 +7,7 @@ import 'package:meta/meta.dart';
/// ///
/// @author Pierre HUBERT /// @author Pierre HUBERT
class Conversation extends CacheModel { class Conversation extends CacheModel implements Comparable {
final int ownerID; final int ownerID;
final int lastActive; final int lastActive;
final String name; final String name;
@ -56,4 +56,9 @@ class Conversation extends CacheModel {
ConversationTableContract.C_MEMBERS: members.join(","), ConversationTableContract.C_MEMBERS: members.join(","),
}; };
} }
@override
int compareTo(other) {
return other.lastActive.compareTo(this.lastActive);
}
} }

View File

@ -33,23 +33,34 @@ class _ConversationScreenState extends State<ConversationsScreen> {
void setLoading(bool loading) => setState(() => _loading = loading); void setLoading(bool loading) => setState(() => _loading = loading);
/// Get the list of conversations, once from the cache, once from the server
Future<void> _loadConversations() async {
await _loadConversationsList(true);
await _loadConversationsList(false);
}
void _gotLoadingError() { void _gotLoadingError() {
setLoading(false); setLoading(false);
setError(_list == null ? LoadErrorLevel.MAJOR : LoadErrorLevel.MINOR); setError(_list == null ? LoadErrorLevel.MAJOR : LoadErrorLevel.MINOR);
} }
/// Load the list of conversations /// Load the list of conversations
Future<void> _loadConversations() async { Future<void> _loadConversationsList(bool cached) async {
setError(LoadErrorLevel.NONE); setError(LoadErrorLevel.NONE);
setLoading(true); setLoading(true);
//Process the list of conversations //Get the list of conversations
final list = await _conversationsHelper.downloadList(); var list;
if(cached)
list = await _conversationsHelper.getCachedList();
else
list = await _conversationsHelper.downloadList();
if (list == null) return _gotLoadingError(); if (list == null) return _gotLoadingError();
//Get information about the members of the conversations //Get information about the members of the conversations
list.users = await _usersHelper.getUsersInfo(list.allUsersID); list.users = await _usersHelper.getUsersInfo(list.allUsersID);
if(list.users == null) return _gotLoadingError(); if (list.users == null) return _gotLoadingError();
//Save list //Save list
setState(() { setState(() {
@ -65,7 +76,7 @@ class _ConversationScreenState extends State<ConversationsScreen> {
tr("Could not retrieve the list of conversations!"), tr("Could not retrieve the list of conversations!"),
actions: <Widget>[ actions: <Widget>[
FlatButton( FlatButton(
onPressed: _loadConversations, onPressed: () => _loadConversationsList(true),
child: Text( child: Text(
tr("Retry").toUpperCase(), tr("Retry").toUpperCase(),
style: TextStyle( style: TextStyle(
@ -83,7 +94,15 @@ class _ConversationScreenState extends State<ConversationsScreen> {
if (_list == null) return buildCenteredProgressBar(); if (_list == null) return buildCenteredProgressBar();
// Show the list of conversations // Show the list of conversations
return ListView.builder( return Stack(
children: <Widget>[
Column(
children: <Widget>[
Container(
child: _error == LoadErrorLevel.MINOR ? _buildErrorCard() : null,
),
Expanded(
child: ListView.builder(
itemBuilder: (context, index) { itemBuilder: (context, index) {
return ConversationTile( return ConversationTile(
conversation: _list.elementAt(index), conversation: _list.elementAt(index),
@ -91,6 +110,23 @@ class _ConversationScreenState extends State<ConversationsScreen> {
); );
}, },
itemCount: _list.length, itemCount: _list.length,
),
),
],
),
Positioned(
top: 8.0,
left: 0.0,
right: 0.0,
child: Container(
child: !_loading
? null
: Center(
child: CircularProgressIndicator(),
),
),
)
],
); );
} }
} }