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:famedlysdk/encryption.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_gen/gen_l10n/l10n.dart';
import '../avatar.dart';
import 'simple_dialogs.dart';
import '../../utils/string_color.dart';
import '../components/adaptive_page_layout.dart';
import '../components/avatar.dart';
import '../components/dialogs/simple_dialogs.dart';
import '../utils/string_color.dart';
import 'chat_list.dart';
class KeyVerificationDialog extends StatefulWidget {
Future<void> show(BuildContext context) => PlatformInfos.isCupertinoStyle
? showCupertinoDialog(context: context, builder: (context) => this)
: showDialog(context: context, builder: (context) => this);
class KeyVerificationView extends StatelessWidget {
final KeyVerification request;
KeyVerificationView({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});
KeyVerificationDialog({this.request});
@override
_KeyVerificationPageState createState() => _KeyVerificationPageState();
}
class _KeyVerificationPageState extends State<KeyVerificationPage> {
class _KeyVerificationPageState extends State<KeyVerificationDialog> {
void Function() originalOnUpdate;
@override
@ -133,20 +121,14 @@ class _KeyVerificationPageState extends State<KeyVerificationPage> {
mainAxisSize: MainAxisSize.min,
),
);
buttons.add(RaisedButton(
color: Theme.of(context).primaryColor,
elevation: 5,
textColor: Colors.white,
buttons.add(_AdaptiveFlatButton(
child: Text(L10n.of(context).submit),
onPressed: () {
input = textEditingController.text;
checkInput();
},
));
buttons.add(RaisedButton(
textColor: Theme.of(context).primaryColor,
elevation: 5,
color: Colors.white,
buttons.add(_AdaptiveFlatButton(
child: Text(L10n.of(context).skip),
onPressed: () => widget.request.openSSSS(skip: true),
));
@ -158,17 +140,11 @@ class _KeyVerificationPageState extends State<KeyVerificationPage> {
style: TextStyle(fontSize: 20)),
margin: EdgeInsets.only(left: 8.0, right: 8.0),
);
buttons.add(RaisedButton(
color: Theme.of(context).primaryColor,
elevation: 5,
textColor: Colors.white,
buttons.add(_AdaptiveFlatButton(
child: Text(L10n.of(context).accept),
onPressed: () => widget.request.acceptVerification(),
));
buttons.add(RaisedButton(
textColor: Theme.of(context).primaryColor,
elevation: 5,
color: Colors.white,
buttons.add(_AdaptiveFlatButton(
child: Text(L10n.of(context).reject),
onPressed: () {
widget.request.rejectVerification().then((_) {
@ -180,7 +156,9 @@ class _KeyVerificationPageState extends State<KeyVerificationPage> {
case KeyVerificationState.waitingAccept:
body = Column(
children: <Widget>[
CircularProgressIndicator(),
PlatformInfos.isCupertinoStyle
? CupertinoActivityIndicator()
: CircularProgressIndicator(),
SizedBox(height: 10),
Text(
L10n.of(context).waitingPartnerAcceptRequest,
@ -214,7 +192,7 @@ class _KeyVerificationPageState extends State<KeyVerificationPage> {
Center(
child: Text(
compareText,
style: TextStyle(fontSize: 20),
style: TextStyle(fontSize: 16),
textAlign: TextAlign.center,
),
),
@ -226,23 +204,12 @@ class _KeyVerificationPageState extends State<KeyVerificationPage> {
],
mainAxisSize: MainAxisSize.min,
);
buttons.add(RaisedButton(
color: Theme.of(context).primaryColor,
elevation: 7,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(6),
),
textColor: Colors.white,
buttons.add(_AdaptiveFlatButton(
child: Text(L10n.of(context).theyMatch),
onPressed: () => widget.request.acceptSas(),
));
buttons.add(RaisedButton(
textColor: Theme.of(context).primaryColor,
elevation: 7,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(6),
),
color: Colors.white,
buttons.add(_AdaptiveFlatButton(
textColor: Colors.red,
child: Text(L10n.of(context).theyDontMatch),
onPressed: () => widget.request.rejectSas(),
));
@ -253,7 +220,9 @@ class _KeyVerificationPageState extends State<KeyVerificationPage> {
: L10n.of(context).waitingPartnerNumbers;
body = Column(
children: <Widget>[
CircularProgressIndicator(),
PlatformInfos.isCupertinoStyle
? CupertinoActivityIndicator()
: CircularProgressIndicator(),
SizedBox(height: 10),
Text(
acceptText,
@ -275,13 +244,7 @@ class _KeyVerificationPageState extends State<KeyVerificationPage> {
],
mainAxisSize: MainAxisSize.min,
);
buttons.add(RaisedButton(
color: Theme.of(context).primaryColor,
elevation: 7,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(6),
),
textColor: Colors.white,
buttons.add(_AdaptiveFlatButton(
child: Text(L10n.of(context).close),
onPressed: () => Navigator.of(context).pop(),
));
@ -298,13 +261,7 @@ class _KeyVerificationPageState extends State<KeyVerificationPage> {
],
mainAxisSize: MainAxisSize.min,
);
buttons.add(RaisedButton(
color: Theme.of(context).primaryColor,
elevation: 7,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(6),
),
textColor: Colors.white,
buttons.add(FlatButton(
child: Text(L10n.of(context).close),
onPressed: () => Navigator.of(context).pop(),
));
@ -328,13 +285,8 @@ class _KeyVerificationPageState extends State<KeyVerificationPage> {
style: TextStyle(color: Theme.of(context).textTheme.caption.color)),
);
}
return Scaffold(
appBar: AppBar(
title: ListTile(
leading: Avatar(profile?.avatarUrl, otherName),
contentPadding: EdgeInsets.zero,
subtitle: Text(L10n.of(context).verifyTitle, maxLines: 1),
title: Row(
final userNameTitle = Row(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Text(
otherName,
@ -354,26 +306,38 @@ class _KeyVerificationPageState extends State<KeyVerificationPage> {
),
],
crossAxisAlignment: CrossAxisAlignment.start,
),
),
elevation: 0,
),
body: SafeArea(
child: Column(
);
final title = PlatformInfos.isCupertinoStyle
? Text(L10n.of(context).verifyTitle)
: ListTile(
leading: Avatar(profile?.avatarUrl, otherName),
contentPadding: EdgeInsets.zero,
subtitle: Text(L10n.of(context).verifyTitle, maxLines: 1),
title: userNameTitle,
);
final content = Column(
mainAxisSize: MainAxisSize.min,
children: [
Expanded(
child: Center(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: body,
),
),
),
if (PlatformInfos.isCupertinoStyle) ...{
SizedBox(height: 8),
Center(child: userNameTitle),
SizedBox(height: 12),
},
body,
if (bottom != null) bottom,
],
),
),
persistentFooterButtons: buttons.isEmpty ? null : buttons,
);
if (PlatformInfos.isCupertinoStyle) {
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:desktop_notifications/desktop_notifications.dart';*/
import '../utils/app_route.dart';
import '../utils/beautify_string_extension.dart';
import '../utils/famedlysdk_store.dart';
import '../views/key_verification.dart';
import 'dialogs/key_verification_dialog.dart';
import '../utils/platform_infos.dart';
import '../config/app_config.dart';
import '../config/setting_keys.dart';
@ -324,12 +323,7 @@ class MatrixState extends State<Matrix> {
request.onUpdate = null;
hidPopup = true;
await request.acceptVerification();
await Navigator.of(context).push(
AppRoute.defaultRoute(
context,
KeyVerificationView(request: request),
),
);
await KeyVerificationDialog(request: request).show(context);
} else {
request.onUpdate = null;
hidPopup = true;

View File

@ -3,10 +3,9 @@ import 'package:famedlysdk/famedlysdk.dart';
import 'package:fluffychat/components/audio_player.dart';
import 'package:fluffychat/components/dialogs/simple_dialogs.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/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:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/l10n.dart';
@ -53,12 +52,7 @@ class MessageContent extends StatelessWidget {
timeline.cancelSubscriptions();
}
};
await Navigator.of(context).push(
AppRoute.defaultRoute(
context,
KeyVerificationView(request: req),
),
);
await KeyVerificationDialog(request: req).show(context);
} else {
final success = await SimpleDialogs(context).tryRequestWithLoadingDialog(
event.requestKey(),

View File

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

View File

@ -5,6 +5,9 @@ import 'package:flutter/foundation.dart';
abstract class PlatformInfos {
static bool get isWeb => kIsWeb;
static bool get isCupertinoStyle =>
!kIsWeb && (Platform.isIOS || Platform.isMacOS);
static bool get isMobile => !kIsWeb && (Platform.isAndroid || Platform.isIOS);
/// 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:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/l10n.dart';
import '../utils/app_route.dart';
import 'key_verification.dart';
import '../components/dialogs/key_verification_dialog.dart';
class ChatEncryptionSettingsView extends StatelessWidget {
final String id;
@ -55,12 +54,7 @@ class _ChatEncryptionSettingsState extends State<ChatEncryptionSettings> {
setState(() => null);
}
};
await Navigator.of(context).push(
AppRoute.defaultRoute(
context,
KeyVerificationView(request: req),
),
);
await KeyVerificationDialog(request: req).show(context);
break;
case 'verify_manual':
if (await showOkCancelAlertDialog(
@ -83,12 +77,7 @@ class _ChatEncryptionSettingsState extends State<ChatEncryptionSettings> {
setState(() => null);
}
};
await Navigator.of(context).push(
AppRoute.defaultRoute(
context,
KeyVerificationView(request: req),
),
);
await KeyVerificationDialog(request: req).show(context);
break;
case 'block':
if (key.directVerified) {