-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ab02e98
commit 1ad3f95
Showing
17 changed files
with
836 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
abstract class BusketEvent {} | ||
|
||
class FetchBusketEvent extends BusketEvent {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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("خطایی رخ داده است"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.