showTrendingVideos method

Future<void> showTrendingVideos(
  1. {required AppModel appModel,
  2. required BuildContext context}
)

Launch a trending videos page.

Implementation

Future<void> showTrendingVideos({
  required AppModel appModel,
  required BuildContext context,
}) async {
  String playlistId = getTrendingPlaylistId(appModel.targetLanguage)!;
  Playlist trendingPlaylist = await getPlaylistFromId(playlistId);

  PagingController<int, MediaItem>? pagingController =
      _pagingControllerCache[playlistId];
  if (pagingController == null) {
    pagingController = PagingController(firstPageKey: 1);

    pagingController.addPageRequestListener((pageKey) async {
      List<MediaItem> items = [];
      List<Video> videos = [];

      try {
        videos = await _playlistClient
            .getVideos(playlistId)
            .where((e) => e.duration != null)
            .toList();
        for (Video video in videos) {
          items.add(getMediaItem(video));
        }
      } finally {
        pagingController?.appendLastPage(items);
      }
    });

    _pagingControllerCache[playlistId] = pagingController;
  }

  // Prevent recursion.
  Navigator.of(context).popUntil((route) => route.isFirst);
  await Navigator.of(context).push(
    MaterialPageRoute<void>(
      builder: (context) => YoutubeVideoResultsPage(
        showAppBar: true,
        title: trendingPlaylist.title,
        pagingController: pagingController!,
      ),
    ),
  );
}