fix(livetv): show guide without favorites
This commit is contained in:
@@ -31,6 +31,21 @@ String favoriteChannelKey(String source, String id) => '$source\u0000$id';
|
||||
String liveTvChannelScopeKey(LiveTvChannel channel) =>
|
||||
'${channel.serverId ?? ''}\u0000${channel.liveDvrKey ?? ''}\u0000${channel.key}';
|
||||
|
||||
List<LiveTvChannel> filterLiveTvChannelsForFavorites({
|
||||
required List<LiveTvChannel> channels,
|
||||
required bool favoritesOnly,
|
||||
required Iterable<FavoriteChannel> favorites,
|
||||
required String Function(LiveTvChannel channel) sourceForChannel,
|
||||
}) {
|
||||
if (!favoritesOnly || favorites.isEmpty) return channels;
|
||||
|
||||
final channelMap = {
|
||||
for (final channel in channels) favoriteChannelKey(sourceForChannel(channel), channel.key): channel,
|
||||
};
|
||||
|
||||
return [for (final favorite in favorites) ?channelMap[favorite.stableKey]];
|
||||
}
|
||||
|
||||
@JsonSerializable(createToJson: false)
|
||||
class LiveTvChannel with MultiServerFields {
|
||||
@JsonKey(readValue: _readChannelKey)
|
||||
|
||||
@@ -71,15 +71,12 @@ class _LiveTvScreenState extends State<LiveTvScreen>
|
||||
final Map<String, String> _favoriteStoreBySource = {};
|
||||
final Map<String, FavoriteChannelPersistenceMode> _favoriteModeByStore = {};
|
||||
|
||||
List<LiveTvChannel> get _filteredChannels {
|
||||
if (!_showFavoritesOnly) return _channels;
|
||||
if (_favoriteKeys.isEmpty) return const [];
|
||||
final channelMap = {for (final c in _channels) _favoriteKeyForChannel(c): c};
|
||||
return [
|
||||
for (final fav in _favoriteChannels)
|
||||
if (channelMap.containsKey(fav.stableKey)) channelMap[fav.stableKey]!,
|
||||
];
|
||||
}
|
||||
List<LiveTvChannel> get _filteredChannels => filterLiveTvChannelsForFavorites(
|
||||
channels: _channels,
|
||||
favoritesOnly: _showFavoritesOnly,
|
||||
favorites: _favoriteChannels,
|
||||
sourceForChannel: _sourceForChannel,
|
||||
);
|
||||
|
||||
String _liveServerScopeKey(LiveTvServerInfo serverInfo) => '${serverInfo.serverId}\u0000${serverInfo.dvrKey}';
|
||||
|
||||
|
||||
@@ -16,4 +16,35 @@ void main() {
|
||||
|
||||
expect(liveTvChannelScopeKey(a), isNot(liveTvChannelScopeKey(b)));
|
||||
});
|
||||
|
||||
test('favorite filtering falls back to all channels when no favorites are loaded', () {
|
||||
final channels = [LiveTvChannel(key: '101'), LiveTvChannel(key: '102')];
|
||||
|
||||
final filtered = filterLiveTvChannelsForFavorites(
|
||||
channels: channels,
|
||||
favoritesOnly: true,
|
||||
favorites: const [],
|
||||
sourceForChannel: (_) => 'server://server-1/provider-a',
|
||||
);
|
||||
|
||||
expect(filtered, same(channels));
|
||||
});
|
||||
|
||||
test('favorite filtering preserves favorite order and source scope', () {
|
||||
final channels = [LiveTvChannel(key: '101'), LiveTvChannel(key: '102'), LiveTvChannel(key: '101')];
|
||||
const sourceA = 'server://server-1/provider-a';
|
||||
const sourceB = 'server://server-2/provider-a';
|
||||
|
||||
final filtered = filterLiveTvChannelsForFavorites(
|
||||
channels: channels,
|
||||
favoritesOnly: true,
|
||||
favorites: [
|
||||
FavoriteChannel(source: sourceB, id: '101'),
|
||||
FavoriteChannel(source: sourceA, id: '102'),
|
||||
],
|
||||
sourceForChannel: (channel) => identical(channel, channels[2]) ? sourceB : sourceA,
|
||||
);
|
||||
|
||||
expect(filtered, [channels[2], channels[1]]);
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user