Skip to content

Beta release v0.5.0 #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 8 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 6 additions & 27 deletions lib/app.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:flutter_localizations/flutter_localizations.dart';

import 'bloc/language/language_cubit.dart';
import 'bloc/post/post_cubit.dart';
import 'bloc/theme/theme_cubit.dart';
import 'constants/app_themes.dart';
import 'constants/supported_locales.dart';
import 'data/repositories/post_repository.dart';
import 'presentation/pages/posts/posts_page.dart';
import 'presentation/pages/splash/splash_page.dart';
import 'presentation/router/route_controller.dart';
import 'utils/constants/app_themes.dart';
import 'utils/constants/supported_locales.dart';

class App extends StatelessWidget {
@override
Expand All @@ -25,34 +24,14 @@ class App extends StatelessWidget {
darkTheme: AppThemes.darkTheme,
locale: locale,
supportedLocales: supportedLocales,
home: SplashPage(),
localizationsDelegates: [
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
AppLocalizations.delegate,
],
home: Navigator(
pages: [
MaterialPage(
child: RepositoryProvider(
create: (_) => PostRepository(),
child: BlocProvider(
create: (ctx) => PostCubit(
ctx.read<PostRepository>(),
)..fetch(),
child: PostsPage(),
),
),
),
],
onPopPage: (route, result) {
if (route.didPop(result)) {
return true;
}

return false;
},
),
onGenerateRoute: RouteController.onGenerateRoute,
);
}
}
3 changes: 3 additions & 0 deletions lib/bloc/app_state/app_state.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import 'package:flutter/cupertino.dart';

class AppState extends ChangeNotifier {}
17 changes: 17 additions & 0 deletions lib/bloc/comment/comment_cubit.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import 'dart:async';

import '../../data/contractors/base_comment_repository.dart';
import '../../data/models/comment.dart';
import '../data/data_cubit.dart';

export '../data/data_cubit.dart';

