Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion lib/models/podcast.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ class Podcast {
final String lastPlayedAt;
final PodcastState state;

/// Whether the current user has favorited this podcast. Mutable so
/// the optimistic favorite toggle can flip it in place.
bool favorite;

ImageProvider? _image;

Podcast({
Expand All @@ -29,6 +33,7 @@ class Podcast {
required this.subscribedAt,
required this.lastPlayedAt,
required this.state,
this.favorite = false,
});

ImageProvider get image {
Expand All @@ -40,6 +45,8 @@ class Podcast {
String? id,
String? title,
String? author,
bool favorite = false,
PodcastState? state,
}) {
final faker = Faker();
return Podcast(
Expand All @@ -52,7 +59,8 @@ class Podcast {
imageUrl: faker.image.loremPicsum(width: 192, height: 192),
subscribedAt: '2026-01-01T00:00:00Z',
lastPlayedAt: '2026-01-01T00:00:00Z',
state: PodcastState(progresses: {}),
state: state ?? PodcastState(progresses: {}),
favorite: favorite,
);
}

Expand All @@ -68,6 +76,7 @@ class Podcast {
subscribedAt: json['subscribed_at'],
lastPlayedAt: json['last_played_at'],
state: PodcastState.fromJson(json['state']),
favorite: json['favorite'] == true,
);
}

Expand Down
9 changes: 8 additions & 1 deletion lib/providers/playable_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,17 @@ class PlayableProvider with ChangeNotifier, StreamSubscriber {
}) async {
if (forceRefresh) AppState.delete(['podcast.episodes', podcastId]);

return _stateAwareFetch(
final episodes = await _stateAwareFetch(
'podcasts/$podcastId/episodes${getUpdates ? '?refresh=1' : ''}',
['podcast.episodes', podcastId],
);

// A forced refresh repopulates the cache with fresh data — let any
// screen rendering the episode list (e.g. PodcastDetailsScreen)
// know it should rebuild.
if (forceRefresh) notifyListeners();

return episodes;
}

Future<List<Playable>> _stateAwareFetch(String url, Object cacheKey) async {
Expand Down
17 changes: 17 additions & 0 deletions lib/providers/podcast_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,23 @@ class PodcastProvider with ChangeNotifier, StreamSubscriber {
return fetchAll();
}

Future<void> toggleFavorite(Podcast podcast) async {
// Optimistic flip + restore on failure.
podcast.favorite = !podcast.favorite;
notifyListeners();

try {
await post('favorites/toggle', data: {
'type': 'podcast',
'id': podcast.id,
});
} catch (_) {
podcast.favorite = !podcast.favorite;
notifyListeners();
rethrow;
}
}

Future<void> unsubscribePodcast(Podcast podcast) async {
// Optimistic removal so a Dismissible's onDismissed callback can
// call this without leaving the dismissed widget in the tree
Expand Down
Loading
Loading