spotube/lib/provider/download_manager_provider.dart
Alessio fece073def This pull request primarily involves the removal of several configuration files and assets, as well as minor updates to documentation. The most significant changes are the deletion of various .vscode configuration files and the removal of unused assets from the project.
Configuration File Removals:

    .vscode/c_cpp_properties.json: Removed the entire configuration for C/C++ properties.
    .vscode/launch.json: Removed the Dart launch configurations for different environments and modes.
    .vscode/settings.json: Removed settings related to CMake, spell checking, file nesting, and Dart Flutter SDK path.
    .vscode/snippets.code-snippets: Removed code snippets for Dart, including PaginatedState and PaginatedNotifier templates.
    .vscode/tasks.json: Removed the tasks configuration file.

Documentation Updates:

    CONTRIBUTION.md: Removed heart emoji from the introductory text.
    README.md: Updated the logo image and made minor text adjustments, including removing emojis and updating section titles. [1] [2] [3] [4] [5]

Asset Removals:

    lib/collections/assets.gen.dart: Removed multiple unused asset references, including images related to Spotube logos and banners. [1] [2] [3]

Minor Code Cleanups:

    cli/commands/build/linux.dart, cli/commands/build/windows.dart, cli/commands/translated.dart, cli/commands/untranslated.dart: Adjusted import statements for consistency. [1] [2] [3] [4]
    integration_test/app_test.dart: Removed an unnecessary blank line.
    lib/collections/routes.dart: Commented out the TrackRoute configuration.
2025-04-13 18:40:37 +02:00

238 lines
7.1 KiB
Dart

