Skip to content

Commit

Permalink
Revert "fix!: Fix configured cache manager error (#247)"
Browse files Browse the repository at this point in the history
This reverts commit 3aff4d1.
  • Loading branch information
sidrao2006 authored Nov 28, 2024
1 parent 22adfc5 commit 475535d
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 196 deletions.
2 changes: 0 additions & 2 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ void main() {
darkTheme: ThemeData.dark(),
),
);

DynamicCachedFonts.runMigrationTool();
}

class DynamicCachedFontsDemo1 extends StatefulWidget {
Expand Down
74 changes: 2 additions & 72 deletions lib/dynamic_cached_fonts.dart
Original file line number Diff line number Diff line change
Expand Up @@ -263,8 +263,6 @@ class DynamicCachedFonts {
fontFiles = await loadCachedFamily(
downloadUrls,
fontFamily: fontFamily,
maxCacheObjects: maxCacheObjects,
cacheStalePeriod: cacheStalePeriod,
);

// Checks whether any of the files is invalid.
Expand Down Expand Up @@ -292,8 +290,6 @@ class DynamicCachedFonts {
fontFiles = await loadCachedFamily(
downloadUrls,
fontFamily: fontFamily,
maxCacheObjects: maxCacheObjects,
cacheStalePeriod: cacheStalePeriod,
);
}

Expand Down Expand Up @@ -337,8 +333,6 @@ class DynamicCachedFonts {
downloadUrls,
fontFamily: fontFamily,
progressListener: itemCountProgressListener,
maxCacheObjects: maxCacheObjects,
cacheStalePeriod: cacheStalePeriod,
);

try {
Expand Down Expand Up @@ -379,8 +373,6 @@ class DynamicCachedFonts {
downloadUrls,
fontFamily: fontFamily,
progressListener: itemCountProgressListener,
maxCacheObjects: maxCacheObjects,
cacheStalePeriod: cacheStalePeriod,
);
}
}
Expand Down Expand Up @@ -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.
///
Expand All @@ -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,
);

Expand All @@ -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,
);

Expand Down Expand Up @@ -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,
);

Expand All @@ -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.
Expand All @@ -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();
}
86 changes: 19 additions & 67 deletions lib/src/raw_dynamic_cached_fonts.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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;
}
Expand All @@ -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);
Expand All @@ -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');
Expand Down Expand Up @@ -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);
Expand All @@ -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');
Expand Down Expand Up @@ -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);
Expand All @@ -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');
Expand Down Expand Up @@ -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);
}
}
Loading

0 comments on commit 475535d

Please sign in to comment.