mirror of
https://gitlab.com/comunic/comunicmobile
synced 2025-06-18 15:58:04 +00:00
Store locally the list of conversations
This commit is contained in:
20
lib/helpers/database/conversations_database_helper.dart
Normal file
20
lib/helpers/database/conversations_database_helper.dart
Normal file
@ -0,0 +1,20 @@
|
||||
import 'package:comunic/helpers/database/database_contract.dart';
|
||||
import 'package:comunic/helpers/database/model_database_helper.dart';
|
||||
import 'package:comunic/models/conversation.dart';
|
||||
|
||||
/// Conversations database helper
|
||||
///
|
||||
/// @author Pierre HUBERT
|
||||
|
||||
class ConversationsDatabaseHelper extends ModelDatabaseHelper<Conversation> {
|
||||
@override
|
||||
Conversation initializeFromMap(Map<String, dynamic> map) {
|
||||
return Conversation.fromMap(map);
|
||||
}
|
||||
|
||||
@override
|
||||
String tableName() {
|
||||
return ConversationTableContract.TABLE_NAME;
|
||||
}
|
||||
|
||||
}
|
@ -22,4 +22,16 @@ abstract class UserTableContract {
|
||||
static const C_VISIBILITY = "visibility";
|
||||
static const C_VIRTUAL_DIRECTORY = "virtual_directory";
|
||||
static const C_ACCOUNT_IMAGE_URL = "account_image_url";
|
||||
}
|
||||
|
||||
/// Conversations table contract
|
||||
abstract class ConversationTableContract {
|
||||
static const TABLE_NAME = "conversations";
|
||||
static const C_ID = BaseTableContract.C_ID;
|
||||
static const C_OWNER_ID = "owner_id";
|
||||
static const C_LAST_ACTIVE = "last_active";
|
||||
static const C_NAME = "name";
|
||||
static const C_FOLLOWING = "following";
|
||||
static const C_SAW_LAST_MESSAGE = "saw_last_message";
|
||||
static const C_MEMBERS = "members";
|
||||
}
|
@ -34,7 +34,14 @@ abstract class DatabaseHelper {
|
||||
/// Currently : delete all the database tables and initialize it again
|
||||
static Future<void> _performDatabaseUpdate(
|
||||
Database db, int oldVersion, int newVersion) async {
|
||||
//Initialize database again
|
||||
// Drop users table
|
||||
await db.execute("DROP TABLE IF EXISTS ${UserTableContract.TABLE_NAME}");
|
||||
|
||||
// Drop conversations table
|
||||
await db
|
||||
.execute("DROP TABLE IF EXISTS ${ConversationTableContract.TABLE_NAME}");
|
||||
|
||||
// Initialize database again
|
||||
await _initializeDatabase(db, newVersion);
|
||||
}
|
||||
|
||||
@ -45,9 +52,20 @@ abstract class DatabaseHelper {
|
||||
"${UserTableContract.C_ID} INTEGER PRIMARY KEY, "
|
||||
"${UserTableContract.C_FIRST_NAME} TEXT, "
|
||||
"${UserTableContract.C_LAST_NAME} TEXT, "
|
||||
"${UserTableContract.C_VISIBILITY} TEXT,"
|
||||
"${UserTableContract.C_VIRTUAL_DIRECTORY} TEXT,"
|
||||
"${UserTableContract.C_VISIBILITY} TEXT, "
|
||||
"${UserTableContract.C_VIRTUAL_DIRECTORY} TEXT, "
|
||||
"${UserTableContract.C_ACCOUNT_IMAGE_URL} TEXT"
|
||||
")");
|
||||
|
||||
//Create conversations table
|
||||
await db.execute("CREATE TABLE ${ConversationTableContract.TABLE_NAME} ("
|
||||
"${ConversationTableContract.C_ID} INTEGER PRIMARY KEY, "
|
||||
"${ConversationTableContract.C_OWNER_ID} INTEGER, "
|
||||
"${ConversationTableContract.C_LAST_ACTIVE} INTEGER, "
|
||||
"${ConversationTableContract.C_NAME} TEXT, "
|
||||
"${ConversationTableContract.C_FOLLOWING} INTEGER, "
|
||||
"${ConversationTableContract.C_SAW_LAST_MESSAGE} INTEGER, "
|
||||
"${ConversationTableContract.C_MEMBERS} TEXT"
|
||||
")");
|
||||
}
|
||||
}
|
||||
|
@ -9,7 +9,6 @@ import 'package:comunic/models/cache_model.dart';
|
||||
/// @author Pierre HUBERT
|
||||
|
||||
abstract class ModelDatabaseHelper<T extends CacheModel> {
|
||||
|
||||
/// Get the name of the target table. Must be specified by the children
|
||||
/// of this class
|
||||
String tableName();
|
||||
@ -19,8 +18,7 @@ abstract class ModelDatabaseHelper<T extends CacheModel> {
|
||||
|
||||
/// Insert an entry in the database
|
||||
Future<void> _insertDB(T el) async {
|
||||
await (await DatabaseHelper.get())
|
||||
.insert(tableName(), el.toMap());
|
||||
await (await DatabaseHelper.get()).insert(tableName(), el.toMap());
|
||||
}
|
||||
|
||||
/// Update an element in the database
|
||||
@ -48,6 +46,11 @@ abstract class ModelDatabaseHelper<T extends CacheModel> {
|
||||
return null;
|
||||
}
|
||||
|
||||
/// Empty the table
|
||||
Future<void> clearTable() async {
|
||||
await (await DatabaseHelper.get()).execute("DELETE FROM ${tableName()}");
|
||||
}
|
||||
|
||||
/// Check out whether an element specified with its [id] is present
|
||||
/// or not in the local database
|
||||
Future<bool> has(int id) async {
|
||||
@ -65,8 +68,13 @@ abstract class ModelDatabaseHelper<T extends CacheModel> {
|
||||
|
||||
/// Insert or update an important amount of elements
|
||||
Future<void> insertOrUpdateAll(List<T> list) async {
|
||||
for(int i = 0; i < list.length; i++)
|
||||
await insertOrUpdate(list[i]);
|
||||
for (int i = 0; i < list.length; i++) await insertOrUpdate(list[i]);
|
||||
}
|
||||
|
||||
}
|
||||
/// Insert an important amount of elements
|
||||
///
|
||||
/// This might throw if there are conflicting IDs
|
||||
Future<void> insertAll(List<T> list) async {
|
||||
for (T el in list) await _insertDB(el);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user