Skip to content

Commit

Permalink
feat: update episodes when a podcast page is loaded (#959)
Browse files Browse the repository at this point in the history
  • Loading branch information
Feichtmeier authored Oct 16, 2024
1 parent 7b3c193 commit f925201
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 69 deletions.
2 changes: 1 addition & 1 deletion lib/app/view/master_detail_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ List<MasterItem> createMasterItems({required LibraryModel libraryModel}) {
),
pageId: podcast.key,
pageBuilder: (_) => PodcastPage(
pageId: podcast.key,
feedUrl: podcast.key,
title: podcast.value.firstOrNull?.album ??
podcast.value.firstOrNull?.title ??
podcast.value.firstOrNull.toString(),
Expand Down
2 changes: 0 additions & 2 deletions lib/library/library_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,6 @@ class LibraryModel extends SafeChangeNotifier {
int get podcastsLength => podcasts.length;
void addPodcast(String feedUrl, List<Audio> audios) =>
_service.addPodcast(feedUrl, audios);
void updatePodcast(String feedUrl, List<Audio> audios) =>
_service.updatePodcast(feedUrl, audios);

void removePodcast(String feedUrl) {
_service.removePodcast(feedUrl);
Expand Down
11 changes: 4 additions & 7 deletions lib/podcasts/podcast_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,14 @@ class PodcastModel extends SafeChangeNotifier {
}

void update({
required String updateMessage,
String? updateMessage,
// Note: because the podcasts can be modified to include downloads
// this needs a map and not only the feedurl
Map<String, List<Audio>>? oldPodcasts,
Function({required String message})? notify,
}) {
_setCheckingForUpdates(true);
_podcastService
.updatePodcasts(
oldPodcasts: oldPodcasts,
updateMessage: updateMessage,
notify: notify,
)
.updatePodcasts(updateMessage: updateMessage, oldPodcasts: oldPodcasts)
.then((_) => _setCheckingForUpdates(false));
}

Expand Down
12 changes: 7 additions & 5 deletions lib/podcasts/podcast_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,14 @@ class PodcastService {
return _searchResult;
}

bool _updateLock = false;

Future<void> updatePodcasts({
Map<String, List<Audio>>? oldPodcasts,
required String updateMessage,
Function({required String message})? notify,
String? updateMessage,
}) async {
if (_updateLock) return;
_updateLock = true;
for (final old in (oldPodcasts ?? _libraryService.podcasts).entries) {
if (old.value.isNotEmpty) {
final list = old.value;
Expand All @@ -111,9 +114,7 @@ class PodcastService {
audios.isEmpty) return;

_libraryService.updatePodcast(old.key, audios);
if (notify != null) {
notify(message: '$updateMessage ${firstOld.album ?? old.value}');
} else {
if (updateMessage != null) {
_notificationsService.notify(
message: '$updateMessage ${firstOld.album ?? old.value}',
);
Expand All @@ -122,5 +123,6 @@ class PodcastService {
}
}
}
_updateLock = false;
}
}
2 changes: 1 addition & 1 deletion lib/podcasts/podcast_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ Future<void> searchAndPushPodcastPage({
builder: (_) => PodcastPage(
imageUrl: itemImageUrl ?? podcast.firstOrNull?.imageUrl,
preFetchedEpisodes: podcast,
pageId: feedUrl,
feedUrl: feedUrl,
title: podcast.firstOrNull?.album ??
podcast.firstOrNull?.title ??
feedUrl,
Expand Down
24 changes: 5 additions & 19 deletions lib/podcasts/view/podcast_collection_control_panel.dart
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import 'package:flutter/material.dart';
import 'package:watch_it/watch_it.dart';
import 'package:yaru/yaru.dart';

import '../../app/connectivity_model.dart';
import '../../common/view/offline_page.dart';
import '../../common/view/snackbars.dart';
import '../../common/view/theme.dart';
import '../../extensions/build_context_x.dart';
import '../../l10n/l10n.dart';
import '../podcast_model.dart';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:watch_it/watch_it.dart';
import 'package:yaru/yaru.dart';

class PodcastCollectionControlPanel extends StatelessWidget with WatchItMixin {
const PodcastCollectionControlPanel({super.key});
Expand Down Expand Up @@ -55,20 +54,7 @@ class PodcastCollectionControlPanel extends StatelessWidget with WatchItMixin {
if (updatesOnly) {
model.setUpdatesOnly(false);
} else {
model.update(
updateMessage: context.l10n.newEpisodeAvailable,
notify: Platform.isLinux
? null
: ({required message}) {
if (context.mounted) {
showSnackBar(
context: context,
content: Text(message),
);
}
},
);

model.update(updateMessage: context.l10n.newEpisodeAvailable);
model.setUpdatesOnly(true);
model.setDownloadsOnly(false);
}
Expand Down
68 changes: 45 additions & 23 deletions lib/podcasts/view/podcast_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import '../../common/view/icons.dart';
import '../../common/view/safe_network_image.dart';
import '../../common/view/search_button.dart';
import '../../common/view/sliver_audio_page_control_panel.dart';
import '../../common/view/snackbars.dart';
import '../../common/view/theme.dart';
import '../../constants.dart';
import '../../extensions/build_context_x.dart';
Expand All @@ -33,32 +32,60 @@ import 'podcast_sub_button.dart';
import 'podcast_timer_button.dart';
import 'sliver_podcast_page_list.dart';

class PodcastPage extends StatelessWidget with WatchItMixin {
class PodcastPage extends StatefulWidget with WatchItStatefulWidgetMixin {
const PodcastPage({
super.key,
this.imageUrl,
required this.pageId,
required this.feedUrl,
this.preFetchedEpisodes,
required this.title,
});

final String? imageUrl;

/// The feedUrl
final String pageId;
final String feedUrl;
final String title;
final List<Audio>? preFetchedEpisodes;

@override
State<PodcastPage> createState() => _PodcastPageState();
}

class _PodcastPageState extends State<PodcastPage> {
@override
void initState() {
super.initState();
final libraryModel = di<LibraryModel>();
final episodes =
widget.preFetchedEpisodes ?? libraryModel.podcasts[widget.feedUrl];

if (episodes == null || episodes.isEmpty) return;

Future.delayed(const Duration(milliseconds: 500)).then(
(_) {
final episodesWithDownloads = episodes
.map((e) => e.copyWith(path: libraryModel.getDownload(e.url)))
.toList();
di<PodcastModel>().update(
oldPodcasts: {
widget.feedUrl: episodesWithDownloads,
},
);
},
);
}

@override
Widget build(BuildContext context) {
final episodes = preFetchedEpisodes ??
watchPropertyValue((LibraryModel m) => m.podcasts[pageId]);
final episodes = widget.preFetchedEpisodes ??
watchPropertyValue((LibraryModel m) => m.podcasts[widget.feedUrl]);
watchPropertyValue((PlayerModel m) => m.lastPositions?.length);
watchPropertyValue((LibraryModel m) => m.downloadsLength);

final libraryModel = di<LibraryModel>();
if (watchPropertyValue(
(LibraryModel m) => m.showPodcastAscending(pageId),
(LibraryModel m) => m.showPodcastAscending(widget.feedUrl),
)) {
sortListByAudioFilter(
audioFilter: AudioFilter.year,
Expand Down Expand Up @@ -91,12 +118,7 @@ class PodcastPage extends StatelessWidget with WatchItMixin {
builder: (context, constraints) {
return RefreshIndicator(
onRefresh: () async => di<PodcastModel>().update(
oldPodcasts: {pageId: episodesWithDownloads},
updateMessage: context.l10n.newEpisodeAvailable,
notify: ({required message}) => showSnackBar(
context: context,
content: Text(message),
),
oldPodcasts: {widget.feedUrl: episodesWithDownloads},
),
child: CustomScrollView(
slivers: [
Expand All @@ -107,9 +129,9 @@ class PodcastPage extends StatelessWidget with WatchItMixin {
),
sliver: SliverToBoxAdapter(
child: AudioPageHeader(
image: imageUrl == null
image: widget.imageUrl == null
? null
: _PodcastPageImage(imageUrl: imageUrl),
: _PodcastPageImage(imageUrl: widget.imageUrl),
label: episodesWithDownloads
.firstWhereOrNull((e) => e.genre != null)
?.genre ??
Expand All @@ -121,9 +143,9 @@ class PodcastPage extends StatelessWidget with WatchItMixin {
: AudioPageHeaderHtmlDescription(
description: episodesWithDownloads
.firstOrNull!.albumArtist!,
title: title,
title: widget.title,
),
title: title,
title: widget.title,
onLabelTab: (text) => _onGenreTap(
context: context,
text: text,
Expand All @@ -145,15 +167,15 @@ class PodcastPage extends StatelessWidget with WatchItMixin {
const PodcastTimerButton(),
PodcastSubButton(
audios: episodesWithDownloads,
pageId: pageId,
pageId: widget.feedUrl,
),
AvatarPlayButton(
audios: episodesWithDownloads,
pageId: pageId,
pageId: widget.feedUrl,
),
PodcastRefreshButton(pageId: pageId),
PodcastReorderButton(feedUrl: pageId),
if (!isMobile) ExploreOnlinePopup(text: title),
PodcastRefreshButton(pageId: widget.feedUrl),
PodcastReorderButton(feedUrl: widget.feedUrl),
if (!isMobile) ExploreOnlinePopup(text: widget.title),
],
),
),
Expand All @@ -164,7 +186,7 @@ class PodcastPage extends StatelessWidget with WatchItMixin {
.copyWith(bottom: kYaruPagePadding),
sliver: SliverPodcastPageList(
audios: episodesWithDownloads,
pageId: pageId,
pageId: widget.feedUrl,
),
),
],
Expand Down
8 changes: 3 additions & 5 deletions lib/podcasts/view/podcast_refresh_button.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import 'package:flutter/material.dart';
import 'package:watch_it/watch_it.dart';

import '../../common/view/icons.dart';
import '../../common/view/snackbars.dart';
import '../../l10n/l10n.dart';
import '../../library/library_model.dart';
import '../podcast_model.dart';
import 'package:flutter/material.dart';
import 'package:watch_it/watch_it.dart';

class PodcastRefreshButton extends StatelessWidget {
const PodcastRefreshButton({
Expand All @@ -27,8 +27,6 @@ class PodcastRefreshButton extends StatelessWidget {
pageId: podcast,
},
updateMessage: context.l10n.newEpisodeAvailable,
notify: ({required message}) =>
showSnackBar(context: context, content: Text(message)),
),
icon: Icon(Iconz.refresh),
);
Expand Down
6 changes: 0 additions & 6 deletions needs_translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -853,12 +853,6 @@
"regionZimbabwe"
],

"it": [
"downloadsDirectory",
"downloadsDirectoryDescription",
"downloadsChangeWarning"
],

"nl": [
"shuffle",
"repeat",
Expand Down

0 comments on commit f925201

Please sign in to comment.