refactor: Make verification in dialogs

This commit is contained in:
Christian Pauly 2020-11-22 22:48:10 +01:00
parent 1aa9c08a3e
commit 1f9e953296
6 changed files with 118 additions and 150 deletions

View File

@ -1,40 +1,28 @@
import 'package:adaptive_dialog/adaptive_dialog.dart'; import 'package:adaptive_dialog/adaptive_dialog.dart';
import 'package:famedlysdk/encryption.dart'; import 'package:famedlysdk/encryption.dart';
import 'package:famedlysdk/matrix_api.dart'; import 'package:famedlysdk/matrix_api.dart';
import 'package:fluffychat/utils/platform_infos.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/l10n.dart'; import 'package:flutter_gen/gen_l10n/l10n.dart';
import '../avatar.dart';
import 'simple_dialogs.dart';
import '../../utils/string_color.dart';
import '../components/adaptive_page_layout.dart'; class KeyVerificationDialog extends StatefulWidget {
import '../components/avatar.dart'; Future<void> show(BuildContext context) => PlatformInfos.isCupertinoStyle
import '../components/dialogs/simple_dialogs.dart'; ? showCupertinoDialog(context: context, builder: (context) => this)
import '../utils/string_color.dart'; : showDialog(context: context, builder: (context) => this);
import 'chat_list.dart';
class KeyVerificationView extends StatelessWidget {
final KeyVerification request; final KeyVerification request;
KeyVerificationView({this.request}); KeyVerificationDialog({this.request});
@override
Widget build(BuildContext context) {
return AdaptivePageLayout(
primaryPage: FocusPage.SECOND,
firstScaffold: ChatList(),
secondScaffold: KeyVerificationPage(request: request),
);
}
}
class KeyVerificationPage extends StatefulWidget {
final KeyVerification request;
KeyVerificationPage({this.request});
@override @override
_KeyVerificationPageState createState() => _KeyVerificationPageState(); _KeyVerificationPageState createState() => _KeyVerificationPageState();
} }
class _KeyVerificationPageState extends State<KeyVerificationPage> { class _KeyVerificationPageState extends State<KeyVerificationDialog> {
void Function() originalOnUpdate; void Function() originalOnUpdate;
@override @override
@ -133,20 +121,14 @@ class _KeyVerificationPageState extends State<KeyVerificationPage> {
mainAxisSize: MainAxisSize.min, mainAxisSize: MainAxisSize.min,
), ),
); );
buttons.add(RaisedButton( buttons.add(_AdaptiveFlatButton(
color: Theme.of(context).primaryColor,
elevation: 5,
textColor: Colors.white,
child: Text(L10n.of(context).submit), child: Text(L10n.of(context).submit),
onPressed: () { onPressed: () {
input = textEditingController.text; input = textEditingController.text;
checkInput(); checkInput();
}, },
)); ));
buttons.add(RaisedButton( buttons.add(_AdaptiveFlatButton(
textColor: Theme.of(context).primaryColor,
elevation: 5,
color: Colors.white,
child: Text(L10n.of(context).skip), child: Text(L10n.of(context).skip),
onPressed: () => widget.request.openSSSS(skip: true), onPressed: () => widget.request.openSSSS(skip: true),
)); ));
@ -158,17 +140,11 @@ class _KeyVerificationPageState extends State<KeyVerificationPage> {
style: TextStyle(fontSize: 20)), style: TextStyle(fontSize: 20)),
margin: EdgeInsets.only(left: 8.0, right: 8.0), margin: EdgeInsets.only(left: 8.0, right: 8.0),
); );
buttons.add(RaisedButton( buttons.add(_AdaptiveFlatButton(
color: Theme.of(context).primaryColor,
elevation: 5,
textColor: Colors.white,
child: Text(L10n.of(context).accept), child: Text(L10n.of(context).accept),
onPressed: () => widget.request.acceptVerification(), onPressed: () => widget.request.acceptVerification(),
)); ));
buttons.add(RaisedButton( buttons.add(_AdaptiveFlatButton(
textColor: Theme.of(context).primaryColor,
elevation: 5,
color: Colors.white,
child: Text(L10n.of(context).reject), child: Text(L10n.of(context).reject),
onPressed: () { onPressed: () {
widget.request.rejectVerification().then((_) { widget.request.rejectVerification().then((_) {
@ -180,7 +156,9 @@ class _KeyVerificationPageState extends State<KeyVerificationPage> {
case KeyVerificationState.waitingAccept: case KeyVerificationState.waitingAccept:
body = Column( body = Column(
children: <Widget>[ children: <Widget>[
CircularProgressIndicator(), PlatformInfos.isCupertinoStyle
? CupertinoActivityIndicator()
: CircularProgressIndicator(),
SizedBox(height: 10), SizedBox(height: 10),
Text( Text(
L10n.of(context).waitingPartnerAcceptRequest, L10n.of(context).waitingPartnerAcceptRequest,
@ -214,7 +192,7 @@ class _KeyVerificationPageState extends State<KeyVerificationPage> {
Center( Center(
child: Text( child: Text(
compareText, compareText,
style: TextStyle(fontSize: 20), style: TextStyle(fontSize: 16),
textAlign: TextAlign.center, textAlign: TextAlign.center,
), ),
), ),
@ -226,23 +204,12 @@ class _KeyVerificationPageState extends State<KeyVerificationPage> {
], ],
mainAxisSize: MainAxisSize.min, mainAxisSize: MainAxisSize.min,
); );
buttons.add(RaisedButton( buttons.add(_AdaptiveFlatButton(
color: Theme.of(context).primaryColor,
elevation: 7,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(6),
),
textColor: Colors.white,
child: Text(L10n.of(context).theyMatch), child: Text(L10n.of(context).theyMatch),
onPressed: () => widget.request.acceptSas(), onPressed: () => widget.request.acceptSas(),
)); ));
buttons.add(RaisedButton( buttons.add(_AdaptiveFlatButton(
textColor: Theme.of(context).primaryColor, textColor: Colors.red,
elevation: 7,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(6),
),
color: Colors.white,
child: Text(L10n.of(context).theyDontMatch), child: Text(L10n.of(context).theyDontMatch),
onPressed: () => widget.request.rejectSas(), onPressed: () => widget.request.rejectSas(),
)); ));
@ -253,7 +220,9 @@ class _KeyVerificationPageState extends State<KeyVerificationPage> {
: L10n.of(context).waitingPartnerNumbers; : L10n.of(context).waitingPartnerNumbers;
body = Column( body = Column(
children: <Widget>[ children: <Widget>[
CircularProgressIndicator(), PlatformInfos.isCupertinoStyle
? CupertinoActivityIndicator()
: CircularProgressIndicator(),
SizedBox(height: 10), SizedBox(height: 10),
Text( Text(
acceptText, acceptText,
@ -275,13 +244,7 @@ class _KeyVerificationPageState extends State<KeyVerificationPage> {
], ],
mainAxisSize: MainAxisSize.min, mainAxisSize: MainAxisSize.min,
); );
buttons.add(RaisedButton( buttons.add(_AdaptiveFlatButton(
color: Theme.of(context).primaryColor,
elevation: 7,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(6),
),
textColor: Colors.white,
child: Text(L10n.of(context).close), child: Text(L10n.of(context).close),
onPressed: () => Navigator.of(context).pop(), onPressed: () => Navigator.of(context).pop(),
)); ));
@ -298,13 +261,7 @@ class _KeyVerificationPageState extends State<KeyVerificationPage> {
], ],
mainAxisSize: MainAxisSize.min, mainAxisSize: MainAxisSize.min,
); );
buttons.add(RaisedButton( buttons.add(FlatButton(
color: Theme.of(context).primaryColor,
elevation: 7,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(6),
),
textColor: Colors.white,
child: Text(L10n.of(context).close), child: Text(L10n.of(context).close),
onPressed: () => Navigator.of(context).pop(), onPressed: () => Navigator.of(context).pop(),
)); ));
@ -328,13 +285,8 @@ class _KeyVerificationPageState extends State<KeyVerificationPage> {
style: TextStyle(color: Theme.of(context).textTheme.caption.color)), style: TextStyle(color: Theme.of(context).textTheme.caption.color)),
); );
} }
return Scaffold( final userNameTitle = Row(
appBar: AppBar( mainAxisSize: MainAxisSize.min,
title: ListTile(
leading: Avatar(profile?.avatarUrl, otherName),
contentPadding: EdgeInsets.zero,
subtitle: Text(L10n.of(context).verifyTitle, maxLines: 1),
title: Row(
children: <Widget>[ children: <Widget>[
Text( Text(
otherName, otherName,
@ -354,26 +306,38 @@ class _KeyVerificationPageState extends State<KeyVerificationPage> {
), ),
], ],
crossAxisAlignment: CrossAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start,
), );
), final title = PlatformInfos.isCupertinoStyle
elevation: 0, ? Text(L10n.of(context).verifyTitle)
), : ListTile(
body: SafeArea( leading: Avatar(profile?.avatarUrl, otherName),
child: Column( contentPadding: EdgeInsets.zero,
subtitle: Text(L10n.of(context).verifyTitle, maxLines: 1),
title: userNameTitle,
);
final content = Column(
mainAxisSize: MainAxisSize.min,
children: [ children: [
Expanded( if (PlatformInfos.isCupertinoStyle) ...{
child: Center( SizedBox(height: 8),
child: Padding( Center(child: userNameTitle),
padding: const EdgeInsets.all(16.0), SizedBox(height: 12),
child: body, },
), body,
),
),
if (bottom != null) bottom, if (bottom != null) bottom,
], ],
), );
), if (PlatformInfos.isCupertinoStyle) {
persistentFooterButtons: buttons.isEmpty ? null : buttons, return CupertinoAlertDialog(
title: title,
content: content,
actions: buttons,
);
}
return AlertDialog(
title: title,
content: content,
actions: buttons,
); );
} }
} }
@ -395,3 +359,32 @@ class _Emoji extends StatelessWidget {
); );
} }
} }
class _AdaptiveFlatButton extends StatelessWidget {
final Widget child;
final Color textColor;
final Function onPressed;
const _AdaptiveFlatButton({
Key key,
this.child,
this.textColor,
this.onPressed,
}) : super(key: key);
@override
Widget build(BuildContext context) {
if (PlatformInfos.isCupertinoStyle) {
return CupertinoDialogAction(
child: child,
onPressed: onPressed,
textStyle: textColor != null ? TextStyle(color: textColor) : null,
);
}
return FlatButton(
child: child,
textColor: textColor,
onPressed: onPressed,
);
}
}

