Skip to content

Commit ad2a88f

Browse files
New features
New features
1 parent 0ef6046 commit ad2a88f

39 files changed

Lines changed: 2940 additions & 96 deletions

lib/core/blocs/category/gaming_category/gaming_category_cubit.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ class GamingCategoryCubit
88
/// Send `request` to Load `Data` from server
99
Future<void> fetchData() async {
1010
try {
11-
final List<StreamInfoItem>? result =
11+
final SearchResult? result =
1212
await _videosRepo.searchVideos(KeywordsConstant.gaming);
13-
return emit((loading: false, data: result ?? state.data));
13+
return emit((loading: false, data: result?.videos ?? state.data));
1414
} catch (e) {
1515
e.toString().print("GamingCategoryCubit fetchData Error");
1616
return emit((loading: false, data: state.data));

lib/core/blocs/category/movies_category/movies_category_cubit.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ class MoviesCategoryCubit
88
/// Send `request` to Load `Data` from server
99
Future<void> fetchData() async {
1010
try {
11-
final List<StreamInfoItem>? result =
11+
final SearchResult? result =
1212
await _videosRepo.searchVideos(KeywordsConstant.movies);
13-
return emit((loading: false, data: result ?? state.data));
13+
return emit((loading: false, data: result?.videos ?? state.data));
1414
} catch (e) {
1515
e.toString().print("MoviesCategoryCubit fetchData Error");
1616
return emit((loading: false, data: state.data));

lib/core/blocs/category/music_category/music_category_cubit.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ class MusicCategoryCubit
88
/// Send `request` to Load `Data` from server
99
Future<void> fetchData() async {
1010
try {
11-
final List<StreamInfoItem>? result =
11+
final SearchResult? result =
1212
await _videosRepo.searchVideos(KeywordsConstant.music);
13-
return emit((loading: false, data: result ?? state.data));
13+
return emit((loading: false, data: result?.videos ?? state.data));
1414
} catch (e) {
1515
e.toString().print("MusicCategoryCubit fetchData Error");
1616
return emit((loading: false, data: state.data));

lib/core/blocs/category/news_category/news_category_cubit.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ class NewsCategoryCubit
88
/// Send `request` to Load `Data` from server
99
Future<void> fetchData() async {
1010
try {
11-
final List<StreamInfoItem>? result =
11+
final SearchResult? result =
1212
await _videosRepo.searchVideos(KeywordsConstant.news);
13-
return emit((loading: false, data: result ?? state.data));
13+
return emit((loading: false, data: result?.videos ?? state.data));
1414
} catch (e) {
1515
e.toString().print("NewsCategoryCubit fetchData Error");
1616
return emit((loading: false, data: state.data));

lib/core/blocs/category/sports_category/sports_category_cubit.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ class SportsCategoryCubit
88
/// Send `request` to Load `Data` from server
99
Future<void> fetchData() async {
1010
try {
11-
final List<StreamInfoItem>? result =
11+
final SearchResult? result =
1212
await _videosRepo.searchVideos(KeywordsConstant.sports);
13-
return emit((loading: false, data: result ?? state.data));
13+
return emit((loading: false, data: result?.videos ?? state.data));
1414
} catch (e) {
1515
e.toString().print("SportsCategoryCubit fetchData Error");
1616
return emit((loading: false, data: state.data));
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import '../../../meta/hooks/hooks_meta.dart';
2+
3+
part 'channel_state.dart';
4+
5+
class ChannelCubit extends Cubit<ChannelState> {
6+
final ChannelRepository _channelRepo;
7+
ChannelCubit(this._channelRepo) : super(ChannelInitialState());
8+
9+
void resetFields() => emit(ChannelInitialState());
10+
11+
Future<void> loadChannel(String url) async {
12+
if (url.isEmpty) {
13+
emit(ChannelErrorState());
14+
return;
15+
}
16+
emit(ChannelLoadingState());
17+
try {
18+
final List<Object?> results = await Future.wait<Object?>([
19+
_channelRepo.getChannelInfo(url),
20+
_channelRepo.getChannelUploads(url),
21+
]);
22+
final YoutubeChannel? info = results[0] as YoutubeChannel?;
23+
final List<StreamInfoItem>? uploads =
24+
results[1] as List<StreamInfoItem>?;
25+
if (info != null) {
26+
return emit(ChannelLoadedState(
27+
channel: info,
28+
uploads: uploads ?? [],
29+
));
30+
}
31+
return emit(ChannelErrorState());
32+
} catch (e) {
33+
e.toString().print("ChannelCubit loadChannel Error");
34+
return emit(ChannelErrorState());
35+
}
36+
}
37+
38+
Future<void> loadNextPage() async {
39+
final ChannelState currentState = state;
40+
if (currentState is! ChannelLoadedState ||
41+
currentState.isLoadingMore ||
42+
!currentState.hasMorePages) {
43+
return;
44+
}
45+
emit(currentState.copyWith(isLoadingMore: true));
46+
try {
47+
final List<StreamInfoItem>? nextPage =
48+
await _channelRepo.getChannelNextPage();
49+
if (nextPage != null && nextPage.isNotEmpty) {
50+
return emit(currentState.copyWith(
51+
uploads: <StreamInfoItem>[...currentState.uploads, ...nextPage],
52+
isLoadingMore: false,
53+
));
54+
}
55+
return emit(currentState.copyWith(
56+
isLoadingMore: false,
57+
hasMorePages: false,
58+
));
59+
} catch (e) {
60+
e.toString().print("ChannelCubit loadNextPage Error");
61+
return emit(currentState.copyWith(
62+
isLoadingMore: false,
63+
hasMorePages: false,
64+
));
65+
}
66+
}
67+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
part of 'channel_cubit.dart';
2+
3+
@immutable
4+
abstract class ChannelState extends Equatable {
5+
const ChannelState();
6+
@override
7+
List<Object> get props => [];
8+
}
9+
10+
@immutable
11+
class ChannelInitialState extends ChannelState {}
12+
13+
@immutable
14+
class ChannelLoadingState extends ChannelState {}
15+
16+
@immutable
17+
class ChannelLoadedState extends ChannelState {
18+
final YoutubeChannel channel;
19+
final List<StreamInfoItem> uploads;
20+
final bool isLoadingMore;
21+
final bool hasMorePages;
22+
23+
const ChannelLoadedState({
24+
required this.channel,
25+
required this.uploads,
26+
this.isLoadingMore = false,
27+
this.hasMorePages = true,
28+
});
29+
30+
ChannelLoadedState copyWith({
31+
YoutubeChannel? channel,
32+
List<StreamInfoItem>? uploads,
33+
bool? isLoadingMore,
34+
bool? hasMorePages,
35+
}) {
36+
return ChannelLoadedState(
37+
channel: channel ?? this.channel,
38+
uploads: uploads ?? this.uploads,
39+
isLoadingMore: isLoadingMore ?? this.isLoadingMore,
40+
hasMorePages: hasMorePages ?? this.hasMorePages,
41+
);
42+
}
43+
44+
@override
45+
List<Object> get props => [channel, uploads, isLoadingMore, hasMorePages];
46+
}
47+
48+
@immutable
49+
class ChannelErrorState extends ChannelState {}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import '../../../meta/hooks/hooks_meta.dart';
2+
3+
part 'comment_replies_state.dart';
4+
5+
class CommentRepliesCubit extends Cubit<CommentRepliesState> {
6+
final CommentsRepository _commentsRepo;
7+
CommentRepliesCubit(this._commentsRepo)
8+
: super(CommentRepliesInitialState());
9+
10+
void resetFields() => emit(CommentRepliesInitialState());
11+
12+
Future<void> loadReplies(int commentIndex) async {
13+
emit(CommentRepliesLoadingState());
14+
try {
15+
final CommentsPage? page =
16+
await _commentsRepo.getCommentReplies(commentIndex);
17+
if (page != null && page.comments.isNotEmpty) {
18+
return emit(CommentRepliesLoadedState(page.comments));
19+
}
20+
return emit(CommentRepliesErrorState());
21+
} catch (e) {
22+
e.toString().print("CommentRepliesCubit loadReplies Error");
23+
return emit(CommentRepliesErrorState());
24+
}
25+
}
26+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
part of 'comment_replies_cubit.dart';
2+
3+
@immutable
4+
abstract class CommentRepliesState extends Equatable {
5+
const CommentRepliesState();
6+
@override
7+
List<Object> get props => [];
8+
}
9+
10+
@immutable
11+
class CommentRepliesInitialState extends CommentRepliesState {}
12+
13+
@immutable
14+
class CommentRepliesLoadingState extends CommentRepliesState {}
15+
16+
@immutable
17+
class CommentRepliesLoadedState extends CommentRepliesState {
18+
final List<YoutubeComment> replies;
19+
const CommentRepliesLoadedState(this.replies);
20+
@override
21+
List<Object> get props => [replies];
22+
}
23+
24+
@immutable
25+
class CommentRepliesErrorState extends CommentRepliesState {}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import '../../../meta/hooks/hooks_meta.dart';
2+
3+
part 'comments_state.dart';
4+
5+
class CommentsCubit extends Cubit<CommentsState> {
6+
final CommentsRepository _commentsRepo;
7+
CommentsCubit(this._commentsRepo) : super(CommentsInitialState());
8+
9+
void resetFields() => emit(CommentsInitialState());
10+
11+
Future<void> loadComments(String url) async {
12+
if (url.isEmpty) {
13+
emit(CommentsErrorState());
14+
return;
15+
}
16+
emit(CommentsLoadingState());
17+
try {
18+
final CommentsPage? page = await _commentsRepo.getComments(url);
19+
if (page != null && page.comments.isNotEmpty) {
20+
return emit(CommentsLoadedState(
21+
comments: page.comments,
22+
hasNextPage: page.hasNextPage,
23+
));
24+
}
25+
return emit(CommentsErrorState());
26+
} catch (e) {
27+
e.toString().print("CommentsCubit loadComments Error");
28+
return emit(CommentsErrorState());
29+
}
30+
}
31+
32+
Future<void> loadNextPage() async {
33+
final CommentsState currentState = state;
34+
if (currentState is! CommentsLoadedState ||
35+
currentState.isLoadingMore ||
36+
!currentState.hasNextPage) {
37+
return;
38+
}
39+
emit(currentState.copyWith(isLoadingMore: true));
40+
try {
41+
final CommentsPage? page = await _commentsRepo.getNextCommentsPage();
42+
if (page != null && page.comments.isNotEmpty) {
43+
return emit(currentState.copyWith(
44+
comments: <YoutubeComment>[
45+
...currentState.comments,
46+
...page.comments,
47+
],
48+
hasNextPage: page.hasNextPage,
49+
isLoadingMore: false,
50+
));
51+
}
52+
return emit(currentState.copyWith(
53+
isLoadingMore: false,
54+
hasNextPage: false,
55+
));
56+
} catch (e) {
57+
e.toString().print("CommentsCubit loadNextPage Error");
58+
return emit(currentState.copyWith(
59+
isLoadingMore: false,
60+
hasNextPage: false,
61+
));
62+
}
63+
}
64+
}

0 commit comments

Comments
 (0)