1
1
import 'dart:async' ;
2
+ import 'dart:io' ;
2
3
3
4
import 'package:collection/collection.dart' ;
5
+ import 'package:dio/dio.dart' ;
4
6
import 'package:flutter/foundation.dart' ;
5
7
6
8
import '../../constants.dart' ;
@@ -227,7 +229,70 @@ class LibraryService {
227
229
}
228
230
229
231
// Podcasts
232
+ final dio = Dio ();
233
+ Map <String , String > _downloads = {};
234
+ Map <String , String > get downloads => _downloads;
235
+ String ? getDownload (String ? url) => downloads[url];
236
+
237
+ Set <String > _feedsWithDownloads = {};
238
+ bool feedHasDownloads (String feedUrl) =>
239
+ _feedsWithDownloads.contains (feedUrl);
240
+ int get feedsWithDownloadsLength => _feedsWithDownloads.length;
241
+
242
+ final _downloadsController = StreamController <bool >.broadcast ();
243
+ Stream <bool > get downloadsChanged => _downloadsController.stream;
244
+ void addDownload ({
245
+ required String url,
246
+ required String path,
247
+ required String feedUrl,
248
+ }) {
249
+ _downloads.putIfAbsent (url, () => path);
250
+ _feedsWithDownloads.add (feedUrl);
251
+ writeStringMap (_downloads, kDownloads)
252
+ .then (
253
+ (_) => writeStringSet (
254
+ set : _feedsWithDownloads,
255
+ filename: kFeedsWithDownloads,
256
+ ),
257
+ )
258
+ .then ((_) => _downloadsController.add (true ));
259
+ }
260
+
261
+ void removeDownload ({required String url, required String feedUrl}) {
262
+ final path = _downloads[url];
263
+
264
+ if (path != null ) {
265
+ final file = File (path);
266
+ if (file.existsSync ()) {
267
+ file.deleteSync ();
268
+ }
269
+ }
270
+
271
+ if (_downloads.containsKey (url)) {
272
+ _downloads.remove (url);
273
+ _feedsWithDownloads.remove (feedUrl);
274
+
275
+ writeStringMap (_downloads, kDownloads)
276
+ .then (
277
+ (_) => writeStringSet (
278
+ set : _feedsWithDownloads,
279
+ filename: kFeedsWithDownloads,
280
+ ),
281
+ )
282
+ .then ((_) => _downloadsController.add (true ));
283
+ }
284
+ }
285
+
286
+ void _removeFeedWithDownload (String feedUrl) {
287
+ _feedsWithDownloads.remove (feedUrl);
288
+ writeStringSet (
289
+ set : _feedsWithDownloads,
290
+ filename: kFeedsWithDownloads,
291
+ ).then ((_) => _downloadsController.add (true ));
292
+ }
230
293
294
+ String ? _downloadsDir;
295
+ String ? get downloadsDir => _downloadsDir;
231
296
Map <String , Set <Audio >> _podcasts = {};
232
297
Map <String , Set <Audio >> get podcasts => _podcasts;
233
298
int get podcastsLength => _podcasts.length;
@@ -274,7 +339,9 @@ class LibraryService {
274
339
void removePodcast (String name) {
275
340
_podcasts.remove (name);
276
341
writeAudioMap (_podcasts, kPodcastsFileName)
277
- .then ((_) => _podcastsController.add (true ));
342
+ .then ((_) => _podcastsController.add (true ))
343
+ .then ((_) => removePodcastUpdate (name))
344
+ .then ((_) => _removeFeedWithDownload (name));
278
345
}
279
346
280
347
//
@@ -342,6 +409,9 @@ class LibraryService {
342
409
(await readAudioMap (kLikedAudios)).entries.firstOrNull? .value ??
343
410
< Audio > {};
344
411
_favTags = (await readStringSet (filename: kTagFavsFileName));
412
+ _downloadsDir = await getDownloadsDir ();
413
+ _downloads = await readStringMap (kDownloads);
414
+ _feedsWithDownloads = await readStringSet (filename: kFeedsWithDownloads);
345
415
_libraryInitialized = true ;
346
416
}
347
417
@@ -399,6 +469,7 @@ class LibraryService {
399
469
}
400
470
401
471
Future <void > dispose () async {
472
+ dio.close ();
402
473
await safeStates ();
403
474
await _useLocalAudioCacheController.close ();
404
475
await _albumsController.close ();
@@ -414,6 +485,7 @@ class LibraryService {
414
485
await _neverShowFailedImportsController.close ();
415
486
await _lastFavController.close ();
416
487
await _updateController.close ();
488
+ await _downloadsController.close ();
417
489
}
418
490
419
491
Future <void > safeStates () async {
0 commit comments