From 475535de16019270460a9984a63426f821cdabfe Mon Sep 17 00:00:00 2001 From: Aneesh Rao <sidrao2006@gmail.com> Date: Thu, 28 Nov 2024 12:15:06 +0530 Subject: [PATCH] Revert "fix!: Fix configured cache manager error (#247)" This reverts commit 3aff4d1db3d6939255477ca57be3aa0959e5ca22. --- example/lib/main.dart | 2 - lib/dynamic_cached_fonts.dart | 74 +---------------------- lib/src/raw_dynamic_cached_fonts.dart | 86 ++++++--------------------- lib/src/utils.dart | 78 +++++++++++++++--------- test/dynamic_cached_fonts_test.dart | 44 ++++++-------- 5 files changed, 88 insertions(+), 196 deletions(-) diff --git a/example/lib/main.dart b/example/lib/main.dart index ab51f29..386b7ba 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -15,8 +15,6 @@ void main() { darkTheme: ThemeData.dark(), ), ); - - DynamicCachedFonts.runMigrationTool(); } class DynamicCachedFontsDemo1 extends StatefulWidget { diff --git a/lib/dynamic_cached_fonts.dart b/lib/dynamic_cached_fonts.dart index 31dd880..991b9fd 100644 --- a/lib/dynamic_cached_fonts.dart +++ b/lib/dynamic_cached_fonts.dart @@ -263,8 +263,6 @@ class DynamicCachedFonts { fontFiles = await loadCachedFamily( downloadUrls, fontFamily: fontFamily, - maxCacheObjects: maxCacheObjects, - cacheStalePeriod: cacheStalePeriod, ); // Checks whether any of the files is invalid. @@ -292,8 +290,6 @@ class DynamicCachedFonts { fontFiles = await loadCachedFamily( downloadUrls, fontFamily: fontFamily, - maxCacheObjects: maxCacheObjects, - cacheStalePeriod: cacheStalePeriod, ); } @@ -337,8 +333,6 @@ class DynamicCachedFonts { downloadUrls, fontFamily: fontFamily, progressListener: itemCountProgressListener, - maxCacheObjects: maxCacheObjects, - cacheStalePeriod: cacheStalePeriod, ); try { @@ -379,8 +373,6 @@ class DynamicCachedFonts { downloadUrls, fontFamily: fontFamily, progressListener: itemCountProgressListener, - maxCacheObjects: maxCacheObjects, - cacheStalePeriod: cacheStalePeriod, ); } } @@ -496,20 +488,7 @@ class DynamicCachedFonts { /// - **REQUIRED** The [url] property is used to specify the url /// for the required font. It should be a valid http/https url which points to /// a font file. The [url] should match the url passed to [cacheFont]. - /// - /// - The [maxCacheObjects] property should match the value passed to [cacheFont]. - /// - /// - The [cacheStalePeriod] property should match the value passed to [cacheFont]. - static Future<bool> canLoadFont( - String url, { - int maxCacheObjects = kDefaultMaxCacheObjects, - Duration cacheStalePeriod = kDefaultCacheStalePeriod, - }) => - RawDynamicCachedFonts.canLoadFont( - url, - maxCacheObjects: maxCacheObjects, - cacheStalePeriod: cacheStalePeriod, - ); + static Future<bool> canLoadFont(String url) => RawDynamicCachedFonts.canLoadFont(url); /// Fetches the given [url] from cache and loads it as an asset. /// @@ -519,22 +498,14 @@ class DynamicCachedFonts { /// /// - **REQUIRED** The [fontFamily] property is used to specify the name /// of the font family which is to be used as [TextStyle.fontFamily]. - /// - /// - The [maxCacheObjects] property should match the value passed to [cacheFont]. - /// - /// - The [cacheStalePeriod] property should match the value passed to [cacheFont]. static Future<FileInfo> loadCachedFont( String url, { required String fontFamily, - int maxCacheObjects = kDefaultMaxCacheObjects, - Duration cacheStalePeriod = kDefaultCacheStalePeriod, @visibleForTesting FontLoader? fontLoader, }) => RawDynamicCachedFonts.loadCachedFont( url, fontFamily: fontFamily, - maxCacheObjects: maxCacheObjects, - cacheStalePeriod: cacheStalePeriod, fontLoader: fontLoader, ); @@ -554,22 +525,14 @@ class DynamicCachedFonts { /// /// - **REQUIRED** The [fontFamily] property is used to specify the name /// of the font family which is to be used as [TextStyle.fontFamily]. - /// - /// - The [maxCacheObjects] property should match the value passed to [cacheFont]. - /// - /// - The [cacheStalePeriod] property should match the value passed to [cacheFont]. static Future<Iterable<FileInfo>> loadCachedFamily( List<String> urls, { required String fontFamily, - int maxCacheObjects = kDefaultMaxCacheObjects, - Duration cacheStalePeriod = kDefaultCacheStalePeriod, @visibleForTesting FontLoader? fontLoader, }) => RawDynamicCachedFonts.loadCachedFamily( urls, fontFamily: fontFamily, - maxCacheObjects: maxCacheObjects, - cacheStalePeriod: cacheStalePeriod, fontLoader: fontLoader, ); @@ -606,16 +569,12 @@ class DynamicCachedFonts { List<String> urls, { required String fontFamily, ItemCountProgressListener? progressListener, - int maxCacheObjects = kDefaultMaxCacheObjects, - Duration cacheStalePeriod = kDefaultCacheStalePeriod, @visibleForTesting FontLoader? fontLoader, }) => RawDynamicCachedFonts.loadCachedFamilyStream( urls, fontFamily: fontFamily, progressListener: progressListener, - maxCacheObjects: maxCacheObjects, - cacheStalePeriod: cacheStalePeriod, fontLoader: fontLoader, ); @@ -624,19 +583,8 @@ class DynamicCachedFonts { /// - **REQUIRED** The [url] property is used to specify the url /// for the required font. It should be a valid http/https url which points to /// a font file. The [url] should match the url passed to [cacheFont]. - /// - /// - The [maxCacheObjects] property should match the value passed to [cacheFont]. - /// - /// - The [cacheStalePeriod] property should match the value passed to [cacheFont]. - static Future<void> removeCachedFont( - String url, { - int maxCacheObjects = kDefaultMaxCacheObjects, - Duration cacheStalePeriod = kDefaultCacheStalePeriod, - }) => - RawDynamicCachedFonts.removeCachedFont( + static Future<void> removeCachedFont(String url) => RawDynamicCachedFonts.removeCachedFont( url, - maxCacheObjects: maxCacheObjects, - cacheStalePeriod: cacheStalePeriod, ); /// Used to specify whether detailed logs should be printed for debugging. @@ -661,22 +609,4 @@ class DynamicCachedFonts { overrideLoggerConfig: true, ); } - - /// ### MIGRATION TOOL FOR v2 - /// - /// Deletes any cache files downloaded using the default cache manager. - /// v2 creates separate folders for each font file. - /// - /// **WARNING: Any fonts downloaded using v1 of this package ( without a custom - /// `cacheStalePeriod` or `maxCacheObjects` ) will be deleted.** - /// - /// Can be placed before or after other [DynamicCachedFonts] methods. The tool - /// is required to be run only once but multiple executions will have no side - /// effects. An empty folder named 'DynamicCachedFontsFontCacheKey' will be - /// present in the cache folder after running this tool. - /// - /// Sample Usage: Users may add this to the next version of their app and - /// remove it in the next version. The purpose is to ensure atleast 1 execution - /// of the tool. Subsequent runs will be useless. - static Future<void> runMigrationTool() => migrationTool(); } diff --git a/lib/src/raw_dynamic_cached_fonts.dart b/lib/src/raw_dynamic_cached_fonts.dart index 460fac5..6bc004d 100644 --- a/lib/src/raw_dynamic_cached_fonts.dart +++ b/lib/src/raw_dynamic_cached_fonts.dart @@ -68,11 +68,10 @@ abstract class RawDynamicCachedFonts { final String cacheKey = Utils.sanitizeUrl(url); - final FileInfo font = await DynamicCachedFontsCacheManager.getCacheManager( - cacheKey, - cacheStalePeriod, - maxCacheObjects, - ).downloadFile( + DynamicCachedFontsCacheManager.handleCacheManager(cacheKey, cacheStalePeriod, maxCacheObjects); + + final FileInfo font = + await DynamicCachedFontsCacheManager.getCacheManager(cacheKey).downloadFile( url, key: cacheKey, ); @@ -128,11 +127,10 @@ abstract class RawDynamicCachedFonts { final String cacheKey = Utils.sanitizeUrl(url); - final Stream<FileResponse> stream = DynamicCachedFontsCacheManager.getCacheManager( - cacheKey, - cacheStalePeriod, - maxCacheObjects, - ).getFileStream( + DynamicCachedFontsCacheManager.handleCacheManager(cacheKey, cacheStalePeriod, maxCacheObjects); + + final Stream<FileResponse> stream = + DynamicCachedFontsCacheManager.getCacheManager(cacheKey).getFileStream( url, key: cacheKey, withProgress: progressListener != null, @@ -166,24 +164,13 @@ abstract class RawDynamicCachedFonts { /// - **REQUIRED** The [url] property is used to specify the url /// for the required font. It should be a valid http/https url which points to /// a font file. The [url] should match the url passed to [cacheFont]. - /// - /// - The [maxCacheObjects] property should match the value passed to [cacheFont]. - /// - /// - The [cacheStalePeriod] property should match the value passed to [cacheFont]. - static Future<bool> canLoadFont( - String url, { - int maxCacheObjects = kDefaultMaxCacheObjects, - Duration cacheStalePeriod = kDefaultCacheStalePeriod, - }) async { + static Future<bool> canLoadFont(String url) async { WidgetsFlutterBinding.ensureInitialized(); final String cacheKey = Utils.sanitizeUrl(url); - final FileInfo? font = await DynamicCachedFontsCacheManager.getCacheManager( - cacheKey, - cacheStalePeriod, - maxCacheObjects, - ).getFileFromCache(cacheKey); + final FileInfo? font = + await DynamicCachedFontsCacheManager.getCacheManager(cacheKey).getFileFromCache(cacheKey); return font != null; } @@ -202,8 +189,6 @@ abstract class RawDynamicCachedFonts { static Future<FileInfo> loadCachedFont( String url, { required String fontFamily, - int maxCacheObjects = kDefaultMaxCacheObjects, - Duration cacheStalePeriod = kDefaultCacheStalePeriod, @visibleForTesting FontLoader? fontLoader, }) async { fontLoader ??= FontLoader(fontFamily); @@ -212,11 +197,8 @@ abstract class RawDynamicCachedFonts { final String cacheKey = Utils.sanitizeUrl(url); - final FileInfo? font = await DynamicCachedFontsCacheManager.getCacheManager( - cacheKey, - cacheStalePeriod, - maxCacheObjects, - ).getFileFromCache(cacheKey); + final FileInfo? font = + await DynamicCachedFontsCacheManager.getCacheManager(cacheKey).getFileFromCache(cacheKey); if (font == null) { throw StateError('Font should already be cached to be loaded'); @@ -257,15 +239,9 @@ abstract class RawDynamicCachedFonts { /// /// - **REQUIRED** The [fontFamily] property is used to specify the name /// of the font family which is to be used as [TextStyle.fontFamily]. - /// - /// - The [maxCacheObjects] property should match the value passed to [cacheFont]. - /// - /// - The [cacheStalePeriod] property should match the value passed to [cacheFont]. static Future<Iterable<FileInfo>> loadCachedFamily( List<String> urls, { required String fontFamily, - int maxCacheObjects = kDefaultMaxCacheObjects, - Duration cacheStalePeriod = kDefaultCacheStalePeriod, @visibleForTesting FontLoader? fontLoader, }) async { fontLoader ??= FontLoader(fontFamily); @@ -276,11 +252,8 @@ abstract class RawDynamicCachedFonts { for (final String url in urls) { final String cacheKey = Utils.sanitizeUrl(url); - final FileInfo? font = await DynamicCachedFontsCacheManager.getCacheManager( - cacheKey, - cacheStalePeriod, - maxCacheObjects, - ).getFileFromCache(cacheKey); + final FileInfo? font = + await DynamicCachedFontsCacheManager.getCacheManager(cacheKey).getFileFromCache(cacheKey); if (font == null) { throw StateError('Font should already be cached to be loaded'); @@ -331,16 +304,10 @@ abstract class RawDynamicCachedFonts { /// an [int] value which indicates the total number of items to be downloaded and, /// another [int] value which indicates the number of items that have been /// downloaded so far. - /// - /// - The [maxCacheObjects] property should match the value passed to [cacheFont]. - /// - /// - The [cacheStalePeriod] property should match the value passed to [cacheFont]. static Stream<FileInfo> loadCachedFamilyStream( List<String> urls, { required String fontFamily, required ItemCountProgressListener? progressListener, - int maxCacheObjects = kDefaultMaxCacheObjects, - Duration cacheStalePeriod = kDefaultCacheStalePeriod, @visibleForTesting FontLoader? fontLoader, }) async* { fontLoader ??= FontLoader(fontFamily); @@ -350,11 +317,8 @@ abstract class RawDynamicCachedFonts { for (final String url in urls) { final String cacheKey = Utils.sanitizeUrl(url); - final FileInfo? font = await DynamicCachedFontsCacheManager.getCacheManager( - cacheKey, - cacheStalePeriod, - maxCacheObjects, - ).getFileFromCache(cacheKey); + final FileInfo? font = + await DynamicCachedFontsCacheManager.getCacheManager(cacheKey).getFileFromCache(cacheKey); if (font == null) { throw StateError('Font should already be cached to be loaded'); @@ -393,23 +357,11 @@ abstract class RawDynamicCachedFonts { /// - **REQUIRED** The [url] property is used to specify the url /// for the required font. It should be a valid http/https url which points to /// a font file. The [url] should match the url passed to [cacheFont]. - /// - /// - The [maxCacheObjects] property should match the value passed to [cacheFont]. - /// - /// - The [cacheStalePeriod] property should match the value passed to [cacheFont]. - static Future<void> removeCachedFont( - String url, { - int maxCacheObjects = kDefaultMaxCacheObjects, - Duration cacheStalePeriod = kDefaultCacheStalePeriod, - }) async { + static Future<void> removeCachedFont(String url) async { WidgetsFlutterBinding.ensureInitialized(); final String cacheKey = Utils.sanitizeUrl(url); - await DynamicCachedFontsCacheManager.getCacheManager( - cacheKey, - cacheStalePeriod, - maxCacheObjects, - ).removeFile(cacheKey); + await DynamicCachedFontsCacheManager.getCacheManager(cacheKey).removeFile(cacheKey); } } diff --git a/lib/src/utils.dart b/lib/src/utils.dart index 4aecd9f..476e021 100644 --- a/lib/src/utils.dart +++ b/lib/src/utils.dart @@ -26,10 +26,6 @@ typedef ItemCountProgressListener = void Function( /// information about the download progress. typedef DownloadProgressListener = void Function(DownloadProgress progress); -/// Migratoin tool for v2 -/// Deletes any cache files downloaded using the default cache manager. -Future<void> migrationTool() => CacheManager(Config('DynamicCachedFontsFontCacheKey')).emptyCache(); - /// Gets the sanitized url from [url] which is used as `cacheKey` when /// downloading, caching or loading. @visibleForTesting @@ -64,11 +60,13 @@ void devLog( /// This approach prevents the creation of multiple instance of [CacheManager] using /// the same [Config.cacheKey].` /// -/// When `cacheStalePeriod` or `maxCacheObjects` is not modified, an instance +/// When `cacheStalePeriod` or `maxCacheObjects` is not modified, a default instance /// of [CacheManager] is created when a font cache/load is requested. This instance -/// assigns the provided `cacheKey` to [Config.cacheKey], [kDefaultCacheStalePeriod] -/// to [Config.stalePeriod] and [kDefaultMaxCacheObjects] to [Config.maxNrOfCacheObjects] -/// and is added to [_cacheManagers]. +/// assigns [_defaultCacheKey] to [Config.cacheKey], [kDefaultCacheStalePeriod] +/// to [Config.stalePeriod] and [kDefaultMaxCacheObjects] to [Config.maxNrOfCacheObjects]. +/// The instance is added to [_cacheManagers] with [_defaultCacheKey] as the key. +/// +/// The default instance can be easily accessed with [defaultCacheManager]. /// /// When caching/loading a font, if `cacheStalePeriod` or `maxCacheObjects` is modified /// by the caller, a new instance of [CacheManager] is created and added to [_cacheManagers]. @@ -78,17 +76,32 @@ void devLog( class DynamicCachedFontsCacheManager { DynamicCachedFontsCacheManager._(); + /// The default cache key for cache managers' configurations + static const String _defaultCacheKey = 'DynamicCachedFontsFontCacheKey'; + /// A map of [CacheManager]s used throughout the package. The key used /// will correspond to [Config.cacheKey] of the respective [CacheManager]. - static final Map<String, CacheManager> _cacheManagers = {}; + static final Map<String, CacheManager> _cacheManagers = { + _defaultCacheKey: CacheManager( + Config( + _defaultCacheKey, + stalePeriod: kDefaultCacheStalePeriod, + maxNrOfCacheObjects: kDefaultMaxCacheObjects, + ), + ), + }; static String? _customCacheKey; + /// The getter for the default instance of [CacheManager] in [_cacheManagers]. + static CacheManager get defaultCacheManager => _cacheManagers[_defaultCacheKey]!; + /// The getter for the custom instance of [CacheManager] in [_cacheManagers]. static CacheManager? getCustomCacheManager() => _cacheManagers[_customCacheKey]; /// The setter for the custom instance of [CacheManager] in [_cacheManagers]. - /// [Config.cacheKey] will be used as the key when adding the instance to [_cacheManagers]. + /// [Config.cacheKey] will be used as the key when adding the instance to + /// [_cacheManagers]. static setCustomCacheManager(CacheManager cacheManager) { _customCacheKey = cacheManager.store.storeKey; // This is the same key provided to Config.cacheKey. @@ -103,28 +116,37 @@ class DynamicCachedFontsCacheManager { _customCacheKey = null; } - /// Returns a custom [CacheManager], if present, or create, add to [_cacheManagers] - /// and return a new instance of [CacheManager] with [cacheKey] as [Config.cacheKey]. - static CacheManager getCacheManager( - String cacheKey, - Duration cacheStalePeriod, - int maxCacheObjects, - ) { - final cacheManager = _cacheManagers.putIfAbsent( - cacheKey, - () => CacheManager(Config( - cacheKey, - stalePeriod: cacheStalePeriod, - maxNrOfCacheObjects: maxCacheObjects, - )), - ); - - return getCustomCacheManager() ?? cacheManager; + /// Returns a custom [CacheManager], if present, or + static CacheManager getCacheManager(String cacheKey) => + getCustomCacheManager() ?? _cacheManagers[cacheKey] ?? defaultCacheManager; + + /// Creates a new instance of [CacheManager] if the default can't be used. + static void handleCacheManager(String cacheKey, Duration cacheStalePeriod, int maxCacheObjects) { + if (cacheStalePeriod != kDefaultCacheStalePeriod || + maxCacheObjects != kDefaultMaxCacheObjects) { + _cacheManagers[cacheKey] ??= CacheManager( + Config( + cacheKey, + stalePeriod: cacheStalePeriod, + maxNrOfCacheObjects: maxCacheObjects, + ), + ); + } } /// Clears the list of the [CacheManager]s. @visibleForTesting - static void clearCacheManagers() => _cacheManagers.clear(); + static void clearCacheManagers() { + _cacheManagers.clear(); + + _cacheManagers[_defaultCacheKey] = CacheManager( + Config( + _defaultCacheKey, + stalePeriod: kDefaultCacheStalePeriod, + maxNrOfCacheObjects: kDefaultMaxCacheObjects, + ), + ); + } } class _FontFileExtensionManager { diff --git a/test/dynamic_cached_fonts_test.dart b/test/dynamic_cached_fonts_test.dart index 8911f87..44f2991 100644 --- a/test/dynamic_cached_fonts_test.dart +++ b/test/dynamic_cached_fonts_test.dart @@ -116,30 +116,27 @@ void main() { group('DynamicCachedFontsCacheManager', () { setUp(() => DynamicCachedFontsCacheManager.clearCacheManagers()); - test('getCacheManager returns a cache manager which uses cacheKey', () { - final cacheManager = DynamicCachedFontsCacheManager.getCacheManager( - cacheKey, - kDefaultCacheStalePeriod, - kDefaultMaxCacheObjects, - ); + test('uses the correct cache key for the default instance', () { + const String defaultCacheKey = 'DynamicCachedFontsFontCacheKey'; + final CacheManager defaultCacheManager = DynamicCachedFontsCacheManager.defaultCacheManager; - expect(cacheManager.store.storeKey, equals(cacheKey)); + expect(defaultCacheManager.store.storeKey, equals(defaultCacheKey)); }); - test('getCacheManager prioritizes first-defined configuration values', () { - final defaultCacheManager = DynamicCachedFontsCacheManager.getCacheManager( - cacheKey, - kDefaultCacheStalePeriod, - kDefaultMaxCacheObjects, - ); + test('getCacheManager returns the default cache manager', () { + final cacheManager = DynamicCachedFontsCacheManager.getCacheManager(cacheKey); + + expect(cacheManager, equals(DynamicCachedFontsCacheManager.defaultCacheManager)); + }); - final cacheManager = DynamicCachedFontsCacheManager.getCacheManager( - cacheKey, - kDefaultCacheStalePeriod + const Duration(hours: 1), - kDefaultMaxCacheObjects + 1, - ); + test('handleCacheManager creates a new instance for non-default values', () { + // A new instance of CacheManager is created only if the default values aren't used. + DynamicCachedFontsCacheManager.handleCacheManager(cacheKey, const Duration(days: 366), 201); - expect(cacheManager, equals(defaultCacheManager)); + final cacheManager = DynamicCachedFontsCacheManager.getCacheManager(cacheKey); + + expect(cacheManager.store.storeKey, equals(cacheKey)); + expect(cacheManager, isNot(equals(DynamicCachedFontsCacheManager.defaultCacheManager))); }); test('custom cache managers can be set and retrieved', () { @@ -155,14 +152,7 @@ void main() { final CacheManager cacheManager = CacheManager(Config(cacheKey)); DynamicCachedFontsCacheManager.setCustomCacheManager(cacheManager); - expect( - DynamicCachedFontsCacheManager.getCacheManager( - cacheKey, - kDefaultCacheStalePeriod, - kDefaultMaxCacheObjects, - ), - equals(cacheManager), - ); + expect(DynamicCachedFontsCacheManager.getCacheManager(cacheKey), equals(cacheManager)); }); });