fluffychat/lib/pages/invitation_selection.dart

121 lines
3.9 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:famedlysdk/famedlysdk.dart';
import 'package:fluffychat/pages/views/invitation_selection_view.dart';
2020-12-25 09:58:34 +01:00
import 'package:future_loading_dialog/future_loading_dialog.dart';
2021-05-22 08:53:52 +02:00
import 'package:fluffychat/widgets/matrix.dart';
2020-01-01 19:10:13 +01:00
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/l10n.dart';
2021-05-23 13:11:55 +02:00
import 'package:vrouter/vrouter.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-05-23 13:11:55 +02: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();
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-05-23 13:11:55 +02:00
String get roomId => VRouter.of(context).pathParameters['roomid'];
2020-01-01 19:10:13 +01:00
Future<List<User>> getContacts(BuildContext context) async {
final client = Matrix.of(context).client;
2021-05-23 13:11:55 +02: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-04-14 10:37:15 +02:00
final contacts = <User>[];
final userMap = <String, bool>{};
2020-05-13 15:58:59 +02:00
for (var i = 0; i < client.rooms.length; i++) {
2021-04-14 10:37:15 +02:00
final 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 {
2021-05-23 13:11:55 +02:00
final room = Matrix.of(context).client.getRoomById(roomId);
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-05-23 13:11:55 +02:00
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
2021-04-03 13:09:20 +02: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(
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 {
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(
2021-04-03 13:09:20 +02:00
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-04-13 16:25:08 +02:00
final participants = Matrix.of(context)
.client
2021-05-23 13:11:55 +02: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
}