2021-04-15 09:05:41 +02:00
|
|
|
import 'package:flutter/material.dart';
|
2021-10-26 18:50:34 +02:00
|
|
|
|
2022-02-15 09:25:13 +01: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-02-15 09:25:13 +01: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> {
|
2021-11-13 18:09:16 +01:00
|
|
|
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-15 09:25:13 +01:00
|
|
|
|
|
|
|
bool _hideFab = true;
|
|
|
|
bool _qrUnsupported = true;
|
|
|
|
|
|
|
|
bool get hideFab => !_qrUnsupported && _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/#/';
|
|
|
|
|
2021-11-13 18:09:16 +01:00
|
|
|
void setHideFab() {
|
2022-02-15 09:25:13 +01:00
|
|
|
if (textFieldFocus.hasFocus != _hideFab) {
|
|
|
|
setState(() => _hideFab = textFieldFocus.hasFocus);
|
2021-11-13 18:09:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
2022-02-15 09:25:13 +01:00
|
|
|
_checkQrSupported();
|
2021-11-13 18:09:16 +01:00
|
|
|
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);
|
2022-02-15 09:25:13 +01:00
|
|
|
|
|
|
|
// 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),
|
|
|
|
);
|
|
|
|
}
|
2021-04-15 09:05:41 +02:00
|
|
|
}
|