diff --git a/lib/core/mixins/load_callback.dart b/lib/core/mixins/load_callback.dart index 874e4c6bc..58c65a11a 100644 --- a/lib/core/mixins/load_callback.dart +++ b/lib/core/mixins/load_callback.dart @@ -6,7 +6,7 @@ mixin LunaLoadCallbackMixin on State { @override void initState() { super.initState(); - WidgetsBinding.instance!.addPostFrameCallback((_) => loadCallback()); + WidgetsBinding.instance?.addPostFrameCallback((_) => loadCallback()); } Future loadCallback(); diff --git a/lib/modules/overseerr/routes/requests/widgets/request_list_view.dart b/lib/modules/overseerr/routes/requests/widgets/request_list_view.dart index d276db415..87610cc1a 100644 --- a/lib/modules/overseerr/routes/requests/widgets/request_list_view.dart +++ b/lib/modules/overseerr/routes/requests/widgets/request_list_view.dart @@ -49,14 +49,6 @@ class _State extends State { if (_requests.isNotEmpty) { _requests.forEach((request) { int id = request.media!.tmdbId!; - switch (request.type!) { - case OverseerrMediaType.MOVIE: - context.read().fetchMovie(id); - break; - case OverseerrMediaType.TV: - context.read().fetchSeries(id); - break; - } }); } diff --git a/lib/modules/overseerr/routes/requests/widgets/request_tile.dart b/lib/modules/overseerr/routes/requests/widgets/request_tile.dart index 0c8245f28..ed6a1b38b 100644 --- a/lib/modules/overseerr/routes/requests/widgets/request_tile.dart +++ b/lib/modules/overseerr/routes/requests/widgets/request_tile.dart @@ -2,7 +2,7 @@ import 'package:flutter/material.dart'; import 'package:lunasea/core.dart'; import 'package:lunasea/modules/overseerr.dart'; -class OverseerrRequestTile extends StatelessWidget { +class OverseerrRequestTile extends StatefulWidget { final OverseerrRequest request; const OverseerrRequestTile({ @@ -10,42 +10,74 @@ class OverseerrRequestTile extends StatelessWidget { required this.request, }) : super(key: key); + @override + State createState() => _State(); +} + +class _State extends State with LunaLoadCallbackMixin { + bool _loaded = false; + + @override + Future loadCallback() async { + int id = widget.request.media!.tmdbId!; + switch (widget.request.type!) { + case OverseerrMediaType.MOVIE: + if (mounted) await context.read().fetchMovie(id); + break; + case OverseerrMediaType.TV: + if (mounted) await context.read().fetchSeries(id); + break; + } + if (mounted) setState(() => _loaded = true); + } + @override Widget build(BuildContext context) { + if (!_loaded) return _loadingBlock(); + return Selector, Future>>( selector: (_, state) => Tuple2( - state.getMovie(request.media!.tmdbId!), - state.getSeries(request.media!.tmdbId!), + state.getMovie(widget.request.media!.tmdbId!), + state.getSeries(widget.request.media!.tmdbId!), ), builder: (context, data, _) => FutureBuilder( future: Future.wait([data.item1, data.item2]), builder: (context, AsyncSnapshot snapshot) { if (snapshot.hasData) { - switch (request.type!) { + switch (widget.request.type!) { case OverseerrMediaType.MOVIE: - return _movieBlock(context, snapshot.data![0]); + if (snapshot.data?[0] != null) + return _movieBlock(context, snapshot.data![0]); + break; case OverseerrMediaType.TV: - return _seriesBlock(context, snapshot.data![1]); + if (snapshot.data?[1] != null) + return _seriesBlock(context, snapshot.data![1]); + break; } } - return const LunaBlock(skeletonEnabled: true, skeletonSubtitles: 3); + return _loadingBlock(); }, ), ); } + Widget _loadingBlock() { + return const LunaBlock(skeletonEnabled: true, skeletonSubtitles: 3); + } + Widget _movieBlock(BuildContext context, OverseerrMovie movie) { return LunaBlock( title: movie.lunaTitle(), body: [ TextSpan(text: movie.lunaYear()), - TextSpan(text: request.lunaRequestedBy()), + TextSpan(text: widget.request.lunaRequestedBy()), TextSpan( - text: request.lunaRequestStatus(), + text: widget.request.lunaRequestStatus(), style: TextStyle( fontWeight: LunaUI.FONT_WEIGHT_BOLD, - color: request.status.lunaColour(request.lunaMediaStatus()), + color: widget.request.status + .lunaColour(widget.request.lunaMediaStatus()), ), ), ], @@ -62,12 +94,14 @@ class OverseerrRequestTile extends StatelessWidget { title: series.lunaTitle(), body: [ TextSpan(text: series.lunaYear()), - TextSpan(text: request.lunaRequestedBy()), + TextSpan(text: widget.request.lunaRequestedBy()), TextSpan( - text: request.lunaRequestStatus(), + text: widget.request.lunaRequestStatus(), style: TextStyle( fontWeight: LunaUI.FONT_WEIGHT_BOLD, - color: request.status.lunaColour(request.lunaMediaStatus()), + color: widget.request.status.lunaColour( + widget.request.lunaMediaStatus(), + ), ), ), ],