1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-11-22 21:09:21 +00:00

Load memberships

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

View File

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

View File

@ -0,0 +1,8 @@
import 'package:comunic/lists/abstract_list.dart';
import 'package:comunic/models/membership.dart';
/// Memberships list
///
/// @author Pierre Hubert
class MembershipList extends AbstractList<Membership> {}

View File

@ -22,8 +22,6 @@ class Friend extends CacheModel implements Comparable<Friend> {
}) : assert(id != null), }) : assert(id != null),
assert(accepted != null), assert(accepted != null),
assert(lastActive != null), assert(lastActive != null),
assert(following != null),
assert(canPostTexts != null),
super(id: id); super(id: id);
/// Check out whether friend is connected or not /// Check out whether friend is connected or not

View File

@ -0,0 +1,38 @@
import 'package:comunic/models/conversation.dart';
import 'package:comunic/models/friend.dart';
import 'package:flutter/material.dart';
/// Membership information
///
/// @author Pierre Hubert
enum MembershipType { FRIEND, GROUP, CONVERSATION }
class Membership {
final MembershipType type;
final Conversation conversation;
final Friend friend;
final int groupID;
final int groupLastActive;
Membership.conversation(this.conversation)
: type = MembershipType.CONVERSATION,
friend = null,
groupID = null,
groupLastActive = null,
assert(conversation != null);
Membership.friend(this.friend)
: type = MembershipType.FRIEND,
conversation = null,
groupID = null,
groupLastActive = null,
assert(friend != null);
Membership.group({@required this.groupID, @required this.groupLastActive})
: type = MembershipType.GROUP,
conversation = null,
friend = null,
assert(groupID != null),
assert(groupLastActive != null);
}

View File

@ -1,5 +1,6 @@
import 'package:comunic/ui/routes/main_route/main_route.dart'; import 'package:comunic/ui/routes/main_route/main_route.dart';
import 'package:comunic/ui/widgets/tablet_mode/current_user_panel.dart'; import 'package:comunic/ui/widgets/tablet_mode/current_user_panel.dart';
import 'package:comunic/ui/widgets/tablet_mode/memberships_panel.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
/// Main tablet route /// Main tablet route
@ -40,7 +41,11 @@ class _TabletRouteState extends State<TabletRoute> {
width: 300, width: 300,
color: Color(0xFF222D32), color: Color(0xFF222D32),
child: Column( child: Column(
children: <Widget>[CurrentUserPanel()], children: <Widget>[
CurrentUserPanel(),
Container(height: 20),
Expanded(child: MembershipsPanel())
],
), ),
), ),
); );

View File

@ -0,0 +1,41 @@
import 'package:comunic/helpers/webapp_helper.dart';
import 'package:comunic/lists/memberships_list.dart';
import 'package:comunic/ui/widgets/safe_state.dart';
import 'package:flutter/material.dart';
/// Memberships panel
///
/// @author Pierre Hubert
class MembershipsPanel extends StatefulWidget {
@override
_MembershipsPanelState createState() => _MembershipsPanelState();
}
class _MembershipsPanelState extends SafeState<MembershipsPanel> {
MembershipList _membershipList;
Future<void> _refresh() async {
try {
final memberships = await WebAppHelper.getMemberships();
setState(() {
_membershipList = memberships;
});
} catch (e, s) {
print("Could not load the list of memberships! $e\n$s");
setTimeout(5, _refresh);
}
}
@override
void initState() {
super.initState();
_refresh();
}
@override
Widget build(BuildContext context) {
return Container();
}
}