youtube_explode/lib/src/search/search_list.dart

69 lines
2.0 KiB
Dart
Raw Normal View History

import 'dart:async';
import 'package:collection/collection.dart';
import '../../youtube_explode_dart.dart';
2021-03-20 18:31:53 +01:00
import '../extensions/helpers_extension.dart';
2021-09-28 16:49:38 +02:00
import '../reverse_engineering/pages/search_page.dart';
import 'base_search_content.dart';
2021-02-26 16:18:51 +01:00
/// This list contains search videos.
///This behaves like a [List] but has the [SearchList.nextPage] to get the next batch of videos.
class SearchList<T extends BaseSearchContent> extends DelegatingList<T> {
2021-03-20 18:31:53 +01:00
final SearchPage _page;
final YoutubeHttpClient _httpClient;
2021-04-30 23:49:49 +02:00
/// Construct an instance of [SearchList]
/// See [SearchList]
SearchList(List<T> base, this._page, this._httpClient) : super(base);
2021-04-30 23:49:49 +02:00
/// Fetches the next batch of videos or returns null if there are no more
/// results.
2021-03-20 18:31:53 +01:00
Future<SearchList?> nextPage() async {
final page = await _page.nextPage(_httpClient);
if (page == null) {
2021-02-27 18:58:42 +01:00
return null;
}
return SearchList<T>(page.searchContent as List<T>, page, _httpClient);
}
}
class VideoSearchList extends DelegatingList<Video> {
final SearchPage _page;
final YoutubeHttpClient _httpClient;
/// Construct an instance of [SearchList]
/// See [SearchList]
VideoSearchList(List<Video> base, this._page, this._httpClient) : super(base);
/// Fetches the next batch of videos or returns null if there are no more
/// results.
Future<VideoSearchList?> nextPage() async {
final page = await _page.nextPage(_httpClient);
if (page == null) {
return null;
}
return VideoSearchList(
2021-08-31 18:06:34 +02:00
page.searchContent
2021-03-20 18:31:53 +01:00
.whereType<SearchVideo>()
2021-03-24 06:17:49 +01:00
.map((e) => Video(
e.id,
e.title,
e.author,
ChannelId(e.channelId),
2021-03-24 06:17:49 +01:00
e.uploadDate.toDateTime(),
null,
e.description,
e.duration.toDuration(),
ThumbnailSet(e.id.value),
null,
Engagement(e.viewCount, null, null),
e.isLive))
2021-03-20 18:31:53 +01:00
.toList(),
page,
_httpClient);
2021-02-27 18:58:42 +01:00
}
}