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/components/adaptive_page_layout.dart';
|
|
|
|
import 'package:fluffychat/components/avatar.dart';
|
2020-04-27 13:36:39 +02:00
|
|
|
import 'package:fluffychat/components/dialogs/simple_dialogs.dart';
|
2020-01-01 19:10:13 +01:00
|
|
|
import 'package:fluffychat/components/matrix.dart';
|
2020-05-07 07:52:40 +02:00
|
|
|
import 'package:fluffychat/l10n/l10n.dart';
|
2020-01-01 19:10:13 +01:00
|
|
|
import 'package:flutter/material.dart';
|
2020-04-12 09:19:22 +02:00
|
|
|
import 'package:flutter_styled_toast/flutter_styled_toast.dart';
|
2020-01-01 19:10:13 +01:00
|
|
|
|
|
|
|
import 'chat_list.dart';
|
|
|
|
|
2020-02-16 12:07:48 +01:00
|
|
|
class InvitationSelection extends StatefulWidget {
|
2020-01-01 19:10:13 +01:00
|
|
|
final Room room;
|
|
|
|
const InvitationSelection(this.room, {Key key}) : super(key: key);
|
|
|
|
|
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;
|
|
|
|
List<Map<String, dynamic>> foundProfiles = [];
|
|
|
|
Timer coolDown;
|
|
|
|
|
2020-01-01 19:10:13 +01:00
|
|
|
Future<List<User>> getContacts(BuildContext context) async {
|
|
|
|
final Client client = Matrix.of(context).client;
|
2020-02-16 12:07:48 +01:00
|
|
|
List<User> participants = await widget.room.requestParticipants();
|
2020-04-02 13:14:39 +02:00
|
|
|
participants.removeWhere(
|
|
|
|
(u) => ![Membership.join, Membership.invite].contains(u.membership),
|
|
|
|
);
|
2020-01-01 19:10:13 +01:00
|
|
|
List<User> contacts = [];
|
|
|
|
Map<String, bool> userMap = {};
|
2020-01-02 15:10:21 +01:00
|
|
|
for (int i = 0; i < client.rooms.length; i++) {
|
|
|
|
List<User> roomUsers = client.rooms[i].getParticipants();
|
2020-04-02 13:14:39 +02:00
|
|
|
|
2020-01-01 19:10:13 +01:00
|
|
|
for (int j = 0; j < roomUsers.length; j++) {
|
|
|
|
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-04-27 13:36:39 +02:00
|
|
|
final success = await SimpleDialogs(context).tryRequestWithLoadingDialog(
|
2020-02-16 12:07:48 +01:00
|
|
|
widget.room.invite(id),
|
2020-01-01 19:10:13 +01:00
|
|
|
);
|
2020-01-02 22:31:39 +01:00
|
|
|
if (success != false) {
|
2020-05-07 07:52:40 +02:00
|
|
|
showToast(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) {
|
|
|
|
setState(() {
|
|
|
|
foundProfiles = [];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
currentSearchTerm = text;
|
|
|
|
if (currentSearchTerm.isEmpty) return;
|
|
|
|
if (loading) return;
|
|
|
|
setState(() => loading = true);
|
|
|
|
final MatrixState matrix = Matrix.of(context);
|
2020-04-27 13:36:39 +02:00
|
|
|
final response = await SimpleDialogs(context).tryRequestWithErrorToast(
|
2020-02-16 12:07:48 +01:00
|
|
|
matrix.client.jsonRequest(
|
|
|
|
type: HTTPType.POST,
|
|
|
|
action: "/client/r0/user_directory/search",
|
|
|
|
data: {
|
|
|
|
"search_term": text,
|
|
|
|
"limit": 10,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
setState(() => loading = false);
|
|
|
|
if (response == false ||
|
|
|
|
!(response is Map) ||
|
|
|
|
(response["results"] == null)) return;
|
|
|
|
setState(() {
|
|
|
|
foundProfiles = List<Map<String, dynamic>>.from(response["results"]);
|
|
|
|
if ("@$text".isValidMatrixId &&
|
|
|
|
foundProfiles
|
|
|
|
.indexWhere((profile) => "@$text" == profile["user_id"]) ==
|
|
|
|
-1) {
|
|
|
|
setState(() => foundProfiles = [
|
|
|
|
{"user_id": "@$text"}
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
foundProfiles.removeWhere((profile) =>
|
|
|
|
widget.room
|
|
|
|
.getParticipants()
|
|
|
|
.indexWhere((u) => u.id == profile["user_id"]) !=
|
|
|
|
-1);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-01-01 19:10:13 +01:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2020-02-16 12:07:48 +01:00
|
|
|
final String groupName = widget.room.name?.isEmpty ?? false
|
2020-05-07 07:52:40 +02:00
|
|
|
? L10n.of(context).group
|
2020-02-16 12:07:48 +01:00
|
|
|
: widget.room.name;
|
2020-01-01 19:10:13 +01:00
|
|
|
return AdaptivePageLayout(
|
|
|
|
primaryPage: FocusPage.SECOND,
|
2020-02-16 12:07:48 +01:00
|
|
|
firstScaffold: ChatList(activeChat: widget.room.id),
|
2020-01-01 19:10:13 +01:00
|
|
|
secondScaffold: Scaffold(
|
|
|
|
appBar: AppBar(
|
2020-05-07 07:52:40 +02:00
|
|
|
title: Text(L10n.of(context).inviteContact),
|
2020-02-16 12:07:48 +01:00
|
|
|
bottom: PreferredSize(
|
2020-02-23 08:49:58 +01:00
|
|
|
preferredSize: Size.fromHeight(92),
|
2020-02-16 12:07:48 +01:00
|
|
|
child: Padding(
|
|
|
|
padding: const EdgeInsets.all(16.0),
|
|
|
|
child: TextField(
|
|
|
|
controller: controller,
|
|
|
|
autofocus: true,
|
|
|
|
autocorrect: false,
|
|
|
|
textInputAction: TextInputAction.search,
|
|
|
|
onChanged: (String text) =>
|
|
|
|
searchUserWithCoolDown(context, text),
|
|
|
|
onSubmitted: (String text) => searchUser(context, text),
|
|
|
|
decoration: InputDecoration(
|
|
|
|
border: OutlineInputBorder(),
|
|
|
|
prefixText: "@",
|
2020-05-07 07:52:40 +02:00
|
|
|
hintText: L10n.of(context).username,
|
|
|
|
labelText: L10n.of(context).inviteContactToGroup(groupName),
|
2020-02-16 12:07:48 +01:00
|
|
|
suffixIcon: loading
|
|
|
|
? Container(
|
|
|
|
padding: const EdgeInsets.all(8.0),
|
|
|
|
width: 12,
|
|
|
|
height: 12,
|
|
|
|
child: CircularProgressIndicator(),
|
|
|
|
)
|
|
|
|
: Icon(Icons.search),
|
2020-01-18 13:22:22 +01:00
|
|
|
),
|
2020-01-01 19:10:13 +01:00
|
|
|
),
|
2020-02-16 12:07:48 +01:00
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
body: foundProfiles.isNotEmpty
|
|
|
|
? ListView.builder(
|
|
|
|
itemCount: foundProfiles.length,
|
|
|
|
itemBuilder: (BuildContext context, int i) => ListTile(
|
|
|
|
leading: Avatar(
|
2020-04-28 14:11:56 +02:00
|
|
|
foundProfiles[i]["avatar_url"] == null
|
|
|
|
? null
|
|
|
|
: Uri.parse(foundProfiles[i]["avatar_url"]),
|
2020-02-16 12:07:48 +01:00
|
|
|
foundProfiles[i]["display_name"] ??
|
|
|
|
foundProfiles[i]["user_id"],
|
|
|
|
),
|
|
|
|
title: Text(
|
|
|
|
foundProfiles[i]["display_name"] ??
|
|
|
|
(foundProfiles[i]["user_id"] as String).localpart,
|
|
|
|
),
|
|
|
|
subtitle: Text(foundProfiles[i]["user_id"]),
|
|
|
|
onTap: () =>
|
|
|
|
inviteAction(context, foundProfiles[i]["user_id"]),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
: FutureBuilder<List<User>>(
|
|
|
|
future: getContacts(context),
|
|
|
|
builder: (BuildContext context, snapshot) {
|
|
|
|
if (!snapshot.hasData) {
|
|
|
|
return Center(
|
|
|
|
child: CircularProgressIndicator(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
List<User> contacts = snapshot.data;
|
|
|
|
return ListView.builder(
|
|
|
|
itemCount: contacts.length,
|
|
|
|
itemBuilder: (BuildContext context, int i) => ListTile(
|
|
|
|
leading: Avatar(
|
|
|
|
contacts[i].avatarUrl,
|
|
|
|
contacts[i].calcDisplayname(),
|
|
|
|
),
|
|
|
|
title: Text(contacts[i].calcDisplayname()),
|
|
|
|
subtitle: Text(contacts[i].id),
|
|
|
|
onTap: () => inviteAction(context, contacts[i].id),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
)),
|
2020-01-01 19:10:13 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|