Skip to content

Commit

Permalink
complated busket
Browse files Browse the repository at this point in the history
  • Loading branch information
mahdiramezani-dev committed May 6, 2024
1 parent ab02e98 commit 1ad3f95
Show file tree
Hide file tree
Showing 17 changed files with 836 additions and 138 deletions.
17 changes: 17 additions & 0 deletions lib/bloc/busket/busket_bloc.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import 'package:apple_shop/bloc/busket/busket_event.dart';
import 'package:apple_shop/bloc/busket/busket_state.dart';
import 'package:apple_shop/data/datasource/busket_repository.dart';
import 'package:apple_shop/di/service_locator.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

class BusketBloc extends Bloc<BusketEvent, BusketState> {
final IBusketRepository _repository = locator.get();

BusketBloc() : super(InitialBusketState()) {
on<FetchBusketEvent>((event, emit) async {
var response = await _repository.fetchBucketList();

emit(ResponsebusketState(response: response));
});
}
}
3 changes: 3 additions & 0 deletions lib/bloc/busket/busket_event.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
abstract class BusketEvent {}

class FetchBusketEvent extends BusketEvent {}
15 changes: 15 additions & 0 deletions lib/bloc/busket/busket_state.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import 'package:apple_shop/data/models/bucket_model.dart';
import 'package:dartz/dartz.dart';

abstract class BusketState {}

class InitialBusketState extends BusketState {}

class ResponsebusketState extends BusketState {
Either<String, List<Bucket>> response;

ResponsebusketState({
required this.response,
});

}
7 changes: 7 additions & 0 deletions lib/bloc/product/product_bloc.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import 'package:apple_shop/bloc/product/product_event.dart';
import 'package:apple_shop/bloc/product/product_state.dart';
import 'package:apple_shop/data/datasource/busket_repository.dart';
import 'package:apple_shop/data/repository/product_detail_repository.dart';
import 'package:apple_shop/di/service_locator.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

class ProductBloc extends Bloc<ProductEvent, ProductState> {
final IProductDetailRespotory _productRepository = locator.get();
final IBusketRepository _busketRepository = locator.get();

ProductBloc() : super(ProductDetailIinitState()) {
on<InitialProductDetailEvent>((event, emit) async {
Expand All @@ -24,5 +26,10 @@ class ProductBloc extends Bloc<ProductEvent, ProductState> {
getProperties: properties,
));
});

on<AddProductToBusket>((event, emit) async {
await _busketRepository.addProductToBusket(event.product);
print("sucsess");
});
}
}
9 changes: 9 additions & 0 deletions lib/bloc/product/product_event.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:apple_shop/data/models/product_gallery_model.dart';
import 'package:apple_shop/data/models/product_model.dart';
import 'package:dartz/dartz.dart';

abstract class ProductEvent {}
Expand All @@ -13,3 +14,11 @@ class InitialProductDetailEvent extends ProductEvent {

});
}

class AddProductToBusket extends ProductEvent{

Product product;

AddProductToBusket({required this.product});

}
36 changes: 36 additions & 0 deletions lib/data/datasource/busket_datasource.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import 'package:apple_shop/data/models/bucket_model.dart';
import 'package:apple_shop/data/models/product_model.dart';
import 'package:hive/hive.dart';

abstract class IBusketDataSource {
Future<void> addProductToBusket(Product product);
Future<List<Bucket>> fetchBucketList();
}

class BusketDataSource extends IBusketDataSource {
@override
Future<void> addProductToBusket(Product product) async {
var box = Hive.box<Bucket>("BucketBox");
var item = Bucket(
id: product.id,
collectionId: product.collectionId,
collectionName: product.collectionName,
discount_price: product.discount_price,
name: product.name,
price: product.price,
quantity: product.quantity,
thumbnail: product.thumbnail,
);
box.add(item);
}

@override
Future<List<Bucket>> fetchBucketList() async {

var box = Hive.box<Bucket>("BucketBox");
List<Bucket> response = box.values.toList();
print(response);
return response;

}
}
34 changes: 34 additions & 0 deletions lib/data/datasource/busket_repository.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import 'package:apple_shop/data/datasource/busket_datasource.dart';
import 'package:apple_shop/data/models/bucket_model.dart';
import 'package:apple_shop/data/models/product_model.dart';
import 'package:apple_shop/di/service_locator.dart';
import 'package:dartz/dartz.dart';

abstract class IBusketRepository {
Future<Either<String, String>> addProductToBusket(Product product);
Future<Either<String, List<Bucket>>> fetchBucketList();
}

