Skip to content

Commit

Permalink
Add debug setting to customize image dimension timeout value (thunder…
Browse files Browse the repository at this point in the history
…-app#1462)

added debug setting to customize image dimension timeout value
  • Loading branch information
hjiangsu authored Jun 21, 2024
1 parent 6ded143 commit 036e681
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/core/enums/local_settings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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: ''),

Expand Down
4 changes: 4 additions & 0 deletions lib/l10n/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
5 changes: 4 additions & 1 deletion lib/post/utils/post.dart
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,10 @@ Future<PostViewMedia> 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');
}
Expand Down
32 changes: 32 additions & 0 deletions lib/settings/pages/debug_settings_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@ 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';
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';
Expand Down Expand Up @@ -58,6 +60,12 @@ class _DebugSettingsPageState extends State<DebugSettingsPage> {
/// 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<int> imageDimensionTimeouts = List.generate(10, (index) => index + 1);

Future<void> setPreferences(attribute, value) async {
final prefs = (await UserPreferences.instance).sharedPreferences;

Expand All @@ -66,6 +74,10 @@ class _DebugSettingsPageState extends State<DebugSettingsPage> {
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;
}
}

Expand Down Expand Up @@ -123,6 +135,7 @@ class _DebugSettingsPageState extends State<DebugSettingsPage> {

setState(() {
enableExperimentalFeatures = prefs.getBool(LocalSettings.enableExperimentalFeatures.name) ?? false;
imageDimensionTimeout = prefs.getInt(LocalSettings.imageDimensionTimeout.name) ?? 2;
});

if (widget.settingToHighlight != null) {
Expand Down Expand Up @@ -566,6 +579,25 @@ class _DebugSettingsPageState extends State<DebugSettingsPage> {
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)),
],
),
Expand Down

0 comments on commit 036e681

Please sign in to comment.