fluffychat/lib/pages/new_group.dart

54 lines
1.8 KiB
Dart
Raw Normal View History

import 'package:matrix/matrix.dart' as sdk;
import 'package:fluffychat/pages/views/new_group_view.dart';
2021-04-13 16:28:27 +02:00
import 'package:future_loading_dialog/future_loading_dialog.dart';
2021-05-22 08:53:52 +02:00
import 'package:fluffychat/widgets/matrix.dart';
2021-04-13 16:28:27 +02:00
import 'package:flutter/material.dart';
2021-05-23 13:11:55 +02:00
import 'package:vrouter/vrouter.dart';
2021-04-13 16:28:27 +02:00
class NewGroup extends StatefulWidget {
2021-10-14 18:09:30 +02:00
const NewGroup({Key key}) : super(key: key);
2021-04-13 16:28:27 +02:00
@override
NewGroupController createState() => NewGroupController();
}
class NewGroupController extends State<NewGroup> {
TextEditingController controller = TextEditingController();
bool publicGroup = false;
void setPublicGroup(bool b) => setState(() => publicGroup = b);
void submitAction([_]) async {
final client = Matrix.of(context).client;
2021-04-13 16:28:27 +02:00
final roomID = await showFutureLoadingDialog(
context: context,
future: () async {
final roomId = await client.createRoom(
preset: publicGroup
? sdk.CreateRoomPreset.publicChat
: sdk.CreateRoomPreset.privateChat,
visibility: publicGroup ? sdk.Visibility.public : null,
roomAliasName: publicGroup && controller.text.isNotEmpty
? controller.text.trim().toLowerCase().replaceAll(' ', '_')
: null,
name: controller.text.isNotEmpty ? controller.text : null,
);
if (client.getRoomById(roomId) == null) {
await client.onSync.stream.firstWhere(
(sync) => sync.rooms?.join?.containsKey(roomId) ?? false);
}
if (!publicGroup && client.encryptionEnabled) {
await client.getRoomById(roomId).enableEncryption();
}
return roomId;
},
2021-04-13 16:28:27 +02:00
);
2021-05-23 13:11:55 +02:00
if (roomID.error == null) {
2021-08-15 13:26:16 +02:00
VRouter.of(context).toSegments(['rooms', roomID.result, 'invite']);
2021-04-13 16:28:27 +02:00
}
}
@override
2021-05-22 09:13:47 +02:00
Widget build(BuildContext context) => NewGroupView(this);
2021-04-13 16:28:27 +02:00
}