From fcdf5a7ee4178682d756808c76deb6ceed813267 Mon Sep 17 00:00:00 2001 From: Krille Fear Date: Tue, 26 Oct 2021 18:47:05 +0200 Subject: [PATCH] refactor: Magic numbers --- lib/main.dart | 3 +- lib/pages/chat.dart | 78 +++++++++++++------------- lib/pages/chat_details.dart | 4 +- lib/pages/image_viewer.dart | 4 +- lib/pages/key_verification_dialog.dart | 13 ++--- lib/pages/search.dart | 7 ++- lib/pages/send_file_dialog.dart | 4 +- lib/pages/settings_emotes.dart | 4 +- 8 files changed, 63 insertions(+), 54 deletions(-) diff --git a/lib/main.dart b/lib/main.dart index a01bf7b0..ce2bfd77 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -99,9 +99,10 @@ class _FluffyChatAppState extends State { initial: AdaptiveThemeMode.system, builder: (theme, darkTheme) => LayoutBuilder( builder: (context, constraints) { + const maxColumns = 3; var newColumns = (constraints.maxWidth / FluffyThemes.columnWidth).floor(); - if (newColumns > 3) newColumns = 3; + if (newColumns > maxColumns) newColumns = maxColumns; columnMode ??= newColumns > 1; _router ??= GlobalKey(); if (columnMode != newColumns > 1) { diff --git a/lib/pages/chat.dart b/lib/pages/chat.dart index f2337ed2..76457978 100644 --- a/lib/pages/chat.dart +++ b/lib/pages/chat.dart @@ -552,50 +552,50 @@ class ChatController extends State { if (eventIndex == -1) { // event id not found...maybe we can fetch it? // the try...finally is here to start and close the loading dialog reliably - final task = Future.microtask(() async { - // okay, we first have to fetch if the event is in the room - try { - final event = await timeline.getEventById(eventId); - if (event == null) { - // event is null...meaning something is off - return; - } - } catch (err) { - if (err is MatrixException && err.errcode == 'M_NOT_FOUND') { - // event wasn't found, as the server gave a 404 or something - return; - } - rethrow; - } - // okay, we know that the event *is* in the room - while (eventIndex == -1) { - if (!canLoadMore) { - // we can't load any more events but still haven't found ours yet...better stop here - return; - } - try { - await timeline.requestHistory(historyCount: _loadHistoryCount); - } catch (err) { - if (err is TimeoutException) { - // loading the history timed out...so let's do nothing - return; + await showFutureLoadingDialog( + context: context, + future: () async { + // okay, we first have to fetch if the event is in the room + try { + final event = await timeline.getEventById(eventId); + if (event == null) { + // event is null...meaning something is off + return; + } + } catch (err) { + if (err is MatrixException && err.errcode == 'M_NOT_FOUND') { + // event wasn't found, as the server gave a 404 or something + return; + } + rethrow; } - rethrow; - } - eventIndex = filteredEvents.indexWhere((e) => e.eventId == eventId); - } - }); - if (context != null) { - await showFutureLoadingDialog(context: context, future: () => task); - } else { - await task; - } + // okay, we know that the event *is* in the room + while (eventIndex == -1) { + if (!canLoadMore) { + // we can't load any more events but still haven't found ours yet...better stop here + return; + } + try { + await timeline.requestHistory(historyCount: _loadHistoryCount); + } catch (err) { + if (err is TimeoutException) { + // loading the history timed out...so let's do nothing + return; + } + rethrow; + } + eventIndex = + filteredEvents.indexWhere((e) => e.eventId == eventId); + } + }); } if (!mounted) { return; } - await scrollController.scrollToIndex(eventIndex, - preferPosition: AutoScrollPosition.middle); + await scrollController.scrollToIndex( + eventIndex, + preferPosition: AutoScrollPosition.middle, + ); _updateScrollController(); } diff --git a/lib/pages/chat_details.dart b/lib/pages/chat_details.dart index e2bf5acd..fb076676 100644 --- a/lib/pages/chat_details.dart +++ b/lib/pages/chat_details.dart @@ -294,11 +294,13 @@ class ChatDetailsController extends State { } } + static const fixedWidth = 360.0; + @override Widget build(BuildContext context) { members ??= Matrix.of(context).client.getRoomById(roomId).getParticipants(); return SizedBox( - width: 360.0, + width: fixedWidth, child: ChatDetailsView(this), ); } diff --git a/lib/pages/image_viewer.dart b/lib/pages/image_viewer.dart index df77d783..e03e92d5 100644 --- a/lib/pages/image_viewer.dart +++ b/lib/pages/image_viewer.dart @@ -27,11 +27,13 @@ class ImageViewerController extends State { /// Save this file with a system call. void saveFileAction() => widget.event.saveFile(context); + static const maxScaleFactor = 1.5; + /// Go back if user swiped it away void onInteractionEnds(ScaleEndDetails endDetails) { if (PlatformInfos.usesTouchscreen == false) { if (endDetails.velocity.pixelsPerSecond.dy > - MediaQuery.of(context).size.height * 1.50) { + MediaQuery.of(context).size.height * maxScaleFactor) { Navigator.of(context, rootNavigator: false).pop(); } } diff --git a/lib/pages/key_verification_dialog.dart b/lib/pages/key_verification_dialog.dart index c352e300..633b0c5f 100644 --- a/lib/pages/key_verification_dialog.dart +++ b/lib/pages/key_verification_dialog.dart @@ -155,10 +155,7 @@ class _KeyVerificationPageState extends State { ); buttons.add(AdaptiveFlatButton( label: L10n.of(context).submit, - onPressed: () { - input = textEditingController.text; - checkInput(input); - }, + onPressed: () => checkInput(textEditingController.text), )); buttons.add(AdaptiveFlatButton( label: L10n.of(context).skip, @@ -207,11 +204,9 @@ class _KeyVerificationPageState extends State { buttons.add(AdaptiveFlatButton( label: L10n.of(context).reject, textColor: Colors.red, - onPressed: () { - widget.request.rejectVerification().then((_) { - Navigator.of(context, rootNavigator: false).pop(); - }); - }, + onPressed: () => widget.request + .rejectVerification() + .then((_) => Navigator.of(context, rootNavigator: false).pop()), )); buttons.add(AdaptiveFlatButton( label: L10n.of(context).accept, diff --git a/lib/pages/search.dart b/lib/pages/search.dart index 1cc05f2c..227f988b 100644 --- a/lib/pages/search.dart +++ b/lib/pages/search.dart @@ -104,6 +104,8 @@ class SearchController extends State { String currentSearchTerm; List foundProfiles = []; + static const searchUserDirectoryLimit = 10; + void searchUser(BuildContext context, String text) async { if (text.isEmpty) { setState(() { @@ -115,7 +117,10 @@ class SearchController extends State { final matrix = Matrix.of(context); SearchUserDirectoryResponse response; try { - response = await matrix.client.searchUserDirectory(text, limit: 10); + response = await matrix.client.searchUserDirectory( + text, + limit: searchUserDirectoryLimit, + ); } catch (_) {} foundProfiles = List.from(response?.results ?? []); if (foundProfiles.isEmpty && text.isValidMatrixId && text.sigil == '@') { diff --git a/lib/pages/send_file_dialog.dart b/lib/pages/send_file_dialog.dart index 8449a7d4..667d0f7a 100644 --- a/lib/pages/send_file_dialog.dart +++ b/lib/pages/send_file_dialog.dart @@ -24,11 +24,13 @@ class SendFileDialog extends StatefulWidget { class _SendFileDialogState extends State { bool origImage = false; bool _isSending = false; + + static const maxWidth = 1600; Future _send() async { var file = widget.file; if (file is MatrixImageFile && !origImage) { try { - file = await resizeImage(file, max: 1600); + file = await resizeImage(file, max: maxWidth); } catch (e) { // couldn't resize } diff --git a/lib/pages/settings_emotes.dart b/lib/pages/settings_emotes.dart index db97b6d0..860a7582 100644 --- a/lib/pages/settings_emotes.dart +++ b/lib/pages/settings_emotes.dart @@ -198,6 +198,8 @@ class EmotesSettingsController extends State { }); } + static const maxImageWidth = 1600; + void imagePickerAction( ValueNotifier controller) async { final result = @@ -208,7 +210,7 @@ class EmotesSettingsController extends State { name: result.fileName, ); try { - file = await resizeImage(file, max: 1600); + file = await resizeImage(file, max: maxImageWidth); } catch (_) { // do nothing }