Skip to content

Commit 0bbc7d6

Browse files
committed
update
1 parent f7612c4 commit 0bbc7d6

File tree

69 files changed

+1229
-584
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

69 files changed

+1229
-584
lines changed

assets/locale/en.json

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,9 @@
137137
"payment_on_receipt_of_goods": "Payment on receipt of goods",
138138
"online": "Online",
139139
"total": "Total",
140-
"total_price": "Total price",
141-
"total_delivery_fee": "Total delivery fee",
140+
"price_of_goods": "Price of goods",
141+
"delivery_fee": "Delivery fee",
142+
"coupon": "Coupon",
142143
"products": "products",
143144
"your_feedback": "Your feeback",
144145
"support": "Support",
@@ -150,5 +151,9 @@
150151
"message_for_you": "Message for you",
151152
"recent_search": "Recent search",
152153
"hot_keywords": "Hot keywords",
153-
"search_by_product_name": "Search by product name"
154+
"search_by_product_name": "Search by product name",
155+
"ship_to": "Ship to",
156+
"payment_success": "Payment Success",
157+
"detail_order": "Detail order",
158+
"my_orders": "My orders"
154159
}

assets/locale/vi.json

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,18 +112,23 @@
112112
"payment_on_receipt_of_goods": "Thanh toán khi nhận hàng",
113113
"online": "Trực tuyến",
114114
"total": "Tổng ",
115-
"total_price": "Tổng giá sản phẩm",
116-
"total_delivery_fee": "Tổng chi phí vận chuyển",
115+
"price_of_goods": "Tổng giá sản phẩm",
116+
"delivery_fee": "Phí vận chuyển",
117+
"coupon": "Phiếu mua hàng",
117118
"products": "sản phẩm",
118119
"your_feedback": "Phản hồi của bạn",
119120
"support": "Support",
120121
"created": "Được tạo",
121-
"check_out": "Thủ tục thanh toán",
122+
"check_out": "Thanh toán",
122123
"continue": "Tiếp tục",
123124
"don't_match_password": "Mật khẩu không khớp",
124125
"login_slogan": "Đăng nhập bằng email và mật khẩu",
125126
"message_for_you": "Tin nhắn cho bạn",
126127
"recent_search": "Tìm kiếm gần đây",
127128
"hot_keywords": "Từ khóa đang hot",
128-
"search_by_product_name": "Tìm kiếm theo tên sản phẩm"
129+
"search_by_product_name": "Tìm kiếm theo tên sản phẩm",
130+
"ship_to": "Chuyển tới",
131+
"payment_success": "Thanh toán thành công",
132+
"detail_order": "Chi tiết đơn hàng",
133+
"my_orders": "Đơn hàng của tôi"
129134
}

lib/app_view.dart

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import 'package:e_commerce_app/configs/application.dart';
2-
import 'package:firebase_auth/firebase_auth.dart';
32
import 'package:flutter_localizations/flutter_localizations.dart';
43
import 'package:flutter/material.dart';
54
import 'package:flutter_bloc/flutter_bloc.dart';
@@ -34,6 +33,16 @@ class _AppViewState extends State<AppView> {
3433
super.dispose();
3534
}
3635

36+
void _onNavigate(String route) {
37+
_navigator!.pushNamedAndRemoveUntil(route, (route) => false);
38+
}
39+
40+
void _loadData() {
41+
// Only load data when authenticated
42+
BlocProvider.of<ProfileBloc>(context).add(LoadProfile());
43+
BlocProvider.of<CartBloc>(context).add(LoadCart());
44+
}
45+
3746
@override
3847
Widget build(BuildContext context) {
3948
return BlocBuilder<LanguageBloc, LanguageState>(
@@ -61,8 +70,7 @@ class _AppViewState extends State<AppView> {
6170
} else if (state is Unauthenticated) {
6271
_onNavigate(AppRouter.LOGIN);
6372
} else if (state is Authenticated) {
64-
_loadUserProfile(state.loggedFirebaseUser);
65-
_loadCart(state.loggedFirebaseUser);
73+
_loadData();
6674
_onNavigate(AppRouter.LOGIN_SUCCESS);
6775
} else {
6876
_onNavigate(AppRouter.SPLASH);
@@ -75,16 +83,4 @@ class _AppViewState extends State<AppView> {
7583
},
7684
);
7785
}
78-
79-
void _onNavigate(String route) {
80-
_navigator!.pushNamedAndRemoveUntil(route, (route) => false);
81-
}
82-
83-
void _loadUserProfile(User loggedFirebaseUser) {
84-
BlocProvider.of<ProfileBloc>(context).add(LoadProfile(loggedFirebaseUser));
85-
}
86-
87-
void _loadCart(User loggedFirebaseUser) {
88-
BlocProvider.of<CartBloc>(context).add(LoadCart(loggedFirebaseUser));
89-
}
9086
}

