fluffychat/lib/pages/invitation_selection/invitation_selection.dart

115 lines
3.7 KiB
Dart
Raw Normal View History

2020-02-16 12:07:48 +01:00
import 'dart:async';
2020-01-01 19:10:13 +01:00
import 'package:flutter/material.dart';
2021-10-26 18:50:34 +02:00
import 'package:flutter_gen/gen_l10n/l10n.dart';
2021-10-26 18:50:34 +02:00
import 'package:future_loading_dialog/future_loading_dialog.dart';
import 'package:matrix/matrix.dart';
2021-05-23 13:11:55 +02:00
import 'package:vrouter/vrouter.dart';
2020-01-01 19:10:13 +01:00
2021-11-09 21:32:16 +01:00
import 'package:fluffychat/pages/invitation_selection/invitation_selection_view.dart';
2021-10-26 18:50:34 +02:00
import 'package:fluffychat/widgets/matrix.dart';
2021-11-09 21:32:16 +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 {
2022-01-29 12:35:03 +01:00
const InvitationSelection({Key? key}) : super(key: key);
2020-01-01 19:10:13 +01:00
2020-02-16 12:07:48 +01:00
@override
2021-04-13 16:25:08 +02:00
InvitationSelectionController createState() =>
InvitationSelectionController();
2020-02-16 12:07:48 +01:00
}
2021-04-13 16:25:08 +02:00
class InvitationSelectionController extends State<InvitationSelection> {
2020-02-16 12:07:48 +01:00
TextEditingController controller = TextEditingController();
2022-01-29 12:35:03 +01:00
late String currentSearchTerm;
2020-02-16 12:07:48 +01:00
bool loading = false;
2020-06-10 10:07:01 +02:00
List<Profile> foundProfiles = [];
2022-01-29 12:35:03 +01:00
Timer? coolDown;
2020-02-16 12:07:48 +01:00
2022-01-29 12:35:03 +01:00
String? get roomId => VRouter.of(context).pathParameters['roomid'];
2021-05-23 13:11:55 +02:00
2020-01-01 19:10:13 +01:00
Future<List<User>> getContacts(BuildContext context) async {
final client = Matrix.of(context).client;
2022-01-29 12:35:03 +01:00
final room = client.getRoomById(roomId!)!;
2021-04-14 10:37:15 +02:00
final participants = await room.requestParticipants();
2020-04-02 13:14:39 +02:00
participants.removeWhere(
(u) => ![Membership.join, Membership.invite].contains(u.membership),
);
2021-11-13 21:42:35 +01:00
final participantsIds = participants.map((p) => p.stateKey).toList();
final contacts = client.rooms
.where((r) => r.isDirectChat)
2022-01-29 12:35:03 +01:00
.map((r) => r.getUserByMXIDSync(r.directChatMatrixID!))
2021-11-13 21:42:35 +01:00
.toList()
..removeWhere((u) => participantsIds.contains(u.stateKey));
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 {
2022-01-29 12:35:03 +01:00
final room = Matrix.of(context).client.getRoomById(roomId!);
2020-12-25 09:58:34 +01:00
final success = await showFutureLoadingDialog(
context: context,
2022-01-29 12:35:03 +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-05-23 13:11:55 +02:00
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
2022-01-29 12:35:03 +01:00
content: Text(L10n.of(context)!.contactHasBeenInvitedToTheGroup)));
2020-01-02 22:31:39 +01:00
}
2020-01-01 19:10:13 +01:00
}
2021-04-13 16:25:08 +02:00
void searchUserWithCoolDown(String text) async {
2020-02-16 12:07:48 +01:00
coolDown?.cancel();
coolDown = Timer(
2021-10-14 18:09:30 +02:00
const Duration(seconds: 1),
2020-02-16 12:07:48 +01:00
() => 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);
SearchUserDirectoryResponse response;
2020-12-25 09:58:34 +01:00
try {
2021-05-20 13:59:55 +02:00
response = await matrix.client.searchUserDirectory(text, limit: 10);
2020-12-25 09:58:34 +01:00
} catch (e) {
2021-05-23 13:11:55 +02:00
ScaffoldMessenger.of(context).showSnackBar(
2022-01-29 12:35:03 +01:00
SnackBar(content: Text((e).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-04-13 16:25:08 +02:00
final participants = Matrix.of(context)
.client
2022-01-29 12:35:03 +01:00
.getRoomById(roomId!)!
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
2021-05-22 09:13:47 +02:00
Widget build(BuildContext context) => InvitationSelectionView(this);
2020-01-01 19:10:13 +01:00
}