class BusketRepository extends IBusketRepository {
IBusketDataSource _dataSuorce = locator.get();
@override
Future<Either<String, String>> addProductToBusket(product) async {
try {
var respone = await _dataSuorce.addProductToBusket(product);

return const Right("محصول با موفقیت به سبد خرید اضافه شد");
} catch (ex) {
return const Left("خطای غیر منتظره رخ داد");
}
}

@override
Future<Either<String, List<Bucket>>> fetchBucketList() async {
try {
var respone = await _dataSuorce.fetchBucketList();
return Right(respone);
} catch (ex) {
return const Left("خطایی رخ داده است");
}
}
}
43 changes: 43 additions & 0 deletions lib/data/models/bucket_model.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import 'package:hive/hive.dart';

part 'bucket_model.g.dart';

@HiveType(typeId: 0)
class Bucket {
@HiveField(0)
String id;
@HiveField(1)
String collectionId;
@HiveField(2)
String collectionName;
@HiveField(3)
int discount_price;
@HiveField(4)
String name;
@HiveField(5)
int price;
@HiveField(6)
int quantity;
@HiveField(7)
String thumbnail;
@HiveField(8)
num? percent;
@HiveField(9)
int? realPrice;

Bucket({
required this.id,
required this.collectionId,
required this.collectionName,
required this.discount_price,
required this.name,
required this.price,
required this.quantity,
required this.thumbnail,
}) {
this.realPrice = (price + this.discount_price);
this.percent = ((realPrice! - price) / price) * 100;
this.thumbnail =
"https://startflutter.ir/api/files/$collectionId/$id/$thumbnail";
}
}
68 changes: 68 additions & 0 deletions lib/data/models/bucket_model.g.dart

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

4 changes: 4 additions & 0 deletions lib/di/service_locator.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
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/busket_repository.dart';
import 'package:apple_shop/data/datasource/product_category_data_source.dart';
import 'package:apple_shop/data/datasource/product_detail_datasource.dart';
import 'package:apple_shop/data/repository/banner_repository.dart';
Expand Down Expand Up @@ -32,6 +34,7 @@ Future<void> initLocator() async {
});

locator.registerFactory<IProductCategoryDataSource>(() => ProductCategoryDataSource());
locator.registerFactory<IBusketDataSource>(() => BusketDataSource());

//Repository Resource
locator.registerFactory<IAuthenticatinRepository>(
Expand All @@ -41,6 +44,7 @@ Future<void> initLocator() async {
locator.registerFactory<IProductRepository>(() => ProductRemoteRepository());
locator.registerFactory<IProductDetailRespotory>(() => ProductDetailRepository());
locator.registerFactory<IProductCategoryRepostory>(() => ProductCategoryRepository());
locator.registerFactory<IBusketRepository>(() => BusketRepository());
//components
locator.registerSingleton<SharedPreferences>(
await SharedPreferences.getInstance());
Expand Down
6 changes: 5 additions & 1 deletion lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import 'package:apple_shop/data/models/bucket_model.dart';
import 'package:apple_shop/di/service_locator.dart';
import 'package:apple_shop/screan/bootom_navigation.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';

import 'package:hive_flutter/hive_flutter.dart';
void main() async {
WidgetsFlutterBinding();
await Hive.initFlutter();
Hive.registerAdapter(BucketAdapter());
await Hive.openBox<Bucket>("BucketBox");
await initLocator();
runApp(MyApp());
}
Expand Down
16 changes: 12 additions & 4 deletions lib/screan/bootom_navigation.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import "dart:ui";

import "package:apple_shop/bloc/busket/busket_bloc.dart";
import "package:apple_shop/bloc/busket/busket_event.dart";
import "package:apple_shop/constants/colors.dart";
import "package:apple_shop/data/datasource/authentication_datasource.dart";
import "package:apple_shop/screan/category_screan.dart";
import "package:apple_shop/screan/home_screan.dart";
import "package:apple_shop/screan/order_screan.dart";
import "package:apple_shop/screan/profile_screan.dart";
import "package:flutter/material.dart";
import "package:flutter_bloc/flutter_bloc.dart";

class BottomNavigatonScrean extends StatefulWidget {
BottomNavigatonScrean({super.key});
Expand All @@ -18,8 +21,6 @@ class BottomNavigatonScrean extends StatefulWidget {
class _BottomNavigatonScreanState extends State<BottomNavigatonScrean> {
var _selectedIndex = 3;



@override
Widget build(BuildContext context) {
return Scaffold(
Expand Down Expand Up @@ -84,8 +85,15 @@ class _BottomNavigatonScreanState extends State<BottomNavigatonScrean> {
List<Widget> get_layout() {
List<Widget> layouts = [
ProfileScrean(),
const OrderScrean(),
CategoryScrean(),
BlocProvider(
create: (context) {
var bloc = BusketBloc();
bloc.add(FetchBusketEvent());
return bloc;
},
child: OrderScrean(),
),
CategoryScrean(),
const HomeScrean(),
];

Expand Down
Loading

0 comments on commit 1ad3f95

Please sign in to comment.