lib/business_logic/common_blocs/cart/cart_bloc.dart

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@ import 'dart:async';
22

33
import 'package:e_commerce_app/business_logic/common_blocs/cart/bloc.dart';
44
import 'package:e_commerce_app/business_logic/repository/app_repository.dart';
5+
import 'package:e_commerce_app/business_logic/repository/auth_repository/auth_repo.dart';
56
import 'package:e_commerce_app/business_logic/repository/cart_repository/cart_repo.dart';
7+
import 'package:e_commerce_app/business_logic/repository/product_repository/product_repo.dart';
68
import 'package:firebase_auth/firebase_auth.dart';
79
import 'package:flutter_bloc/flutter_bloc.dart';
810

911
class CartBloc extends Bloc<CartEvent, CartState> {
12+
final AuthRepository _authRepository = AppRepository.authRepository;
1013
final CartRepository _cartRepository = AppRepository.cartRepository;
14+
final ProductRepository _productRepository = AppRepository.productRepository;
1115
late User _loggedFirebaseUser;
1216
StreamSubscription? _cartStreamSub;
1317

@@ -32,12 +36,11 @@ class CartBloc extends Bloc<CartEvent, CartState> {
3236

3337
Stream<CartState> _mapLoadCartToState(LoadCart event) async* {
3438
try {
35-
_loggedFirebaseUser = event.loggedFirebaseUser;
3639
_cartStreamSub?.cancel();
37-
_cartStreamSub =
38-
_cartRepository.cartStream(_loggedFirebaseUser.uid).listen(
39-
(cart) => add(CartUpdated(cart)),
40-
);
40+
_loggedFirebaseUser = _authRepository.loggedFirebaseUser;
41+
_cartStreamSub = _cartRepository
42+
.cartStream(_loggedFirebaseUser.uid)
43+
.listen((cart) => add(CartUpdated(cart)));
4144
} catch (e) {
4245
yield CartLoadFailure(e.toString());
4346
}
@@ -83,9 +86,26 @@ class CartBloc extends Bloc<CartEvent, CartState> {
8386
}
8487

8588
Stream<CartState> _mapCartUpdatedToState(CartUpdated event) async* {
86-
var totalCartPrice = 0;
87-
event.cart.forEach((c) => totalCartPrice += c.price);
88-
yield CartLoaded(event.cart, totalCartPrice);
89+
yield CartLoading();
90+
91+
var updatedCart = event.updatedCart;
92+
var priceOfGoods = 0;
93+
for (var i = 0; i < updatedCart.length; i++) {
94+
priceOfGoods += updatedCart[i].price;
95+
// Get product by id that is provided from cart item
96+
try {
97+
var productInfo =
98+
await _productRepository.getProductById(updatedCart[i].productId);
99+
updatedCart[i] = updatedCart[i].cloneWith(productInfo: productInfo);
100+
} catch (e) {
101+
yield CartLoadFailure(e.toString());
102+
}
103+
}
104+
105+
yield CartLoaded(
106+
cart: updatedCart,
107+
priceOfGoods: priceOfGoods,
108+
);
89109
}
90110

91111
@override

lib/business_logic/common_blocs/cart/cart_event.dart

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import 'package:e_commerce_app/business_logic/entities/cart_item.dart';
22
import 'package:equatable/equatable.dart';
3-
import 'package:firebase_auth/firebase_auth.dart';
43

54
abstract class CartEvent extends Equatable {
65
const CartEvent();
@@ -10,25 +9,19 @@ abstract class CartEvent extends Equatable {
109
}
1110

1211
/// When open cart screen -> load cart event
13-
class LoadCart extends CartEvent {
14-
final User loggedFirebaseUser;
15-
16-
LoadCart(this.loggedFirebaseUser);
17-
18-
List<Object> get props => [loggedFirebaseUser];
19-
}
12+
class LoadCart extends CartEvent {}
2013

2114
/// When user clicks to a clear cart => clear cart event
2215
class ClearCart extends CartEvent {}
2316

2417
/// Cart was cleared
2518
class CartUpdated extends CartEvent {
26-
final List<CartItem> cart;
19+
final List<CartItem> updatedCart;
2720

28-
CartUpdated(this.cart);
21+
CartUpdated(this.updatedCart);
2922

3023
@override
31-
List<Object> get props => [cart];
24+
List<Object> get props => [updatedCart];
3225
}
3326

3427
/// When user clicks to add button => add cart item event

lib/business_logic/common_blocs/cart/cart_state.dart

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,16 @@ class CartLoading extends CartState {}
1414
/// Cart was loaded
1515
class CartLoaded extends CartState {
1616
final List<CartItem> cart;
17-
final int totalCartPrice;
17+
final int priceOfGoods;
1818

19-
CartLoaded(this.cart, this.totalCartPrice);
19+
20+
CartLoaded({
21+
required this.cart,
22+
required this.priceOfGoods,
23+
});
2024

2125
@override
22-
List<Object> get props => [cart, totalCartPrice];
26+
List<Object> get props => [cart, priceOfGoods];
2327
}
2428

2529
/// Cart wasn't loaded

lib/business_logic/common_blocs/common_bloc.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import 'package:e_commerce_app/business_logic/common_blocs/application/bloc.dart
22
import 'package:e_commerce_app/business_logic/common_blocs/auth/bloc.dart';
33
import 'package:e_commerce_app/business_logic/common_blocs/cart/bloc.dart';
44
import 'package:e_commerce_app/business_logic/common_blocs/language/bloc.dart';
5+
import 'package:e_commerce_app/business_logic/common_blocs/order/bloc.dart';
56
import 'package:e_commerce_app/business_logic/common_blocs/profile/bloc.dart';
67
import 'package:flutter_bloc/flutter_bloc.dart';
78

@@ -11,6 +12,7 @@ class CommonBloc {
1112
static final authencationBloc = AuthenticationBloc();
1213
static final languageBloc = LanguageBloc();
1314
static final cartBloc = CartBloc();
15+
static final orderBloc = OrderBloc();
1416
static final profileBloc = ProfileBloc();
1517

1618
static final List<BlocProvider> blocProviders = [
@@ -29,13 +31,17 @@ class CommonBloc {
2931
BlocProvider<CartBloc>(
3032
create: (context) => cartBloc,
3133
),
34+
BlocProvider<OrderBloc>(
35+
create: (context) => orderBloc,
36+
),
3237
];
3338

3439
/// Dispose
3540
static void dispose() {
3641
applicationBloc.close();
3742
authencationBloc.close();
3843
cartBloc.close();
44+
orderBloc.close();
3945
profileBloc.close();
4046
languageBloc.close();
4147
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export 'order_bloc.dart';
2+
export 'order_event.dart';
3+
export 'order_state.dart';
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import 'package:e_commerce_app/business_logic/common_blocs/order/bloc.dart';
2+
import 'package:e_commerce_app/business_logic/entities/entites.dart';
3+
import 'package:e_commerce_app/business_logic/repository/repository.dart';
4+
import 'package:flutter_bloc/flutter_bloc.dart';
5+
6+
class OrderBloc extends Bloc<OrderEvent, OrderState> {
7+
OrderRepository _orderRepository = AppRepository.orderRepository;
8+
AuthRepository _authRepository = AppRepository.authRepository;
9+
OrderBloc() : super(MyOrdersLoading());
10+
11+
@override
12+
Stream<OrderState> mapEventToState(OrderEvent event) async* {
13+
if (event is LoadMyOrders) {
14+
yield* _mapLoadMyOrdersToState(event);
15+
} else if (event is AddOrder) {
16+
yield* _mapAddOrderToState(event);
17+
}
18+
}
19+
20+
Stream<OrderState> _mapLoadMyOrdersToState(LoadMyOrders event) async* {
21+
try {
22+
var loggedFirebaseUser = _authRepository.loggedFirebaseUser;
23+
List<Order> orders =
24+
await _orderRepository.getOrders(loggedFirebaseUser.uid);
25+
yield MyOrdersLoaded(myOrders: orders);
26+
} catch (e) {
27+
yield MyOrdersLoadFailure(e.toString());
28+
}
29+
}
30+
31+
Stream<OrderState> _mapAddOrderToState(AddOrder event) async* {
32+
try {
33+
var newOrder =
34+
event.newOrder.cloneWith(uid: _authRepository.loggedFirebaseUser.uid);
35+
await _orderRepository.addOrder(newOrder);
36+
} catch (e) {
37+
print(e.toString());
38+
}
39+
}
40+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import 'package:e_commerce_app/business_logic/entities/entites.dart';
2+
import 'package:equatable/equatable.dart';
3+
import 'package:firebase_auth/firebase_auth.dart';
4+
5+
abstract class OrderEvent extends Equatable {
6+
const OrderEvent();
7+
8+
@override
9+
List<Object> get props => [];
10+
}
11+
12+
class LoadMyOrders extends OrderEvent {
13+
final User loggedFirebaseUser;
14+
15+
LoadMyOrders(this.loggedFirebaseUser);
16+
17+
List<Object> get props => [loggedFirebaseUser];
18+
}
19+
20+
class AddOrder extends OrderEvent {
21+
final Order newOrder;
22+
23+
AddOrder(this.newOrder);
24+
25+
List<Object> get props => [newOrder];
26+
}

0 commit comments

Comments
 (0)