Skip to content

Commit

Permalink
feat: improving performance
Browse files Browse the repository at this point in the history
  • Loading branch information
BlackLeg15 committed Aug 7, 2023
1 parent ef22349 commit 18dbe12
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 37 deletions.
25 changes: 25 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "desafio_bloc_1",
"request": "launch",
"type": "dart"
},
{
"name": "desafio_bloc_1 (profile mode)",
"request": "launch",
"type": "dart",
"flutterMode": "profile"
},
{
"name": "desafio_bloc_1 (release mode)",
"request": "launch",
"type": "dart",
"flutterMode": "release"
}
]
}
2 changes: 1 addition & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ subprojects {
project.evaluationDependsOn(':app')
}

task clean(type: Delete) {
tasks.register("clean", Delete) {
delete rootProject.buildDir
}
2 changes: 1 addition & 1 deletion lib/app/core/constants/fetch_blog_posts_parameters.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class FetchBlogPostsParameters {
static const initialPage = 0;
static var postsPerPage = 10;
static const postsPerPage = 10;
}
10 changes: 8 additions & 2 deletions lib/app/modules/home/domain/entities/blog_post_entity.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
class BlogPostEntity {
import 'package:equatable/equatable.dart';

class BlogPostEntity extends Equatable {
final String? title;
final String? publicationDate;
final String? description;
final String? link;
final String? id;

const BlogPostEntity({this.title, this.publicationDate, this.description, this.link, this.id});

const BlogPostEntity({this.title, this.publicationDate, this.description, this.link});
@override
List<Object?> get props => [id];
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,26 @@ class GetAllPostsFromApiMapper {
}

BlogPostEntity fromJson(Map<String, dynamic> json) {
checkKey(json, 'title', 'Campo title é não existe/é nulo. Response: $json');
checkKey(json['title'], 'rendered', 'Campo title.rendered é não existe/é nulo. Response: $json');
checkKey(json, 'date', 'Campo date é não existe/é nulo. Response: $json');
checkKey(json, 'excerpt', 'Campo excerpt é não existe/é nulo. Response: $json');
checkKey(json, 'title', 'Campo title não existe/é nulo. Response: $json');
checkKey(json['title'], 'rendered', 'Campo title.rendered não existe/é nulo. Response: $json');
checkKey(json, 'date', 'Campo date não existe/é nulo. Response: $json');
checkKey(json, 'excerpt', 'Campo excerpt não existe/é nulo. Response: $json');
checkKey(json['excerpt'], 'rendered', 'Campo excerpt.rendered não existe/é nulo. Response: $json');
checkKey(json, 'link', 'Campo excerpt.rendered não existe/é nulo. Response: $json');
checkKey(json, 'id', 'Campo id não existe/é nulo. Response: $json');

final title = json['title']?['rendered'];
final publicationDate = json['date'];
final description = json['excerpt']!['rendered'];
final link = json['link'];
final id = json['id'];

return BlogPostEntity(
title: title,
publicationDate: publicationDate,
description: description,
link: link,
id: id.toString(),
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ part 'states/blog_posts_states.dart';
class BlogPostsBloc extends Bloc<BlogPostsEvent, BlogPostsState> {
final GetAllPostsUseCase getAllPostsUseCase;

BlogPostsBloc(this.getAllPostsUseCase) : super(BlogPostsInitialState(const [])) {
BlogPostsBloc(this.getAllPostsUseCase) : super(const BlogPostsInitialState([])) {
on<GetBlogPostsEvent>((event, emit) async {
if(state is BlogPostsInitialState){
if (state is BlogPostsInitialState) {
await initializeDateFormatting('pt_BR');
}
final currentPage = state.page;
Expand All @@ -28,7 +28,7 @@ class BlogPostsBloc extends Bloc<BlogPostsEvent, BlogPostsState> {
}, (fetchedListOfPosts) {
final listOfPostsToShow = [
...state.blogPosts,
...fetchedListOfPosts
...fetchedListOfPosts,
];
return BlogPostsSuccessState(listOfPostsToShow, pageToBeFetched);
}));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,28 @@ abstract class BlogPostsState extends Equatable {
const BlogPostsState(this.blogPosts, this.page, this.postsPerPage);

@override
List<Object?> get props => [
page,
postsPerPage,
blogPosts
];
List<Object?> get props => [page, postsPerPage, blogPosts];
}

class BlogPostsInitialState extends BlogPostsState {
BlogPostsInitialState(List<BlogPostEntity> blogPosts, {int? initialPage}) : super(blogPosts, initialPage ?? FetchBlogPostsParameters.initialPage, FetchBlogPostsParameters.postsPerPage);
const BlogPostsInitialState(List<BlogPostEntity> blogPosts, {int? initialPage})
: super(blogPosts, initialPage ?? FetchBlogPostsParameters.initialPage, FetchBlogPostsParameters.postsPerPage);
}

class BlogPostsLoadingState extends BlogPostsState {
BlogPostsLoadingState(List<BlogPostEntity> blogPosts, int page) : super(blogPosts, page, FetchBlogPostsParameters.postsPerPage);
const BlogPostsLoadingState(List<BlogPostEntity> blogPosts, int page) : super(blogPosts, page, FetchBlogPostsParameters.postsPerPage);
}

class BlogPostsSuccessState extends BlogPostsState {
BlogPostsSuccessState(List<BlogPostEntity> blogPosts, int page) : super(blogPosts, page, FetchBlogPostsParameters.postsPerPage);
const BlogPostsSuccessState(List<BlogPostEntity> blogPosts, int page) : super(blogPosts, page, FetchBlogPostsParameters.postsPerPage);
}

class BlogPostsErrorState extends BlogPostsState {
final String message;

BlogPostsErrorState(this.message, List<BlogPostEntity> blogPosts, int page) : super(blogPosts, page, FetchBlogPostsParameters.postsPerPage);
const BlogPostsErrorState(this.message, List<BlogPostEntity> blogPosts, int page)
: super(blogPosts, page, FetchBlogPostsParameters.postsPerPage);

@override
List<Object?> get props => super.props
..addAll([
message
]);
List<Object?> get props => super.props..addAll([message]);
}
1 change: 1 addition & 0 deletions lib/app/modules/home/presenter/pages/home/home_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ class _HomePageState extends State<HomePage> {
return blogPostsState is BlogPostsLoadingState ? const HomeLoadingWidget() : const SizedBox();
}
return BlogPostCardWidget(
key: Key(blogPostsList[index].id!),
blogPost: blogPostsList[index],
onTap: () => onTapBlogPostCard(blogPostsList[index].link),
);
Expand Down
20 changes: 10 additions & 10 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ packages:
dependency: "direct dev"
description:
name: bloc_test
sha256: "43d5b2f3d09ba768d6b611151bdf20ca141ffb46e795eb9550a58c9c2f4eae3f"
sha256: af0de1a1e16a7536e95dcd7491e0a6d6078e11d2d691988e862280b74f5c7968
url: "https://pub.dev"
source: hosted
version: "9.1.3"
version: "9.1.4"
boolean_selector:
dependency: transitive
description:
Expand Down Expand Up @@ -221,10 +221,10 @@ packages:
dependency: "direct main"
description:
name: dio
sha256: a9d76e72985d7087eb7c5e7903224ae52b337131518d127c554b9405936752b8
sha256: ce75a1b40947fea0a0e16ce73337122a86762e38b982e1ccb909daa3b9bc4197
url: "https://pub.dev"
source: hosted
version: "5.2.1+1"
version: "5.3.2"
equatable:
dependency: "direct main"
description:
Expand Down Expand Up @@ -508,10 +508,10 @@ packages:
dependency: transitive
description:
name: plugin_platform_interface
sha256: "6a2128648c854906c53fa8e33986fc0247a1116122f9534dd20e3ab9e16a32bc"
sha256: "43798d895c929056255600343db8f049921cbec94d31ec87f1dc5c16c01935dd"
url: "https://pub.dev"
source: hosted
version: "2.1.4"
version: "2.1.5"
pool:
dependency: transitive
description:
Expand Down Expand Up @@ -729,10 +729,10 @@ packages:
dependency: transitive
description:
name: url_launcher_android
sha256: "15f5acbf0dce90146a0f5a2c4a002b1814a6303c4c5c075aa2623b2d16156f03"
sha256: "78cb6dea3e93148615109e58e42c35d1ffbf5ef66c44add673d0ab75f12ff3af"
url: "https://pub.dev"
source: hosted
version: "6.0.36"
version: "6.0.37"
url_launcher_ios:
dependency: transitive
description:
Expand Down Expand Up @@ -793,10 +793,10 @@ packages:
dependency: transitive
description:
name: vm_service
sha256: b8c67f5fa3897b122cf60fe9ff314f7b0ef71eab25c5f8b771480bc338f48823
sha256: "0fae432c85c4ea880b33b497d32824b97795b04cdaa74d270219572a1f50268d"
url: "https://pub.dev"
source: hosted
version: "11.7.2"
version: "11.9.0"
watcher:
dependency: transitive
description:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ void main() {
build: () => BlogPostsBloc(useCaseMock),
act: (bloc) => bloc.add(const GetBlogPostsEvent()),
expect: () => [
BlogPostsLoadingState(const [], 0),
const BlogPostsLoadingState([], 0),
BlogPostsSuccessState(resultFromUsecase, 1)
],
verify: (bloc) {
Expand All @@ -45,8 +45,8 @@ void main() {
build: () => BlogPostsBloc(useCaseMock),
act: (bloc) => bloc.add(const GetBlogPostsEvent()),
expect: () => [
BlogPostsLoadingState(const [], 0),
BlogPostsErrorState('Erro', const [], 0)
const BlogPostsLoadingState([], 0),
const BlogPostsErrorState('Erro', [], 0)
],
verify: (bloc) {
final blogPosts = bloc.state.blogPosts;
Expand Down

0 comments on commit 18dbe12

Please sign in to comment.