From d64da986fe3626cb21fef65e2f0aa8fe563ebdfe Mon Sep 17 00:00:00 2001 From: lucien144 Date: Sat, 9 Dec 2023 21:09:53 +0100 Subject: [PATCH] feat: custom Fyx cache is optional #413 --- lib/components/avatar.dart | 3 ++- lib/components/post/post_hero_attachment.dart | 3 ++- lib/controllers/SettingsProvider.dart | 7 +++++++ lib/model/Settings.dart | 1 + lib/pages/GalleryPage.dart | 3 ++- lib/pages/settings_screen.dart | 14 ++++++++++++-- 6 files changed, 26 insertions(+), 5 deletions(-) diff --git a/lib/components/avatar.dart b/lib/components/avatar.dart index b568741..f70f624 100644 --- a/lib/components/avatar.dart +++ b/lib/components/avatar.dart @@ -3,6 +3,7 @@ import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:flutter/widgets.dart'; import 'package:fyx/libs/fyx_image_cache_manager.dart'; +import 'package:fyx/model/MainRepository.dart'; import 'package:fyx/theme/skin/Skin.dart'; import 'package:fyx/theme/skin/SkinColors.dart'; @@ -34,7 +35,7 @@ class Avatar extends StatelessWidget { errorWidget: (context, url, error) => Icon(Icons.error), width: width, height: height, - cacheManager: FyxImageCacheManager(), + cacheManager: MainRepository().settings.useFyxImageCache ? FyxImageCacheManager() : null, ), )), ); diff --git a/lib/components/post/post_hero_attachment.dart b/lib/components/post/post_hero_attachment.dart index 88331e5..61436b2 100644 --- a/lib/components/post/post_hero_attachment.dart +++ b/lib/components/post/post_hero_attachment.dart @@ -8,6 +8,7 @@ import 'package:fyx/components/post/post_hero_attachment_box.dart'; import 'package:fyx/controllers/SettingsProvider.dart'; import 'package:fyx/controllers/log_service.dart'; import 'package:fyx/libs/fyx_image_cache_manager.dart'; +import 'package:fyx/model/MainRepository.dart'; import 'package:fyx/model/post/Image.dart' as model; import 'package:fyx/model/post/Link.dart'; import 'package:fyx/model/post/Video.dart'; @@ -74,7 +75,7 @@ class PostHeroAttachment extends StatelessWidget { width: _crop ? size.width : null, height: _crop ? size.height : null, memCacheWidth: (MediaQuery.of(context).size.width * 0.5 * MediaQuery.of(context).devicePixelRatio).toInt(), - cacheManager: FyxImageCacheManager(), + cacheManager: MainRepository().settings.useFyxImageCache ? FyxImageCacheManager() : null, ), if (blur) Positioned.fill(child: T.nsfwMask()) ], diff --git a/lib/controllers/SettingsProvider.dart b/lib/controllers/SettingsProvider.dart index 4221000..d33a71b 100644 --- a/lib/controllers/SettingsProvider.dart +++ b/lib/controllers/SettingsProvider.dart @@ -70,6 +70,12 @@ class SettingsProvider { _settings.quickRating = mode; } + bool get useFyxImageCache => _settings.useFyxImageCache; + set useFyxImageCache(bool mode) { + _box.put('useFyxImageCache', mode); + _settings.useFyxImageCache = mode; + } + bool get useAutocorrect => _settings.useAutocorrect; set useAutocorrect(bool autocorrect) { _box.put('useAutocorrect', autocorrect); @@ -111,6 +117,7 @@ class SettingsProvider { _settings.defaultView = _box.get('defaultView', defaultValue: Settings().defaultView); _settings.latestView = _box.get('latestView', defaultValue: Settings().latestView); _settings.quickRating = _box.get('quickRating', defaultValue: Settings().quickRating); + _settings.useFyxImageCache = _box.get('useFyxImageCache', defaultValue: Settings().useFyxImageCache); _settings.useCompactMode = _box.get('useCompactMode', defaultValue: Settings().useCompactMode); _settings.useAutocorrect = _box.get('useAutocorrect', defaultValue: Settings().useAutocorrect); _settings.firstUnread = _box.get('firstUnread', defaultValue: Settings().firstUnread); diff --git a/lib/model/Settings.dart b/lib/model/Settings.dart index 21f0bc9..a496ec3 100644 --- a/lib/model/Settings.dart +++ b/lib/model/Settings.dart @@ -9,6 +9,7 @@ class Settings { bool useCompactMode = false; bool useAutocorrect = true; bool quickRating = true; + bool useFyxImageCache = false; // Wheter to use internal image case or not. May fix crashes on big images. #413 // Settings -> what is the default view when app restart? DefaultView defaultView = DefaultView.history; // Save the last screen view diff --git a/lib/pages/GalleryPage.dart b/lib/pages/GalleryPage.dart index 6728373..bcff8e8 100644 --- a/lib/pages/GalleryPage.dart +++ b/lib/pages/GalleryPage.dart @@ -12,6 +12,7 @@ import 'package:fyx/controllers/AnalyticsProvider.dart'; import 'package:fyx/controllers/log_service.dart'; import 'package:fyx/exceptions/UnsupportedDownloadFormatException.dart'; import 'package:fyx/libs/fyx_image_cache_manager.dart'; +import 'package:fyx/model/MainRepository.dart'; import 'package:fyx/theme/Helpers.dart'; import 'package:fyx/theme/L.dart'; import 'package:fyx/theme/T.dart'; @@ -113,7 +114,7 @@ class _GalleryPageState extends State { ), imageUrl: _arguments!.images[index].image, memCacheWidth: (MediaQuery.of(context).size.width * MediaQuery.of(context).devicePixelRatio).toInt(), - cacheManager: FyxImageCacheManager()), + cacheManager: MainRepository().settings.useFyxImageCache ? FyxImageCacheManager() : null), )); }, itemCount: _arguments!.images.length, diff --git a/lib/pages/settings_screen.dart b/lib/pages/settings_screen.dart index 6c1a0d6..10ffaa3 100644 --- a/lib/pages/settings_screen.dart +++ b/lib/pages/settings_screen.dart @@ -30,7 +30,7 @@ class SettingsScreen extends StatefulWidget { _SettingsScreenState createState() => _SettingsScreenState(); } -enum CacheKeys {images, gifs, videos, other} +enum CacheKeys { images, gifs, videos, other } class _SettingsScreenState extends State { Map cacheUsage = {CacheKeys.images: 0.0, CacheKeys.gifs: 0.0, CacheKeys.videos: 0.0, CacheKeys.other: 0.0}; @@ -38,6 +38,7 @@ class _SettingsScreenState extends State { bool _compactMode = false; bool _autocorrect = false; bool _quickRating = true; + bool _useFyxImageCache = false; bool _emptyingCache = false; Future> _cacheSize = Future.value({}); DefaultView _defaultView = DefaultView.latest; @@ -53,6 +54,7 @@ class _SettingsScreenState extends State { _firstUnread = MainRepository().settings.firstUnread; _linksMode = MainRepository().settings.linksMode; _quickRating = MainRepository().settings.quickRating; + _useFyxImageCache = MainRepository().settings.useFyxImageCache; _cacheSize = _getCacheSize(); AnalyticsProvider().setScreen('Settings', 'SettingsPage'); } @@ -218,6 +220,13 @@ class _SettingsScreenState extends State { SettingsSection( title: Text('Paměť'), tiles: [ + SettingsTile.switchTile( + onToggle: (bool value) { + setState(() => _useFyxImageCache = value); + MainRepository().settings.useFyxImageCache = value; + }, + initialValue: _useFyxImageCache, + title: Text('Zmenšovat obrázky')), SettingsTile( title: Text('Blokovaných uživatelů'), trailing: ValueListenableBuilder( @@ -269,7 +278,8 @@ class _SettingsScreenState extends State { MainRepository().settings.resetNsfwDiscussion(); T.success(L.SETTINGS_CACHE_RESET, bg: colors.success); AnalyticsProvider().logEvent('resetBlockedContent'); - }), + }, + description: Text('Zmenšovat obrázky - zmenšuje obrázky před stažením a předchází tak pádům aplikavce. V určitých případech ale může snižovat FPS.')), ], ), CustomSettingsSection(