fluffychat/lib/pages/new_private_chat.dart

92 lines
2.8 KiB
Dart
Raw Normal View History

2021-08-22 21:19:22 +02:00
import 'package:fluffychat/pages/qr_scanner_modal.dart';
import 'package:matrix/matrix.dart';
2021-04-15 09:05:41 +02:00
import 'package:fluffychat/utils/fluffy_share.dart';
import 'package:fluffychat/pages/views/new_private_chat_view.dart';
2021-04-15 09:05:41 +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-15 09:05:41 +02:00
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/l10n.dart';
2021-08-23 18:36:13 +02:00
import 'package:permission_handler/permission_handler.dart';
2021-05-23 13:11:55 +02:00
import 'package:vrouter/vrouter.dart';
2021-04-15 09:05:41 +02:00
class NewPrivateChat extends StatefulWidget {
@override
NewPrivateChatController createState() => NewPrivateChatController();
}
class NewPrivateChatController extends State<NewPrivateChat> {
TextEditingController controller = TextEditingController();
final formKey = GlobalKey<FormState>();
bool loading = false;
2021-08-22 21:09:05 +02:00
static const Set<String> supportedSigils = {'@', '!', '#'};
2021-04-15 09:05:41 +02:00
void submitAction([_]) async {
2021-08-22 21:09:05 +02:00
controller.text = controller.text.trim();
2021-04-15 09:05:41 +02:00
if (!formKey.currentState.validate()) return;
2021-08-22 21:09:05 +02:00
final client = Matrix.of(context).client;
2021-04-15 09:05:41 +02:00
2021-08-22 21:09:05 +02:00
LoadingDialogResult roomIdResult;
2021-04-15 09:05:41 +02:00
2021-08-22 21:09:05 +02:00
switch (controller.text.sigil) {
case '@':
roomIdResult = await showFutureLoadingDialog(
context: context,
future: () => client.startDirectChat(controller.text),
);
break;
case '#':
case '!':
roomIdResult = await showFutureLoadingDialog(
context: context,
future: () async {
final roomId = await client.joinRoom(controller.text);
if (client.getRoomById(roomId) == null) {
await client.onSync.stream
.where((s) => s.rooms.join.containsKey(roomId))
.first;
}
return roomId;
},
);
break;
2021-04-15 09:05:41 +02:00
}
2021-08-22 21:09:05 +02:00
if (roomIdResult.error == null) {
VRouter.of(context).toSegments(['rooms', roomIdResult.result]);
2021-04-15 09:05:41 +02:00
}
}
String validateForm(String value) {
if (value.isEmpty) {
return L10n.of(context).pleaseEnterAMatrixIdentifier;
}
2021-08-22 21:09:05 +02:00
if (!controller.text.isValidMatrixId ||
!supportedSigils.contains(controller.text.sigil)) {
2021-04-15 09:05:41 +02:00
return L10n.of(context).makeSureTheIdentifierIsValid;
}
2021-08-22 21:09:05 +02:00
if (controller.text == Matrix.of(context).client.userID) {
return L10n.of(context).youCannotInviteYourself;
2021-04-15 09:05:41 +02:00
}
return null;
}
void inviteAction() => FluffyShare.share(
2021-08-23 08:40:01 +02:00
'https://matrix.to/#/${Matrix.of(context).client.userID}',
2021-04-15 09:05:41 +02:00
context,
);
2021-08-23 18:36:13 +02:00
void openScannerAction() async {
final status = await Permission.camera.request();
if (!status.isGranted) return;
await showDialog(
context: context,
useRootNavigator: false,
builder: (_) => QrScannerModal(),
);
}
2021-08-22 21:19:22 +02:00
2021-04-15 09:05:41 +02:00
@override
2021-05-22 09:13:47 +02:00
Widget build(BuildContext context) => NewPrivateChatView(this);
2021-04-15 09:05:41 +02:00
}