import 'dart:async';
import 'dart:io';
import 'package:collection/collection.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:metadata_god/metadata_god.dart';
import 'package:path/path.dart';
import 'package:spotify/spotify.dart';
import 'package:spotube/extensions/artist_simple.dart';
import 'package:spotube/extensions/image.dart';
import 'package:spotube/extensions/track.dart';
import 'package:spotube/provider/user_preferences/user_preferences_provider.dart';
import 'package:spotube/services/download_manager/download_manager.dart';
import 'package:spotube/services/logger/logger.dart';
import 'package:spotube/services/sourced_track/enums.dart';
import 'package:spotube/services/sourced_track/sourced_track.dart';
import 'package:spotube/utils/primitive_utils.dart';
import 'package:spotube/utils/service_utils.dart';
class DownloadManagerProvider extends ChangeNotifier {
DownloadManagerProvider({required this.ref})
: $history = <SourcedTrack>{},
$backHistory = <Track>{},
dl = DownloadManager() {
dl.statusStream.listen((event) async {
try {
final (:request, :status) = event;
final track = $history.firstWhereOrNull(
(element) => element.getUrlOfCodec(downloadCodec) == request.url,
);
if (track == null) return;
final savePath = getTrackFileUrl(track);
// related to onFileExists
final oldFile = File("$savePath.old");
// if download failed and old file exists, rename it back
if ((status == DownloadStatus.failed ||
status == DownloadStatus.canceled) &&
await oldFile.exists()) {
await oldFile.rename(savePath);
}
if (status != DownloadStatus.completed ||
//? WebA audiotagging is not supported yet
//? Although in future by converting weba to opus & then tagging it
//? is possible using vorbis comments
downloadCodec == SourceCodecs.weba) return;
final file = File(request.path);
if (await oldFile.exists()) {
await oldFile.delete();
}
final imageBytes = await ServiceUtils.downloadImage(
(track.album?.images).asUrlString(
placeholder: ImagePlaceholder.albumArt,
index: 1,
),
);
final metadata = track.toMetadata(
fileLength: await file.length(),
imageBytes: imageBytes,
);
await MetadataGod.writeMetadata(
file: file.path,
metadata: metadata,
);
} catch (e, stack) {
AppLogger.reportError(e, stack);
}
});
}
Future<bool> Function(Track track) onFileExists = (Track track) async => true;
final Ref<DownloadManagerProvider> ref;
String get downloadDirectory =>
ref.read(userPreferencesProvider.select((s) => s.downloadLocation));
SourceCodecs get downloadCodec =>
ref.read(userPreferencesProvider.select((s) => s.downloadMusicCodec));
int get $downloadCount => dl
.getAllDownloads()
.where(
(download) =>
download.status.value == DownloadStatus.downloading ||
download.status.value == DownloadStatus.paused ||
download.status.value == DownloadStatus.queued,
)
.length;
final Set<SourcedTrack> $history;
// these are the tracks which metadata hasn't been fetched yet
final Set<Track> $backHistory;
final DownloadManager dl;
String getTrackFileUrl(Track track) {
final name =
"${track.name} - ${track.artists?.asString() ?? ""}.${downloadCodec.name}";
return join(downloadDirectory, PrimitiveUtils.toSafeFileName(name));
}
bool isActive(Track track) {
if ($backHistory.contains(track)) return true;
final sourcedTrack = mapToSourcedTrack(track);
if (sourcedTrack == null) return false;
return dl
.getAllDownloads()
.where(
(download) =>
download.status.value == DownloadStatus.downloading ||
download.status.value == DownloadStatus.paused ||
download.status.value == DownloadStatus.queued,
)
.map((e) => e.request.url)
.contains(sourcedTrack.getUrlOfCodec(downloadCodec));
}
/// For singular downloads
Future<void> addToQueue(Track track) async {
final savePath = getTrackFileUrl(track);
final oldFile = File(savePath);
if (await oldFile.exists() && !await onFileExists(track)) {
return;
}
if (await oldFile.exists()) {
await oldFile.rename("$savePath.old");
}
if (track is SourcedTrack && track.codec == downloadCodec) {
final downloadTask =
await dl.addDownload(track.getUrlOfCodec(downloadCodec), savePath);
if (downloadTask != null) {
$history.add(track);
}
} else {
$backHistory.add(track);
final sourcedTrack = await SourcedTrack.fetchFromTrack(
ref: ref,
track: track,
).then((d) {
$backHistory.remove(track);
return d;
});
final downloadTask = await dl.addDownload(
sourcedTrack.getUrlOfCodec(downloadCodec),
savePath,
);
if (downloadTask != null) {
$history.add(sourcedTrack);
}
}
notifyListeners();
}
Future<void> batchAddToQueue(List<Track> tracks) async {
$backHistory.addAll(
tracks.where((element) => element is! SourcedTrack),
);
notifyListeners();
for (final track in tracks) {
try {
if (track == tracks.first) {
await addToQueue(track);
} else {
await Future.delayed(
const Duration(seconds: 1),
() => addToQueue(track),
);
}
} catch (e) {
AppLogger.reportError(e, StackTrace.current);
continue;
}
}
}
Future<void> removeFromQueue(SourcedTrack track) async {
await dl.removeDownload(track.getUrlOfCodec(downloadCodec));
$history.remove(track);
}
Future<void> pause(SourcedTrack track) {
return dl.pauseDownload(track.getUrlOfCodec(downloadCodec));
}
Future<void> resume(SourcedTrack track) {
return dl.resumeDownload(track.getUrlOfCodec(downloadCodec));
}
Future<void> retry(SourcedTrack track) {
return addToQueue(track);
}
void cancel(SourcedTrack track) {
dl.cancelDownload(track.getUrlOfCodec(downloadCodec));
}
void cancelAll() {
for (final download in dl.getAllDownloads()) {
if (download.status.value == DownloadStatus.completed) continue;
dl.cancelDownload(download.request.url);
}
}
SourcedTrack? mapToSourcedTrack(Track track) {
if (track is SourcedTrack) {
return track;
} else {
return $history.firstWhereOrNull((element) => element.id == track.id);
}
}
ValueNotifier<DownloadStatus>? getStatusNotifier(SourcedTrack track) {
return dl.getDownload(track.getUrlOfCodec(downloadCodec))?.status;
}
ValueNotifier<double>? getProgressNotifier(SourcedTrack track) {
return dl.getDownload(track.getUrlOfCodec(downloadCodec))?.progress;
}
}
final downloadManagerProvider = ChangeNotifierProvider<DownloadManagerProvider>(
(ref) => DownloadManagerProvider(ref: ref),
);