mirror of
https://gitlab.com/comunic/comunicmobile
synced 2025-06-19 08:15:16 +00:00
Cache the list of friends
This commit is contained in:
@ -45,4 +45,14 @@ abstract class ConversationsMessagesTableContract {
|
||||
static const C_TIME_INSERT = "time_insert";
|
||||
static const C_MESSAGE = "message";
|
||||
static const C_IMAGE_URL = "image_url";
|
||||
}
|
||||
|
||||
/// Friends table contract
|
||||
abstract class FriendsListTableContract {
|
||||
static const TABLE_NAME = "friends";
|
||||
static const C_ID = BaseTableContract.C_ID;
|
||||
static const C_ACCEPTED = "accepted";
|
||||
static const C_LAST_ACTIVE = "last_active";
|
||||
static const C_FOLLOWING = "following";
|
||||
static const C_CAN_POST_TEXTS = "can_post_texts";
|
||||
}
|
@ -45,6 +45,10 @@ abstract class DatabaseHelper {
|
||||
await db.execute(
|
||||
"DROP TABLE IF EXISTS ${ConversationsMessagesTableContract.TABLE_NAME}");
|
||||
|
||||
// Drop friends list table
|
||||
await db.execute(
|
||||
"DROP TABLE IF EXISTS ${FriendsListTableContract.TABLE_NAME}");
|
||||
|
||||
// Initialize database again
|
||||
await _initializeDatabase(db, newVersion);
|
||||
}
|
||||
@ -82,5 +86,14 @@ abstract class DatabaseHelper {
|
||||
"${ConversationsMessagesTableContract.C_MESSAGE} TEXT, "
|
||||
"${ConversationsMessagesTableContract.C_IMAGE_URL} TEXT"
|
||||
")");
|
||||
|
||||
// Friends list table
|
||||
await db.execute("CREATE TABLE ${FriendsListTableContract.TABLE_NAME} ("
|
||||
"${FriendsListTableContract.C_ID} INTEGER PRIMARY KEY, "
|
||||
"${FriendsListTableContract.C_ACCEPTED} INTEGER, "
|
||||
"${FriendsListTableContract.C_LAST_ACTIVE} INTEGER, "
|
||||
"${FriendsListTableContract.C_FOLLOWING} INTEGER, "
|
||||
"${FriendsListTableContract.C_CAN_POST_TEXTS} INTEGER"
|
||||
")");
|
||||
}
|
||||
}
|
||||
|
15
lib/helpers/database/friends_database_helper.dart
Normal file
15
lib/helpers/database/friends_database_helper.dart
Normal file
@ -0,0 +1,15 @@
|
||||
import 'package:comunic/helpers/database/database_contract.dart';
|
||||
import 'package:comunic/helpers/database/model_database_helper.dart';
|
||||
import 'package:comunic/models/friend.dart';
|
||||
|
||||
/// Friends database helper
|
||||
///
|
||||
/// @author Pierre HUBERT
|
||||
|
||||
class FriendsDatabaseHelper extends ModelDatabaseHelper<Friend> {
|
||||
@override
|
||||
Friend initializeFromMap(Map<String, dynamic> map) => Friend.fromMap(map);
|
||||
|
||||
@override
|
||||
String tableName() => FriendsListTableContract.TABLE_NAME;
|
||||
}
|
@ -1,17 +1,22 @@
|
||||
import 'package:comunic/helpers/database/friends_database_helper.dart';
|
||||
import 'package:comunic/lists/friends_list.dart';
|
||||
import 'package:comunic/models/api_request.dart';
|
||||
import 'package:comunic/models/friend.dart';
|
||||
import 'package:meta/meta.dart';
|
||||
|
||||
/// Friends helper
|
||||
///
|
||||
/// @author Pierre HUBERT
|
||||
|
||||
class FriendsHelper {
|
||||
/// Get the list of friends of the user
|
||||
|
||||
final FriendsDatabaseHelper _friendsDatabaseHelper = FriendsDatabaseHelper();
|
||||
|
||||
/// Get the list of friends of the user from the server
|
||||
///
|
||||
/// Returns the list of friends in case of success, or null if an error
|
||||
/// occurred
|
||||
Future<FriendsList> downloadList() async {
|
||||
Future<FriendsList> _downloadList() async {
|
||||
final response = await APIRequest(
|
||||
uri: "friends/getList",
|
||||
needLogin: true,
|
||||
@ -36,9 +41,23 @@ class FriendsHelper {
|
||||
),
|
||||
);
|
||||
|
||||
// Save the list of friends
|
||||
_friendsDatabaseHelper.clearTable();
|
||||
_friendsDatabaseHelper.insertAll(list);
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
/// Get the list, either from an online or an offline source
|
||||
Future<FriendsList> getList({@required bool online}) async {
|
||||
|
||||
if(online)
|
||||
return await _downloadList();
|
||||
|
||||
else
|
||||
return FriendsList()..addAll(await _friendsDatabaseHelper.getAll());
|
||||
}
|
||||
|
||||
/// Respond to friendship request
|
||||
Future<bool> respondRequest(int friendID, bool accept) async {
|
||||
final response = await APIRequest(
|
||||
|
Reference in New Issue
Block a user