mirror of
https://gitlab.com/famedly/fluffychat.git
synced 2024-12-27 00:22:38 +01:00
e5c03ffb53
Signed-off-by: TheOneWithTheBraid <the-one@with-the-braid.cf>
103 lines
3.0 KiB
Dart
103 lines
3.0 KiB
Dart
import 'package:flutter/material.dart';
|
|
|
|
import 'package:device_info_plus/device_info_plus.dart';
|
|
import 'package:flutter_gen/gen_l10n/l10n.dart';
|
|
import 'package:matrix/matrix.dart';
|
|
import 'package:permission_handler/permission_handler.dart';
|
|
|
|
import 'package:fluffychat/pages/new_private_chat/new_private_chat_view.dart';
|
|
import 'package:fluffychat/pages/new_private_chat/qr_scanner_modal.dart';
|
|
import 'package:fluffychat/utils/fluffy_share.dart';
|
|
import 'package:fluffychat/utils/platform_infos.dart';
|
|
import 'package:fluffychat/utils/url_launcher.dart';
|
|
import 'package:fluffychat/widgets/matrix.dart';
|
|
|
|
class NewPrivateChat extends StatefulWidget {
|
|
const NewPrivateChat({Key? key}) : super(key: key);
|
|
|
|
@override
|
|
NewPrivateChatController createState() => NewPrivateChatController();
|
|
}
|
|
|
|
class NewPrivateChatController extends State<NewPrivateChat> {
|
|
final TextEditingController controller = TextEditingController();
|
|
final FocusNode textFieldFocus = FocusNode();
|
|
final formKey = GlobalKey<FormState>();
|
|
bool loading = false;
|
|
|
|
bool _hideFab = true;
|
|
bool _qrUnsupported = true;
|
|
|
|
bool get hideFab => !_qrUnsupported && _hideFab;
|
|
|
|
static const Set<String> supportedSigils = {'@', '!', '#'};
|
|
|
|
static const String prefix = 'https://matrix.to/#/';
|
|
|
|
void setHideFab() {
|
|
if (textFieldFocus.hasFocus != _hideFab) {
|
|
setState(() => _hideFab = textFieldFocus.hasFocus);
|
|
}
|
|
}
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_checkQrSupported();
|
|
textFieldFocus.addListener(setHideFab);
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
textFieldFocus.removeListener(setHideFab);
|
|
super.dispose();
|
|
}
|
|
|
|
void submitAction([_]) async {
|
|
controller.text = controller.text.trim();
|
|
if (!formKey.currentState!.validate()) return;
|
|
UrlLauncher(context, '$prefix${controller.text}').openMatrixToUrl();
|
|
}
|
|
|
|
String? validateForm(String? value) {
|
|
if (value!.isEmpty) {
|
|
return L10n.of(context)!.pleaseEnterAMatrixIdentifier;
|
|
}
|
|
if (!controller.text.isValidMatrixId ||
|
|
!supportedSigils.contains(controller.text.sigil)) {
|
|
return L10n.of(context)!.makeSureTheIdentifierIsValid;
|
|
}
|
|
if (controller.text == Matrix.of(context).client.userID) {
|
|
return L10n.of(context)!.youCannotInviteYourself;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
void inviteAction() => FluffyShare.share(
|
|
'https://matrix.to/#/${Matrix.of(context).client.userID}',
|
|
context,
|
|
);
|
|
|
|
void openScannerAction() async {
|
|
await Permission.camera.request();
|
|
await showModalBottomSheet(
|
|
context: context,
|
|
useRootNavigator: false,
|
|
//useSafeArea: false,
|
|
builder: (_) => const QrScannerModal(),
|
|
);
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) => NewPrivateChatView(this);
|
|
|
|
// checks whether Android < 21 in order to support Android KitKat
|
|
void _checkQrSupported() {
|
|
if (!PlatformInfos.isAndroid) _qrUnsupported = false;
|
|
DeviceInfoPlugin().androidInfo.then(
|
|
(info) =>
|
|
setState(() => _qrUnsupported = (info.version.sdkInt ?? 16) < 21),
|
|
);
|
|
}
|
|
}
|