From 7f41f0dae1a02d85c395afb1b9afbf01273f7bd1 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Wed, 24 Apr 2019 13:56:56 +0200 Subject: [PATCH] Make it easy to cache new data --- lib/helpers/database/database_contract.dart | 7 +- .../database/model_database_helper.dart | 72 +++++++++++++++++++ .../database/users_database_helper.dart | 58 +++------------ lib/models/cache_model.dart | 19 +++++ lib/models/user.dart | 17 +++-- 5 files changed, 117 insertions(+), 56 deletions(-) create mode 100644 lib/helpers/database/model_database_helper.dart create mode 100644 lib/models/cache_model.dart diff --git a/lib/helpers/database/database_contract.dart b/lib/helpers/database/database_contract.dart index a408826..4885bd9 100644 --- a/lib/helpers/database/database_contract.dart +++ b/lib/helpers/database/database_contract.dart @@ -8,10 +8,15 @@ class DatabaseContract { static const DATABASE_FILE_NAME = "database.sqlite"; } +/// Base table contract +abstract class BaseTableContract { + static const C_ID = "id"; +} + /// User table contract abstract class UserTableContract { static const TABLE_NAME = "users"; - static const C_ID = "id"; + static const C_ID = BaseTableContract.C_ID; static const C_FIRST_NAME = "first_name"; static const C_LAST_NAME = "last_name"; static const C_VISIBILITY = "visibility"; diff --git a/lib/helpers/database/model_database_helper.dart b/lib/helpers/database/model_database_helper.dart new file mode 100644 index 0000000..547e10c --- /dev/null +++ b/lib/helpers/database/model_database_helper.dart @@ -0,0 +1,72 @@ +import 'package:comunic/helpers/database/database_contract.dart'; +import 'package:comunic/helpers/database/database_helper.dart'; +import 'package:comunic/models/cache_model.dart'; + +/// Model database helper +/// +/// Base model for caching data in the database +/// +/// @author Pierre HUBERT + +abstract class ModelDatabaseHelper { + + /// Get the name of the target table. Must be specified by the children + /// of this class + String tableName(); + + /// Initialize an element from a map + T initializeFromMap(Map map); + + /// Insert an entry in the database + Future _insertDB(T el) async { + await (await DatabaseHelper.get()) + .insert(tableName(), el.toMap()); + } + + /// Update an element in the database + Future _updateDB(T el) async { + await (await DatabaseHelper.get()).update( + tableName(), + el.toMap(), + where: "${BaseTableContract.C_ID} = ?", + whereArgs: [el.id], + ); + } + + /// Get an element from the database with a specified [id] + /// + /// Returns null if none found + Future get(int id) async { + List maps = await (await DatabaseHelper.get()).query( + UserTableContract.TABLE_NAME, + where: '${UserTableContract.C_ID} = ?', + whereArgs: [id], + ); + + if (maps.length > 0) return initializeFromMap(maps[0]); + + return null; + } + + /// Check out whether an element specified with its [id] is present + /// or not in the local database + Future has(int id) async { + return (await get(id)) != null; + } + + /// Insert or update information about a el (depends of the presence + /// of previous element information in the database + Future insertOrUpdate(T el) async { + if (await has(el.id)) + await _updateDB(el); + else + await _insertDB(el); + } + + /// Insert or update an important amount of elements + Future insertOrUpdateAll(List list) async { + for(int i = 0; i < list.length; i++) + await insertOrUpdate(list[i]); + } + +} \ No newline at end of file diff --git a/lib/helpers/database/users_database_helper.dart b/lib/helpers/database/users_database_helper.dart index cff3a99..8b2e413 100644 --- a/lib/helpers/database/users_database_helper.dart +++ b/lib/helpers/database/users_database_helper.dart @@ -1,62 +1,24 @@ +import 'package:comunic/helpers/database/model_database_helper.dart'; import 'package:comunic/models/user.dart'; import 'database_contract.dart'; -import 'database_helper.dart'; /// User database helper /// /// @author Pierre HUBERT -class UsersDatabaseHelper { - /// Insert a user in the database - Future _insertDB(User user) async { - await (await DatabaseHelper.get()) - .insert(UserTableContract.TABLE_NAME, user.toMap()); +class UsersDatabaseHelper extends ModelDatabaseHelper { + + + @override + String tableName() { + return UserTableContract.TABLE_NAME; } - /// Update a user in the database - Future _updateDB(User user) async { - await (await DatabaseHelper.get()).update( - UserTableContract.TABLE_NAME, - user.toMap(), - where: "${UserTableContract.C_ID} = ?", - whereArgs: [user.id], - ); + @override + User initializeFromMap(Map map) { + return User.fromMap(map); } - /// Get a user from the database with a specified [id] - /// - /// Returns null if none found - Future get(int id) async { - List maps = await (await DatabaseHelper.get()).query( - UserTableContract.TABLE_NAME, - where: '${UserTableContract.C_ID} = ?', - whereArgs: [id], - ); - if (maps.length > 0) return User.fromMap(maps[0]); - - return null; - } - - /// Check out whether a user information specified with its [id] is present - /// or not in the local database - Future has(int id) async { - return (await get(id)) != null; - } - - /// Insert or update information about a user (depends of the presence - /// of previous user information in the database - Future insertOrUpdate(User user) async { - if (await has(user.id)) - await _updateDB(user); - else - await _insertDB(user); - } - - /// Insert or update an important amount of user information - Future insertOrUpdateAll(List list) async { - for(int i = 0; i < list.length; i++) - await insertOrUpdate(list[i]); - } } diff --git a/lib/models/cache_model.dart b/lib/models/cache_model.dart new file mode 100644 index 0000000..bb89536 --- /dev/null +++ b/lib/models/cache_model.dart @@ -0,0 +1,19 @@ +import 'package:comunic/helpers/database/database_contract.dart'; +import 'package:meta/meta.dart'; + +/// Cache base model +/// +/// @author Pierre HUBERT + +abstract class CacheModel { + final int id; + + const CacheModel({@required this.id}) : assert(id != null); + + /// Initialize a CacheModel from a map + CacheModel.fromMap(Map map) + : id = map[BaseTableContract.C_ID]; + + /// Convert the object to a map + Map toMap(); +} diff --git a/lib/models/user.dart b/lib/models/user.dart index 318d81d..e762169 100644 --- a/lib/models/user.dart +++ b/lib/models/user.dart @@ -1,13 +1,13 @@ import 'package:comunic/enums/user_page_visibility.dart'; import 'package:comunic/helpers/database/database_contract.dart'; +import 'package:comunic/models/cache_model.dart'; import 'package:meta/meta.dart'; /// Single user information /// /// @author Pierre HUBERT -class User { - final int id; +class User extends CacheModel { final String firstName; final String lastName; final UserPageVisibility pageVisibility; @@ -15,7 +15,7 @@ class User { final String accountImageURL; const User({ - @required this.id, + @required int id, @required this.firstName, @required this.lastName, @required this.pageVisibility, @@ -25,7 +25,8 @@ class User { assert(firstName != null), assert(lastName != null), assert(pageVisibility != null), - assert(accountImageURL != null); + assert(accountImageURL != null), + super(id: id); /// Get user full name String get fullName => firstName + " " + lastName; @@ -42,11 +43,13 @@ class User { } User.fromMap(Map map) - : this.id = map[UserTableContract.C_ID], - this.firstName = map[UserTableContract.C_FIRST_NAME], + : this.firstName = map[UserTableContract.C_FIRST_NAME], this.lastName = map[UserTableContract.C_LAST_NAME], this.pageVisibility = userPageVisibilityFromString(map[UserTableContract.C_VISIBILITY]), this.virtualDirectory = map[UserTableContract.C_VIRTUAL_DIRECTORY], - this.accountImageURL = map[UserTableContract.C_ACCOUNT_IMAGE_URL]; + this.accountImageURL = map[UserTableContract.C_ACCOUNT_IMAGE_URL], + super.fromMap(map); + + }