mirror of
https://gitlab.com/comunic/comunicmobile
synced 2025-06-18 15:58:04 +00:00
Cache user information
This commit is contained in:
20
lib/helpers/database/database_contract.dart
Normal file
20
lib/helpers/database/database_contract.dart
Normal file
@ -0,0 +1,20 @@
|
||||
/// Database contract
|
||||
///
|
||||
/// @author Pierre HUBERT
|
||||
|
||||
/// Main information
|
||||
class DatabaseContract {
|
||||
static const DATABASE_VERSION = 1;
|
||||
static const DATABASE_FILE_NAME = "database.sqlite";
|
||||
}
|
||||
|
||||
/// User table contract
|
||||
abstract class UserTableContract {
|
||||
static const TABLE_NAME = "users";
|
||||
static const C_ID = "id";
|
||||
static const C_FIRST_NAME = "first_name";
|
||||
static const C_LAST_NAME = "last_name";
|
||||
static const C_VISIBILITY = "visibility";
|
||||
static const C_VIRTUAL_DIRECTORY = "virtual_directory";
|
||||
static const C_ACCOUNT_IMAGE_URL = "account_image_url";
|
||||
}
|
53
lib/helpers/database/database_helper.dart
Normal file
53
lib/helpers/database/database_helper.dart
Normal file
@ -0,0 +1,53 @@
|
||||
import 'package:comunic/helpers/database/database_contract.dart';
|
||||
import 'package:path/path.dart';
|
||||
import 'package:sqflite/sqflite.dart';
|
||||
|
||||
/// Database Helper
|
||||
///
|
||||
/// Manage the connection to the local SQLite database
|
||||
///
|
||||
/// @author Pierre HUBERT
|
||||
|
||||
abstract class DatabaseHelper {
|
||||
static Database _db;
|
||||
|
||||
/// Open the database
|
||||
static Future<void> open() async {
|
||||
if (_db != null && _db.isOpen) return;
|
||||
|
||||
var databasePath = await getDatabasesPath();
|
||||
_db = await openDatabase(
|
||||
join(databasePath, DatabaseContract.DATABASE_FILE_NAME),
|
||||
version: DatabaseContract.DATABASE_VERSION,
|
||||
onUpgrade: _performDatabaseUpdate,
|
||||
onCreate: _initializeDatabase);
|
||||
}
|
||||
|
||||
/// Get a database instance
|
||||
static Future<Database> get() async {
|
||||
await open();
|
||||
return _db;
|
||||
}
|
||||
|
||||
/// Perform database update
|
||||
///
|
||||
/// Currently : delete all the database tables and initialize it again
|
||||
static Future<void> _performDatabaseUpdate(
|
||||
Database db, int oldVersion, int newVersion) async {
|
||||
//Initialize database again
|
||||
await _initializeDatabase(db, newVersion);
|
||||
}
|
||||
|
||||
/// Initialize the database
|
||||
static Future<void> _initializeDatabase(Database db, int version) async {
|
||||
//Create users table
|
||||
await db.execute("CREATE TABLE ${UserTableContract.TABLE_NAME} ("
|
||||
"${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_ACCOUNT_IMAGE_URL} TEXT"
|
||||
")");
|
||||
}
|
||||
}
|
62
lib/helpers/database/users_database_helper.dart
Normal file
62
lib/helpers/database/users_database_helper.dart
Normal file
@ -0,0 +1,62 @@
|
||||
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());
|
||||
}
|
||||
|
||||
/// 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],
|
||||
);
|
||||
}
|
||||
|
||||
/// 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]);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user