-
-
Notifications
You must be signed in to change notification settings - Fork 99
/
podcast_bloc.dart
444 lines (379 loc) · 15.7 KB
/
podcast_bloc.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
// Copyright 2020 Ben Hills and the project contributors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:anytime/bloc/bloc.dart';
import 'package:anytime/entities/downloadable.dart';
import 'package:anytime/entities/episode.dart';
import 'package:anytime/entities/feed.dart';
import 'package:anytime/entities/podcast.dart';
import 'package:anytime/services/audio/audio_player_service.dart';
import 'package:anytime/services/download/download_service.dart';
import 'package:anytime/services/download/mobile_download_service.dart';
import 'package:anytime/services/podcast/podcast_service.dart';
import 'package:anytime/services/settings/settings_service.dart';
import 'package:anytime/state/bloc_state.dart';
import 'package:collection/collection.dart' show IterableExtension;
import 'package:logging/logging.dart';
import 'package:rxdart/rxdart.dart';
enum PodcastEvent {
subscribe,
unsubscribe,
markAllPlayed,
clearAllPlayed,
reloadSubscriptions,
refresh,
// Filter
episodeFilterNone,
episodeFilterStarted,
episodeFilterNotFinished,
episodeFilterFinished,
// Sort
episodeSortDefault,
episodeSortLatest,
episodeSortEarliest,
episodeSortAlphabeticalAscending,
episodeSortAlphabeticalDescending,
}
/// This BLoC provides access to the details of a given Podcast.
///
/// It takes a feed URL and creates a [Podcast] instance. There are several listeners that
/// handle actions on a podcast such as requesting an episode download, following/unfollowing
/// a podcast and marking/un-marking all episodes as played.
class PodcastBloc extends Bloc {
final log = Logger('PodcastBloc');
final PodcastService podcastService;
final AudioPlayerService audioPlayerService;
final DownloadService downloadService;
final SettingsService settingsService;
final BehaviorSubject<Feed> _podcastFeed = BehaviorSubject<Feed>(sync: true);
/// Add to sink to start an Episode download
final PublishSubject<Episode?> _downloadEpisode = PublishSubject<Episode?>();
/// Listen to this subject's stream to obtain list of current subscriptions.
late PublishSubject<List<Podcast>> _subscriptions;
/// Stream containing details of the current podcast.
final BehaviorSubject<BlocState<Podcast>> _podcastStream = BehaviorSubject<BlocState<Podcast>>(sync: true);
/// A separate stream that allows us to listen to changes in the podcast's episodes.
final BehaviorSubject<List<Episode?>?> _episodesStream = BehaviorSubject<List<Episode?>?>();
/// Receives subscription and mark/clear as played events.
final PublishSubject<PodcastEvent> _podcastEvent = PublishSubject<PodcastEvent>();
final BehaviorSubject<BlocState<void>> _backgroundLoadStream = BehaviorSubject<BlocState<void>>();
Podcast? _podcast;
List<Episode> _episodes = <Episode>[];
late Feed lastFeed;
bool first = true;
PodcastBloc({
required this.podcastService,
required this.audioPlayerService,
required this.downloadService,
required this.settingsService,
}) {
_init();
}
void _init() {
/// When someone starts listening for subscriptions, load them.
_subscriptions = PublishSubject<List<Podcast>>(onListen: _loadSubscriptions);
/// When we receive a load podcast request, send back a BlocState.
_listenPodcastLoad();
/// Listen to an Episode download request
_listenDownloadRequest();
/// Listen to active downloads
_listenDownloads();
/// Listen to episode change events sent by the [Repository]
_listenEpisodeRepositoryEvents();
/// Listen to Podcast subscription, mark/cleared played events
_listenPodcastStateEvents();
}
void _loadSubscriptions() async {
_subscriptions.add(await podcastService.subscriptions());
}
/// Sets up a listener to handle Podcast load requests. We first push a [BlocLoadingState] to
/// indicate that the Podcast is being loaded, before calling the [PodcastService] to handle
/// the loading. Once loaded, we extract the episodes from the Podcast and push them out via
/// the episode stream before pushing a [BlocPopulatedState] containing the Podcast.
void _listenPodcastLoad() async {
_podcastFeed.listen((feed) async {
var silent = false;
lastFeed = feed;
_episodes = [];
_episodesStream.add(_episodes);
_podcastStream.sink.add(BlocLoadingState<Podcast>(feed.podcast));
try {
await _loadEpisodes(feed, feed.refresh);
/// Do we also need to perform a background refresh?
if (feed.podcast.id != null && feed.backgroundFresh && _shouldAutoRefresh()) {
silent = feed.silently;
log.fine('Performing background refresh of ${feed.podcast.url}');
_backgroundLoadStream.sink.add(BlocLoadingState<void>());
await _loadNewEpisodes(feed);
}
_backgroundLoadStream.sink.add(BlocSuccessfulState<void>());
} catch (e) {
_backgroundLoadStream.sink.add(BlocDefaultState<void>());
// For now we'll assume a network error as this is the most likely.
if ((_podcast == null || lastFeed.podcast.url == _podcast!.url) && !silent) {
_podcastStream.sink.add(BlocErrorState<Podcast>());
log.fine('Error loading podcast', e);
log.fine(e);
}
}
});
}
/// Determines if the current feed should be updated in the background.
///
/// If the autoUpdatePeriod is -1 this means never; 0 means always and any other
/// value is the time in minutes.
bool _shouldAutoRefresh() {
/// If we are currently following this podcast it will have an id. At
/// this point we can compare the last updated time to the update
/// after setting time.
if (settingsService.autoUpdateEpisodePeriod == -1) {
return false;
} else if (_podcast == null || settingsService.autoUpdateEpisodePeriod == 0) {
return true;
} else if (_podcast != null && _podcast!.id != null) {
var currentTime = DateTime.now().subtract(Duration(minutes: settingsService.autoUpdateEpisodePeriod));
var lastUpdated = _podcast!.lastUpdated;
return currentTime.isAfter(lastUpdated);
}
return false;
}
Future<void> _loadEpisodes(Feed feed, bool force) async {
_podcast = await podcastService.loadPodcast(
podcast: feed.podcast,
refresh: force,
);
/// Only populate episodes if the ID we started the load with is the
/// same as the one we have ended up with.
if (_podcast != null && _podcast?.url != null) {
if (lastFeed.podcast.url == _podcast!.url) {
_episodes = _podcast!.episodes;
_episodesStream.add(_episodes);
_podcastStream.sink.add(BlocPopulatedState<Podcast>(results: _podcast));
}
}
}
void _refresh() {
_episodesStream.add(_episodes);
}
Future<void> _loadNewEpisodes(Feed feed) async {
_podcast = await podcastService.loadPodcast(
podcast: feed.podcast,
highlightNewEpisodes: true,
refresh: true,
);
/// Only populate episodes if the ID we started the load with is the
/// same as the one we have ended up with.
if (_podcast != null && lastFeed.podcast.url == _podcast!.url) {
_episodes = _podcast!.episodes;
if (_podcast!.newEpisodes) {
log.fine('We have new episodes to display');
_backgroundLoadStream.sink.add(BlocPopulatedState<void>());
_podcastStream.sink.add(BlocPopulatedState<Podcast>(results: _podcast));
} else if (_podcast!.updatedEpisodes) {
log.fine('We have updated episodes to re-display');
_episodesStream.add(_episodes);
}
}
log.fine('Background loading successful state');
_backgroundLoadStream.sink.add(BlocSuccessfulState<void>());
}
Future<void> _loadFilteredEpisodes() async {
if (_podcast != null) {
_podcast = await podcastService.loadPodcast(
podcast: _podcast!,
highlightNewEpisodes: false,
refresh: false,
);
_episodes = _podcast!.episodes;
_podcastStream.add(BlocPopulatedState<Podcast>(results: _podcast));
_refresh();
}
}
/// Sets up a listener to handle requests to download an episode.
void _listenDownloadRequest() {
_downloadEpisode.listen((Episode? e) async {
log.fine('Received download request for ${e!.title}');
// To prevent a pause between the user tapping the download icon and
// the UI showing some sort of progress, set it to queued now.
var episode = _episodes.firstWhereOrNull((ep) => ep.guid == e.guid);
if (episode != null) {
episode.downloadState = e.downloadState = DownloadState.queued;
_episodesStream.add(_episodes);
var result = await downloadService.downloadEpisode(e);
// If there was an error downloading the episode, push an error state
// and then restore to none.
if (!result) {
episode.downloadState = e.downloadState = DownloadState.failed;
_episodesStream.add(_episodes);
episode.downloadState = e.downloadState = DownloadState.none;
_episodesStream.add(_episodes);
}
}
});
}
/// Sets up a listener to listen for status updates from any currently downloading episode.
///
/// If the ID of a current download matches that of an episode currently in
/// use, we update the status of the episode and push it back into the episode stream.
void _listenDownloads() {
// Listen to download progress
MobileDownloadService.downloadProgress.listen((downloadProgress) {
downloadService.findEpisodeByTaskId(downloadProgress.id).then((downloadable) {
if (downloadable != null) {
// If the download matches a current episode push the update back into the stream.
var episode = _episodes.firstWhereOrNull((e) => e.downloadTaskId == downloadProgress.id);
if (episode != null) {
// Update the stream.
_episodesStream.add(_episodes);
}
} else {
log.severe('Downloadable not found with id ${downloadProgress.id}');
}
});
});
}
/// Listen to episode change events sent by the [Repository]
void _listenEpisodeRepositoryEvents() {
podcastService.episodeListener!.listen((state) {
// Do we have this episode?
var eidx = _episodes.indexWhere((e) => e.guid == state.episode.guid && e.pguid == state.episode.pguid);
if (eidx != -1) {
_episodes[eidx] = state.episode;
_episodesStream.add(_episodes);
}
});
}
// TODO: This needs refactoring to simplify the long switch statement.
void _listenPodcastStateEvents() async {
_podcastEvent.listen((event) async {
switch (event) {
case PodcastEvent.subscribe:
if (_podcast != null) {
_podcast = await podcastService.subscribe(_podcast!);
_podcastStream.add(BlocPopulatedState<Podcast>(results: _podcast));
_loadSubscriptions();
_episodesStream.add(_podcast?.episodes);
}
break;
case PodcastEvent.unsubscribe:
if (_podcast != null) {
await podcastService.unsubscribe(_podcast!);
_podcast!.id = null;
_podcastStream.add(BlocPopulatedState<Podcast>(results: _podcast));
_loadSubscriptions();
_episodesStream.add(_podcast!.episodes);
}
break;
case PodcastEvent.markAllPlayed:
if (_podcast != null && _podcast?.episodes != null) {
final changedEpisodes = <Episode>[];
for (var e in _podcast!.episodes) {
if (!e.played) {
e.played = true;
e.position = 0;
changedEpisodes.add(e);
}
}
await podcastService.saveEpisodes(changedEpisodes);
_episodesStream.add(_podcast!.episodes);
}
break;
case PodcastEvent.clearAllPlayed:
if (_podcast != null && _podcast?.episodes != null) {
final changedEpisodes = <Episode>[];
for (var e in _podcast!.episodes) {
if (e.played) {
e.played = false;
e.position = 0;
changedEpisodes.add(e);
}
}
await podcastService.saveEpisodes(changedEpisodes);
_episodesStream.add(_podcast!.episodes);
}
break;
case PodcastEvent.reloadSubscriptions:
_loadSubscriptions();
break;
case PodcastEvent.refresh:
_refresh();
break;
case PodcastEvent.episodeFilterNone:
if (_podcast != null) {
_podcast!.filter = PodcastEpisodeFilter.none;
_podcast = await podcastService.save(_podcast!, withEpisodes: false);
await _loadFilteredEpisodes();
}
break;
case PodcastEvent.episodeFilterStarted:
_podcast!.filter = PodcastEpisodeFilter.started;
_podcast = await podcastService.save(_podcast!, withEpisodes: false);
await _loadFilteredEpisodes();
break;
case PodcastEvent.episodeFilterFinished:
_podcast!.filter = PodcastEpisodeFilter.played;
_podcast = await podcastService.save(_podcast!, withEpisodes: false);
await _loadFilteredEpisodes();
break;
case PodcastEvent.episodeFilterNotFinished:
_podcast!.filter = PodcastEpisodeFilter.notPlayed;
_podcast = await podcastService.save(_podcast!, withEpisodes: false);
await _loadFilteredEpisodes();
break;
case PodcastEvent.episodeSortDefault:
_podcast!.sort = PodcastEpisodeSort.none;
_podcast = await podcastService.save(_podcast!, withEpisodes: false);
await _loadFilteredEpisodes();
break;
case PodcastEvent.episodeSortLatest:
_podcast!.sort = PodcastEpisodeSort.latestFirst;
_podcast = await podcastService.save(_podcast!, withEpisodes: false);
await _loadFilteredEpisodes();
break;
case PodcastEvent.episodeSortEarliest:
_podcast!.sort = PodcastEpisodeSort.earliestFirst;
_podcast = await podcastService.save(_podcast!, withEpisodes: false);
await _loadFilteredEpisodes();
break;
case PodcastEvent.episodeSortAlphabeticalAscending:
_podcast!.sort = PodcastEpisodeSort.alphabeticalAscending;
_podcast = await podcastService.save(_podcast!, withEpisodes: false);
await _loadFilteredEpisodes();
break;
case PodcastEvent.episodeSortAlphabeticalDescending:
_podcast!.sort = PodcastEpisodeSort.alphabeticalDescending;
_podcast = await podcastService.save(_podcast!, withEpisodes: false);
await _loadFilteredEpisodes();
break;
}
});
}
@override
void detach() {
downloadService.dispose();
}
@override
void dispose() {
_podcastFeed.close();
_downloadEpisode.close();
_subscriptions.close();
_podcastStream.close();
_episodesStream.close();
_podcastEvent.close();
MobileDownloadService.downloadProgress.close();
downloadService.dispose();
super.dispose();
}
/// Sink to load a podcast.
void Function(Feed) get load => _podcastFeed.add;
/// Sink to trigger an episode download.
void Function(Episode?) get downloadEpisode => _downloadEpisode.add;
void Function(PodcastEvent) get podcastEvent => _podcastEvent.add;
/// Stream containing the current state of the podcast load.
Stream<BlocState<Podcast>> get details => _podcastStream.stream;
Stream<BlocState<void>> get backgroundLoading => _backgroundLoadStream.stream;
/// Stream containing the current list of Podcast episodes.
Stream<List<Episode?>?> get episodes => _episodesStream;
/// Obtain a list of podcast currently subscribed to.
Stream<List<Podcast>> get subscriptions => _subscriptions.stream;
}