fluffychat/lib/pages/new_private_chat/new_private_chat.dart

113 lines
3.4 KiB
Dart
Raw Permalink Normal View History

2021-04-15 09:05:41 +02:00
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
2021-10-26 18:50:34 +02:00
import 'package:device_info_plus/device_info_plus.dart';
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';
2022-05-03 16:32:00 +02:00
import 'package:fluffychat/utils/platform_infos.dart';
2021-10-26 18:50:34 +02:00
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;
2022-02-20 10:35:37 +01:00
bool _hideFab = false;
// remove leading matrix.to from text field in order to simplify pasting
final List<TextInputFormatter> removeMatrixToFormatters = [
FilteringTextInputFormatter.deny(NewPrivateChatController.prefix),
FilteringTextInputFormatter.deny(NewPrivateChatController.prefixNoProtocol),
];
2022-02-20 10:35:37 +01:00
bool get hideFab => _hideFab;
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/#/';
static const String prefixNoProtocol = 'matrix.to/#/';
2021-08-24 14:15:35 +02:00
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 {
2022-05-03 16:31:51 +02:00
if (PlatformInfos.isAndroid) {
final info = await DeviceInfoPlugin().androidInfo;
if ((info.version.sdkInt ?? 16) < 21) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(
L10n.of(context)!.unsupportedAndroidVersionLong,
),
2022-02-20 10:35:37 +01:00
),
2022-05-03 16:31:51 +02:00
);
return;
}
2022-02-20 10:35:37 +01:00
}
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
}