fix: preserve live TV state across refreshes

This commit is contained in:
edde746
2026-07-12 18:59:42 +02:00
parent 0ef1e773bb
commit b4575c9780
4 changed files with 185 additions and 59 deletions
+76 -22
View File
@@ -72,6 +72,9 @@ class _LiveTvScreenState extends State<LiveTvScreen>
final Map<String, String> _favoriteStoreByChannel = {};
final Map<String, String> _favoriteStoreBySource = {};
final Map<String, FavoriteChannelPersistenceMode> _favoriteModeByStore = {};
Future<void>? _channelsLoadFuture;
int _favoritesLoadGeneration = 0;
int _favoritesMutationGeneration = 0;
List<LiveTvChannel> get _filteredChannels => filterLiveTvChannelsForFavorites(
channels: _channels,
@@ -268,7 +271,18 @@ class _LiveTvScreenState extends State<LiveTvScreen>
return trimmed == null || trimmed.isEmpty ? null : trimmed;
}
Future<void> _loadChannels() async {
Future<void> _loadChannels() {
final inFlight = _channelsLoadFuture;
if (inFlight != null) return inFlight;
late final Future<void> load;
load = _loadChannelsOnce().whenComplete(() {
if (identical(_channelsLoadFuture, load)) _channelsLoadFuture = null;
});
_channelsLoadFuture = load;
return load;
}
Future<void> _loadChannelsOnce() async {
if (!mounted) return;
setState(() {
_isLoading = true;
@@ -289,12 +303,12 @@ class _LiveTvScreenState extends State<LiveTvScreen>
final allChannels = <LiveTvChannel>[];
final seenChannels = <String>{};
_favoriteSourceByLiveServer.clear();
_favoriteSourceByChannel.clear();
_favoriteStoreByLiveServer.clear();
_favoriteStoreByChannel.clear();
_favoriteStoreBySource.clear();
_favoriteModeByStore.clear();
final favoriteSourceByLiveServer = <String, String>{};
final favoriteSourceByChannel = <String, String>{};
final favoriteStoreByLiveServer = <String, String>{};
final favoriteStoreByChannel = <String, String>{};
final favoriteStoreBySource = <String, String>{};
final favoriteModeByStore = <String, FavoriteChannelPersistenceMode>{};
appLogger.d(
'Live TV DVRs: ${liveTvServers.map((s) => '${s.serverId}/${s.dvrKey} lineup=${s.lineup}').join(', ')}',
@@ -319,10 +333,10 @@ class _LiveTvScreenState extends State<LiveTvScreen>
final sourceTitle = _sourceTitleForServerInfo(serverInfo);
final storeKey = liveTv.favoriteStoreKey;
final liveServerKey = _liveServerScopeKey(serverInfo);
_favoriteSourceByLiveServer[liveServerKey] = source;
_favoriteStoreByLiveServer[liveServerKey] = storeKey;
_favoriteStoreBySource[source] = storeKey;
_favoriteModeByStore[storeKey] = liveTv.favoritePersistenceMode;
favoriteSourceByLiveServer[liveServerKey] = source;
favoriteStoreByLiveServer[liveServerKey] = storeKey;
favoriteStoreBySource[source] = storeKey;
favoriteModeByStore[storeKey] = liveTv.favoritePersistenceMode;
final channels = await genericClient.liveTv.fetchChannels(lineup: serverInfo.lineup);
// Plex's DVR exposes a separate enabled-channel mapping; Jellyfin
@@ -342,8 +356,8 @@ class _LiveTvScreenState extends State<LiveTvScreen>
final dedupKey = liveTvChannelScopeKey(scopedChannel);
if (seenChannels.add(dedupKey)) {
final scopeKey = liveTvChannelScopeKey(scopedChannel);
_favoriteSourceByChannel[scopeKey] = source;
_favoriteStoreByChannel[scopeKey] = storeKey;
favoriteSourceByChannel[scopeKey] = source;
favoriteStoreByChannel[scopeKey] = storeKey;
allChannels.add(scopedChannel);
}
}
@@ -364,6 +378,24 @@ class _LiveTvScreenState extends State<LiveTvScreen>
setState(() {
_channels = allChannels;
_favoriteSourceByLiveServer
..clear()
..addAll(favoriteSourceByLiveServer);
_favoriteSourceByChannel
..clear()
..addAll(favoriteSourceByChannel);
_favoriteStoreByLiveServer
..clear()
..addAll(favoriteStoreByLiveServer);
_favoriteStoreByChannel
..clear()
..addAll(favoriteStoreByChannel);
_favoriteStoreBySource
..clear()
..addAll(favoriteStoreBySource);
_favoriteModeByStore
..clear()
..addAll(favoriteModeByStore);
_isLoading = false;
});
@@ -389,10 +421,13 @@ class _LiveTvScreenState extends State<LiveTvScreen>
}
Future<void> _loadFavorites(MultiServerProvider multiServer) async {
final loadGeneration = ++_favoritesLoadGeneration;
final mutationGeneration = _favoritesMutationGeneration;
try {
_favoriteSourceByLiveServer.clear();
_favoriteStoreBySource.clear();
_favoriteModeByStore.clear();
final sourceByLiveServer = Map<String, String>.of(_favoriteSourceByLiveServer);
final storeByLiveServer = Map<String, String>.of(_favoriteStoreByLiveServer);
final storeBySource = Map<String, String>.of(_favoriteStoreBySource);
final modeByStore = Map<String, FavoriteChannelPersistenceMode>.of(_favoriteModeByStore);
final merged = <FavoriteChannel>[];
final fetchedStores = <String>{};
final seenFavorites = <String>{};
@@ -403,20 +438,36 @@ class _LiveTvScreenState extends State<LiveTvScreen>
final source = await liveTv.buildFavoriteChannelSource(lineup: serverInfo.lineup);
final storeKey = liveTv.favoriteStoreKey;
final liveServerKey = _liveServerScopeKey(serverInfo);
_favoriteSourceByLiveServer[liveServerKey] = source;
_favoriteStoreByLiveServer[liveServerKey] = storeKey;
_favoriteStoreBySource[source] = storeKey;
_favoriteModeByStore[storeKey] = liveTv.favoritePersistenceMode;
sourceByLiveServer[liveServerKey] = source;
storeByLiveServer[liveServerKey] = storeKey;
storeBySource[source] = storeKey;
modeByStore[storeKey] = liveTv.favoritePersistenceMode;
if (!fetchedStores.add(storeKey)) continue;
final serverFavorites = await liveTv.fetchFavoriteChannels();
for (final favorite in serverFavorites) {
_favoriteStoreBySource[favorite.source] = storeKey;
storeBySource[favorite.source] = storeKey;
if (seenFavorites.add(favorite.stableKey)) merged.add(favorite);
}
}
if (!mounted) return;
if (!mounted ||
loadGeneration != _favoritesLoadGeneration ||
mutationGeneration != _favoritesMutationGeneration) {
return;
}
setState(() {
_favoriteSourceByLiveServer
..clear()
..addAll(sourceByLiveServer);
_favoriteStoreByLiveServer
..clear()
..addAll(storeByLiveServer);
_favoriteStoreBySource
..clear()
..addAll(storeBySource);
_favoriteModeByStore
..clear()
..addAll(modeByStore);
_favoriteChannels = merged;
_refreshFavoriteKeys();
});
@@ -433,6 +484,7 @@ class _LiveTvScreenState extends State<LiveTvScreen>
}
void _toggleFavorite(LiveTvChannel channel) {
++_favoritesMutationGeneration;
final source = _sourceForChannel(channel);
final favoriteKey = favoriteChannelKey(source, channel.key);
final scopeKey = liveTvChannelScopeKey(channel);
@@ -460,6 +512,7 @@ class _LiveTvScreenState extends State<LiveTvScreen>
favorites: List.from(_favoriteChannels),
channelMap: channelMap,
onReorder: (reordered) {
++_favoritesMutationGeneration;
setState(() {
_favoriteChannels = reordered;
_refreshFavoriteKeys();
@@ -467,6 +520,7 @@ class _LiveTvScreenState extends State<LiveTvScreen>
_persistFavorites();
},
onRemove: (removed) {
++_favoritesMutationGeneration;
setState(() {
_favoriteChannels = _favoriteChannels.where((f) => f.stableKey != removed.stableKey).toList();
_refreshFavoriteKeys();
+3 -2
View File
@@ -198,9 +198,9 @@ class GuideTabState extends State<GuideTab> with MountedSetStateMixin, WidgetsBi
switch (state) {
case AppLifecycleState.paused:
case AppLifecycleState.hidden:
_hiddenSince ??= DateTime.now();
pauseRefresh();
case AppLifecycleState.resumed:
_catchUpIfStale();
if (_isGuideVisible) resumeRefresh();
case AppLifecycleState.inactive:
case AppLifecycleState.detached:
break;
@@ -1103,6 +1103,7 @@ class GuideTabState extends State<GuideTab> with MountedSetStateMixin, WidgetsBi
}),
],
);
if (!mounted) return;
if (value == null) {
_guideFocusNode.requestFocus();
return;
+36 -9
View File
@@ -58,26 +58,45 @@ class _RuleEntry {
enum _RuleAction { edit, delete }
class RecordingsTabState extends State<RecordingsTab> {
class RecordingsTabState extends State<RecordingsTab> with WidgetsBindingObserver {
List<_ServerRecordings> _serverRecordings = [];
bool _isLoading = true;
bool _adminBlocked = false;
String? _error;
Timer? _refreshTimer;
bool _pendingFocus = false;
bool _refreshRequested = true;
bool _tickerEnabled = false;
bool _appResumed = true;
final _firstTileFocusNode = FocusNode(debugLabel: 'recordings_tab_first_tile');
@override
void initState() {
super.initState();
WidgetsBinding.instance.addObserver(this);
_load();
_refreshTimer = Timer.periodic(const Duration(seconds: 30), (_) {
if (mounted) _load();
});
}
@override
void didChangeDependencies() {
super.didChangeDependencies();
final enabled = TickerMode.valuesOf(context).enabled;
if (enabled == _tickerEnabled) return;
_tickerEnabled = enabled;
_syncRefreshTimer();
}
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
final resumed = state == AppLifecycleState.resumed;
if (resumed == _appResumed) return;
_appResumed = resumed;
_syncRefreshTimer();
}
@override
void dispose() {
WidgetsBinding.instance.removeObserver(this);
_refreshTimer?.cancel();
_firstTileFocusNode.dispose();
super.dispose();
@@ -94,14 +113,22 @@ class RecordingsTabState extends State<RecordingsTab> {
}
}
void pauseRefresh() => _refreshTimer?.cancel();
void pauseRefresh() {
_refreshRequested = false;
_syncRefreshTimer();
}
void resumeRefresh() {
_refreshRequested = true;
_syncRefreshTimer(reload: true);
}
void _syncRefreshTimer({bool reload = false}) {
_refreshTimer?.cancel();
_refreshTimer = Timer.periodic(const Duration(seconds: 30), (_) {
if (mounted) _load();
});
_load();
_refreshTimer = null;
if (!_refreshRequested || !_tickerEnabled || !_appResumed || !mounted) return;
_refreshTimer = Timer.periodic(const Duration(seconds: 30), (_) => _load());
if (reload) unawaited(_load());
}
/// Public reload helper for the parent screen's refresh action.
+70 -26
View File
@@ -28,6 +28,7 @@ import '../../../widgets/overlay_sheet.dart';
import '../../../utils/scroll_utils.dart';
import '../../../widgets/horizontal_scroll_with_arrows.dart';
import '../../../widgets/optimized_media_image.dart';
import '../../../widgets/sliver_child_memo.dart';
import '../live_tv_actions_mixin.dart';
import '../live_tv_show_schedule_screen.dart';
@@ -42,11 +43,16 @@ class WhatsOnTab extends StatefulWidget {
State<WhatsOnTab> createState() => WhatsOnTabState();
}
class WhatsOnTabState extends State<WhatsOnTab> with LiveTvActionsMixin<WhatsOnTab>, MountedSetStateMixin {
class WhatsOnTabState extends State<WhatsOnTab>
with LiveTvActionsMixin<WhatsOnTab>, MountedSetStateMixin, WidgetsBindingObserver {
List<LiveTvHubResult> _hubs = [];
bool _isLoading = true;
Timer? _refreshTimer;
final Map<String, GlobalKey<_LiveTvHubSectionState>> _hubKeysById = {};
List<GlobalKey<_LiveTvHubSectionState>> _hubKeys = [];
bool _refreshRequested = true;
bool _tickerEnabled = false;
bool _appResumed = true;
@override
List<LiveTvChannel> get liveTvChannels => widget.channels;
@@ -54,23 +60,47 @@ class WhatsOnTabState extends State<WhatsOnTab> with LiveTvActionsMixin<WhatsOnT
@override
void initState() {
super.initState();
WidgetsBinding.instance.addObserver(this);
_loadHubs();
_refreshTimer = Timer.periodic(const Duration(seconds: 60), (_) {
if (mounted) _loadHubs();
});
}
void pauseRefresh() => _refreshTimer?.cancel();
@override
void didChangeDependencies() {
super.didChangeDependencies();
final enabled = TickerMode.valuesOf(context).enabled;
if (enabled == _tickerEnabled) return;
_tickerEnabled = enabled;
_syncRefreshTimer();
}
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
final resumed = state == AppLifecycleState.resumed;
if (resumed == _appResumed) return;
_appResumed = resumed;
_syncRefreshTimer();
}
void pauseRefresh() {
_refreshRequested = false;
_syncRefreshTimer();
}
void resumeRefresh() {
_refreshRequested = true;
_syncRefreshTimer();
}
void _syncRefreshTimer() {
_refreshTimer?.cancel();
_refreshTimer = Timer.periodic(const Duration(seconds: 60), (_) {
if (mounted) _loadHubs();
});
_refreshTimer = null;
if (!_refreshRequested || !_tickerEnabled || !_appResumed || !mounted) return;
_refreshTimer = Timer.periodic(const Duration(seconds: 60), (_) => _loadHubs());
}
@override
void dispose() {
WidgetsBinding.instance.removeObserver(this);
_refreshTimer?.cancel();
super.dispose();
}
@@ -83,6 +113,7 @@ class WhatsOnTabState extends State<WhatsOnTab> with LiveTvActionsMixin<WhatsOnT
final multiServer = context.read<MultiServerProvider>();
final liveTvServers = multiServer.liveTvServers;
final allHubs = <LiveTvHubResult>[];
final allHubIds = <String>[];
final queriedServers = <String>{};
for (final serverInfo in liveTvServers) {
@@ -93,16 +124,23 @@ class WhatsOnTabState extends State<WhatsOnTab> with LiveTvActionsMixin<WhatsOnT
if (client == null) continue;
final hubs = await client.getLiveTvHubs();
allHubs.addAll(hubs);
for (final hub in hubs) {
allHubs.add(hub);
allHubIds.add('${serverInfo.serverId}\u0000${hub.hubKey}');
}
} catch (e) {
appLogger.e('Failed to load hubs from server ${serverInfo.serverId}', error: e);
}
}
if (!mounted) return;
final hubIds = allHubIds.toSet();
_hubKeysById.removeWhere((id, _) => !hubIds.contains(id));
setState(() {
_hubs = allHubs;
_hubKeys = List.generate(allHubs.length, (_) => GlobalKey<_LiveTvHubSectionState>());
_hubKeys = [
for (final hubId in allHubIds) _hubKeysById.putIfAbsent(hubId, () => GlobalKey<_LiveTvHubSectionState>()),
];
_isLoading = false;
});
} catch (e) {
@@ -224,6 +262,7 @@ class _LiveTvHubSectionState extends State<_LiveTvHubSection> with MountedSetSta
static const double _leadingPadding = 12.0;
final _selectLongPress = DpadSelectLongPressController();
final SliverChildMemo<LiveTvHubEntry> _childMemo = SliverChildMemo<LiveTvHubEntry>();
@override
void initState() {
@@ -442,22 +481,27 @@ class _LiveTvHubSectionState extends State<_LiveTvHubSection> with MountedSetSta
itemBuilder: (context, index) {
final entry = widget.hub.entries[index];
final isItemFocused = hasFocus && index == _focusedIndex;
return Padding(
padding: const EdgeInsets.symmetric(horizontal: 2),
child: _LiveTvPosterCard(
entry: entry,
width: cardWidth,
posterHeight: posterHeight,
isFocused: isItemFocused,
onTap: () {
_onItemTapped(index);
widget.onTap(entry);
},
onLongPress: () {
_onItemTapped(index);
widget.onLongPress(entry);
},
return _childMemo.widgetFor(
index,
entry,
epoch: (cardWidth, posterHeight, widget.hub.entries.length),
salt: isItemFocused,
build: () => Padding(
padding: const EdgeInsets.symmetric(horizontal: 2),
child: _LiveTvPosterCard(
entry: entry,
width: cardWidth,
posterHeight: posterHeight,
isFocused: isItemFocused,
onTap: () {
_onItemTapped(index);
widget.onTap(entry);
},
onLongPress: () {
_onItemTapped(index);
widget.onLongPress(entry);
},
),
),
);
},