mirror of
https://gitlab.com/comunic/comunicmobile
synced 2024-11-24 22:09:21 +00:00
Make it easy to cache new data
This commit is contained in:
parent
f8298919c2
commit
7f41f0dae1
@ -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";
|
||||
|
72
lib/helpers/database/model_database_helper.dart
Normal file
72
lib/helpers/database/model_database_helper.dart
Normal file
@ -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<T extends CacheModel> {
|
||||
|
||||
/// 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<String, dynamic> map);
|
||||
|
||||
/// Insert an entry in the database
|
||||
Future<void> _insertDB(T el) async {
|
||||
await (await DatabaseHelper.get())
|
||||
.insert(tableName(), el.toMap());
|
||||
}
|
||||
|
||||
/// Update an element in the database
|
||||
Future<void> _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<T> get(int id) async {
|
||||
List<Map> 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<bool> 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<void> 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<void> insertOrUpdateAll(List<T> list) async {
|
||||
for(int i = 0; i < list.length; i++)
|
||||
await insertOrUpdate(list[i]);
|
||||
}
|
||||
|
||||
}
|
@ -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<void> _insertDB(User user) async {
|
||||
await (await DatabaseHelper.get())
|
||||
.insert(UserTableContract.TABLE_NAME, user.toMap());
|
||||
class UsersDatabaseHelper extends ModelDatabaseHelper<User> {
|
||||
|
||||
|
||||
@override
|
||||
String tableName() {
|
||||
return UserTableContract.TABLE_NAME;
|
||||
}
|
||||
|
||||
/// Update a user in the database
|
||||
Future<void> _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<String, dynamic> map) {
|
||||
return User.fromMap(map);
|
||||
}
|
||||
|
||||
/// Get a user from the database with a specified [id]
|
||||
///
|
||||
/// Returns null if none found
|
||||
Future<User> get(int id) async {
|
||||
List<Map> 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<bool> 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<void> 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<void> insertOrUpdateAll(List<User> list) async {
|
||||
for(int i = 0; i < list.length; i++)
|
||||
await insertOrUpdate(list[i]);
|
||||
}
|
||||
}
|
||||
|
19
lib/models/cache_model.dart
Normal file
19
lib/models/cache_model.dart
Normal file
@ -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<String, dynamic> map)
|
||||
: id = map[BaseTableContract.C_ID];
|
||||
|
||||
/// Convert the object to a map
|
||||
Map<String, dynamic> toMap();
|
||||
}
|
@ -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<String, dynamic> 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);
|
||||
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user