Skip to content

Commit f4a8f43

Browse files
committed
image handeling, Open/Close implementation of SOLID principle
1 parent fa7bdaa commit f4a8f43

23 files changed

+197
-169
lines changed

lib/core/db/db_helper.dart

Lines changed: 36 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,60 @@
11
import 'dart:async';
2-
import 'package:sqflite/sqflite.dart';
3-
import 'package:path/path.dart';
42
import '../model/post_model.dart';
3+
import 'package:sqflite/sqflite.dart' as sql;
54

65
class DatabaseHelper {
7-
static final DatabaseHelper _instance = DatabaseHelper._internal();
8-
9-
factory DatabaseHelper() => _instance;
10-
11-
DatabaseHelper._internal();
12-
13-
Database? _database;
14-
15-
Future<Database?> get database async {
16-
if (_database != null) return _database;
17-
18-
_database = await initDatabase();
19-
return _database;
6+
static Future<sql.Database> db() async {
7+
return sql.openDatabase('blog.db', version: 1, onCreate: (
8+
sql.Database database,
9+
int version,
10+
) async {
11+
await createTables(database);
12+
});
2013
}
2114

22-
Future<Database> initDatabase() async {
23-
final databasesPath = await getDatabasesPath();
24-
final path = join(databasesPath, 'blog.db');
25-
26-
return await openDatabase(path, version: 1, onCreate: (Database db, int version) async {
27-
await db.execute('''
28-
CREATE TABLE posts (
29-
id TEXT PRIMARY KEY,
30-
title TEXT,
31-
content TEXT,
32-
imagePath TEXT,
33-
isSelected integer
34-
)
35-
''');
36-
});
15+
static Future<void> createTables(sql.Database database) async {
16+
await database.execute("""CREATE TABLE post(
17+
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
18+
title TEXT,
19+
content TEXT,
20+
imagePath TEXT,
21+
isSelected INTEGER,
22+
createdAt TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
23+
)""");
3724
}
3825

39-
Future<int> insertPost(PostModel post) async {
40-
final db = await database;
41-
return await db!.insert('posts', post.toMap());
26+
static Future<int> insertPost(PostModel post) async {
27+
final db = await DatabaseHelper.db();
28+
return await db.insert('post', post.toMap());
4229
}
4330

44-
Future<List<PostModel>> getPosts() async {
45-
final db = await database;
46-
final List<Map<String, dynamic>> maps = await db!.query('posts');
31+
static Future<List<PostModel>> getPosts() async {
32+
final db = await DatabaseHelper.db();
33+
final List<Map<String, dynamic>> maps = await db.query('post');
4734
return List.generate(maps.length, (i) {
4835
return PostModel(
49-
maps[i]['id'],
50-
maps[i]['title'],
51-
maps[i]['content'],
52-
maps[i]['imagePath'],
53-
maps[i]['isSelected'],
36+
id: maps[i]['id'],
37+
title: maps[i]['title'],
38+
content: maps[i]['content'],
39+
imagePath: maps[i]['imagePath'],
40+
isSelected: maps[i]['isSelected'],
41+
createdAt: DateTime.parse(maps[i]['createdAt']),
5442
);
5543
});
5644
}
5745

58-
Future<int> updatePost(PostModel post) async {
59-
final db = await database;
60-
return await db!.update(
61-
'posts',
46+
static Future<int> updatePost(PostModel post) async {
47+
final db = await DatabaseHelper.db();
48+
return await db.update(
49+
'post',
6250
post.toMap(),
6351
where: 'id = ?',
6452
whereArgs: [post.id],
6553
);
6654
}
6755

68-
Future<int> deletePost(String postId) async {
69-
final db = await database;
70-
return await db!.delete('posts', where: 'id = ?', whereArgs: [postId]);
56+
static Future<int> deletePost(int postId) async {
57+
final db = await DatabaseHelper.db();
58+
return await db.delete('post', where: 'id = ?', whereArgs: [postId]);
7159
}
7260
}

lib/core/entities/post.dart

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
class Post {
2-
final String id;
2+
int? id;
33
String title;
44
String content;
5-
String imagePath;
5+
String? imagePath;
66
int isSelected;
7+
DateTime? createdAt;
78

8-
Post(this.id, this.title, this.content, this.imagePath, this.isSelected);
9+
Post({
10+
this.id,
11+
required this.title,
12+
required this.content,
13+
this.imagePath,
14+
required this.isSelected,
15+
this.createdAt,
16+
});
917
}

lib/core/model/post_model.dart

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,37 @@
11
import 'package:my_blog_bloc/core/entities/post.dart';
22

33
class PostModel extends Post {
4-
PostModel(
5-
String id,
6-
String title,
7-
String content,
8-
String imagePath,
9-
int isSelected,
10-
) : super(
11-
id,
12-
title,
13-
content,
14-
imagePath,
15-
isSelected,
4+
PostModel({
5+
int? id,
6+
required String title,
7+
required String content,
8+
String? imagePath,
9+
required int isSelected,
10+
DateTime? createdAt,
11+
}) : super(
12+
id: id,
13+
title: title,
14+
content: content,
15+
imagePath: imagePath,
16+
isSelected: isSelected,
17+
createdAt: createdAt,
1618
);
1719

1820
Map<String, dynamic> toMap() {
1921
return {
20-
'id': id,
2122
'title': title,
2223
'content': content,
2324
'imagePath': imagePath,
2425
'isSelected': isSelected,
2526
};
2627
}
28+
29+
factory PostModel.fromJson(Map<String, dynamic> json) => PostModel(
30+
id: json["id"],
31+
title: json["title"],
32+
content: json["content"],
33+
imagePath: json["imagePath"],
34+
isSelected: json["isSelected"],
35+
createdAt: json["createdAt"],
36+
);
2737
}

lib/features/add_post/data/datasources/post_posts_local_data_sources.dart

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,12 @@ abstract class PostPostsLocalDataSources {
66
}
77

88
class PostPostsLocalDataSourcesImpl extends PostPostsLocalDataSources {
9-
DatabaseHelper databaseHelper = DatabaseHelper();
10-
119
PostPostsLocalDataSourcesImpl();
1210
@override
1311
Future<int> postPosts(PostModel postModel) => _postPostsToLocal(postModel);
1412

1513
Future<int> _postPostsToLocal(PostModel postModel) async {
16-
int response = await databaseHelper.insertPost(postModel);
14+
int response = await DatabaseHelper.insertPost(postModel);
1715
return response;
1816
}
1917
}

lib/features/add_post/data/datasources/update_posts_local_data_sources.dart

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,12 @@ abstract class UpdatePostLocalDataSources {
66
}
77

88
class UpdatePostLocalDataSourcesImpl implements UpdatePostLocalDataSources {
9-
DatabaseHelper databaseHelper = DatabaseHelper();
10-
119
UpdatePostLocalDataSourcesImpl();
1210
@override
1311
Future<int> updatePost(PostModel postModel) => _updatePostToLocal(postModel);
1412

1513
Future<int> _updatePostToLocal(PostModel postModel) async {
16-
final int response = await databaseHelper.updatePost(postModel);
14+
final int response = await DatabaseHelper.updatePost(postModel);
1715
return response;
1816
}
1917
}

lib/features/add_post/data/repositories/post_posts_repositories_impl.dart

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import 'package:dartz/dartz.dart';
22
import 'package:my_blog_bloc/core/errors/failures.dart';
33
import 'package:my_blog_bloc/core/model/post_model.dart';
44
import 'package:my_blog_bloc/features/add_post/domain/repositories/post_posts_repositories.dart';
5+
import '../../../../core/entities/post.dart';
56
import '../../../../core/errors/exceptions.dart';
67
import '../datasources/post_posts_local_data_sources.dart';
78

@@ -10,7 +11,13 @@ class PostPostsRepositoriesImpl implements PostPostsRepository {
1011
PostPostsRepositoriesImpl({required this.postPostsLocalDataSources});
1112

1213
@override
13-
Future<Either<Failure, int>>? postPosts(PostModel postModel) async {
14+
Future<Either<Failure, int>>? postPosts(Post post) async {
15+
PostModel postModel = PostModel(
16+
title: post.title,
17+
content: post.content,
18+
imagePath: post.imagePath,
19+
isSelected: post.isSelected,
20+
);
1421
try {
1522
int response = await postPostsLocalDataSources.postPosts(postModel);
1623
return Right(response);

lib/features/add_post/data/repositories/update_post_repositories_impl.dart

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,24 @@
11
import 'package:dartz/dartz.dart';
22
import 'package:my_blog_bloc/core/errors/exceptions.dart';
33
import 'package:my_blog_bloc/core/errors/failures.dart';
4-
import 'package:my_blog_bloc/core/model/post_model.dart';
54
import 'package:my_blog_bloc/features/add_post/data/datasources/update_posts_local_data_sources.dart';
65
import 'package:my_blog_bloc/features/add_post/domain/repositories/update_post_repository.dart';
7-
6+
7+
import '../../../../core/entities/post.dart';
8+
import '../../../../core/model/post_model.dart';
9+
810
class UpdatePostRepositoriesImpl implements UpdatePostRepository {
911
UpdatePostLocalDataSources updatePostLocalDataSources;
1012
UpdatePostRepositoriesImpl({required this.updatePostLocalDataSources});
1113

1214
@override
13-
Future<Either<Failure, int>>? updatePost(PostModel postModel) async {
15+
Future<Either<Failure, int>>? updatePost(Post post) async {
16+
PostModel postModel = PostModel(
17+
title: post.title,
18+
content: post.content,
19+
imagePath: post.imagePath,
20+
isSelected: post.isSelected,
21+
);
1422
try {
1523
int response = await updatePostLocalDataSources.updatePost(postModel);
1624
return Right(response);
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import 'package:dartz/dartz.dart';
2-
import 'package:my_blog_bloc/core/model/post_model.dart';
2+
import '../../../../core/entities/post.dart';
33
import '../../../../core/errors/failures.dart';
44

55
abstract class UpdatePostRepository {
6-
Future<Either<Failure, int>>? updatePost(PostModel post);
6+
Future<Either<Failure, int>>? updatePost(Post post);
77
}
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
import 'package:dartz/dartz.dart';
22
import 'package:my_blog_bloc/core/errors/failures.dart';
3-
import 'package:my_blog_bloc/core/model/post_model.dart';
43
import 'package:my_blog_bloc/features/add_post/domain/repositories/update_post_repository.dart';
4+
import '../../../../core/entities/post.dart';
55
import '../../../../core/usecases/usecase.dart';
66

7-
class UpdatePost implements UseCase<int, PostModel> {
7+
class UpdatePost implements UseCase<int, Post> {
88
UpdatePostRepository updatePostRepository;
99

1010
UpdatePost(this.updatePostRepository);
1111

1212
@override
13-
Future<Either<Failure, int>?> call(PostModel postModel) async {
14-
return await updatePostRepository.updatePost(postModel);
13+
Future<Either<Failure, int>?> call(Post post) async {
14+
return await updatePostRepository.updatePost(post);
1515
}
1616
}

lib/features/add_post/presentation/bloc/post_add_bloc.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import 'package:bloc/bloc.dart';
55
import 'package:image_picker/image_picker.dart';
66
import 'package:my_blog_bloc/features/add_post/domain/usecases/post_post.dart';
77
import 'package:my_blog_bloc/features/add_post/domain/usecases/update_post.dart';
8+
import 'package:my_blog_bloc/features/home/domain/usecases/get_posts.dart';
89
import 'package:path_provider/path_provider.dart';
910
import '../../../../core/db/db_helper.dart';
1011
import 'post_add_event.dart';
@@ -13,8 +14,9 @@ import 'post_add_state.dart';
1314
class PostAddBloc extends Bloc<PostAddEvent, PostAddState> {
1415
final PostPosts postPosts;
1516
final UpdatePost updatePost;
17+
final GetPosts getPosts;
1618
final DatabaseHelper dbHelper = DatabaseHelper();
17-
PostAddBloc({required this.postPosts, required this.updatePost}) : super(PostAddInitialState()) {
19+
PostAddBloc({required this.postPosts, required this.updatePost, required this.getPosts}) : super(PostAddInitialState()) {
1820
on<PostAddInitialEvent>(postAddInitialEvent);
1921
on<PostAddReadyToUpdateEvent>(postAddReadyToUpdateEvent);
2022
on<PostAddPickFromGalaryButtonPressEvent>(addPostPickFromGalaryButtonPressEvent);
@@ -67,7 +69,6 @@ class PostAddBloc extends Bloc<PostAddEvent, PostAddState> {
6769

6870
FutureOr<void> addPostSaveButtonPressEvent(PostAddSaveButtonPressEvent event, Emitter<PostAddState> emit) async {
6971
await postPosts(event.newPost);
70-
// await dbHelper.insertPost(event.newPost);
7172
emit(AddPostSavedState());
7273
}
7374

@@ -77,11 +78,10 @@ class PostAddBloc extends Bloc<PostAddEvent, PostAddState> {
7778

7879
FutureOr<void> postAddUpdateButtonPressEvent(PostAddUpdateButtonPressEvent event, Emitter<PostAddState> emit) async {
7980
await updatePost(event.updatedPost);
80-
// await dbHelper.updatePost(event.updatedPost);
8181
emit(AddPostUpdatedState());
8282
}
8383

8484
FutureOr<void> postAddReadyToUpdateEvent(PostAddReadyToUpdateEvent event, Emitter<PostAddState> emit) {
85-
emit(PostAddReadyToUpdateState(event.postModel.imagePath));
85+
emit(PostAddReadyToUpdateState(event.post.imagePath));
8686
}
8787
}

0 commit comments

Comments
 (0)