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
23 changes: 23 additions & 0 deletions lib/src/cache_key_helpers.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/// The prefix used for all keys in this package.
const packagePrefix = 'cached_video_player_plus_';

/// The prefix used for all cache keys in this package.
const cacheKeyPrefix = '${packagePrefix}caching_time_of_';

/// The prefix used for old cache keys in this package.
///
/// This is used for migration purposes to handle old cache keys.
const oldCacheKeyPrefix = '${packagePrefix}video_expiration_of_';

/// The key used to track migration completion.
const migrationKey = '${packagePrefix}migration_completed_v4';

/// Generates a storage key for the given [dataSource].
String getCacheKey(String dataSource) {
return '$cacheKeyPrefix${Uri.parse(dataSource)}';
}

/// Generates a storage key using the provided custom [cacheKey].
String getCustomCacheKey(String cacheKey) {
return '$cacheKeyPrefix$cacheKey';
}
35 changes: 13 additions & 22 deletions lib/src/cached_video_player_plus.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,10 @@ import 'package:flutter/foundation.dart';
import 'package:flutter_cache_manager/flutter_cache_manager.dart';
import 'package:video_player/video_player.dart';

import 'cache_key_helpers.dart';
import 'video_cache_manager.dart';
import 'video_player_storage.dart';

/// The default cache manager for video file caching operations.
final _defaultCacheManager = VideoCacheManager();

/// Global storage for cache metadata and expiration timestamps.
final _storage = VideoPlayerStorage();

/// Generates a storage key for the given [dataSource].
String _getCacheKey(String dataSource) {
return 'cached_video_player_plus_video_expiration_of_${Uri.parse(dataSource)}';
}

/// Generates a storage key using the provided custom [cacheKey].
String _getCustomCacheKey(String cacheKey) {
return 'cached_video_player_plus_video_expiration_of_$cacheKey';
}

/// A video player that wraps [VideoPlayerController] with intelligent
/// caching capabilities using [flutter_cache_manager].
///
Expand Down Expand Up @@ -116,8 +101,8 @@ class CachedVideoPlayerPlus {
package = null,
_authHeaders = downloadHeaders ?? httpHeaders,
_cacheKey = cacheKey != null
? _getCustomCacheKey(cacheKey)
: _getCacheKey(url.toString()),
? getCustomCacheKey(cacheKey)
: getCacheKey(url.toString()),
_cacheManager = cacheManager ?? _defaultCacheManager;

/// Constructs a [CachedVideoPlayerPlus] playing a video from a file.
Expand Down Expand Up @@ -270,6 +255,12 @@ class CachedVideoPlayerPlus {
return dataSourceType == DataSourceType.network && !kIsWeb && !skipCache;
}

/// The default cache manager for video file caching operations.
static final _defaultCacheManager = VideoCacheManager();

/// Default storage for cache metadata and expiration timestamps.
static final _storage = VideoPlayerStorage();

/// Initializes the video player and sets up caching if applicable.
///
/// This method must be called before accessing the [controller] or playing
Expand Down Expand Up @@ -433,7 +424,7 @@ class CachedVideoPlayerPlus {
CacheManager? cacheManager,
}) async {
final urlString = url.toString();
final cacheKey = _getCacheKey(urlString);
final cacheKey = getCacheKey(urlString);

cacheManager ??= _defaultCacheManager;

Expand All @@ -458,7 +449,7 @@ class CachedVideoPlayerPlus {
String cacheKey, {
CacheManager? cacheManager,
}) async {
cacheKey = _getCustomCacheKey(cacheKey);
cacheKey = getCustomCacheKey(cacheKey);

cacheManager ??= _defaultCacheManager;

Expand Down Expand Up @@ -517,8 +508,8 @@ class CachedVideoPlayerPlus {
cacheManager ??= _defaultCacheManager;

final effectiveCacheKey = cacheKey != null
? _getCustomCacheKey(cacheKey)
: _getCacheKey(url.toString());
? getCustomCacheKey(cacheKey)
: getCacheKey(url.toString());

// First check if the video is already cached
FileInfo? cachedFile = await cacheManager.getFileFromCache(
Expand Down
7 changes: 3 additions & 4 deletions lib/src/video_player_storage.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import 'package:shared_preferences/shared_preferences.dart';

import 'cache_key_helpers.dart' show cacheKeyPrefix;

/// Storage abstraction for cached video metadata.
///
/// This class handles the storage of cache expiration timestamps and provides
Expand All @@ -8,9 +10,6 @@ class VideoPlayerStorage {
/// SharedPreferences instance for storing cache metadata.
final _asyncPrefs = SharedPreferencesAsync();

/// Key prefix for all video player storage keys.
static const _keyPrefix = 'cached_video_player_plus_video_expiration_of_';

/// Singleton instance of VideoPlayerStorage.
static final _instance = VideoPlayerStorage._internal();

Expand Down Expand Up @@ -46,7 +45,7 @@ class VideoPlayerStorage {
/// This removes all keys that start with the video player prefix.
Future<void> erase() async {
final keys = await _asyncPrefs.getKeys();
final videoPlayerKeys = keys.where((key) => key.startsWith(_keyPrefix));
final videoPlayerKeys = keys.where((key) => key.startsWith(cacheKeyPrefix));

for (final key in videoPlayerKeys) {
await _asyncPrefs.remove(key);
Expand Down
9 changes: 6 additions & 3 deletions lib/util/migration_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import 'package:flutter/foundation.dart';
import 'package:get_storage/get_storage.dart';
import 'package:shared_preferences/shared_preferences.dart';

import '../src/cache_key_helpers.dart';

/// Migrates cached video data from get_storage to shared_preferences.
///
/// This method can be run multiple times without worry of overwriting
Expand All @@ -35,8 +37,6 @@ import 'package:shared_preferences/shared_preferences.dart';
/// This migration is necessary to ensures that existing cached video data is
/// preserved and accessible through shared_preferences.
Future<void> migrateCachedVideoDataToSharedPreferences() async {
const migrationKey = 'cached_video_player_plus_migration_completed_v4';

try {
// Check if migration has already been completed
final asyncPrefs = SharedPreferencesAsync();
Expand All @@ -62,7 +62,10 @@ Future<void> migrateCachedVideoDataToSharedPreferences() async {
for (final key in getStorageKeys) {
final value = getStorage.read(key);
if (value is int) {
await asyncPrefs.setInt(key, value);
await asyncPrefs.setInt(
key.replaceFirst(oldCacheKeyPrefix, cacheKeyPrefix),
value,
);
migratedCount++;
}
}
Expand Down