fluffychat/lib/views/invitation_selection.dart

173 lines
5.8 KiB
Dart
Raw Normal View History

2020-02-16 12:07:48 +01:00
import 'dart:async';
2021-04-03 13:09:20 +02:00
import 'package:adaptive_page_layout/adaptive_page_layout.dart';
2020-12-20 17:00:09 +01:00
import 'package:fluffychat/components/default_app_bar_search_field.dart';
2021-04-03 13:09:20 +02:00
2020-01-01 19:10:13 +01:00
import 'package:famedlysdk/famedlysdk.dart';
import 'package:fluffychat/components/avatar.dart';
2020-12-25 09:58:34 +01:00
import 'package:future_loading_dialog/future_loading_dialog.dart';
2020-01-01 19:10:13 +01:00
import 'package:fluffychat/components/matrix.dart';
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/l10n.dart';
2020-01-01 19:10:13 +01:00
2020-12-25 09:58:34 +01:00
import '../utils/localized_exception_extension.dart';
2020-01-01 19:10:13 +01:00
2020-02-16 12:07:48 +01:00
class InvitationSelection extends StatefulWidget {
2021-01-16 12:46:38 +01:00
final String roomId;
const InvitationSelection(this.roomId, {Key key}) : super(key: key);
2020-01-01 19:10:13 +01:00
2020-02-16 12:07:48 +01:00
@override
_InvitationSelectionState createState() => _InvitationSelectionState();
}
class _InvitationSelectionState extends State<InvitationSelection> {
TextEditingController controller = TextEditingController();
String currentSearchTerm;
bool loading = false;
2020-06-10 10:07:01 +02:00
List<Profile> foundProfiles = [];
2020-02-16 12:07:48 +01:00
Timer coolDown;
2021-01-16 12:46:38 +01:00
Room room;
2020-02-16 12:07:48 +01:00
2020-01-01 19:10:13 +01:00
Future<List<User>> getContacts(BuildContext context) async {
2020-05-13 15:58:59 +02:00
var client2 = Matrix.of(context).client;
final client = client2;
2021-01-16 12:46:38 +01:00
var participants = await room.requestParticipants();
2020-04-02 13:14:39 +02:00
participants.removeWhere(
(u) => ![Membership.join, Membership.invite].contains(u.membership),
);
2020-05-13 15:58:59 +02:00
var contacts = <User>[];
var userMap = <String, bool>{};
for (var i = 0; i < client.rooms.length; i++) {
var roomUsers = client.rooms[i].getParticipants();
2020-04-02 13:14:39 +02:00
2020-05-13 15:58:59 +02:00
for (var j = 0; j < roomUsers.length; j++) {
2020-01-01 19:10:13 +01:00
if (userMap[roomUsers[j].id] != true &&
2020-01-02 22:31:39 +01:00
participants.indexWhere((u) => u.id == roomUsers[j].id) == -1) {
2020-01-01 19:10:13 +01:00
contacts.add(roomUsers[j]);
2020-01-02 22:31:39 +01:00
}
2020-01-01 19:10:13 +01:00
userMap[roomUsers[j].id] = true;
}
}
2020-04-02 13:14:39 +02:00
contacts.sort(
(a, b) => a.calcDisplayname().toLowerCase().compareTo(
b.calcDisplayname().toLowerCase(),
),
);
2020-01-01 19:10:13 +01:00
return contacts;
}
void inviteAction(BuildContext context, String id) async {
2020-12-25 09:58:34 +01:00
final success = await showFutureLoadingDialog(
context: context,
2021-01-16 12:46:38 +01:00
future: () => room.invite(id),
2020-01-01 19:10:13 +01:00
);
2020-12-25 09:58:34 +01:00
if (success.error == null) {
2021-04-03 13:09:20 +02:00
AdaptivePageLayout.of(context).showSnackBar(SnackBar(
content: Text(L10n.of(context).contactHasBeenInvitedToTheGroup)));
2020-01-02 22:31:39 +01:00
}
2020-01-01 19:10:13 +01:00
}
2020-02-16 12:07:48 +01:00
void searchUserWithCoolDown(BuildContext context, String text) async {
coolDown?.cancel();
coolDown = Timer(
Duration(seconds: 1),
() => searchUser(context, text),
);
}
void searchUser(BuildContext context, String text) async {
coolDown?.cancel();
if (text.isEmpty) {
2021-01-23 11:57:47 +01:00
setState(() => foundProfiles = []);
2020-02-16 12:07:48 +01:00
}
currentSearchTerm = text;
if (currentSearchTerm.isEmpty) return;
if (loading) return;
setState(() => loading = true);
2020-05-13 15:58:59 +02:00
final matrix = Matrix.of(context);
2020-12-25 09:58:34 +01:00
UserSearchResult response;
try {
response = await matrix.client.searchUser(text, limit: 10);
} catch (e) {
2021-04-03 13:09:20 +02:00
AdaptivePageLayout.of(context).showSnackBar(
SnackBar(content: Text((e as Object).toLocalizedString(context))));
2020-12-25 09:58:34 +01:00
return;
} finally {
setState(() => loading = false);
}
2020-02-16 12:07:48 +01:00
setState(() {
2020-06-10 10:07:01 +02:00
foundProfiles = List<Profile>.from(response.results);
2021-01-18 08:43:29 +01:00
if (text.isValidMatrixId &&
foundProfiles.indexWhere((profile) => text == profile.userId) == -1) {
2020-02-16 12:07:48 +01:00
setState(() => foundProfiles = [
2021-01-18 08:43:29 +01:00
Profile.fromJson({'user_id': text}),
2020-02-16 12:07:48 +01:00
]);
}
2021-01-16 12:46:38 +01:00
final participants = room
2020-10-02 11:24:19 +02:00
.getParticipants()
.where((user) =>
[Membership.join, Membership.invite].contains(user.membership))
.toList();
2020-02-16 12:07:48 +01:00
foundProfiles.removeWhere((profile) =>
2020-10-02 11:24:19 +02:00
participants.indexWhere((u) => u.id == profile.userId) != -1);
2020-02-16 12:07:48 +01:00
});
}
2020-01-01 19:10:13 +01:00
@override
Widget build(BuildContext context) {
2021-01-16 12:46:38 +01:00
room ??= Matrix.of(context).client.getRoomById(widget.roomId);
final groupName =
room.name?.isEmpty ?? false ? L10n.of(context).group : room.name;
return Scaffold(
appBar: AppBar(
2021-01-16 14:24:52 +01:00
leading: BackButton(),
2021-01-16 12:46:38 +01:00
titleSpacing: 0,
title: DefaultAppBarSearchField(
autofocus: true,
hintText: L10n.of(context).inviteContactToGroup(groupName),
onChanged: (String text) => searchUserWithCoolDown(context, text),
),
),
body: foundProfiles.isNotEmpty
? ListView.builder(
itemCount: foundProfiles.length,
itemBuilder: (BuildContext context, int i) => ListTile(
leading: Avatar(
foundProfiles[i].avatarUrl,
foundProfiles[i].displayname ?? foundProfiles[i].userId,
),
title: Text(
foundProfiles[i].displayname ??
foundProfiles[i].userId.localpart,
),
subtitle: Text(foundProfiles[i].userId),
onTap: () => inviteAction(context, foundProfiles[i].userId),
),
)
: FutureBuilder<List<User>>(
future: getContacts(context),
builder: (BuildContext context, snapshot) {
if (!snapshot.hasData) {
return Center(
child: CircularProgressIndicator(),
);
}
var contacts = snapshot.data;
return ListView.builder(
itemCount: contacts.length,
2020-02-16 12:07:48 +01:00
itemBuilder: (BuildContext context, int i) => ListTile(
leading: Avatar(
2021-01-16 12:46:38 +01:00
contacts[i].avatarUrl,
contacts[i].calcDisplayname(),
2020-02-16 12:07:48 +01:00
),
2021-01-16 12:46:38 +01:00
title: Text(contacts[i].calcDisplayname()),
subtitle: Text(contacts[i].id),
onTap: () => inviteAction(context, contacts[i].id),
2020-02-16 12:07:48 +01:00
),
2021-01-16 12:46:38 +01:00
);
},
),
2020-01-01 19:10:13 +01:00
);
}
}