mirror of
				https://gitlab.com/comunic/comunicmobile
				synced 2025-11-03 19:54:12 +00:00 
			
		
		
		
	Show cached conversations list before getting list from the server
This commit is contained in:
		@@ -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
 | 
			
		||||
  /// users of this conversation
 | 
			
		||||
  static String getConversationName(
 | 
			
		||||
 
 | 
			
		||||
@@ -1,5 +1,6 @@
 | 
			
		||||
import 'package:comunic/helpers/database/database_contract.dart';
 | 
			
		||||
import 'package:comunic/helpers/database/model_database_helper.dart';
 | 
			
		||||
import 'package:comunic/lists/conversations_list.dart';
 | 
			
		||||
import 'package:comunic/models/conversation.dart';
 | 
			
		||||
 | 
			
		||||
/// Conversations database helper
 | 
			
		||||
@@ -17,4 +18,12 @@ class ConversationsDatabaseHelper extends ModelDatabaseHelper<Conversation> {
 | 
			
		||||
    return ConversationTableContract.TABLE_NAME;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  @override
 | 
			
		||||
  Future<ConversationsList> getAll() async {
 | 
			
		||||
    ConversationsList list = ConversationsList();
 | 
			
		||||
    list.addAll(await super.getAll());
 | 
			
		||||
    return list;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
@@ -36,8 +36,8 @@ abstract class ModelDatabaseHelper<T extends CacheModel> {
 | 
			
		||||
  /// Returns null if none found
 | 
			
		||||
  Future<T> get(int id) async {
 | 
			
		||||
    List<Map> maps = await (await DatabaseHelper.get()).query(
 | 
			
		||||
      UserTableContract.TABLE_NAME,
 | 
			
		||||
      where: '${UserTableContract.C_ID} = ?',
 | 
			
		||||
      tableName(),
 | 
			
		||||
      where: '${BaseTableContract.C_ID} = ?',
 | 
			
		||||
      whereArgs: [id],
 | 
			
		||||
    );
 | 
			
		||||
 | 
			
		||||
@@ -46,6 +46,12 @@ abstract class ModelDatabaseHelper<T extends CacheModel> {
 | 
			
		||||
    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
 | 
			
		||||
  Future<void> clearTable() async {
 | 
			
		||||
    await (await DatabaseHelper.get()).execute("DELETE FROM ${tableName()}");
 | 
			
		||||
 
 | 
			
		||||
@@ -7,7 +7,7 @@ import 'package:meta/meta.dart';
 | 
			
		||||
///
 | 
			
		||||
/// @author Pierre HUBERT
 | 
			
		||||
 | 
			
		||||
class Conversation extends CacheModel {
 | 
			
		||||
class Conversation extends CacheModel implements Comparable {
 | 
			
		||||
  final int ownerID;
 | 
			
		||||
  final int lastActive;
 | 
			
		||||
  final String name;
 | 
			
		||||
@@ -56,4 +56,9 @@ class Conversation extends CacheModel {
 | 
			
		||||
      ConversationTableContract.C_MEMBERS: members.join(","),
 | 
			
		||||
    };
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  @override
 | 
			
		||||
  int compareTo(other) {
 | 
			
		||||
    return other.lastActive.compareTo(this.lastActive);
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -33,23 +33,34 @@ class _ConversationScreenState extends State<ConversationsScreen> {
 | 
			
		||||
 | 
			
		||||
  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() {
 | 
			
		||||
    setLoading(false);
 | 
			
		||||
    setError(_list == null ? LoadErrorLevel.MAJOR : LoadErrorLevel.MINOR);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /// Load the list of conversations
 | 
			
		||||
  Future<void> _loadConversations() async {
 | 
			
		||||
  Future<void> _loadConversationsList(bool cached) async {
 | 
			
		||||
    setError(LoadErrorLevel.NONE);
 | 
			
		||||
    setLoading(true);
 | 
			
		||||
 | 
			
		||||
    //Process the list of conversations
 | 
			
		||||
    final list = await _conversationsHelper.downloadList();
 | 
			
		||||
    //Get the list of conversations
 | 
			
		||||
    var list;
 | 
			
		||||
    if(cached)
 | 
			
		||||
      list = await _conversationsHelper.getCachedList();
 | 
			
		||||
    else
 | 
			
		||||
      list = await _conversationsHelper.downloadList();
 | 
			
		||||
 | 
			
		||||
    if (list == null) return _gotLoadingError();
 | 
			
		||||
 | 
			
		||||
    //Get information about the members of the conversations
 | 
			
		||||
    list.users = await _usersHelper.getUsersInfo(list.allUsersID);
 | 
			
		||||
    if(list.users == null) return _gotLoadingError();
 | 
			
		||||
    if (list.users == null) return _gotLoadingError();
 | 
			
		||||
 | 
			
		||||
    //Save list
 | 
			
		||||
    setState(() {
 | 
			
		||||
@@ -65,7 +76,7 @@ class _ConversationScreenState extends State<ConversationsScreen> {
 | 
			
		||||
      tr("Could not retrieve the list of conversations!"),
 | 
			
		||||
      actions: <Widget>[
 | 
			
		||||
        FlatButton(
 | 
			
		||||
          onPressed: _loadConversations,
 | 
			
		||||
          onPressed: () => _loadConversationsList(true),
 | 
			
		||||
          child: Text(
 | 
			
		||||
            tr("Retry").toUpperCase(),
 | 
			
		||||
            style: TextStyle(
 | 
			
		||||
@@ -83,14 +94,39 @@ class _ConversationScreenState extends State<ConversationsScreen> {
 | 
			
		||||
    if (_list == null) return buildCenteredProgressBar();
 | 
			
		||||
 | 
			
		||||
    // Show the list of conversations
 | 
			
		||||
    return ListView.builder(
 | 
			
		||||
      itemBuilder: (context, index) {
 | 
			
		||||
        return ConversationTile(
 | 
			
		||||
          conversation: _list.elementAt(index),
 | 
			
		||||
          usersList: _list.users,
 | 
			
		||||
        );
 | 
			
		||||
      },
 | 
			
		||||
      itemCount: _list.length,
 | 
			
		||||
    return Stack(
 | 
			
		||||
      children: <Widget>[
 | 
			
		||||
        Column(
 | 
			
		||||
          children: <Widget>[
 | 
			
		||||
            Container(
 | 
			
		||||
              child: _error == LoadErrorLevel.MINOR ? _buildErrorCard() : null,
 | 
			
		||||
            ),
 | 
			
		||||
            Expanded(
 | 
			
		||||
              child: ListView.builder(
 | 
			
		||||
                itemBuilder: (context, index) {
 | 
			
		||||
                  return ConversationTile(
 | 
			
		||||
                    conversation: _list.elementAt(index),
 | 
			
		||||
                    usersList: _list.users,
 | 
			
		||||
                  );
 | 
			
		||||
                },
 | 
			
		||||
                itemCount: _list.length,
 | 
			
		||||
              ),
 | 
			
		||||
            ),
 | 
			
		||||
          ],
 | 
			
		||||
        ),
 | 
			
		||||
        Positioned(
 | 
			
		||||
          top: 8.0,
 | 
			
		||||
          left: 0.0,
 | 
			
		||||
          right: 0.0,
 | 
			
		||||
          child: Container(
 | 
			
		||||
            child: !_loading
 | 
			
		||||
                ? null
 | 
			
		||||
                : Center(
 | 
			
		||||
                    child: CircularProgressIndicator(),
 | 
			
		||||
                  ),
 | 
			
		||||
          ),
 | 
			
		||||
        )
 | 
			
		||||
      ],
 | 
			
		||||
    );
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user