fluffychat/lib/pages/invitation_selection/invitation_selection.dart

130 lines
3.9 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
2022-07-08 08:55:07 +00:00
import 'package:adaptive_dialog/adaptive_dialog.dart';
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';
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';
2023-01-20 15:59:50 +00:00
import 'package:fluffychat/utils/matrix_sdk_extensions/matrix_locals.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 {
final String roomId;
const InvitationSelection({
super.key,
required this.roomId,
});
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
String? get roomId => widget.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 contacts = client.rooms
.where((r) => r.isDirectChat)
.map((r) => r.unsafeGetUserFromMemoryOrFallback(r.directChatMatrixID!))
.toList();
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, String displayname) async {
2022-07-08 08:55:07 +00:00
final room = Matrix.of(context).client.getRoomById(roomId!)!;
if (OkCancelResult.ok !=
await showOkCancelAlertDialog(
context: context,
title: L10n.of(context)!.inviteContact,
message: L10n.of(context)!.inviteContactToGroupQuestion(
displayname,
2023-01-20 15:59:50 +00:00
room.getLocalizedDisplayname(
MatrixLocals(L10n.of(context)!),
),
),
okLabel: L10n.of(context)!.invite,
2022-07-08 08:55:07 +00:00
cancelLabel: L10n.of(context)!.cancel,
)) {
return;
}
2020-12-25 08:58:34 +00:00
final success = await showFutureLoadingDialog(
context: context,
2022-07-08 08:55:07 +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) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
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(
2023-02-05 12:08:58 +00:00
const Duration(milliseconds: 500),
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(
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) {
setState(
() => foundProfiles = [
Profile.fromJson({'user_id': text}),
],
);
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
}