showChannelPage method

Future<void> showChannelPage(
  1. {required AppModel appModel,
  2. required BuildContext context,
  3. required MediaItem item}
)

Launch a channel and view its videos in a page.

Implementation

Future<void> showChannelPage({
  required AppModel appModel,
  required BuildContext context,
  required MediaItem item,
}) async {
  late String channelId;
  if (item.authorIdentifier == null) {
    String? fetchedId = _channelIdFetchedFromVideoCache[item.mediaIdentifier];
    if (fetchedId != null) {
      channelId = fetchedId;
    } else {
      Navigator.of(context)
          .push(MaterialPageRoute(builder: (context) => const LoadingPage()));

      Channel channel = await _channelClient.getByVideo(item.mediaIdentifier);
      _channelIdFetchedFromVideoCache[item.mediaIdentifier] =
          channel.id.value;
      channelId = channel.id.value;
    }
  } else {
    channelId = item.authorIdentifier!;
  }

  PagingController<int, MediaItem>? pagingController =
      _pagingControllerCache[channelId];
  if (pagingController == null) {
    pagingController = PagingController(firstPageKey: 1);
    pagingController.addPageRequestListener((pageKey) async {
      List<MediaItem> items = [];
      List<Video> videos = [];

      try {
        videos = await _channelClient
            .getUploads(channelId)
            .skip(pagingController?.itemList?.length ?? 0)
            .take(20)
            .where((e) => e.duration != null)
            .toList();
        for (Video video in videos) {
          items.add(getMediaItem(video));
        }
      } finally {
        if (items.isEmpty) {
          pagingController?.appendLastPage(items);
        } else {
          pagingController?.appendPage(items, pageKey + 1);
        }
      }
    });
    _pagingControllerCache[channelId] = pagingController;
  }

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