fluffychat/lib/pages/invitation_selection/invitation_selection.dart

115 lines
3.8 KiB
Dart
Raw Normal View History

2020-02-16 11:07:48 +00:00
import 'dart:async';
2020-01-01 18:10:13 +00:00
import 'package:flutter/material.dart';
2021-10-26 16:50:34 +00:00
import 'package:flutter_gen/gen_l10n/l10n.dart';
2021-10-26 16:50:34 +00:00
import 'package:future_loading_dialog/future_loading_dialog.dart';
import 'package:matrix/matrix.dart';
2021-05-23 11:11:55 +00:00
import 'package:vrouter/vrouter.dart';
2020-01-01 18:10:13 +00:00
2021-11-09 20:32:16 +00:00
import 'package:fluffychat/pages/invitation_selection/invitation_selection_view.dart';
2021-10-26 16:50:34 +00:00
import 'package:fluffychat/widgets/matrix.dart';
2021-11-09 20:32:16 +00:00
import '../../utils/localized_exception_extension.dart';
2020-01-01 18:10:13 +00:00
2020-02-16 11:07:48 +00:00
class InvitationSelection extends StatefulWidget {
2022-01-29 11:35:03 +00:00
const InvitationSelection({Key? key}) : super(key: key);
2020-01-01 18:10:13 +00:00
2020-02-16 11:07:48 +00:00
@override
2021-04-13 14:25:08 +00:00
InvitationSelectionController createState() =>
InvitationSelectionController();
2020-02-16 11:07:48 +00:00
}
2021-04-13 14:25:08 +00:00
class InvitationSelectionController extends State<InvitationSelection> {
2020-02-16 11:07:48 +00:00
TextEditingController controller = TextEditingController();
2022-01-29 11:35:03 +00:00
late String currentSearchTerm;
2020-02-16 11:07:48 +00:00
bool loading = false;
2020-06-10 08:07:01 +00:00
List<Profile> foundProfiles = [];
2022-01-29 11:35:03 +00:00
Timer? coolDown;
2020-02-16 11:07:48 +00:00
2022-01-29 11:35:03 +00:00
String? get roomId => VRouter.of(context).pathParameters['roomid'];
2021-05-23 11:11:55 +00:00
2020-01-01 18:10:13 +00:00
Future<List<User>> getContacts(BuildContext context) async {
final client = Matrix.of(context).client;
2022-01-29 11:35:03 +00:00
final room = client.getRoomById(roomId!)!;
2021-04-14 08:37:15 +00:00
final participants = await room.requestParticipants();
2020-04-02 11:14:39 +00:00
participants.removeWhere(
(u) => ![Membership.join, Membership.invite].contains(u.membership),
);
2021-11-13 20:42:35 +00:00
final participantsIds = participants.map((p) => p.stateKey).toList();
final contacts = client.rooms
.where((r) => r.isDirectChat)
.map((r) => r.unsafeGetUserFromMemoryOrFallback(r.directChatMatrixID!))
2021-11-13 20:42:35 +00:00
.toList()
..removeWhere((u) => participantsIds.contains(u.stateKey));
2020-04-02 11:14:39 +00:00
contacts.sort(
(a, b) => a.calcDisplayname().toLowerCase().compareTo(
b.calcDisplayname().toLowerCase(),
),
);
2020-01-01 18:10:13 +00:00
return contacts;
}
void inviteAction(BuildContext context, String id) async {
2022-01-29 11:35:03 +00:00
final room = Matrix.of(context).client.getRoomById(roomId!);
2020-12-25 08:58:34 +00:00
final success = await showFutureLoadingDialog(
context: context,
2022-01-29 11:35:03 +00:00
future: () => room!.invite(id),
2020-01-01 18:10:13 +00:00
);
2020-12-25 08:58:34 +00:00
if (success.error == null) {
2021-05-23 11:11:55 +00:00
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
2022-01-29 11:35:03 +00:00
content: Text(L10n.of(context)!.contactHasBeenInvitedToTheGroup)));
2020-01-02 21:31:39 +00:00
}
2020-01-01 18:10:13 +00:00
}
2021-04-13 14:25:08 +00:00
void searchUserWithCoolDown(String text) async {
2020-02-16 11:07:48 +00:00
coolDown?.cancel();
coolDown = Timer(
2021-10-14 16:09:30 +00:00
const Duration(seconds: 1),
2020-02-16 11:07:48 +00:00
() => searchUser(context, text),
);
}
void searchUser(BuildContext context, String text) async {
coolDown?.cancel();
if (text.isEmpty) {
2021-01-23 10:57:47 +00:00
setState(() => foundProfiles = []);
2020-02-16 11:07:48 +00:00
}
currentSearchTerm = text;
if (currentSearchTerm.isEmpty) return;
if (loading) return;
setState(() => loading = true);
2020-05-13 13:58:59 +00:00
final matrix = Matrix.of(context);
SearchUserDirectoryResponse response;
2020-12-25 08:58:34 +00:00
try {
2021-05-20 11:59:55 +00:00
response = await matrix.client.searchUserDirectory(text, limit: 10);
2020-12-25 08:58:34 +00:00
} catch (e) {
2021-05-23 11:11:55 +00:00
ScaffoldMessenger.of(context).showSnackBar(
2022-01-29 11:35:03 +00:00
SnackBar(content: Text((e).toLocalizedString(context))));
2020-12-25 08:58:34 +00:00
return;
} finally {
setState(() => loading = false);
}
2020-02-16 11:07:48 +00:00
setState(() {
2020-06-10 08:07:01 +00:00
foundProfiles = List<Profile>.from(response.results);
2021-01-18 07:43:29 +00:00
if (text.isValidMatrixId &&
foundProfiles.indexWhere((profile) => text == profile.userId) == -1) {
2020-02-16 11:07:48 +00:00
setState(() => foundProfiles = [
2021-01-18 07:43:29 +00:00
Profile.fromJson({'user_id': text}),
2020-02-16 11:07:48 +00:00
]);
}
2021-04-13 14:25:08 +00:00
final participants = Matrix.of(context)
.client
2022-01-29 11:35:03 +00:00
.getRoomById(roomId!)!
2020-10-02 09:24:19 +00:00
.getParticipants()
.where((user) =>
[Membership.join, Membership.invite].contains(user.membership))
.toList();
2020-02-16 11:07:48 +00:00
foundProfiles.removeWhere((profile) =>
2020-10-02 09:24:19 +00:00
participants.indexWhere((u) => u.id == profile.userId) != -1);
2020-02-16 11:07:48 +00:00
});
}
2020-01-01 18:10:13 +00:00
@override
2021-05-22 07:13:47 +00:00
Widget build(BuildContext context) => InvitationSelectionView(this);
2020-01-01 18:10:13 +00:00
}