fluffychat/lib/pages/new_private_chat/new_private_chat.dart

87 lines
2.5 KiB
Dart
Raw Normal View History

2021-04-15 09:05:41 +02:00
import 'package:flutter/material.dart';
2021-10-26 18:50:34 +02:00
2021-04-15 09:05:41 +02:00
import 'package:flutter_gen/gen_l10n/l10n.dart';
2021-10-26 18:50:34 +02:00
import 'package:matrix/matrix.dart';
2021-08-23 18:36:13 +02:00
import 'package:permission_handler/permission_handler.dart';
2021-04-15 09:05:41 +02:00
2021-11-09 21:32:16 +01:00
import 'package:fluffychat/pages/new_private_chat/new_private_chat_view.dart';
import 'package:fluffychat/pages/new_private_chat/qr_scanner_modal.dart';
2021-10-26 18:50:34 +02:00
import 'package:fluffychat/utils/fluffy_share.dart';
import 'package:fluffychat/utils/url_launcher.dart';
import 'package:fluffychat/widgets/matrix.dart';
2021-04-15 09:05:41 +02:00
class NewPrivateChat extends StatefulWidget {
2022-01-29 12:35:03 +01:00
const NewPrivateChat({Key? key}) : super(key: key);
2021-10-14 18:09:30 +02:00
2021-04-15 09:05:41 +02:00
@override
NewPrivateChatController createState() => NewPrivateChatController();
}
class NewPrivateChatController extends State<NewPrivateChat> {
final TextEditingController controller = TextEditingController();
final FocusNode textFieldFocus = FocusNode();
2021-04-15 09:05:41 +02:00
final formKey = GlobalKey<FormState>();
bool loading = false;
bool hideFab = 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/#/';
void setHideFab() {
if (textFieldFocus.hasFocus != hideFab) {
setState(() => hideFab = textFieldFocus.hasFocus);
}
}
@override
void initState() {
super.initState();
textFieldFocus.addListener(setHideFab);
}
@override
void dispose() {
textFieldFocus.removeListener(setHideFab);
super.dispose();
}
2021-04-15 09:05:41 +02:00
void submitAction([_]) async {
2021-08-22 21:09:05 +02:00
controller.text = controller.text.trim();
2022-01-29 12:35:03 +01: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
}
2022-01-29 12:35:03 +01:00
String? validateForm(String? value) {
if (value!.isEmpty) {
return L10n.of(context)!.pleaseEnterAMatrixIdentifier;
2021-04-15 09:05:41 +02:00
}
2021-08-22 21:09:05 +02:00
if (!controller.text.isValidMatrixId ||
!supportedSigils.contains(controller.text.sigil)) {
2022-01-29 12:35:03 +01:00
return L10n.of(context)!.makeSureTheIdentifierIsValid;
2021-04-15 09:05:41 +02:00
}
2021-08-22 21:09:05 +02:00
if (controller.text == Matrix.of(context).client.userID) {
2022-01-29 12:35:03 +01:00
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 {
2021-11-30 19:07:07 +01:00
await Permission.camera.request();
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
}