fluffychat/lib/views/invitation_selection.dart

87 lines
2.9 KiB
Dart
Raw Normal View History

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';
import 'package:fluffychat/components/matrix.dart';
2020-01-20 13:46:39 +01:00
import 'package:fluffychat/i18n/i18n.dart';
2020-01-01 19:10:13 +01: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 15:10:21 +01:00
for (int i = 0; i < client.rooms.length; i++) {
List<User> roomUsers = client.rooms[i].getParticipants();
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-01-03 11:47:31 +01: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 {
final success = await Matrix.of(context).tryRequestWithLoadingDialog(
room.invite(id),
);
2020-01-02 22:31:39 +01:00
if (success != false) {
2020-01-01 19:10:13 +01:00
Toast.show(
2020-01-20 13:46:39 +01:00
I18n.of(context).contactHasBeenInvitedToTheGroup,
2020-01-01 19:10:13 +01:00
context,
duration: Toast.LENGTH_LONG,
);
2020-01-02 22:31:39 +01:00
}
2020-01-01 19:10:13 +01:00
}
@override
Widget build(BuildContext context) {
2020-01-20 13:46:39 +01:00
final String groupName =
room.name?.isEmpty ?? false ? I18n.of(context).group : room.name;
2020-01-01 19:10:13 +01:00
return AdaptivePageLayout(
primaryPage: FocusPage.SECOND,
firstScaffold: ChatList(activeChat: room.id),
secondScaffold: Scaffold(
appBar: AppBar(
2020-01-20 13:46:39 +01:00
title: Text(I18n.of(context).inviteContactToGroup(groupName)),
2020-01-01 19:10:13 +01:00
),
body: FutureBuilder<List<User>>(
future: getContacts(context),
builder: (BuildContext context, snapshot) {
2020-01-02 22:31:39 +01:00
if (!snapshot.hasData) {
2020-01-01 19:10:13 +01:00
return Center(
child: CircularProgressIndicator(),
);
2020-01-02 22:31:39 +01:00
}
2020-01-01 19:10:13 +01:00
List<User> contacts = snapshot.data;
return ListView.builder(
itemCount: contacts.length,
itemBuilder: (BuildContext context, int i) => ListTile(
2020-01-18 13:22:22 +01:00
leading: Avatar(
contacts[i].avatarUrl,
contacts[i].calcDisplayname(),
),
2020-01-01 19:10:13 +01:00
title: Text(contacts[i].calcDisplayname()),
subtitle: Text(contacts[i].id),
onTap: () => inviteAction(context, contacts[i].id),
),
);
},
)),
);
}
}