class CommentCubit extends DataCubit<List<Comment>> {
CommentCubit(this.baseCommentRepository);

final BaseCommentRepository baseCommentRepository;

@override
FutureOr<List<Comment>> loadData([int? postId]) =>
baseCommentRepository.getComments(postId: postId!);
}
6 changes: 3 additions & 3 deletions lib/bloc/data/data_cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ abstract class DataCubit<T> extends Cubit<DataState<T>> {

bool get emitInProgress => true;

FutureOr<T> loadData();
FutureOr<T> loadData([int? id]);

void fetch() async {
void fetch([int? id]) async {
emit(state.inProgress());

try {
final data = await loadData();
final data = await loadData(id);

if (data != null) {
_logger.fine('$data');
Expand Down
2 changes: 1 addition & 1 deletion lib/bloc/language/language_cubit.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

import '../../contractors/base_preferences_service.dart';
import '../../data/contractors/base_preferences_service.dart';

class LanguageCubit extends Cubit<Locale> {
LanguageCubit(this.preferencesService) : super(Locale('en', 'US')) {
Expand Down
18 changes: 15 additions & 3 deletions lib/bloc/post/post_cubit.dart
Original file line number Diff line number Diff line change
@@ -1,17 +1,29 @@
import 'dart:async';

import '../../contractors/base_post_repository.dart';
import '../../data/contractors/base_post_repository.dart';
import '../../data/models/post.dart';
import '../data/data_cubit.dart';

export '../data/data_cubit.dart';
export '../../data/models/post.dart';
export '../data/data_cubit.dart';

class PostCubit extends DataCubit<List<Post>> {
PostCubit(this.postRepository);

final BasePostRepository postRepository;

@override
FutureOr<List<Post>> loadData() => postRepository.getPosts();
FutureOr<List<Post>> loadData([int? id]) async {
final posts = await postRepository.getPosts();

return posts
.map<Post>(
(post) => post
..body = post.body.replaceAll(
'\n',
' ',
),
)
.toList();
}
}
3 changes: 2 additions & 1 deletion lib/bloc/theme/theme_cubit.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

import '../../contractors/base_preferences_service.dart';
import '../../data/contractors/base_preferences_service.dart';

class ThemeCubit extends Cubit<ThemeMode> {
ThemeCubit(this.preferencesService) : super(ThemeMode.system) {
Expand All @@ -11,6 +11,7 @@ class ThemeCubit extends Cubit<ThemeMode> {
BasePreferencesService preferencesService;

void changeTheme(ThemeMode themeMode) async {
print('themeMode: $themeMode');
if (state == themeMode) {
return;
}
Expand Down
6 changes: 4 additions & 2 deletions lib/config/init.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

import '../bloc/app_bloc_observer.dart';
import '../data/data_providers/comment_data_provider.dart';
import '../data/data_providers/post_data_provider.dart';
import '../data/services/logging_service.dart';
import '../data/services/preferences_service.dart';
import '../locator.dart';
import '../services/logging_service.dart';
import '../services/preferences_service.dart';
import 'config.dart';

Future<void> init() async {
Expand All @@ -29,4 +30,5 @@ Future<void> _initDataProviders() async {
final dio = Dio();

locator.register<PostDataProvider>(PostDataProvider(dio));
locator.register<CommentDataProvider>(CommentDataProvider(dio));
}
3 changes: 0 additions & 3 deletions lib/constants/app_text_styles.dart

This file was deleted.

3 changes: 0 additions & 3 deletions lib/constants/routes.dart

This file was deleted.

5 changes: 5 additions & 0 deletions lib/data/contractors/base_comment_repository.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import '../../data/models/comment.dart';

abstract class BaseCommentRepository {
Future<List<Comment>> getComments({required int postId});
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import '../data/models/post.dart';
import '../../data/models/post.dart';

abstract class BasePostRepository {
Future<List<Post>> getPosts();
}
}
14 changes: 14 additions & 0 deletions lib/data/data_providers/comment_data_provider.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import 'package:dio/dio.dart';
import 'package:retrofit/retrofit.dart';

import '../models/comment.dart';

part 'comment_data_provider.g.dart';

@RestApi(baseUrl: 'https://jsonplaceholder.typicode.com/')
abstract class CommentDataProvider {
factory CommentDataProvider(Dio dio, {String baseUrl}) = _CommentDataProvider;

@GET('/comments')
Future<List<Comment>> getComments(@Query('postId') int postId);
}
47 changes: 47 additions & 0 deletions lib/data/data_providers/comment_data_provider.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions lib/data/exceptions/network_exceptions.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

25 changes: 25 additions & 0 deletions lib/data/models/comment.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class Comment {
final int postId;
final int id;
final String name;
final String email;
String body;

Comment({
required this.postId,
required this.id,
required this.name,
required this.email,
required this.body,
});

factory Comment.fromJson(Map<String, dynamic> json) {
return Comment(
postId: json['postId'],
id: json['id'],
name: json['name'],
email: json['email'],
body: json['body'],
);
}
}
2 changes: 1 addition & 1 deletion lib/data/models/post.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class Post {
final int userId;
final int id;
final String title;
final String body;
String body;

Post({
required this.userId,
Expand Down
13 changes: 13 additions & 0 deletions lib/data/repositories/comment_repository.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import 'package:architecture_example/data/models/comment.dart';

import '../../locator.dart';
import '../contractors/base_comment_repository.dart';
import '../data_providers/comment_data_provider.dart';

class CommentRepository implements BaseCommentRepository {
final _commentDataProvider = Locator.instance.get<CommentDataProvider>();

@override
Future<List<Comment>> getComments({required int postId}) =>
_commentDataProvider.getComments(postId);
}
2 changes: 1 addition & 1 deletion lib/data/repositories/post_repository.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'package:architecture_example/data/models/post.dart';

import '../../contractors/base_post_repository.dart';
import '../../locator.dart';
import '../contractors/base_post_repository.dart';
import '../data_providers/post_data_provider.dart';

class PostRepository implements BasePostRepository {
Expand Down
Empty file.
4 changes: 3 additions & 1 deletion lib/l10n/app_az.arb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
{
"hi": "Salam!"
"posts": "Postlar",
"profile": "Profil",
"setttings": "Tenzimlemeler"
}
8 changes: 4 additions & 4 deletions lib/l10n/app_en.arb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"hi": "Hi!",
"@hi": {
"description": "The conventional newborn programmer greeting"
}
"posts": "Posts",
"@posts": {},
"profile": "Profile",
"@profile": {}
}
2 changes: 1 addition & 1 deletion lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import 'app.dart';
import 'bloc/language/language_cubit.dart';
import 'bloc/theme/theme_cubit.dart';
import 'config/init.dart';
import 'services/preferences_service.dart';
import 'data/services/preferences_service.dart';

void main() async {
await init();
Expand Down
Empty file removed lib/presentation/global/.gitkeep
Empty file.
Loading