Skip to content

Commit

Permalink
feat: clear audio cache in advanced settings
Browse files Browse the repository at this point in the history
  • Loading branch information
MSOB7YY committed Feb 16, 2024
1 parent 2ab36da commit 752c3b4
Show file tree
Hide file tree
Showing 9 changed files with 634 additions and 389 deletions.
22 changes: 20 additions & 2 deletions lib/base/audio_handler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -623,7 +623,12 @@ class NamidaAudioVideoHandler<Q extends Playable> extends BasicAudioHandler<Q> {

final cachedAudio = stream?.getCachedFile(videoId);
if (cachedAudio != null && useCache) {
await setSource(AudioSource.file(cachedAudio.path, tag: mediaItem), startPlaying: wasPlaying, keepOldVideoSource: true);
await setSource(
AudioSource.file(cachedAudio.path, tag: mediaItem),
startPlaying: wasPlaying,
keepOldVideoSource: true,
cachedAudioPath: cachedAudio.path,
);
refreshNotification();
} else if (stream != null && stream.url != null) {
if (wasPlaying) await super.onPauseRaw();
Expand Down Expand Up @@ -873,6 +878,7 @@ class NamidaAudioVideoHandler<Q extends Playable> extends BasicAudioHandler<Q> {
startPlaying: startPlaying,
videoOptions: videoOptions,
isVideoFile: cachedVideo?.path != null,
cachedAudioPath: cachedAudio.path,
)
: await setSource(
LockCachingAudioSource(
Expand Down Expand Up @@ -993,6 +999,7 @@ class NamidaAudioVideoHandler<Q extends Playable> extends BasicAudioHandler<Q> {
maxTotalCacheSize: _defaultMaxCache,
),
isVideoFile: true,
cachedAudioPath: cachedAudio.file.path,
);
final audioDetails = AudioCacheDetails(
youtubeId: item.id,
Expand All @@ -1014,6 +1021,7 @@ class NamidaAudioVideoHandler<Q extends Playable> extends BasicAudioHandler<Q> {
await setSource(
AudioSource.file(cachedAudio.file.path, tag: mediaItem),
startPlaying: startPlaying,
cachedAudioPath: cachedAudio.file.path,
);
final audioDetails = AudioCacheDetails(
youtubeId: item.id,
Expand Down Expand Up @@ -1255,7 +1263,13 @@ class NamidaAudioVideoHandler<Q extends Playable> extends BasicAudioHandler<Q> {
await onPauseRaw();
// -- try putting cache version if it was cached
_nextSeekSetAudioCache = null;
if (await cachedAudioFile.exists()) await setSource(AudioSource.file(cachedAudioFile.path, tag: mediaItem), keepOldVideoSource: true);
if (await cachedAudioFile.exists()) {
await setSource(
AudioSource.file(cachedAudioFile.path, tag: mediaItem),
keepOldVideoSource: true,
cachedAudioPath: cachedAudioFile.path,
);
}
_isCurrentAudioFromCache = true;
await plsSeek();
if (wasPlaying) await onPlayRaw();
Expand Down Expand Up @@ -1315,10 +1329,14 @@ class NamidaAudioVideoHandler<Q extends Playable> extends BasicAudioHandler<Q> {
VideoOptions? videoOptions,
bool keepOldVideoSource = false,
bool isVideoFile = false,
String? cachedAudioPath,
}) async {
if (isVideoFile && videoOptions != null) {
File(videoOptions.source).setLastAccessedTry(DateTime.now());
}
if (cachedAudioPath != null) {
File(cachedAudioPath).setLastAccessedTry(DateTime.now());
}
return await setAudioSource(
source,
preload: preload,
Expand Down
12 changes: 2 additions & 10 deletions lib/class/video.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'dart:io';

import 'package:namida/controller/thumbnail_manager.dart';
import 'package:namida/core/constants.dart';
import 'package:namida/core/extensions.dart';

Expand Down Expand Up @@ -192,14 +193,5 @@ extension NamidaVideoUtils on NamidaVideo {
return "${AppDirs.THUMBNAILS}$prefix$name.png";
}

String get pathToYTImage {
String getPath(String prefix) => "${AppDirs.YT_THUMBNAILS}$prefix$ytID.png";

final path = getPath('');
if (File(path).existsSync()) {
return path;
}

return getPath('EXT_');
}
String get pathToYTImage => ThumbnailManager.getPathToYTImage(ytID);
}
5 changes: 5 additions & 0 deletions lib/controller/settings_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ class SettingsController with SettingsFileWriter {
final RxInt isTrackPlayedPercentageCount = 40.obs;
final RxInt waveformTotalBars = 140.obs;
final RxInt videosMaxCacheInMB = (2 * 1024).obs; // 2GB
final RxInt audiosMaxCacheInMB = (2 * 1024).obs; // 2GB
final RxInt imagesMaxCacheInMB = (8 * 32).obs; // 256 MB
final RxInt ytMiniplayerDimAfterSeconds = 15.obs;
final RxDouble ytMiniplayerDimOpacity = 0.5.obs;
Expand Down Expand Up @@ -390,6 +391,7 @@ class SettingsController with SettingsFileWriter {
isTrackPlayedPercentageCount.value = json['isTrackPlayedPercentageCount'] ?? isTrackPlayedPercentageCount.value;
waveformTotalBars.value = json['waveformTotalBars'] ?? waveformTotalBars.value;
videosMaxCacheInMB.value = json['videosMaxCacheInMB'] ?? videosMaxCacheInMB.value;
audiosMaxCacheInMB.value = json['audiosMaxCacheInMB'] ?? audiosMaxCacheInMB.value;
imagesMaxCacheInMB.value = json['imagesMaxCacheInMB'] ?? imagesMaxCacheInMB.value;
ytMiniplayerDimAfterSeconds.value = json['ytMiniplayerDimAfterSeconds'] ?? ytMiniplayerDimAfterSeconds.value;
ytMiniplayerDimOpacity.value = json['ytMiniplayerDimOpacity'] ?? ytMiniplayerDimOpacity.value;
Expand Down Expand Up @@ -570,6 +572,7 @@ class SettingsController with SettingsFileWriter {
'isTrackPlayedPercentageCount': isTrackPlayedPercentageCount.value,
'waveformTotalBars': waveformTotalBars.value,
'videosMaxCacheInMB': videosMaxCacheInMB.value,
'audiosMaxCacheInMB': audiosMaxCacheInMB.value,
'imagesMaxCacheInMB': imagesMaxCacheInMB.value,
'ytMiniplayerDimAfterSeconds': ytMiniplayerDimAfterSeconds.value,
'ytMiniplayerDimOpacity': ytMiniplayerDimOpacity.value,
Expand Down Expand Up @@ -734,6 +737,7 @@ class SettingsController with SettingsFileWriter {
bool? pickColorsFromDeviceWallpaper,
int? waveformTotalBars,
int? videosMaxCacheInMB,
int? audiosMaxCacheInMB,
int? imagesMaxCacheInMB,
int? ytMiniplayerDimAfterSeconds,
double? ytMiniplayerDimOpacity,
Expand Down Expand Up @@ -946,6 +950,7 @@ class SettingsController with SettingsFileWriter {
if (pickColorsFromDeviceWallpaper != null) this.pickColorsFromDeviceWallpaper.value = pickColorsFromDeviceWallpaper;
if (waveformTotalBars != null) this.waveformTotalBars.value = waveformTotalBars;
if (videosMaxCacheInMB != null) this.videosMaxCacheInMB.value = videosMaxCacheInMB;
if (audiosMaxCacheInMB != null) this.audiosMaxCacheInMB.value = audiosMaxCacheInMB;
if (imagesMaxCacheInMB != null) this.imagesMaxCacheInMB.value = imagesMaxCacheInMB;
if (ytMiniplayerDimAfterSeconds != null) this.ytMiniplayerDimAfterSeconds.value = ytMiniplayerDimAfterSeconds;
if (ytMiniplayerDimOpacity != null) this.ytMiniplayerDimOpacity.value = ytMiniplayerDimOpacity;
Expand Down
Loading

0 comments on commit 752c3b4

Please sign in to comment.