Skip to content

Commit

Permalink
refactor: Use adaptive dialog action
Browse files Browse the repository at this point in the history
  • Loading branch information
krille-chan committed Oct 18, 2024
1 parent 416cdc1 commit d1e211a
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 25 deletions.
11 changes: 5 additions & 6 deletions lib/pages/chat/send_file_dialog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import 'package:fluffychat/utils/localized_exception_extension.dart';
import 'package:fluffychat/utils/matrix_sdk_extensions/matrix_file_extension.dart';
import 'package:fluffychat/utils/platform_infos.dart';
import 'package:fluffychat/utils/size_string.dart';
import 'package:fluffychat/widgets/adaptive_dialog_action.dart';
import '../../utils/resize_video.dart';

class SendFileDialog extends StatefulWidget {
Expand Down Expand Up @@ -288,14 +289,12 @@ class SendFileDialogState extends State<SendFileDialog> {
title: Text(sendStr),
content: contentWidget,
actions: <Widget>[
TextButton(
onPressed: () {
// just close the dialog
Navigator.of(context, rootNavigator: false).pop();
},
AdaptiveDialogAction(
onPressed: () =>
Navigator.of(context, rootNavigator: false).pop(),
child: Text(L10n.of(context).cancel),
),
TextButton(
AdaptiveDialogAction(
onPressed: _send,
child: Text(L10n.of(context).send),
),
Expand Down
5 changes: 3 additions & 2 deletions lib/pages/chat/send_location_dialog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import 'package:geolocator/geolocator.dart';
import 'package:matrix/matrix.dart';

import 'package:fluffychat/pages/chat/events/map_bubble.dart';
import 'package:fluffychat/widgets/adaptive_dialog_action.dart';
import 'package:fluffychat/widgets/future_loading_dialog.dart';

class SendLocationDialog extends StatefulWidget {
Expand Down Expand Up @@ -114,12 +115,12 @@ class SendLocationDialogState extends State<SendLocationDialog> {
title: Text(L10n.of(context).shareLocation),
content: contentWidget,
actions: [
TextButton(
AdaptiveDialogAction(
onPressed: Navigator.of(context, rootNavigator: false).pop,
child: Text(L10n.of(context).cancel),
),
if (position != null)
TextButton(
AdaptiveDialogAction(
onPressed: isSending ? null : sendAction,
child: Text(L10n.of(context).send),
),
Expand Down
2 changes: 1 addition & 1 deletion lib/pages/chat_details/participant_list_item.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import 'package:fluffychat/config/app_config.dart';
import 'package:flutter/material.dart';

import 'package:flutter_gen/gen_l10n/l10n.dart';
import 'package:matrix/matrix.dart';

import 'package:fluffychat/config/app_config.dart';
import 'package:fluffychat/utils/adaptive_bottom_sheet.dart';
import '../../widgets/avatar.dart';
import '../user_bottom_sheet/user_bottom_sheet.dart';
Expand Down
5 changes: 3 additions & 2 deletions lib/pages/homeserver_picker/homeserver_picker_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'package:flutter_linkify/flutter_linkify.dart';
import 'package:url_launcher/url_launcher.dart';

import 'package:fluffychat/config/app_config.dart';
import 'package:fluffychat/widgets/adaptive_dialog_action.dart';
import 'package:fluffychat/widgets/layouts/login_scaffold.dart';
import 'package:fluffychat/widgets/matrix.dart';
import '../../config/themes.dart';
Expand Down Expand Up @@ -107,15 +108,15 @@ class HomeserverPickerView extends StatelessWidget {
text: L10n.of(context).homeserverDescription,
),
actions: [
TextButton(
AdaptiveDialogAction(
onPressed: () => launchUrl(
Uri.https('servers.joinmatrix.org'),
),
child: Text(
L10n.of(context).discoverHomeservers,
),
),
TextButton(
AdaptiveDialogAction(
onPressed: Navigator.of(context).pop,
child: Text(L10n.of(context).close),
),
Expand Down
9 changes: 5 additions & 4 deletions lib/pages/key_verification/key_verification_dialog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import 'package:flutter_gen/gen_l10n/l10n.dart';
import 'package:matrix/encryption.dart';
import 'package:matrix/matrix.dart';

import 'package:fluffychat/widgets/adaptive_dialog_action.dart';
import 'package:fluffychat/widgets/avatar.dart';
import 'package:fluffychat/widgets/future_loading_dialog.dart';

Expand Down Expand Up @@ -150,15 +151,15 @@ class KeyVerificationPageState extends State<KeyVerificationDialog> {
),
);
buttons.add(
TextButton(
AdaptiveDialogAction(
child: Text(
L10n.of(context).submit,
),
onPressed: () => checkInput(textEditingController.text),
),
);
buttons.add(
TextButton(
AdaptiveDialogAction(
child: Text(
L10n.of(context).skip,
),
Expand Down Expand Up @@ -320,7 +321,7 @@ class KeyVerificationPageState extends State<KeyVerificationDialog> {
],
);
buttons.add(
TextButton(
AdaptiveDialogAction(
child: Text(
L10n.of(context).close,
),
Expand All @@ -343,7 +344,7 @@ class KeyVerificationPageState extends State<KeyVerificationDialog> {
],
);
buttons.add(
TextButton(
AdaptiveDialogAction(
child: Text(
L10n.of(context).close,
),
Expand Down
7 changes: 4 additions & 3 deletions lib/utils/error_reporter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import 'package:matrix/matrix.dart';
import 'package:url_launcher/url_launcher.dart';

import 'package:fluffychat/config/app_config.dart';
import 'package:fluffychat/widgets/adaptive_dialog_action.dart';

class ErrorReporter {
final BuildContext context;
Expand All @@ -34,17 +35,17 @@ class ErrorReporter {
),
),
actions: [
TextButton(
AdaptiveDialogAction(
onPressed: () => Navigator.of(context).pop(),
child: Text(L10n.of(context).close),
),
TextButton(
AdaptiveDialogAction(
onPressed: () => Clipboard.setData(
ClipboardData(text: text),
),
child: Text(L10n.of(context).copy),
),
TextButton(
AdaptiveDialogAction(
onPressed: () => launchUrl(
AppConfig.newIssueUrl.resolveUri(
Uri(
Expand Down
15 changes: 9 additions & 6 deletions lib/utils/size_string.dart
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
extension SizeString on num {
String get sizeString {
var size = toDouble();
if (size < 1000000) {
if (size < 1000) {
return '${size.round()} Bytes';
}
if (size < 1000 * 1000) {
size = size / 1000;
size = (size * 10).round() / 10;
return '${size.toString()} KB';
} else if (size < 1000000000) {
}
if (size < 1000 * 1000 * 1000) {
size = size / 1000000;
size = (size * 10).round() / 10;
return '${size.toString()} MB';
} else {
size = size / 1000000000;
size = (size * 10).round() / 10;
return '${size.toString()} GB';
}
size = size / 1000 * 1000 * 1000 * 1000;
size = (size * 10).round() / 10;
return '${size.toString()} GB';
}
}
28 changes: 28 additions & 0 deletions lib/widgets/adaptive_dialog_action.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class AdaptiveDialogAction extends StatelessWidget {
final VoidCallback? onPressed;
final Widget child;

const AdaptiveDialogAction({
super.key,
required this.onPressed,
required this.child,
});

@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
switch (theme.platform) {
case TargetPlatform.android:
case TargetPlatform.fuchsia:
case TargetPlatform.linux:
case TargetPlatform.windows:
return TextButton(onPressed: onPressed, child: child);
case TargetPlatform.iOS:
case TargetPlatform.macOS:
return CupertinoDialogAction(onPressed: onPressed, child: child);
}
}
}
3 changes: 2 additions & 1 deletion lib/widgets/future_loading_dialog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'package:async/async.dart';
import 'package:flutter_gen/gen_l10n/l10n.dart';

import 'package:fluffychat/utils/localized_exception_extension.dart';
import 'package:fluffychat/widgets/adaptive_dialog_action.dart';

/// Displays a loading dialog which reacts to the given [future]. The dialog
/// will be dismissed and the value will be returned when the future completes.
Expand Down Expand Up @@ -120,7 +121,7 @@ class LoadingDialogState<T> extends State<LoadingDialog> {
actions: exception == null
? null
: [
TextButton(
AdaptiveDialogAction(
onPressed: () => Navigator.of(context).pop<Result<T>>(
Result.error(
exception,
Expand Down

0 comments on commit d1e211a

Please sign in to comment.