-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
create bloc elements to handle fetching posts
- Loading branch information
1 parent
0111d52
commit b910cb7
Showing
19 changed files
with
507 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import 'dart:async'; | ||
|
||
import 'package:bloc/bloc.dart'; | ||
import 'package:equatable/equatable.dart'; | ||
import 'package:minly_media_mobile/data/Repositories/post.repository.dart'; | ||
import 'package:minly_media_mobile/data/models/post/post.dart'; | ||
import 'package:minly_media_mobile/data/services/post.service.dart'; | ||
|
||
part 'post_event.dart'; | ||
part 'post_state.dart'; | ||
|
||
class PostsBloc extends Bloc<PostsEvent, PostsState> { | ||
PostsBloc() : super(PostsInitial()) { | ||
// listeners | ||
on<PostsFetchEvent>(_fetchPosts); | ||
} | ||
// handlers | ||
FutureOr<void> _fetchPosts( | ||
PostsFetchEvent event, Emitter<PostsState> emit) async { | ||
emit(PostsFetching()); | ||
try { | ||
print('hi'); | ||
final posts = await PostRepository(postService: PostService()) | ||
.getPosts(event.pageNumber, event.pageSize); | ||
|
||
emit(PostFetchedSuccessfully(posts: posts)); | ||
|
||
print(posts.toString()); | ||
} catch (e) { | ||
emit(PostFetchError(message: e.toString())); | ||
print(e.toString()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
part of 'post_bloc.dart'; | ||
|
||
abstract class PostsEvent extends Equatable {} | ||
|
||
class PostsFetchEvent extends PostsEvent { | ||
final int pageNumber; | ||
final int pageSize; | ||
|
||
PostsFetchEvent({required this.pageNumber, required this.pageSize}); | ||
|
||
@override | ||
List<Object> get props => [pageNumber, pageSize]; | ||
} | ||
|
||
class PostErrorEvent extends PostsEvent { | ||
final String message; | ||
|
||
PostErrorEvent({required this.message}); | ||
|
||
@override | ||
List<Object> get props => [message]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
part of 'post_bloc.dart'; | ||
|
||
sealed class PostsState extends Equatable { | ||
const PostsState(); | ||
|
||
@override | ||
List<Object> get props => []; | ||
} | ||
|
||
final class PostsInitial extends PostsState { | ||
final List<Post> posts = []; | ||
|
||
PostsInitial(); | ||
} | ||
|
||
final class PostsFetching extends PostsState {} | ||
|
||
final class PostFetchedSuccessfully extends PostsState { | ||
final List<Post> posts; | ||
|
||
const PostFetchedSuccessfully({required this.posts}); | ||
} | ||
|
||
final class PostFetchError extends PostsState { | ||
final String message; | ||
const PostFetchError({required this.message}); | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,21 @@ | ||
import 'package:minly_media_mobile/data/models/post/author.dart'; | ||
import 'package:minly_media_mobile/data/models/post/liked_by.dart'; | ||
|
||
class Post { | ||
late String id; | ||
late String caption; | ||
late String mediaUrl; | ||
late DateTime createdAt; | ||
late String createdAt; | ||
late String type; | ||
late List<LikedBy> likedBy; | ||
late List<dynamic> likedBy; | ||
late Author author; | ||
|
||
Post.fromJson(Map<String, dynamic> data) { | ||
Post.fromJson(dynamic data) { | ||
id = data['id']; | ||
caption = data['caption']; | ||
mediaUrl = data['mediaUrl']; | ||
createdAt = DateTime.parse(data['createdAt']); | ||
createdAt = data['createdAt']; | ||
type = data['type']; | ||
likedBy = data['likedBy']; | ||
author = data['author']; | ||
author = Author.fromJson(data['author']); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.