Skip to content

Commit 248b9ee

Browse files
Update: Notfication view
1 parent 90aa82d commit 248b9ee

File tree

11 files changed

+116
-21
lines changed

11 files changed

+116
-21
lines changed

lib/core/theme/app_theme.dart

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ class AppTheme {
1010
visualDensity: VisualDensity.adaptivePlatformDensity,
1111
scaffoldBackgroundColor: kBackgroundColor,
1212
appBarTheme: AppBarTheme(
13-
backgroundColor: Colors.white,
14-
foregroundColor: kLightSecondaryColor,
15-
elevation: 0,
13+
backgroundColor: kLightPrimaryColor,
14+
foregroundColor: kBackgroundColor,
15+
// elevation: 0,
1616
),
1717
textButtonTheme: TextButtonThemeData(
1818
style: TextButton.styleFrom(foregroundColor: kLightSecondaryColor)),

lib/data/data_sources/local/cart_local_data_source.dart

+21-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ abstract class CartLocalDataSource {
77
Future<List<CartItemModel>> getCart();
88
Future<void> saveCart(List<CartItemModel> cart);
99
Future<void> saveCartItem(CartItemModel cartItem);
10-
Future<bool> clearCart();
10+
Future<bool> deleteCartItem(CartItemModel cartItem);
11+
Future<bool> deleteCart();
1112
}
1213

1314
const cachedCart = 'CACHED_CART';
@@ -53,7 +54,25 @@ class CartLocalDataSourceImpl implements CartLocalDataSource {
5354
}
5455

5556
@override
56-
Future<bool> clearCart()async {
57+
Future<bool> deleteCart() async {
5758
return sharedPreferences.remove(cachedCart);
5859
}
60+
61+
@override
62+
Future<bool> deleteCartItem(CartItemModel cartItem) async {
63+
final jsonString = sharedPreferences.getString(cachedCart);
64+
if (jsonString != null) {
65+
final List<CartItemModel> cart =
66+
cartItemModelListFromLocalJson(jsonString);
67+
cart.removeWhere((element) =>
68+
element.product.id == cartItem.product.id &&
69+
element.priceTag.id == cartItem.priceTag.id);
70+
return sharedPreferences.setString(
71+
cachedCart,
72+
cartItemModelToJson(cart),
73+
);
74+
} else {
75+
throw CacheFailure();
76+
}
77+
}
5978
}

lib/data/data_sources/remote/cart_remote_data_source.dart

+15
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import 'dart:convert';
22

3+
import 'package:eshop/core/usecases/usecase.dart';
34
import 'package:http/http.dart' as http;
45

56
import '../../../../core/error/exceptions.dart';
@@ -9,6 +10,8 @@ import '../../models/cart/cart_item_model.dart';
910
abstract class CartRemoteDataSource {
1011
Future<CartItemModel> addToCart(CartItemModel cartItem, String token);
1112
Future<List<CartItemModel>> syncCart(List<CartItemModel> cart, String token);
13+
Future<CartItemModel> deleteCartItem(CartItemModel cart, String token);
14+
Future<NoParams> deleteCart(String token);
1215
}
1316

1417
class CartRemoteDataSourceSourceImpl implements CartRemoteDataSource {
@@ -54,4 +57,16 @@ class CartRemoteDataSourceSourceImpl implements CartRemoteDataSource {
5457
throw ServerException();
5558
}
5659
}
60+
61+
@override
62+
Future<CartItemModel> deleteCartItem(CartItemModel cart, String token) async {
63+
// TODO: implement deleteCartItem
64+
throw UnimplementedError();
65+
}
66+
67+
@override
68+
Future<NoParams> deleteCart(String token) async {
69+
// TODO: implement deleteCart
70+
throw UnimplementedError();
71+
}
5772
}

lib/data/repositories/cart_repository_impl.dart

+36-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import 'package:dartz/dartz.dart';
2+
import 'package:eshop/core/usecases/usecase.dart';
23

34
import '../../../../core/error/failures.dart';
45
import '../../core/network/network_info.dart';
@@ -85,17 +86,44 @@ class CartRepositoryImpl implements CartRepository {
8586
}
8687