View File

@ -20,10 +20,9 @@ import 'package:fluffychat/config/app_config.dart';
import 'package:dbus/dbus.dart'; import 'package:dbus/dbus.dart';
import 'package:desktop_notifications/desktop_notifications.dart';*/ import 'package:desktop_notifications/desktop_notifications.dart';*/
import '../utils/app_route.dart';
import '../utils/beautify_string_extension.dart'; import '../utils/beautify_string_extension.dart';
import '../utils/famedlysdk_store.dart'; import '../utils/famedlysdk_store.dart';
import '../views/key_verification.dart'; import 'dialogs/key_verification_dialog.dart';
import '../utils/platform_infos.dart'; import '../utils/platform_infos.dart';
import '../config/app_config.dart'; import '../config/app_config.dart';
import '../config/setting_keys.dart'; import '../config/setting_keys.dart';
@ -324,12 +323,7 @@ class MatrixState extends State<Matrix> {
request.onUpdate = null; request.onUpdate = null;
hidPopup = true; hidPopup = true;
await request.acceptVerification(); await request.acceptVerification();
await Navigator.of(context).push( await KeyVerificationDialog(request: request).show(context);
AppRoute.defaultRoute(
context,
KeyVerificationView(request: request),
),
);
} else { } else {
request.onUpdate = null; request.onUpdate = null;
hidPopup = true; hidPopup = true;

View File

@ -3,10 +3,9 @@ import 'package:famedlysdk/famedlysdk.dart';
import 'package:fluffychat/components/audio_player.dart'; import 'package:fluffychat/components/audio_player.dart';
import 'package:fluffychat/components/dialogs/simple_dialogs.dart'; import 'package:fluffychat/components/dialogs/simple_dialogs.dart';
import 'package:fluffychat/components/image_bubble.dart'; import 'package:fluffychat/components/image_bubble.dart';
import 'package:fluffychat/utils/app_route.dart';
import 'package:fluffychat/utils/event_extension.dart'; import 'package:fluffychat/utils/event_extension.dart';
import 'package:fluffychat/utils/matrix_locals.dart'; import 'package:fluffychat/utils/matrix_locals.dart';
import 'package:fluffychat/views/key_verification.dart'; import 'package:fluffychat/components/dialogs/key_verification_dialog.dart';
import 'package:flushbar/flushbar_helper.dart'; import 'package:flushbar/flushbar_helper.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/l10n.dart'; import 'package:flutter_gen/gen_l10n/l10n.dart';
@ -53,12 +52,7 @@ class MessageContent extends StatelessWidget {
timeline.cancelSubscriptions(); timeline.cancelSubscriptions();
} }
}; };
await Navigator.of(context).push( await KeyVerificationDialog(request: req).show(context);
AppRoute.defaultRoute(
context,
KeyVerificationView(request: req),
),
);
} else { } else {
final success = await SimpleDialogs(context).tryRequestWithLoadingDialog( final success = await SimpleDialogs(context).tryRequestWithLoadingDialog(
event.requestKey(), event.requestKey(),

View File

@ -13,7 +13,7 @@ import 'package:flutter_gen/gen_l10n/l10n.dart';
import '../utils/presence_extension.dart'; import '../utils/presence_extension.dart';
import 'dialogs/simple_dialogs.dart'; import 'dialogs/simple_dialogs.dart';
import 'matrix.dart'; import 'matrix.dart';
import '../views/key_verification.dart'; import 'dialogs/key_verification_dialog.dart';
import '../utils/app_route.dart'; import '../utils/app_route.dart';
class UserBottomSheet extends StatelessWidget { class UserBottomSheet extends StatelessWidget {
@ -88,12 +88,7 @@ class UserBottomSheet extends StatelessWidget {
void _verifyAction(BuildContext context) async { void _verifyAction(BuildContext context) async {
final client = Matrix.of(context).client; final client = Matrix.of(context).client;
final req = await client.userDeviceKeys[user.id].startVerification(); final req = await client.userDeviceKeys[user.id].startVerification();
await Navigator.of(context).push( await KeyVerificationDialog(request: req).show(context);
AppRoute.defaultRoute(
context,
KeyVerificationView(request: req),
),
);
} }
@override @override

View File

@ -5,6 +5,9 @@ import 'package:flutter/foundation.dart';
abstract class PlatformInfos { abstract class PlatformInfos {
static bool get isWeb => kIsWeb; static bool get isWeb => kIsWeb;
static bool get isCupertinoStyle =>
!kIsWeb && (Platform.isIOS || Platform.isMacOS);
static bool get isMobile => !kIsWeb && (Platform.isAndroid || Platform.isIOS); static bool get isMobile => !kIsWeb && (Platform.isAndroid || Platform.isIOS);
/// For desktops which don't support ChachedNetworkImage yet /// For desktops which don't support ChachedNetworkImage yet

View File

@ -9,8 +9,7 @@ import 'package:fluffychat/views/chat_list.dart';
import 'package:flushbar/flushbar_helper.dart'; import 'package:flushbar/flushbar_helper.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/l10n.dart'; import 'package:flutter_gen/gen_l10n/l10n.dart';
import '../utils/app_route.dart'; import '../components/dialogs/key_verification_dialog.dart';
import 'key_verification.dart';
class ChatEncryptionSettingsView extends StatelessWidget { class ChatEncryptionSettingsView extends StatelessWidget {
final String id; final String id;
@ -55,12 +54,7 @@ class _ChatEncryptionSettingsState extends State<ChatEncryptionSettings> {
setState(() => null); setState(() => null);
} }
}; };
await Navigator.of(context).push( await KeyVerificationDialog(request: req).show(context);
AppRoute.defaultRoute(
context,
KeyVerificationView(request: req),
),
);
break; break;
case 'verify_manual': case 'verify_manual':
if (await showOkCancelAlertDialog( if (await showOkCancelAlertDialog(
@ -83,12 +77,7 @@ class _ChatEncryptionSettingsState extends State<ChatEncryptionSettings> {
setState(() => null); setState(() => null);
} }
}; };
await Navigator.of(context).push( await KeyVerificationDialog(request: req).show(context);
AppRoute.defaultRoute(
context,
KeyVerificationView(request: req),
),
);
break; break;
case 'block': case 'block':
if (key.directVerified) { if (key.directVerified) {