FurryChat/lib/views/invitation_selection.dart

82 lines
2.7 KiB
Dart
Raw Normal View History

2020-01-01 18:10:13 +00:00
import 'package:famedlysdk/famedlysdk.dart';
import 'package:fluffychat/components/adaptive_page_layout.dart';
import 'package:fluffychat/components/avatar.dart';
import 'package:fluffychat/components/matrix.dart';
import 'package:flutter/material.dart';
import 'package:toast/toast.dart';
import 'chat_list.dart';
class InvitationSelection extends StatelessWidget {
final Room room;
const InvitationSelection(this.room, {Key key}) : super(key: key);
Future<List<User>> getContacts(BuildContext context) async {
final Client client = Matrix.of(context).client;
List<User> participants = await room.requestParticipants();
List<User> contacts = [];
Map<String, bool> userMap = {};
2020-01-02 14:10:21 +00:00
for (int i = 0; i < client.rooms.length; i++) {
List<User> roomUsers = client.rooms[i].getParticipants();
2020-01-01 18:10:13 +00:00
for (int j = 0; j < roomUsers.length; j++) {
if (userMap[roomUsers[j].id] != true &&
2020-01-02 21:31:39 +00:00
participants.indexWhere((u) => u.id == roomUsers[j].id) == -1) {
2020-01-01 18:10:13 +00:00
contacts.add(roomUsers[j]);
2020-01-02 21:31:39 +00:00
}
2020-01-01 18:10:13 +00:00
userMap[roomUsers[j].id] = true;
}
}
2020-01-03 10:47:31 +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 {
final success = await Matrix.of(context).tryRequestWithLoadingDialog(
room.invite(id),
);
2020-01-02 21:31:39 +00:00
if (success != false) {
2020-01-01 18:10:13 +00:00
Toast.show(
"Contact has been invited to the group.",
context,
duration: Toast.LENGTH_LONG,
);
2020-01-02 21:31:39 +00:00
}
2020-01-01 18:10:13 +00:00
}
@override
Widget build(BuildContext context) {
final String groupName = room.name?.isEmpty ?? false ? "group" : room.name;
return AdaptivePageLayout(
primaryPage: FocusPage.SECOND,
firstScaffold: ChatList(activeChat: room.id),
secondScaffold: Scaffold(
appBar: AppBar(
title: Text("Invite contact to $groupName"),
),
body: FutureBuilder<List<User>>(
future: getContacts(context),
builder: (BuildContext context, snapshot) {
2020-01-02 21:31:39 +00:00
if (!snapshot.hasData) {
2020-01-01 18:10:13 +00:00
return Center(
child: CircularProgressIndicator(),
);
2020-01-02 21:31:39 +00:00
}
2020-01-01 18:10:13 +00:00
List<User> contacts = snapshot.data;
return ListView.builder(
itemCount: contacts.length,
itemBuilder: (BuildContext context, int i) => ListTile(
leading: Avatar(contacts[i].avatarUrl),
title: Text(contacts[i].calcDisplayname()),
subtitle: Text(contacts[i].id),
onTap: () => inviteAction(context, contacts[i].id),
),
);
},
)),
);
}
}