1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-12-29 14:28:51 +00:00
comunicmobile/lib/ui/widgets/tablet_mode/memberships_panel.dart

232 lines
6.8 KiB
Dart
Raw Normal View History

2020-05-05 17:33:04 +00:00
import 'package:comunic/helpers/conversations_helper.dart';
import 'package:comunic/helpers/friends_helper.dart';
2020-05-05 17:33:04 +00:00
import 'package:comunic/helpers/groups_helper.dart';
import 'package:comunic/helpers/users_helper.dart';
2020-05-05 16:49:50 +00:00
import 'package:comunic/helpers/webapp_helper.dart';
2020-05-05 17:33:04 +00:00
import 'package:comunic/lists/groups_list.dart';
2020-05-05 16:49:50 +00:00
import 'package:comunic/lists/memberships_list.dart';
2020-05-05 17:33:04 +00:00
import 'package:comunic/lists/users_list.dart';
import 'package:comunic/models/friend.dart';
2020-05-05 17:33:04 +00:00
import 'package:comunic/models/membership.dart';
import 'package:comunic/ui/widgets/account_image_widget.dart';
import 'package:comunic/ui/widgets/group_icon_widget.dart';
import 'package:comunic/ui/widgets/group_membership_widget.dart';
2020-05-05 16:49:50 +00:00
import 'package:comunic/ui/widgets/safe_state.dart';
2020-05-05 17:33:04 +00:00
import 'package:comunic/utils/date_utils.dart';
import 'package:comunic/utils/intl_utils.dart';
import 'package:comunic/utils/ui_utils.dart';
2020-05-05 16:49:50 +00:00
import 'package:flutter/material.dart';
/// Memberships panel
///
/// @author Pierre Hubert
class MembershipsPanel extends StatefulWidget {
@override
_MembershipsPanelState createState() => _MembershipsPanelState();
}
2020-05-05 17:33:04 +00:00
const _MembershipIconsWidth = 30.0;
2020-05-05 16:49:50 +00:00
class _MembershipsPanelState extends SafeState<MembershipsPanel> {
2020-05-05 17:33:04 +00:00
final _refreshKey = GlobalKey<RefreshIndicatorState>();
2020-05-05 16:49:50 +00:00
MembershipList _membershipList;
2020-05-05 17:33:04 +00:00
UsersList _usersList;
GroupsList _groupsList;
2020-05-05 16:49:50 +00:00
Future<void> _refresh() async {
try {
final memberships = await WebAppHelper.getMemberships();
2020-05-05 17:33:04 +00:00
final users = await UsersHelper().getListWithThrow(memberships.usersId);
final groups = await GroupsHelper().getListOrThrow(memberships.groupsId);
2020-05-05 16:49:50 +00:00
setState(() {
_membershipList = memberships;
2020-05-05 17:33:04 +00:00
_usersList = users;
_groupsList = groups;
2020-05-05 16:49:50 +00:00
});
} catch (e, s) {
print("Could not load the list of memberships! $e\n$s");
}
}
@override
void initState() {
super.initState();
setInterval(20, (t) => _refresh());
2020-05-05 16:49:50 +00:00
_refresh();
}
@override
Widget build(BuildContext context) {
2020-05-05 17:33:04 +00:00
if (_membershipList == null) return buildCenteredProgressBar();
return RefreshIndicator(
key: _refreshKey, child: _buildMembershipsList(), onRefresh: _refresh);
}
Widget _buildMembershipsList() {
return ListTileTheme(
iconColor: IconTheme.of(context).color,
child: ListView.builder(
itemBuilder: _buildMembershipTile,
itemCount: _membershipList.length,
),
);
}
Widget _buildMembershipTile(BuildContext context, int index) {
final membership = _membershipList[index];
switch (membership.type) {
case MembershipType.FRIEND:
return _buildFriendMembership(membership);
case MembershipType.GROUP:
return _buildGroupMembership(membership);
case MembershipType.CONVERSATION:
return _buildConversationMembership(membership);
}
}
// TODO : add private messages icon support
Widget _buildFriendMembership(Membership membership) {
final user = _usersList.getUser(membership.friend.id);
final connected = membership.friend.isConnected;
Widget subtitle;
if (!membership.friend.accepted) {
subtitle = RichText(
text: TextSpan(children: [
WidgetSpan(
child: _RespondFriendshipRequestButton(
friend: membership.friend,
accept: true,
text: tr("Accept"),
color: Colors.green,
onTap: _respondFriendshipRequest,
)),
TextSpan(text: " "),
WidgetSpan(
child: _RespondFriendshipRequestButton(
friend: membership.friend,
accept: false,
text: tr("Reject"),
color: Colors.red,
onTap: _respondFriendshipRequest,
)),
]));
} else
subtitle = Text(
connected ? tr("Online") : diffTimeFromNowToStr(membership.lastActive),
style: TextStyle(color: connected ? Colors.green : null),
);
2020-05-05 17:33:04 +00:00
return ListTile(
leading: AccountImageWidget(user: user, width: _MembershipIconsWidth),
title: Text(user.displayName),
subtitle: subtitle,
2020-05-05 17:33:04 +00:00
);
}
Widget _buildGroupMembership(Membership membership) {
final group = _groupsList.getGroup(membership.groupID);
Widget subtitle;
if (!group.isAtLeastMember) {
subtitle = GroupMembershipWidget(
group: group,
onUpdated: () => _refreshKey.currentState.show(),
onError: _onGroupMembershipUpdateError,
);
} else {
subtitle = Text(diffTimeFromNowToStr(membership.lastActive));
}
2020-05-05 17:33:04 +00:00
return ListTile(
leading: GroupIcon(
group: group,
width: _MembershipIconsWidth,
),
title: Text(group.displayName),
subtitle: subtitle,
2020-05-05 17:33:04 +00:00
);
}
Widget _buildConversationMembership(Membership membership) {
2020-05-06 16:05:02 +00:00
final conversation = membership.conversation;
2020-05-06 15:57:49 +00:00
return Container(
2020-05-06 16:05:02 +00:00
color: conversation.isHavingCall
? Color(0xFF815d1d)
: (conversation.sawLastMessage ? null : Color(0xFF1c443a)),
2020-05-06 15:57:49 +00:00
child: ListTile(
dense: true,
2020-05-06 16:05:02 +00:00
leading: Icon(conversation.isHavingCall ? Icons.phone : Icons.message),
title: Text(
ConversationsHelper.getConversationName(conversation, _usersList)),
subtitle: Text(diffTimeFromNowToStr(membership.lastActive) +
(conversation.isHavingCall ? "\n" + tr("Ongoing call") : "")),
2020-05-06 15:57:49 +00:00
),
2020-05-05 17:33:04 +00:00
);
2020-05-05 16:49:50 +00:00
}
/// Respond to a friendship request
Future<void> _respondFriendshipRequest(Friend f, bool accept) async {
try {
if (!accept &&
!await showConfirmDialog(
2020-05-06 15:53:27 +00:00
context: context,
message:
tr("Do you really want to reject this friendship request?")))
return;
await FriendsHelper().respondRequest(f.id, accept);
setState(() {
if (accept)
f.accepted = true;
else
_membershipList.removeFriend(f.id);
});
_refreshKey.currentState.show();
} catch (e, s) {
print("Could not respond to friendship request! $e\n$s");
showSimpleSnack(context, tr("Could not respond to friendship request!"));
}
}
/// Handles the case of failure in group membership update
void _onGroupMembershipUpdateError() {
showSimpleSnack(context, tr("Could not update group membership!"));
_refreshKey.currentState.show();
}
}
class _RespondFriendshipRequestButton extends StatelessWidget {
final Friend friend;
final bool accept;
final String text;
final Color color;
final void Function(Friend, bool) onTap;
const _RespondFriendshipRequestButton({
Key key,
@required this.friend,
@required this.accept,
@required this.text,
@required this.color,
@required this.onTap,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return InkWell(
onTap: () => onTap(friend, accept),
child: Text(text, style: TextStyle(color: color)),
);
}
2020-05-05 16:49:50 +00:00
}