From 036e6811a3053a1cbaa2999c23d094f54ec41418 Mon Sep 17 00:00:00 2001 From: Hamlet Jiang Su <30667958+hjiangsu@users.noreply.github.com> Date: Fri, 21 Jun 2024 10:33:00 -0700 Subject: [PATCH] Add debug setting to customize image dimension timeout value (#1462) added debug setting to customize image dimension timeout value --- lib/core/enums/local_settings.dart | 1 + lib/l10n/app_en.arb | 4 +++ lib/post/utils/post.dart | 5 +++- lib/settings/pages/debug_settings_page.dart | 32 +++++++++++++++++++++ 4 files changed, 41 insertions(+), 1 deletion(-) diff --git a/lib/core/enums/local_settings.dart b/lib/core/enums/local_settings.dart index a1e5ded65..d57ba0958 100644 --- a/lib/core/enums/local_settings.dart +++ b/lib/core/enums/local_settings.dart @@ -300,6 +300,7 @@ enum LocalSettings { combineNavAndFab(name: 'setting_combine_nav_and_fab', key: 'combineNavAndFab', category: LocalSettingsCategories.general, subCategory: LocalSettingsSubCategories.comments), enableExperimentalFeatures(name: 'setting_enable_experimental_features', key: 'enableExperimentalFeatures', category: LocalSettingsCategories.debug), + imageDimensionTimeout(name: 'setting_image_dimension_timeout', key: 'imageDimensionTimeout', category: LocalSettingsCategories.debug), draftsCache(name: 'drafts_cache', key: ''), diff --git a/lib/l10n/app_en.arb b/lib/l10n/app_en.arb index 49ac62aa7..1ae43710c 100644 --- a/lib/l10n/app_en.arb +++ b/lib/l10n/app_en.arb @@ -951,6 +951,10 @@ "@imageCachingModeRelaxedShort": { "description": "Short description for relaxed image caching mode" }, + "imageDimensionTimeout": "Image Dimension Timeout", + "@imageDimensionTimeout": { + "description": "Setting for how long to wait for the image dimensions to be fetched" + }, "importDatabase": "Import Database", "@importDatabase": { "description": "Name of setting for importing the db" diff --git a/lib/post/utils/post.dart b/lib/post/utils/post.dart index 72a68f5b4..52dc0495c 100644 --- a/lib/post/utils/post.dart +++ b/lib/post/utils/post.dart @@ -348,7 +348,10 @@ Future parsePostView(PostView postView, bool fetchImageDimensions Size result = Size(MediaQuery.of(GlobalContext.context).size.width, 200); try { - result = await retrieveImageDimensions(imageUrl: media.thumbnailUrl ?? media.mediaUrl).timeout(const Duration(seconds: 2)); + SharedPreferences prefs = (await UserPreferences.instance).sharedPreferences; + int imageDimensionTimeout = prefs.getInt(LocalSettings.imageDimensionTimeout.name) ?? 2; + + result = await retrieveImageDimensions(imageUrl: media.thumbnailUrl ?? media.mediaUrl).timeout(Duration(seconds: imageDimensionTimeout)); } catch (e) { debugPrint('${media.thumbnailUrl ?? media.originalUrl} - $e: Falling back to default image size'); } diff --git a/lib/settings/pages/debug_settings_page.dart b/lib/settings/pages/debug_settings_page.dart index 7a1716e84..a997626e4 100644 --- a/lib/settings/pages/debug_settings_page.dart +++ b/lib/settings/pages/debug_settings_page.dart @@ -21,6 +21,7 @@ import 'package:thunder/notification/enums/notification_type.dart'; import 'package:thunder/notification/shared/android_notification.dart'; import 'package:thunder/notification/shared/notification_server.dart'; import 'package:thunder/notification/utils/local_notifications.dart'; +import 'package:thunder/settings/widgets/list_option.dart'; import 'package:thunder/settings/widgets/toggle_option.dart'; import 'package:thunder/shared/dialogs.dart'; @@ -28,6 +29,7 @@ import 'package:thunder/shared/divider.dart'; import 'package:thunder/shared/snackbar.dart'; import 'package:thunder/thunder/bloc/thunder_bloc.dart'; import 'package:thunder/settings/widgets/settings_list_tile.dart'; +import 'package:thunder/utils/bottom_sheet_list_picker.dart'; import 'package:thunder/utils/cache.dart'; import 'package:thunder/utils/constants.dart'; import 'package:unifiedpush/unifiedpush.dart'; @@ -58,6 +60,12 @@ class _DebugSettingsPageState extends State { /// Enable experimental features in the app. bool enableExperimentalFeatures = false; + /// The maximum amount of time in seconds to fetch the image dimensions. + int imageDimensionTimeout = 2; + + /// The available timeout values for image dimensions in seconds. + List imageDimensionTimeouts = List.generate(10, (index) => index + 1); + Future setPreferences(attribute, value) async { final prefs = (await UserPreferences.instance).sharedPreferences; @@ -66,6 +74,10 @@ class _DebugSettingsPageState extends State { await prefs.setBool(LocalSettings.enableExperimentalFeatures.name, value); setState(() => enableExperimentalFeatures = value); break; + case LocalSettings.imageDimensionTimeout: + await prefs.setInt(LocalSettings.imageDimensionTimeout.name, value); + setState(() => imageDimensionTimeout = value); + break; } } @@ -123,6 +135,7 @@ class _DebugSettingsPageState extends State { setState(() { enableExperimentalFeatures = prefs.getBool(LocalSettings.enableExperimentalFeatures.name) ?? false; + imageDimensionTimeout = prefs.getInt(LocalSettings.imageDimensionTimeout.name) ?? 2; }); if (widget.settingToHighlight != null) { @@ -566,6 +579,25 @@ class _DebugSettingsPageState extends State { highlightedSetting: settingToHighlight, ), ), + SliverToBoxAdapter( + child: Padding( + padding: const EdgeInsets.only(left: 16.0, right: 16.0, top: 16.0), + child: Text(l10n.feed, style: theme.textTheme.titleMedium), + ), + ), + const SliverToBoxAdapter(child: SizedBox(height: 8.0)), + SliverToBoxAdapter( + child: ListOption( + description: l10n.imageDimensionTimeout, + value: ListPickerItem(label: '${imageDimensionTimeout}s', icon: Icons.timelapse, payload: imageDimensionTimeout), + options: imageDimensionTimeouts.map((value) => ListPickerItem(icon: Icons.timelapse, label: '${value}s', payload: value)).toList(), + icon: Icons.timelapse, + onChanged: (value) async => setPreferences(LocalSettings.imageDimensionTimeout, value.payload), + highlightKey: settingToHighlightKey, + setting: LocalSettings.imageDimensionTimeout, + highlightedSetting: settingToHighlight, + ), + ), const SliverToBoxAdapter(child: SizedBox(height: 48)), ], ),