8788
@override
88-
Future<Either<Failure, bool>> deleteCartItem(CartItem params) {
89-
throw UnimplementedError();
89+
Future<Either<Failure, CartItemModel>> deleteCartItem(CartItem params) async {
90+
final token = await userLocalDataSource.getToken();
91+
final cartItem = CartItemModel.fromParent(params);
92+
// Check if the token is empty or if the network is not connected
93+
// If so, delete the cart item locally and return true
94+
// Otherwise, proceed with the remote data source
95+
// and delete the cart item locally after receiving the response
96+
if (token.isEmpty || !await networkInfo.isConnected) {
97+
localDataSource.deleteCartItem(cartItem);
98+
return Right(cartItem);
99+
}
100+
101+
// Proceed with the remote data source
102+
// and delete the cart item locally after receiving the response
103+
final remoteResponse = await remoteDataSource.deleteCartItem(
104+
cartItem,
105+
token,
106+
);
107+
await localDataSource.deleteCartItem(remoteResponse);
108+
return Right(remoteResponse);
90109
}
91110

92111
@override
93-
Future<Either<Failure, bool>> deleteCart() async {
94-
bool result = await localDataSource.clearCart();
95-
if (result) {
96-
return Right(result);
97-
} else {
98-
return Left(CacheFailure());
112+
Future<Either<Failure, NoParams>> deleteCart() async {
113+
final token = await userLocalDataSource.getToken();
114+
// Check if the token is empty or if the network is not connected
115+
// If so, delete the cart locally and return true
116+
// Otherwise, proceed with the remote data source
117+
// and delete the cart locally after receiving the response
118+
if (token.isEmpty || !await networkInfo.isConnected) {
119+
localDataSource.deleteCart();
120+
return Right(NoParams());
99121
}
122+
123+
// Proceed with the remote data source
124+
// and delete the cart locally after receiving the response
125+
final remoteResponse = await remoteDataSource.deleteCart(token);
126+
await localDataSource.deleteCart();
127+
return Right(remoteResponse);
100128
}
101129
}
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import 'package:dartz/dartz.dart';
2+
import 'package:eshop/core/usecases/usecase.dart';
23

34
import '../../../../core/error/failures.dart';
5+
import '../../data/models/cart/cart_item_model.dart';
46
import '../entities/cart/cart_item.dart';
57

68
abstract class CartRepository {
79
Future<Either<Failure, List<CartItem>>> getLocalCartItems();
810
Future<Either<Failure, List<CartItem>>> getRemoteCartItems();
911
Future<Either<Failure, CartItem>> addCartItem(CartItem params);
10-
Future<Either<Failure, bool>> deleteCartItem(CartItem params);
11-
Future<Either<Failure, bool>> deleteCart();
12+
Future<Either<Failure, CartItem>> deleteCartItem(CartItem params);
13+
Future<Either<Failure, NoParams>> deleteCart();
1214
}

lib/domain/usecases/cart/delete_cart_usecase.dart

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ import '../../../../../core/error/failures.dart';
44
import '../../../../../core/usecases/usecase.dart';
55
import '../../repositories/cart_repository.dart';
66

7-
class DeleteCartUseCase implements UseCase<bool, NoParams> {
7+
class DeleteCartUseCase implements UseCase<NoParams, NoParams> {
88
final CartRepository repository;
99
DeleteCartUseCase(this.repository);
1010

1111
@override
12-
Future<Either<Failure, bool>> call(NoParams params) async {
12+
Future<Either<Failure, NoParams>> call(NoParams params) async {
1313
return await repository.deleteCart();
1414
}
1515
}

lib/presentation/views/main/other/notification/notification_view.dart

+8
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import 'package:eshop/presentation/views/main/other/notification/widgets/notification_card.dart';
12
import 'package:flutter/material.dart';
23

34
class NotificationView extends StatelessWidget {
@@ -9,6 +10,13 @@ class NotificationView extends StatelessWidget {
910
appBar: AppBar(
1011
title: const Text("Notifications"),
1112
),
13+
body: ListView.builder(
14+
itemCount: 10,
15+
padding: const EdgeInsets.all(16),
16+
itemBuilder: (context, index) {
17+
return NotificationCard();
18+
},
19+
),
1220
);
1321
}
1422
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import 'package:flutter/material.dart';
2+
3+
class NotificationCard extends StatelessWidget {
4+
const NotificationCard({super.key});
5+
6+
@override
7+
Widget build(BuildContext context) {
8+
return Card(
9+
shape: RoundedRectangleBorder(
10+
borderRadius: BorderRadius.circular(10),
11+
),
12+
child: ListTile(
13+
leading: const Icon(Icons.notifications),
14+
title: const Text("Notification Title"),
15+
subtitle: const Text("This is the notification message."),
16+
trailing: const Icon(Icons.arrow_forward_ios),
17+
onTap: () {
18+
// Handle tap
19+
},
20+
),
21+
);
22+
}
23+
}

test/data/datasources/local/cart_local_data_source_test.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ void main() {
8585
.thenAnswer((_) async => true);
8686

8787
/// Act
88-
final result = await dataSource.clearCart();
88+
final result = await dataSource.deleteCart();
8989

9090
/// Assert
9191
expect(result, isTrue);

test/domain/usecases/cart/delete_cart_usecase_test.dart

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@ void main() {
2222
() async {
2323
/// Arrange
2424
when(() => mockProductRepository.deleteCart())
25-
.thenAnswer((_) async => const Right(true));
25+
.thenAnswer((_) async => Right(NoParams()));
2626

2727
/// Act
2828
final result = await usecase(NoParams());
2929

3030
/// Assert
31-
expect(result, const Right(true));
31+
expect(result, Right(NoParams()));
3232
verify(() => mockProductRepository.deleteCart());
3333
verifyNoMoreInteractions(mockProductRepository);
3434
},

test/presentation/blocs/cart/cart_bloc_test.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ void main() {
8282
'emits [CartLoading, CartLoaded] when ClearCart is added',
8383
build: () {
8484
when(() => mockClearCartUseCase(NoParams()))
85-
.thenAnswer((_) async => const Right(true));
85+
.thenAnswer((_) async => Right(NoParams()));
8686
return cartBloc;
8787
},
8888
act: (bloc) => bloc.add(const ClearCart()),

0 commit comments

Comments
 (0)