FurryChat/lib/views/invitation_selection.dart

87 lines
2.9 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';
2020-01-20 12:46:39 +00:00
import 'package:fluffychat/i18n/i18n.dart';
2020-01-01 18:10:13 +00:00
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(
2020-01-20 12:46:39 +00:00
I18n.of(context).contactHasBeenInvitedToTheGroup,
2020-01-01 18:10:13 +00:00
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) {
2020-01-20 12:46:39 +00:00
final String groupName =
room.name?.isEmpty ?? false ? I18n.of(context).group : room.name;
2020-01-01 18:10:13 +00:00
return AdaptivePageLayout(
primaryPage: FocusPage.SECOND,
firstScaffold: ChatList(activeChat: room.id),
secondScaffold: Scaffold(
appBar: AppBar(
2020-01-20 12:46:39 +00:00
title: Text(I18n.of(context).inviteContactToGroup(groupName)),
2020-01-01 18:10:13 +00:00
),
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(
2020-01-18 12:22:22 +00:00
leading: Avatar(
contacts[i].avatarUrl,
contacts[i].calcDisplayname(),
),
2020-01-01 18:10:13 +00:00
title: Text(contacts[i].calcDisplayname()),
subtitle: Text(contacts[i].id),
onTap: () => inviteAction(context, contacts[i].id),
),
);
},
)),
);
}
}