Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
.history
.svn/
devtools_options.yaml
.fvmrc
.fvm/

# Environment files
ios/Flutter/Dart-Defines.xcconfig
Expand Down
7 changes: 4 additions & 3 deletions flutter_cache_manager/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
## [Unreleased]

* Update example Android project for Flutter 3.44 (Gradle 9.1 / AGP 9.0.1 / Kotlin 2.3.20, Java 17)
* Migrate example Android app to built-in Kotlin
* Migrate example iOS app from CocoaPods to Swift Package Manager
* Raise minimum Dart SDK to 3.8.0 and update dependencies ([#512](https://github.com/Baseflow/flutter_cache_manager/pull/512))
* Update example Android project for Flutter 3.44 (Gradle 9.1 / AGP 9.0.1 / Kotlin 2.3.20, Java 17) ([#510](https://github.com/Baseflow/flutter_cache_manager/pull/510))
* Migrate example Android app to built-in Kotlin ([#510](https://github.com/Baseflow/flutter_cache_manager/pull/510))
* Migrate example iOS app from CocoaPods to Swift Package Manager ([#511](https://github.com/Baseflow/flutter_cache_manager/pull/511))

## [3.4.1] - 2024-08-13

Expand Down
25 changes: 13 additions & 12 deletions flutter_cache_manager/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,7 @@ class CacheManagerPageState extends State<CacheManagerPage> {
body: const ListTile(
title: Text('Tap the floating action button to download.'),
),
floatingActionButton: Fab(
downloadFile: _downloadFile,
),
floatingActionButton: Fab(downloadFile: _downloadFile),
);
}
return DownloadPage(
Expand All @@ -68,15 +66,18 @@ class CacheManagerPageState extends State<CacheManagerPage> {
}

void _removeFile() {
DefaultCacheManager().removeFile(url).then((value) {
if (kDebugMode) {
print('File removed');
}
}).onError((error, stackTrace) {
if (kDebugMode) {
print(error);
}
});
DefaultCacheManager()
.removeFile(url)
.then((value) {
if (kDebugMode) {
print('File removed');
}
})
.onError((error, stackTrace) {
if (kDebugMode) {
print(error);
}
});
setState(() {
fileStream = null;
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,9 @@ class DownloadPage extends StatelessWidget {

return Scaffold(
body: body,
floatingActionButton:
!loading ? Fab(downloadFile: downloadFile) : null,
floatingActionButton: !loading
? Fab(downloadFile: downloadFile)
: null,
);
},
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ import 'package:flutter/material.dart';
class Fab extends StatelessWidget {
final VoidCallback downloadFile;

const Fab({
required this.downloadFile,
super.key,
});
const Fab({required this.downloadFile, super.key});

@override
Widget build(BuildContext context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ list(APPEND FLUTTER_PLUGIN_LIST
)

list(APPEND FLUTTER_FFI_PLUGIN_LIST
jni
)

set(PLUGIN_BUNDLED_LIBRARIES)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@
import FlutterMacOS
import Foundation

import path_provider_foundation
import sqflite
import sqflite_darwin
import url_launcher_macos

func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
PathProviderPlugin.register(with: registry.registrar(forPlugin: "PathProviderPlugin"))
SqflitePlugin.register(with: registry.registrar(forPlugin: "SqflitePlugin"))
UrlLauncherPlugin.register(with: registry.registrar(forPlugin: "UrlLauncherPlugin"))
}
8 changes: 4 additions & 4 deletions flutter_cache_manager/example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@ description: A project that showcases usage of flutter_cache_manager
publish_to: none
version: 1.0.0+1
environment:
sdk: '>=3.0.0 <4.0.0'
sdk: '>=3.8.0 <4.0.0'

dependencies:
baseflow_plugin_template: ^2.2.0
baseflow_plugin_template: ^2.2.1
cupertino_icons: ^1.0.8
flutter:
sdk: flutter
flutter_cache_manager:
path: ../
url_launcher: ^6.3.0
url_launcher: ^6.3.2

dev_dependencies:
flutter_test:
sdk: flutter
flutter_lints: ^4.0.0
flutter_lints: ^6.0.0

flutter:
uses-material-design: true
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ list(APPEND FLUTTER_PLUGIN_LIST
)

list(APPEND FLUTTER_FFI_PLUGIN_LIST
jni
)

set(PLUGIN_BUNDLED_LIBRARIES)
Expand Down
2 changes: 1 addition & 1 deletion flutter_cache_manager/lib/flutter_cache_manager.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/// Generic cache manager for flutter.
/// Saves web files on the storages of the device and saves the cache info using sqflite
library flutter_cache_manager;
library;

export 'src/cache_manager.dart';
export 'src/cache_managers/cache_managers.dart';
Expand Down
70 changes: 43 additions & 27 deletions flutter_cache_manager/lib/src/cache_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@ class CacheManager implements BaseCacheManager {
/// The [fileService] can be used to customize how files are downloaded. For example
/// to edit the urls, add headers or use a proxy. You can also choose to supply
/// a CacheStore or WebHelper directly if you want more customization.
CacheManager(Config config)
: _config = config,
_store = CacheStore(config) {
CacheManager(Config config) : _config = config, _store = CacheStore(config) {
_webHelper = WebHelper(_store, config.fileService);
}

Expand All @@ -40,8 +38,8 @@ class CacheManager implements BaseCacheManager {
Config config, {
CacheStore? cacheStore,
WebHelper? webHelper,
}) : _config = config,
_store = cacheStore ?? CacheStore(config) {
}) : _config = config,
_store = cacheStore ?? CacheStore(config) {
_webHelper = webHelper ?? WebHelper(_store, config.fileService);
}

Expand Down Expand Up @@ -87,8 +85,11 @@ class CacheManager implements BaseCacheManager {
/// cached file is too old the newly downloaded file is returned afterwards.
@override
@Deprecated('Prefer to use the new getFileStream method')
Stream<FileInfo> getFile(String url,
{String? key, Map<String, String>? headers}) {
Stream<FileInfo> getFile(
String url, {
String? key,
Map<String, String>? headers,
}) {
return getFileStream(
url,
key: key,
Expand All @@ -108,8 +109,12 @@ class CacheManager implements BaseCacheManager {
/// returned from the cache there will be no progress given, although the file
/// might be outdated and a new file is being downloaded in the background.
@override
Stream<FileResponse> getFileStream(String url,
{String? key, Map<String, String>? headers, bool withProgress = false}) {
Stream<FileResponse> getFileStream(
String url, {
String? key,
Map<String, String>? headers,
bool withProgress = false,
}) {
key ??= url;
final streamController = StreamController<FileResponse>();
_pushFileToStream(streamController, url, key, headers, withProgress);
Expand All @@ -133,13 +138,17 @@ class CacheManager implements BaseCacheManager {
}
} on Object catch (e) {
cacheLogger.log(
'CacheManager: Failed to load cached file for $url with error:\n$e',
CacheManagerLogLevel.debug);
'CacheManager: Failed to load cached file for $url with error:\n$e',
CacheManagerLogLevel.debug,
);
}
if (cacheFile == null || cacheFile.validTill.isBefore(DateTime.now())) {
try {
await for (final response
in _webHelper.downloadFile(url, key: key, authHeaders: headers)) {
await for (final response in _webHelper.downloadFile(
url,
key: key,
authHeaders: headers,
)) {
if (response is DownloadProgress && withProgress) {
streamController.add(response);
}
Expand All @@ -149,8 +158,9 @@ class CacheManager implements BaseCacheManager {
}
} on Object catch (e) {
cacheLogger.log(
'CacheManager: Failed to download file from $url with error:\n$e',
CacheManagerLogLevel.debug);
'CacheManager: Failed to download file from $url with error:\n$e',
CacheManagerLogLevel.debug,
);
if (cacheFile == null && streamController.hasListener) {
streamController.addError(e);
}
Expand All @@ -170,10 +180,12 @@ class CacheManager implements BaseCacheManager {

///Download the file and add to cache
@override
Future<FileInfo> downloadFile(String url,
{String? key,
Map<String, String>? authHeaders,
bool force = false}) async {
Future<FileInfo> downloadFile(
String url, {
String? key,
Map<String, String>? authHeaders,
bool force = false,
}) async {
key ??= url;
final fileResponse = await _webHelper
.downloadFile(
Expand All @@ -189,9 +201,10 @@ class CacheManager implements BaseCacheManager {
/// Get the file from the cache.
/// Specify [ignoreMemCache] to force a re-read from the database
@override
Future<FileInfo?> getFileFromCache(String key,
{bool ignoreMemCache = false}) =>
_store.getFile(key, ignoreMemCache: ignoreMemCache);
Future<FileInfo?> getFileFromCache(
String key, {
bool ignoreMemCache = false,
}) => _store.getFile(key, ignoreMemCache: ignoreMemCache);

///Returns the file from memory if it has already been fetched
@override
Expand Down Expand Up @@ -251,11 +264,14 @@ class CacheManager implements BaseCacheManager {
}) async {
key ??= url;
var cacheObject = await _store.retrieveCacheData(key);
cacheObject ??= CacheObject(url,
key: key,
relativePath: '${const Uuid().v1()}'
'.$fileExtension',
validTill: DateTime.now().add(maxAge));
cacheObject ??= CacheObject(
url,
key: key,
relativePath:
'${const Uuid().v1()}'
'.$fileExtension',
validTill: DateTime.now().add(maxAge),
);

cacheObject = cacheObject.copyWith(
validTill: DateTime.now().add(maxAge),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@ abstract class BaseCacheManager {
/// The files are returned as stream. First the cached file if available, when the
/// cached file is too old the newly downloaded file is returned afterwards.
@Deprecated('Prefer to use the new getFileStream method')
Stream<FileInfo> getFile(String url,
{String key, Map<String, String> headers});
Stream<FileInfo> getFile(
String url, {
String key,
Map<String, String> headers,
});

/// Get the file from the cache and/or online, depending on availability and age.
/// Downloaded form [url], [headers] can be used for example for authentication.
Expand All @@ -37,12 +40,20 @@ abstract class BaseCacheManager {
/// set on true and the file is not available in the cache. When the file is
/// returned from the cache there will be no progress given, although the file
/// might be outdated and a new file is being downloaded in the background.
Stream<FileResponse> getFileStream(String url,
{String? key, Map<String, String>? headers, bool withProgress});
Stream<FileResponse> getFileStream(
String url, {
String? key,
Map<String, String>? headers,
bool withProgress,
});

///Download the file and add to cache
Future<FileInfo> downloadFile(String url,
{String? key, Map<String, String>? authHeaders, bool force = false});
Future<FileInfo> downloadFile(
String url, {
String? key,
Map<String, String>? authHeaders,
bool force = false,
});

/// Get the file from the cache.
/// Specify [ignoreMemCache] to force a re-read from the database
Expand Down
Loading
Loading