Skip to content

Commit

Permalink
add buttonsheet to detail product screan for show comments -> have a …
Browse files Browse the repository at this point in the history
…bug for height bottonsheet
  • Loading branch information
mahdiramezani-dev committed May 9, 2024
1 parent 125cdbf commit 11a5bc4
Show file tree
Hide file tree
Showing 8 changed files with 268 additions and 91 deletions.
17 changes: 17 additions & 0 deletions lib/bloc/comments/comment_bloc.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import 'package:apple_shop/bloc/comments/comment_event.dart';
import 'package:apple_shop/bloc/comments/comment_state.dart';
import 'package:apple_shop/data/repository/comments_reository.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

class CommentBloc extends Bloc<CommentEvent, CommentState> {
final ICommentsRepository _repository;

CommentBloc(this._repository) : super(InitialCommentState()) {
on<GetCommntsEvent>((event, emit) async {
emit(LoadingCommentsState());
var response = await _repository.getComments(event.productId);

emit(ResponseCommentState(response: response));
});
}
}
9 changes: 9 additions & 0 deletions lib/bloc/comments/comment_event.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
abstract class CommentEvent {}

class GetCommntsEvent extends CommentEvent {
String productId;

GetCommntsEvent({
required this.productId,
});
}
16 changes: 16 additions & 0 deletions lib/bloc/comments/comment_state.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import 'package:apple_shop/data/models/comments_models.dart';
import 'package:dartz/dartz.dart';

abstract class CommentState {}

class InitialCommentState extends CommentState {}

class LoadingCommentsState extends CommentState {}

class ResponseCommentState extends CommentState {
Either<String, List<Comments>> response;

ResponseCommentState({
required this.response,
});
}
41 changes: 41 additions & 0 deletions lib/data/datasource/comment_data_source.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import 'dart:io';

import 'package:apple_shop/data/models/comments_models.dart';
import 'package:apple_shop/di/service_locator.dart';
import 'package:apple_shop/util/api_exception.dart';
import 'package:dio/dio.dart';

abstract class ICommentDataSource {
Future<List<Comments>> getComments(String productId);
}

class CommentRemoteDataSource extends ICommentDataSource {
final Dio _dio = locator.get();
@override
Future<List<Comments>> getComments(String productId) async {
try {
Map<String, String> qparam = {"filter": 'product_id="$productId"'};

var response = await _dio.get(
"collections/comment/records",
queryParameters: qparam,
);

return response.data["items"].map((jsonMapObject) {
return Comments.fromJson(jsonMapObject);
}).toList();


} on DioException catch (ex) {
throw ApiExceptiopn(
code: ex.response!.statusCode!,
messgae: ex.response!.data["message"],
);
} catch (ex) {
throw ApiExceptiopn(
code: 0,
messgae: "خطا محتوای متنی ندارد",
);
}
}
}
22 changes: 22 additions & 0 deletions lib/data/models/comments_models.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class Comments {
String id;
String product_id;
String text;
String user_id;

Comments({
required this.id,
required this.product_id,
required this.text,
required this.user_id,
});

factory Comments.fromJson(Map<String, dynamic> jsonMapObject) {
return Comments(
id: jsonMapObject["id"],
product_id: jsonMapObject["product_id"],
text: jsonMapObject["text"],
user_id: jsonMapObject["user_id"],
);
}
}
22 changes: 22 additions & 0 deletions lib/data/repository/comments_reository.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import 'package:apple_shop/data/datasource/comment_data_source.dart';
import 'package:apple_shop/data/models/comments_models.dart';
import 'package:apple_shop/di/service_locator.dart';
import 'package:apple_shop/util/api_exception.dart';
import 'package:dartz/dartz.dart';

abstract class ICommentsRepository {
Future<Either<String, List<Comments>>> getComments(String productId);
}

class CommenstRepository extends ICommentsRepository {
ICommentDataSource _dataSource = locator.get();
@override
Future<Either<String, List<Comments>>> getComments(String productId) async {
try {
var response = await _dataSource.getComments(productId);
return Right(response);
} on ApiExceptiopn catch (ex) {
return Left(ex.messgae);
}
}
}
4 changes: 4 additions & 0 deletions lib/di/service_locator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'package:apple_shop/bloc/busket/busket_bloc.dart';
import 'package:apple_shop/data/datasource/authentication_datasource.dart';
import 'package:apple_shop/data/datasource/banner_datasource.dart';
import 'package:apple_shop/data/datasource/busket_datasource.dart';
import 'package:apple_shop/data/datasource/comment_data_source.dart';
import 'package:apple_shop/data/repository/busket_repository.dart';
import 'package:apple_shop/data/datasource/product_category_data_source.dart';
import 'package:apple_shop/data/datasource/product_detail_datasource.dart';
Expand All @@ -10,6 +11,7 @@ import 'package:apple_shop/data/datasource/category_data_source.dart';
import 'package:apple_shop/data/datasource/product_data_source.dart';
import 'package:apple_shop/data/repository/authentication_repository.dart';
import 'package:apple_shop/data/repository/category_repository.dart';
import 'package:apple_shop/data/repository/comments_reository.dart';
import 'package:apple_shop/data/repository/product_category_repository.dart';
import 'package:apple_shop/data/repository/product_detail_repository.dart';
import 'package:apple_shop/data/repository/product_repository.dart';
Expand Down Expand Up @@ -75,6 +77,7 @@ void _initDataSource() {
locator.registerFactory<IProductCategoryDataSource>(
() => ProductCategoryDataSource());
locator.registerFactory<IBusketDataSource>(() => BusketDataSource());
locator.registerFactory<ICommentDataSource>(() => CommentRemoteDataSource());
}

void _initRepository() {
Expand All @@ -88,6 +91,7 @@ void _initRepository() {
locator.registerFactory<IProductCategoryRepostory>(
() => ProductCategoryRepository());
locator.registerFactory<IBusketRepository>(() => BusketRepository());
locator.registerFactory<ICommentsRepository>(() => CommenstRepository());
}

void _initBloc() {
Expand Down
Loading

0 comments on commit 11a5bc4

Please sign in to comment.