fluffychat/lib/pages/new_private_chat.dart

66 lines
2.1 KiB
Dart
Raw Normal View History

2021-08-22 21:19:22 +02:00
import 'package:fluffychat/pages/qr_scanner_modal.dart';
2021-08-24 14:15:35 +02:00
import 'package:fluffychat/utils/url_launcher.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-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-04-15 09:05:41 +02:00
class NewPrivateChat extends StatefulWidget {
2021-10-14 18:09:30 +02:00
const NewPrivateChat({Key key}) : super(key: key);
2021-04-15 09:05:41 +02:00
@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
2021-08-24 14:15:35 +02:00
static const String prefix = 'https://matrix.to/#/';
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-24 14:15:35 +02:00
UrlLauncher(context, '$prefix${controller.text}').openMatrixToUrl();
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;
2021-08-24 14:07:49 +02:00
await showModalBottomSheet(
2021-08-23 18:36:13 +02:00
context: context,
useRootNavigator: false,
2021-08-24 14:07:49 +02:00
//useSafeArea: false,
2021-10-14 18:09:30 +02:00
builder: (_) => const QrScannerModal(),
2021-08-23 18:36:13 +02:00
);
}
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
}