1- import 'package:distributeapp/api/download_api.dart' ;
21import 'package:distributeapp/model/song.dart' ;
2+ import 'package:distributeapp/repositories/download/song_download_service.dart' ;
33import 'package:distributeapp/repositories/playlist_repository.dart' ;
44import 'package:flutter_bloc/flutter_bloc.dart' ;
55import 'package:freezed_annotation/freezed_annotation.dart' ;
@@ -25,14 +25,17 @@ abstract class DownloadState with _$DownloadState {
2525}
2626
2727class DownloadCubit extends Cubit <DownloadState > {
28- final DownloadApi _api ;
28+ final SongDownloadService _downloadService ;
2929 final PlaylistRepository _playlistRepository;
3030
3131 final List <Song > _downloadQueue = [];
3232
3333 bool _isDownloading = false ;
3434
35- DownloadCubit (this ._api, this ._playlistRepository)
35+ DownloadCubit (
36+ this ._downloadService,
37+ this ._playlistRepository,
38+ )
3639 : super (const DownloadState ());
3740
3841 void downloadSong (Song song) {
@@ -109,15 +112,12 @@ class DownloadCubit extends Cubit<DownloadState> {
109112 _updateStatus (song.id, const DownloadStatus .loading (progress: 0.0 ));
110113
111114 try {
112- await _api.downloadFile (song, (received, total) {
113- if (total != - 1 ) {
114- _updateStatus (
115- song.id,
116- DownloadStatus .loading (progress: received / total),
117- );
118- }
115+ await _downloadService.downloadSongWithArtwork (song, (progress) {
116+ _updateStatus (
117+ song.id,
118+ DownloadStatus .loading (progress: progress),
119+ );
119120 });
120- await _playlistRepository.updateSongDownloaded (song.id, true );
121121 _updateStatus (song.id, const DownloadStatus .success ());
122122 } catch (e) {
123123 _updateStatus (song.id, DownloadStatus .error (message: e.toString ()));
@@ -130,8 +130,7 @@ class DownloadCubit extends Cubit<DownloadState> {
130130
131131 Future <void > deleteFile (Song song) async {
132132 try {
133- await _api.deleteFile (song);
134- await _playlistRepository.updateSongDownloaded (song.id, false );
133+ await _downloadService.deleteSongFile (song);
135134 _updateStatus (song.id, const DownloadStatus .initial ());
136135 } catch (e) {
137136 _updateStatus (song.id, DownloadStatus .error (message: e.toString ()));
0 commit comments