1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2025-06-19 00:05:16 +00:00

Load memberships

This commit is contained in:
2020-05-05 18:49:50 +02:00
parent 3d0bfe6c3f
commit 286639889b
8 changed files with 160 additions and 18 deletions

View File

@ -96,7 +96,7 @@ class ConversationsHelper {
try {
ConversationsList list = ConversationsList();
response.getArray().forEach((f) => list.add(_apiToConversation(f)));
response.getArray().forEach((f) => list.add(apiToConversation(f)));
// Update the database
await _conversationsDatabaseHelper.clearTable();
@ -126,7 +126,7 @@ class ConversationsHelper {
if (response.code != 200) return null;
final conversation = _apiToConversation(response.getObject());
final conversation = apiToConversation(response.getObject());
_conversationsDatabaseHelper.insertOrUpdate(conversation);
return conversation;
} on Exception catch (e) {
@ -222,7 +222,7 @@ class ConversationsHelper {
}
/// Turn an API entry into a [Conversation] object
Conversation _apiToConversation(Map<String, dynamic> map) {
static Conversation apiToConversation(Map<String, dynamic> map) {
return Conversation(
id: map["ID"],
ownerID: map["ID_owner"],

View File

@ -28,18 +28,12 @@ class FriendsHelper {
if (response.code != 200) return null;
// Parse and return the list of friends
FriendsList list = FriendsList();
response.getArray().forEach(
(f) => list.add(
Friend(
id: f["ID_friend"],
accepted: f["accepted"] == 1,
lastActive: f["time_last_activity"],
following: f["following"] == 1,
canPostTexts: f["canPostTexts"],
),
),
);
FriendsList list = FriendsList()
..addAll(response
.getArray()
.cast<Map<String, dynamic>>()
.map(apiToFriend)
.toList());
// Save the list of friends
_friendsDatabaseHelper.clearTable();
@ -48,6 +42,17 @@ class FriendsHelper {
return list;
}
/// Turn an API entry into a [Friend] object
static Friend apiToFriend(Map<String, dynamic> row) {
return Friend(
id: row["ID_friend"],
accepted: row["accepted"] == 1,
lastActive: row["time_last_activity"],
following: row["following"] == 1,
canPostTexts: row["canPostTexts"],
);
}
/// Get the list, either from an online or an offline source
Future<FriendsList> getList({@required bool online}) async {
if (online)

View File

@ -0,0 +1,47 @@
import 'package:comunic/helpers/conversations_helper.dart';
import 'package:comunic/helpers/friends_helper.dart';
import 'package:comunic/lists/memberships_list.dart';
import 'package:comunic/models/api_request.dart';
import 'package:comunic/models/membership.dart';
/// Web application helper
///
/// @author Pierre Hubert
class WebAppHelper {
/// Fetch from the server the list of memberships of the user
///
/// Throws in case of failure
static Future<MembershipList> getMemberships() async {
final response =
(await APIRequest.withLogin("webApp/getMemberships").execWithThrow())
.getArray();
return MembershipList()
..addAll(response
.cast<Map<String, dynamic>>()
.map(_apiToMembership)
.where((f) => f != null)
..toList());
}
/// Turn an API entry into a membership entry
static Membership _apiToMembership(Map<String, dynamic> entry) {
switch (entry["type"]) {
case "conversation":
return Membership.conversation(
ConversationsHelper.apiToConversation(entry["conv"]));
case "friend":
return Membership.friend(FriendsHelper.apiToFriend(entry["friend"]));
case "group":
return Membership.group(
groupID: entry["id"], groupLastActive: entry["last_activity"]);
default:
print("Unknown membership type: ${entry["type"]}");
return null;
}
}
}