From bcd03e7e60dfac7ccd56964d8bf0f16f0a4a383b Mon Sep 17 00:00:00 2001 From: Rico Berger Date: Sun, 5 Nov 2023 17:35:37 +0100 Subject: [PATCH] [core] Add Custom Cache Manager (#67) This commit adds a custom cache manager "CustomCacheManager" which is used in the "CachedNetworkImage" widget. The custom cache manager is required, so that we can adjust the stale periode of cached images. By default the package used 30 days as stale periode, but for our use case 7 days should be enough and we can reduce the storage used by the app. Note: We also fixed the "run.sh" script to work with devices where the name contains a space. --- .../widgets/utils/cached_network_image.dart | 28 ++++++++++++++++--- app/pubspec.lock | 2 +- app/pubspec.yaml | 1 + app/run.sh | 11 +++++--- 4 files changed, 33 insertions(+), 9 deletions(-) diff --git a/app/lib/widgets/utils/cached_network_image.dart b/app/lib/widgets/utils/cached_network_image.dart index a0506ac..c11bf2f 100644 --- a/app/lib/widgets/utils/cached_network_image.dart +++ b/app/lib/widgets/utils/cached_network_image.dart @@ -2,6 +2,7 @@ import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:cached_network_image/cached_network_image.dart' as cni; +import 'package:flutter_cache_manager/flutter_cache_manager.dart'; import 'package:supabase_flutter/supabase_flutter.dart'; import 'package:feeddeck/repositories/settings_repository.dart'; @@ -25,10 +26,6 @@ String getImageUrl(String imageUrl) { /// The [CachedNetworkImage] is a wrapper around the [cni.CachedNetworkImage] /// widget, which will automatically use the correct url to display the image, /// via the [getImageUrl] function. -// -/// TODO: Once the media handling is changed everywhere, this is the place where -/// we want to add a custom cache handler, so that the images are only stored -/// for seven days to save storage space. class CachedNetworkImage extends StatelessWidget { final String imageUrl; @@ -50,6 +47,7 @@ class CachedNetworkImage extends StatelessWidget { @override Widget build(BuildContext context) { return cni.CachedNetworkImage( + cacheManager: CustomCacheManager(), imageUrl: getImageUrl(imageUrl), width: width, height: height, @@ -59,3 +57,25 @@ class CachedNetworkImage extends StatelessWidget { ); } } + +/// [CustomCacheManager] is a custom [CacheManager] which is used by the +/// [CachedNetworkImage] widget to cache the images. This is required to adjust +/// the `stalePeriod` to 7 days, instead of the default 30 days. 7 days should +/// be enough for our use case and will reduce the storage usage. +class CustomCacheManager extends CacheManager with ImageCacheManager { + static const key = 'libCachedImageData'; + + static final CustomCacheManager _instance = CustomCacheManager._internal(); + + factory CustomCacheManager() { + return _instance; + } + + CustomCacheManager._internal() + : super( + Config( + key, + stalePeriod: const Duration(days: 7), + ), + ); +} diff --git a/app/pubspec.lock b/app/pubspec.lock index 3e6183d..7337e10 100644 --- a/app/pubspec.lock +++ b/app/pubspec.lock @@ -223,7 +223,7 @@ packages: source: sdk version: "0.0.0" flutter_cache_manager: - dependency: transitive + dependency: "direct main" description: name: flutter_cache_manager sha256: "8207f27539deb83732fdda03e259349046a39a4c767269285f449ade355d54ba" diff --git a/app/pubspec.yaml b/app/pubspec.yaml index 31a61a7..1a102f8 100644 --- a/app/pubspec.yaml +++ b/app/pubspec.yaml @@ -41,6 +41,7 @@ dependencies: cached_network_image: ^3.2.3 carousel_slider: ^4.2.1 collection: ^1.17.0 + flutter_cache_manager: ^3.3.1 flutter_markdown: ^0.6.14 flutter_native_splash: ^2.2.19 html: ^0.15.4 diff --git a/app/run.sh b/app/run.sh index a3f5102..29e243e 100755 --- a/app/run.sh +++ b/app/run.sh @@ -4,8 +4,8 @@ if [[ "$#" -lt 1 ]]; then echo "$(basename "$0") -- program to run the app where: - -e|--environment set the environment on which the app should be run, e.g. -e=local - -d|--device set the device on which the app should be run, e.g. -d=chrome" + -e|--environment set the environment on which the app should be run, e.g. -e=\"local\" + -d|--device set the device on which the app should be run, e.g. -d=\"chrome\"" exit fi @@ -27,10 +27,13 @@ done if [ -z "${environment}" ] || [ -z "${device}" ]; then echo "You have to provide an environment and a device" - echo "Example: $0 -e=local -d=chrome" + echo " Example: $0 -e=\"local\" -d=\"chrome\"" + echo " Provided: $0 -e=\"${environment}\" -d=\"${device}\"" exit 1 fi +echo "Run: $0 -e=\"${environment}\" -d=\"${device}\"" + # Load the environment variables from the corresponding ".env" file in the # "supabase" directory. set -o allexport && source "../supabase/.env.${environment}" && set +o allexport @@ -52,4 +55,4 @@ if [ "${device}" == "chrome" ]; then fi # Run the app on the provided device and environment. -flutter run -d ${device} ${additional_flags} --dart-define SUPABASE_URL=${supabase_url} --dart-define SUPABASE_ANON_KEY=${FEEDDECK_SUPABASE_ANON_KEY} --dart-define SUPABASE_SITE_URL=${FEEDDECK_SUPABASE_SITE_URL} --dart-define GOOGLE_CLIENT_ID=${FEEDDECK_GOOGLE_CLIENT_ID} +flutter run -d "${device}" ${additional_flags} --dart-define SUPABASE_URL=${supabase_url} --dart-define SUPABASE_ANON_KEY=${FEEDDECK_SUPABASE_ANON_KEY} --dart-define SUPABASE_SITE_URL=${FEEDDECK_SUPABASE_SITE_URL} --dart-define GOOGLE_CLIENT_ID=${FEEDDECK_GOOGLE_CLIENT_ID}