favorite
This commit is contained in:
@@ -48,6 +48,9 @@ class HiveBoxNames {
|
||||
/// Address book
|
||||
static const String addressBox = 'address_box';
|
||||
|
||||
/// Favorite products
|
||||
static const String favoriteBox = 'favorite_box';
|
||||
|
||||
/// Offline request queue for failed API calls
|
||||
static const String offlineQueueBox = 'offline_queue_box';
|
||||
|
||||
@@ -65,6 +68,7 @@ class HiveBoxNames {
|
||||
syncStateBox,
|
||||
notificationBox,
|
||||
addressBox,
|
||||
favoriteBox,
|
||||
offlineQueueBox,
|
||||
];
|
||||
}
|
||||
@@ -115,6 +119,7 @@ class HiveTypeIds {
|
||||
static const int memberCardModel = 25;
|
||||
static const int promotionModel = 26;
|
||||
static const int categoryModel = 27;
|
||||
static const int favoriteModel = 28;
|
||||
|
||||
// Enums (30-59)
|
||||
static const int userRole = 30;
|
||||
|
||||
@@ -143,6 +143,9 @@ class HiveService {
|
||||
|
||||
// Notification box (non-sensitive)
|
||||
Hive.openBox<dynamic>(HiveBoxNames.notificationBox),
|
||||
|
||||
// Favorites box (non-sensitive)
|
||||
Hive.openBox<dynamic>(HiveBoxNames.favoriteBox),
|
||||
]);
|
||||
|
||||
// Open potentially encrypted boxes (sensitive data)
|
||||
|
||||
@@ -7,6 +7,7 @@ library;
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
import 'package:worker/features/cart/presentation/pages/cart_page.dart';
|
||||
import 'package:worker/features/favorites/presentation/pages/favorites_page.dart';
|
||||
import 'package:worker/features/main/presentation/pages/main_scaffold.dart';
|
||||
import 'package:worker/features/products/presentation/pages/product_detail_page.dart';
|
||||
import 'package:worker/features/products/presentation/pages/products_page.dart';
|
||||
@@ -84,6 +85,16 @@ class AppRouter {
|
||||
),
|
||||
),
|
||||
|
||||
// Favorites Route
|
||||
GoRoute(
|
||||
path: RouteNames.favorites,
|
||||
name: RouteNames.favorites,
|
||||
pageBuilder: (context, state) => MaterialPage(
|
||||
key: state.pageKey,
|
||||
child: const FavoritesPage(),
|
||||
),
|
||||
),
|
||||
|
||||
// TODO: Add more routes as features are implemented
|
||||
],
|
||||
|
||||
@@ -165,6 +176,7 @@ class RouteNames {
|
||||
static const String products = '/products';
|
||||
static const String productDetail = '/products/:id';
|
||||
static const String cart = '/cart';
|
||||
static const String favorites = '/favorites';
|
||||
static const String checkout = '/checkout';
|
||||
static const String orderSuccess = '/order-success';
|
||||
|
||||
@@ -223,6 +235,9 @@ extension GoRouterExtension on BuildContext {
|
||||
/// Navigate to cart page
|
||||
void goCart() => go(RouteNames.cart);
|
||||
|
||||
/// Navigate to favorites page
|
||||
void goFavorites() => go(RouteNames.favorites);
|
||||
|
||||
/// Navigate to loyalty page
|
||||
void goLoyalty() => go(RouteNames.loyalty);
|
||||
|
||||
|
||||
423
lib/features/favorites/INTEGRATION_SUMMARY.md
Normal file
423
lib/features/favorites/INTEGRATION_SUMMARY.md
Normal file
@@ -0,0 +1,423 @@
|
||||
# Favorites Feature - Integration Summary
|
||||
|
||||
## Overview
|
||||
A complete favorites state management system for the Worker app using Riverpod 3.0 with code generation and Hive for local persistence.
|
||||
|
||||
---
|
||||
|
||||
## Files Created
|
||||
|
||||
### 1. Data Layer
|
||||
**Location**: `/lib/features/favorites/data/datasources/`
|
||||
|
||||
#### `favorites_local_datasource.dart`
|
||||
- **Purpose**: Handles all Hive database operations for favorites
|
||||
- **Key Methods**:
|
||||
- `getAllFavorites(userId)` - Get all favorites for a user
|
||||
- `addFavorite(favorite)` - Add a new favorite
|
||||
- `removeFavorite(productId, userId)` - Remove a favorite
|
||||
- `isFavorite(productId, userId)` - Check favorite status
|
||||
- `clearFavorites(userId)` - Clear all favorites for a user
|
||||
- `getFavoriteCount(userId)` - Get count of favorites
|
||||
- `compact()` - Optimize database size
|
||||
|
||||
- **Features**:
|
||||
- Multi-user support (filters by userId)
|
||||
- Error handling with try-catch
|
||||
- Debug logging
|
||||
- Sorted by creation date (newest first)
|
||||
|
||||
---
|
||||
|
||||
### 2. Presentation Layer
|
||||
**Location**: `/lib/features/favorites/presentation/providers/`
|
||||
|
||||
#### `favorites_provider.dart`
|
||||
- **Purpose**: Riverpod 3.0 state management with code generation
|
||||
- **Generated File**: `favorites_provider.g.dart` (auto-generated by build_runner)
|
||||
|
||||
**Providers Created**:
|
||||
|
||||
1. **`favoritesLocalDataSourceProvider`**
|
||||
- Type: `Provider<FavoritesLocalDataSource>`
|
||||
- Purpose: Dependency injection for datasource
|
||||
- Auto-dispose: Yes
|
||||
|
||||
2. **`currentUserIdProvider`**
|
||||
- Type: `Provider<String>`
|
||||
- Purpose: Provides current logged-in user ID
|
||||
- Current Value: `'user_001'` (hardcoded for development)
|
||||
- **TODO**: Replace with actual auth provider integration
|
||||
- Auto-dispose: Yes
|
||||
|
||||
3. **`favoritesProvider`** (Main Provider)
|
||||
- Type: `AsyncNotifier<Set<String>>`
|
||||
- Purpose: Manages favorites state (Set of product IDs)
|
||||
- Auto-dispose: Yes
|
||||
- **Methods**:
|
||||
- `addFavorite(productId)` - Add product to favorites
|
||||
- `removeFavorite(productId)` - Remove product from favorites
|
||||
- `toggleFavorite(productId)` - Toggle favorite status
|
||||
- `refresh()` - Reload from database
|
||||
- `clearAll()` - Remove all favorites
|
||||
|
||||
4. **`isFavoriteProvider(productId)`** (Family Provider)
|
||||
- Type: `Provider<bool>`
|
||||
- Purpose: Check if specific product is favorited
|
||||
- Returns: `true` if favorited, `false` otherwise
|
||||
- Safe during loading/error states (returns `false`)
|
||||
- Auto-dispose: Yes
|
||||
|
||||
5. **`favoriteCountProvider`**
|
||||
- Type: `Provider<int>`
|
||||
- Purpose: Get total count of favorites
|
||||
- Returns: Number of favorites (0 if loading/error)
|
||||
- Auto-dispose: Yes
|
||||
|
||||
6. **`favoriteProductIdsProvider`**
|
||||
- Type: `Provider<List<String>>`
|
||||
- Purpose: Get all favorite product IDs as a list
|
||||
- Returns: List of product IDs (empty if loading/error)
|
||||
- Auto-dispose: Yes
|
||||
|
||||
---
|
||||
|
||||
## State Management Architecture
|
||||
|
||||
### State Flow
|
||||
```
|
||||
User Action (Add/Remove Favorite)
|
||||
↓
|
||||
favoritesProvider.notifier.addFavorite(productId)
|
||||
↓
|
||||
Update Hive Database (FavoritesLocalDataSource)
|
||||
↓
|
||||
Update In-Memory State (Set<String>)
|
||||
↓
|
||||
Notify Listeners (UI Rebuilds)
|
||||
```
|
||||
|
||||
### Data Persistence
|
||||
- **Primary Storage**: Hive (local database)
|
||||
- **Box Name**: `favorite_box` (from HiveBoxNames.favoriteBox)
|
||||
- **Model**: `FavoriteModel` (Hive TypeId: 28)
|
||||
- **Format**:
|
||||
```dart
|
||||
FavoriteModel(
|
||||
favoriteId: "user_001_product_123_1234567890",
|
||||
productId: "product_123",
|
||||
userId: "user_001",
|
||||
createdAt: DateTime.now(),
|
||||
)
|
||||
```
|
||||
|
||||
### State Type
|
||||
- **Type**: `Set<String>` (Product IDs)
|
||||
- **Reason**: Set provides O(1) lookup for `.contains()` checks
|
||||
- **Alternative**: Could use `List<String>` but Set is more efficient
|
||||
|
||||
---
|
||||
|
||||
## Integration Points
|
||||
|
||||
### ✅ Already Integrated
|
||||
1. **Hive Database**
|
||||
- Uses existing `HiveBoxNames.favoriteBox`
|
||||
- Uses existing `HiveTypeIds.favoriteModel` (28)
|
||||
- FavoriteModel already has generated adapter
|
||||
|
||||
2. **Domain Layer**
|
||||
- `Favorite` entity: `/lib/features/favorites/domain/entities/favorite.dart`
|
||||
- `FavoriteModel`: `/lib/features/favorites/data/models/favorite_model.dart`
|
||||
|
||||
### ⚠️ TODO: Authentication Integration
|
||||
Currently using hardcoded userId (`'user_001'`). To integrate with auth:
|
||||
|
||||
1. **Locate Auth Provider** (when available)
|
||||
2. **Update `currentUserIdProvider`**:
|
||||
```dart
|
||||
@riverpod
|
||||
String currentUserId(Ref ref) {
|
||||
final authState = ref.watch(authProvider);
|
||||
return authState.user?.id ?? 'guest';
|
||||
}
|
||||
```
|
||||
|
||||
3. **Handle Guest Users**:
|
||||
- Decide: Should guests have favorites?
|
||||
- If yes, use device-specific ID
|
||||
- If no, show login prompt when favoriting
|
||||
|
||||
---
|
||||
|
||||
## Usage in Widgets
|
||||
|
||||
### Example 1: Simple Favorite Button
|
||||
```dart
|
||||
class FavoriteButton extends ConsumerWidget {
|
||||
final String productId;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final isFavorited = ref.watch(isFavoriteProvider(productId));
|
||||
|
||||
return IconButton(
|
||||
icon: Icon(isFavorited ? Icons.favorite : Icons.favorite_border),
|
||||
onPressed: () {
|
||||
ref.read(favoritesProvider.notifier).toggleFavorite(productId);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Example 2: Favorites Count Badge
|
||||
```dart
|
||||
class FavoritesBadge extends ConsumerWidget {
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final count = ref.watch(favoriteCountProvider);
|
||||
|
||||
return Badge(
|
||||
label: Text('$count'),
|
||||
child: Icon(Icons.favorite),
|
||||
);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Example 3: Display All Favorites
|
||||
```dart
|
||||
class FavoritesPage extends ConsumerWidget {
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final favoritesAsync = ref.watch(favoritesProvider);
|
||||
|
||||
return favoritesAsync.when(
|
||||
data: (favorites) => ListView.builder(
|
||||
itemCount: favorites.length,
|
||||
itemBuilder: (context, index) {
|
||||
final productId = favorites.elementAt(index);
|
||||
return ProductCard(productId: productId);
|
||||
},
|
||||
),
|
||||
loading: () => CircularProgressIndicator(),
|
||||
error: (error, stack) => Text('Error: $error'),
|
||||
);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
**See `USAGE_EXAMPLES.md` for comprehensive examples.**
|
||||
|
||||
---
|
||||
|
||||
## Testing
|
||||
|
||||
### Unit Test Example
|
||||
```dart
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
|
||||
void main() {
|
||||
test('should add and remove favorites', () async {
|
||||
final container = ProviderContainer();
|
||||
addTearDown(container.dispose);
|
||||
|
||||
// Add favorite
|
||||
await container.read(favoritesProvider.notifier)
|
||||
.addFavorite('product_1');
|
||||
|
||||
// Verify added
|
||||
final favorites = await container.read(favoritesProvider.future);
|
||||
expect(favorites, contains('product_1'));
|
||||
|
||||
// Remove favorite
|
||||
await container.read(favoritesProvider.notifier)
|
||||
.removeFavorite('product_1');
|
||||
|
||||
// Verify removed
|
||||
final updated = await container.read(favoritesProvider.future);
|
||||
expect(updated, isNot(contains('product_1')));
|
||||
});
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Error Handling
|
||||
|
||||
### State Management Errors
|
||||
- Wrapped in `AsyncValue.error()`
|
||||
- UI can handle with `.when()` method
|
||||
- Logged to console with debug messages
|
||||
|
||||
### Database Errors
|
||||
- Try-catch blocks in datasource methods
|
||||
- Rethrow for provider to handle
|
||||
- Graceful fallbacks (empty sets, false returns)
|
||||
|
||||
### Edge Cases Handled
|
||||
- ✅ Product already favorited (no-op)
|
||||
- ✅ Product not in favorites (no-op on remove)
|
||||
- ✅ Empty favorites list
|
||||
- ✅ Hive box not initialized
|
||||
- ✅ Multi-user support (filters by userId)
|
||||
|
||||
---
|
||||
|
||||
## Performance Considerations
|
||||
|
||||
### Optimizations
|
||||
1. **Set vs List**: Using `Set<String>` for O(1) lookup
|
||||
2. **Auto-dispose**: All providers auto-dispose when not in use
|
||||
3. **Selective watching**: `isFavoriteProvider` rebuilds only affected widgets
|
||||
4. **Database compaction**: Available via `compact()` method
|
||||
5. **Sorted by date**: Favorites returned in newest-first order
|
||||
|
||||
### Potential Improvements
|
||||
1. **Batch operations**: Add `addMultipleFavorites()` method
|
||||
2. **Caching**: Add in-memory cache layer
|
||||
3. **Pagination**: For users with many favorites
|
||||
4. **Sync**: Add remote API sync capability
|
||||
|
||||
---
|
||||
|
||||
## Debugging
|
||||
|
||||
### Console Logs
|
||||
All operations log to console with prefix `[FavoritesProvider]` or `[FavoritesLocalDataSource]`:
|
||||
- "Added favorite: product_123"
|
||||
- "Removed favorite: product_123 for user: user_001"
|
||||
- "Loaded 5 favorites for user: user_001"
|
||||
- "Error adding favorite: ..."
|
||||
|
||||
### Check State
|
||||
```dart
|
||||
// In widget
|
||||
final favorites = ref.read(favoritesProvider);
|
||||
print('Favorites state: $favorites');
|
||||
|
||||
// Check if favorited
|
||||
final isFav = ref.read(isFavoriteProvider('product_123'));
|
||||
print('Is favorited: $isFav');
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Known Limitations
|
||||
|
||||
1. **No Auth Integration Yet**: Using hardcoded userId
|
||||
2. **No Remote Sync**: Only local storage (Hive)
|
||||
3. **No Offline Queue**: Changes not synced to backend
|
||||
4. **Single Device**: No cross-device synchronization
|
||||
5. **No Favorites Limit**: Could grow unbounded
|
||||
|
||||
---
|
||||
|
||||
## Next Steps
|
||||
|
||||
### Immediate Tasks
|
||||
1. ✅ ~~Create providers~~ (Complete)
|
||||
2. ✅ ~~Create datasource~~ (Complete)
|
||||
3. ✅ ~~Generate code~~ (Complete)
|
||||
4. ✅ ~~Write documentation~~ (Complete)
|
||||
|
||||
### Future Enhancements
|
||||
1. **Auth Integration**
|
||||
- Replace `currentUserIdProvider` with real auth
|
||||
- Handle login/logout (clear favorites on logout?)
|
||||
|
||||
2. **Remote API**
|
||||
- Sync favorites to backend
|
||||
- Handle offline changes
|
||||
- Implement optimistic updates
|
||||
|
||||
3. **UI Components**
|
||||
- Create reusable `FavoriteButton` widget
|
||||
- Create `FavoritesPage` screen
|
||||
- Add favorites filter to products page
|
||||
|
||||
4. **Analytics**
|
||||
- Track favorite actions
|
||||
- Analyze favorite patterns
|
||||
- Product recommendations based on favorites
|
||||
|
||||
5. **Features**
|
||||
- Share favorites list
|
||||
- Export favorites
|
||||
- Favorite collections/folders
|
||||
- Favorite products in cart
|
||||
|
||||
---
|
||||
|
||||
## File Structure
|
||||
```
|
||||
lib/features/favorites/
|
||||
├── data/
|
||||
│ ├── datasources/
|
||||
│ │ └── favorites_local_datasource.dart ← Created
|
||||
│ └── models/
|
||||
│ ├── favorite_model.dart ← Existing
|
||||
│ └── favorite_model.g.dart ← Generated
|
||||
├── domain/
|
||||
│ └── entities/
|
||||
│ └── favorite.dart ← Existing
|
||||
├── presentation/
|
||||
│ └── providers/
|
||||
│ ├── favorites_provider.dart ← Created
|
||||
│ └── favorites_provider.g.dart ← Generated
|
||||
├── USAGE_EXAMPLES.md ← Created
|
||||
└── INTEGRATION_SUMMARY.md ← This file
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Quick Start Checklist
|
||||
|
||||
To use favorites in your app:
|
||||
|
||||
- [x] 1. Ensure Hive box is opened in `main.dart`
|
||||
```dart
|
||||
await Hive.openBox<FavoriteModel>(HiveBoxNames.favoriteBox);
|
||||
```
|
||||
|
||||
- [ ] 2. Import provider in your widget
|
||||
```dart
|
||||
import 'package:worker/features/favorites/presentation/providers/favorites_provider.dart';
|
||||
```
|
||||
|
||||
- [ ] 3. Watch provider state
|
||||
```dart
|
||||
final isFavorited = ref.watch(isFavoriteProvider(productId));
|
||||
```
|
||||
|
||||
- [ ] 4. Trigger actions
|
||||
```dart
|
||||
ref.read(favoritesProvider.notifier).toggleFavorite(productId);
|
||||
```
|
||||
|
||||
- [ ] 5. Handle loading/error states
|
||||
```dart
|
||||
favoritesAsync.when(
|
||||
data: (favorites) => ...,
|
||||
loading: () => ...,
|
||||
error: (error, stack) => ...,
|
||||
);
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Support
|
||||
|
||||
For questions or issues:
|
||||
1. Check `USAGE_EXAMPLES.md` for code examples
|
||||
2. Review console logs for error messages
|
||||
3. Verify Hive box initialization
|
||||
4. Check that FavoriteModel adapter is registered
|
||||
|
||||
---
|
||||
|
||||
**Status**: ✅ Ready for integration
|
||||
**Version**: 1.0.0
|
||||
**Last Updated**: 2024-10-24
|
||||
284
lib/features/favorites/README.md
Normal file
284
lib/features/favorites/README.md
Normal file
@@ -0,0 +1,284 @@
|
||||
# Favorites Feature
|
||||
|
||||
A complete favorites state management system for the Worker app using **Riverpod 3.0** with code generation and **Hive** for local persistence.
|
||||
|
||||
---
|
||||
|
||||
## 📁 File Structure
|
||||
|
||||
```
|
||||
lib/features/favorites/
|
||||
├── data/
|
||||
│ ├── datasources/
|
||||
│ │ └── favorites_local_datasource.dart # Hive operations
|
||||
│ └── models/
|
||||
│ ├── favorite_model.dart # Hive model (TypeId: 28)
|
||||
│ └── favorite_model.g.dart # Generated adapter
|
||||
├── domain/
|
||||
│ └── entities/
|
||||
│ └── favorite.dart # Business entity
|
||||
├── presentation/
|
||||
│ └── providers/
|
||||
│ ├── favorites_provider.dart # Riverpod providers
|
||||
│ └── favorites_provider.g.dart # Generated providers
|
||||
├── INTEGRATION_SUMMARY.md # Complete documentation
|
||||
├── USAGE_EXAMPLES.md # Code examples
|
||||
└── README.md # This file
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 🚀 Quick Start
|
||||
|
||||
### 1. Import Provider
|
||||
```dart
|
||||
import 'package:worker/features/favorites/presentation/providers/favorites_provider.dart';
|
||||
```
|
||||
|
||||
### 2. Check if Product is Favorited
|
||||
```dart
|
||||
final isFavorited = ref.watch(isFavoriteProvider(productId));
|
||||
```
|
||||
|
||||
### 3. Toggle Favorite
|
||||
```dart
|
||||
ref.read(favoritesProvider.notifier).toggleFavorite(productId);
|
||||
```
|
||||
|
||||
### 4. Get Favorites Count
|
||||
```dart
|
||||
final count = ref.watch(favoriteCountProvider);
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 📦 Available Providers
|
||||
|
||||
| Provider | Type | Description |
|
||||
|----------|------|-------------|
|
||||
| `favoritesProvider` | `AsyncNotifier<Set<String>>` | Main favorites state |
|
||||
| `isFavoriteProvider(productId)` | `Provider<bool>` | Check if product is favorited |
|
||||
| `favoriteCountProvider` | `Provider<int>` | Total count of favorites |
|
||||
| `favoriteProductIdsProvider` | `Provider<List<String>>` | All favorite product IDs |
|
||||
| `favoritesLocalDataSourceProvider` | `Provider<DataSource>` | Database access |
|
||||
| `currentUserIdProvider` | `Provider<String>` | Current user ID (TODO: auth) |
|
||||
|
||||
---
|
||||
|
||||
## 🎯 Common Use Cases
|
||||
|
||||
### Favorite Button in Product Card
|
||||
```dart
|
||||
class ProductCard extends ConsumerWidget {
|
||||
final String productId;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final isFavorited = ref.watch(isFavoriteProvider(productId));
|
||||
|
||||
return IconButton(
|
||||
icon: Icon(
|
||||
isFavorited ? Icons.favorite : Icons.favorite_border,
|
||||
color: isFavorited ? Colors.red : Colors.grey,
|
||||
),
|
||||
onPressed: () {
|
||||
ref.read(favoritesProvider.notifier).toggleFavorite(productId);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Favorites Count Badge
|
||||
```dart
|
||||
class FavoriteBadge extends ConsumerWidget {
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final count = ref.watch(favoriteCountProvider);
|
||||
|
||||
return Badge(
|
||||
label: Text('$count'),
|
||||
child: Icon(Icons.favorite),
|
||||
);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Display All Favorites
|
||||
```dart
|
||||
class FavoritesPage extends ConsumerWidget {
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final favoritesAsync = ref.watch(favoritesProvider);
|
||||
|
||||
return favoritesAsync.when(
|
||||
data: (favorites) => ListView.builder(
|
||||
itemCount: favorites.length,
|
||||
itemBuilder: (context, index) {
|
||||
final productId = favorites.elementAt(index);
|
||||
return ProductTile(productId: productId);
|
||||
},
|
||||
),
|
||||
loading: () => CircularProgressIndicator(),
|
||||
error: (error, stack) => ErrorWidget(error),
|
||||
);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 🔧 Provider Methods
|
||||
|
||||
### Main Provider (`favoritesProvider.notifier`)
|
||||
|
||||
| Method | Description |
|
||||
|--------|-------------|
|
||||
| `addFavorite(productId)` | Add product to favorites |
|
||||
| `removeFavorite(productId)` | Remove product from favorites |
|
||||
| `toggleFavorite(productId)` | Toggle favorite status |
|
||||
| `refresh()` | Reload favorites from database |
|
||||
| `clearAll()` | Remove all favorites |
|
||||
|
||||
---
|
||||
|
||||
## 💾 Data Persistence
|
||||
|
||||
- **Storage**: Hive local database
|
||||
- **Box**: `favorite_box` (HiveBoxNames.favoriteBox)
|
||||
- **Model**: `FavoriteModel` (TypeId: 28)
|
||||
- **Fields**:
|
||||
- `favoriteId`: Unique ID (userId_productId_timestamp)
|
||||
- `productId`: Product reference
|
||||
- `userId`: User reference
|
||||
- `createdAt`: Timestamp
|
||||
|
||||
---
|
||||
|
||||
## ⚠️ Important Notes
|
||||
|
||||
### TODO: Auth Integration
|
||||
Currently using hardcoded userId (`'user_001'`). Replace when auth is ready:
|
||||
|
||||
```dart
|
||||
// In favorites_provider.dart
|
||||
@riverpod
|
||||
String currentUserId(Ref ref) {
|
||||
// TODO: Replace with actual auth provider
|
||||
final user = ref.watch(authProvider).user;
|
||||
return user?.id ?? 'guest';
|
||||
}
|
||||
```
|
||||
|
||||
### Hive Box Initialization
|
||||
Ensure the favorites box is opened in `main.dart`:
|
||||
|
||||
```dart
|
||||
await Hive.openBox<FavoriteModel>(HiveBoxNames.favoriteBox);
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 📚 Documentation
|
||||
|
||||
- **[INTEGRATION_SUMMARY.md](./INTEGRATION_SUMMARY.md)** - Complete technical documentation
|
||||
- **[USAGE_EXAMPLES.md](./USAGE_EXAMPLES.md)** - Comprehensive code examples
|
||||
|
||||
---
|
||||
|
||||
## ✅ Features
|
||||
|
||||
- ✅ Add/remove favorites
|
||||
- ✅ Toggle favorite status
|
||||
- ✅ Persistent storage (Hive)
|
||||
- ✅ Multi-user support
|
||||
- ✅ Optimistic updates
|
||||
- ✅ Error handling
|
||||
- ✅ Loading states
|
||||
- ✅ Debug logging
|
||||
- ✅ Auto-dispose providers
|
||||
|
||||
---
|
||||
|
||||
## 🧪 Testing
|
||||
|
||||
```dart
|
||||
test('should add favorite', () async {
|
||||
final container = ProviderContainer();
|
||||
addTearDown(container.dispose);
|
||||
|
||||
await container.read(favoritesProvider.notifier)
|
||||
.addFavorite('product_1');
|
||||
|
||||
final favorites = await container.read(favoritesProvider.future);
|
||||
expect(favorites, contains('product_1'));
|
||||
});
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 🎨 UI Integration Examples
|
||||
|
||||
See **[USAGE_EXAMPLES.md](./USAGE_EXAMPLES.md)** for:
|
||||
- Favorite buttons with loading states
|
||||
- Favorites page with pull-to-refresh
|
||||
- Empty state handling
|
||||
- Error state handling
|
||||
- App lifecycle management
|
||||
- Performance optimization
|
||||
|
||||
---
|
||||
|
||||
## 🐛 Debugging
|
||||
|
||||
All operations log to console:
|
||||
```
|
||||
[FavoritesProvider] Added favorite: product_123
|
||||
[FavoritesLocalDataSource] Loaded 5 favorites for user: user_001
|
||||
[FavoritesProvider] Error adding favorite: ...
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 📈 Performance
|
||||
|
||||
- **O(1)** favorite lookup using `Set<String>`
|
||||
- **Auto-dispose** providers when not in use
|
||||
- **Selective rebuilds** with `isFavoriteProvider`
|
||||
- **Database compaction** available
|
||||
|
||||
---
|
||||
|
||||
## 🔮 Future Enhancements
|
||||
|
||||
- [ ] Remote API sync
|
||||
- [ ] Offline queue
|
||||
- [ ] Cross-device sync
|
||||
- [ ] Batch operations
|
||||
- [ ] Favorites collections
|
||||
- [ ] Share favorites
|
||||
- [ ] Analytics tracking
|
||||
|
||||
---
|
||||
|
||||
## 🆘 Troubleshooting
|
||||
|
||||
| Issue | Solution |
|
||||
|-------|----------|
|
||||
| Favorites not persisting | Check Hive box initialization in `main.dart` |
|
||||
| State not updating | Use `ref.read()` for mutations, `ref.watch()` for listening |
|
||||
| Performance issues | Use `isFavoriteProvider` instead of watching full `favoritesProvider` |
|
||||
|
||||
---
|
||||
|
||||
## 📞 Support
|
||||
|
||||
For detailed examples and advanced use cases, see:
|
||||
- **[USAGE_EXAMPLES.md](./USAGE_EXAMPLES.md)**
|
||||
- **[INTEGRATION_SUMMARY.md](./INTEGRATION_SUMMARY.md)**
|
||||
|
||||
---
|
||||
|
||||
**Status**: ✅ Ready for integration
|
||||
**Version**: 1.0.0
|
||||
**Last Updated**: 2024-10-24
|
||||
640
lib/features/favorites/USAGE_EXAMPLES.md
Normal file
640
lib/features/favorites/USAGE_EXAMPLES.md
Normal file
@@ -0,0 +1,640 @@
|
||||
# Favorites Provider - Usage Examples
|
||||
|
||||
This document provides practical examples of how to use the Favorites state management provider in the Worker app.
|
||||
|
||||
## Overview
|
||||
|
||||
The Favorites feature provides a complete state management solution for managing user's favorite products using Riverpod 3.0 with code generation and Hive for local persistence.
|
||||
|
||||
## Provider Structure
|
||||
|
||||
```dart
|
||||
// Main providers
|
||||
favoritesProvider // AsyncNotifier<Set<String>>
|
||||
favoritesLocalDataSourceProvider // FavoritesLocalDataSource
|
||||
currentUserIdProvider // String (TODO: replace with auth)
|
||||
|
||||
// Helper providers
|
||||
isFavoriteProvider(productId) // bool
|
||||
favoriteCountProvider // int
|
||||
favoriteProductIdsProvider // List<String>
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Basic Usage Examples
|
||||
|
||||
### 1. Display Favorite Button in Product Card
|
||||
|
||||
```dart
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:worker/features/favorites/presentation/providers/favorites_provider.dart';
|
||||
|
||||
class ProductCard extends ConsumerWidget {
|
||||
final String productId;
|
||||
final String productName;
|
||||
final double price;
|
||||
|
||||
const ProductCard({
|
||||
super.key,
|
||||
required this.productId,
|
||||
required this.productName,
|
||||
required this.price,
|
||||
});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
// Watch if this product is favorited
|
||||
final isFavorited = ref.watch(isFavoriteProvider(productId));
|
||||
|
||||
return Card(
|
||||
child: Column(
|
||||
children: [
|
||||
// Product image and details
|
||||
Text(productName),
|
||||
Text('\$$price'),
|
||||
|
||||
// Favorite button
|
||||
IconButton(
|
||||
icon: Icon(
|
||||
isFavorited ? Icons.favorite : Icons.favorite_border,
|
||||
color: isFavorited ? Colors.red : Colors.grey,
|
||||
),
|
||||
onPressed: () {
|
||||
// Toggle favorite status
|
||||
ref.read(favoritesProvider.notifier).toggleFavorite(productId);
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### 2. Display Favorite Count Badge
|
||||
|
||||
```dart
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:worker/features/favorites/presentation/providers/favorites_provider.dart';
|
||||
|
||||
class FavoritesIconWithBadge extends ConsumerWidget {
|
||||
const FavoritesIconWithBadge({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
// Watch favorite count
|
||||
final favoriteCount = ref.watch(favoriteCountProvider);
|
||||
|
||||
return Stack(
|
||||
children: [
|
||||
const Icon(Icons.favorite, size: 32),
|
||||
if (favoriteCount > 0)
|
||||
Positioned(
|
||||
right: 0,
|
||||
top: 0,
|
||||
child: Container(
|
||||
padding: const EdgeInsets.all(4),
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.red,
|
||||
shape: BoxShape.circle,
|
||||
),
|
||||
constraints: const BoxConstraints(
|
||||
minWidth: 20,
|
||||
minHeight: 20,
|
||||
),
|
||||
child: Text(
|
||||
'$favoriteCount',
|
||||
style: const TextStyle(
|
||||
color: Colors.white,
|
||||
fontSize: 12,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
textAlign: TextAlign.center,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### 3. Favorites Page - Display All Favorites
|
||||
|
||||
```dart
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:worker/features/favorites/presentation/providers/favorites_provider.dart';
|
||||
import 'package:worker/features/products/presentation/providers/products_provider.dart';
|
||||
|
||||
class FavoritesPage extends ConsumerWidget {
|
||||
const FavoritesPage({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final favoritesAsync = ref.watch(favoritesProvider);
|
||||
final productsAsync = ref.watch(productsProvider);
|
||||
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text('My Favorites'),
|
||||
actions: [
|
||||
// Clear all button
|
||||
IconButton(
|
||||
icon: const Icon(Icons.delete_sweep),
|
||||
onPressed: () {
|
||||
_showClearAllDialog(context, ref);
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
body: favoritesAsync.when(
|
||||
data: (favoriteIds) {
|
||||
if (favoriteIds.isEmpty) {
|
||||
return const Center(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
Icon(Icons.favorite_border, size: 64, color: Colors.grey),
|
||||
SizedBox(height: 16),
|
||||
Text(
|
||||
'No favorites yet',
|
||||
style: TextStyle(fontSize: 18, color: Colors.grey),
|
||||
),
|
||||
SizedBox(height: 8),
|
||||
Text(
|
||||
'Add products to your favorites to see them here',
|
||||
style: TextStyle(color: Colors.grey),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
return productsAsync.when(
|
||||
data: (products) {
|
||||
// Filter products to show only favorites
|
||||
final favoriteProducts = products
|
||||
.where((product) => favoriteIds.contains(product.id))
|
||||
.toList();
|
||||
|
||||
return GridView.builder(
|
||||
padding: const EdgeInsets.all(16),
|
||||
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
|
||||
crossAxisCount: 2,
|
||||
childAspectRatio: 0.75,
|
||||
crossAxisSpacing: 16,
|
||||
mainAxisSpacing: 16,
|
||||
),
|
||||
itemCount: favoriteProducts.length,
|
||||
itemBuilder: (context, index) {
|
||||
final product = favoriteProducts[index];
|
||||
return ProductCard(
|
||||
productId: product.id,
|
||||
productName: product.name,
|
||||
price: product.price,
|
||||
);
|
||||
},
|
||||
);
|
||||
},
|
||||
loading: () => const Center(child: CircularProgressIndicator()),
|
||||
error: (error, stack) => Center(child: Text('Error: $error')),
|
||||
);
|
||||
},
|
||||
loading: () => const Center(child: CircularProgressIndicator()),
|
||||
error: (error, stack) => Center(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
const Icon(Icons.error, size: 64, color: Colors.red),
|
||||
const SizedBox(height: 16),
|
||||
Text('Error loading favorites: $error'),
|
||||
const SizedBox(height: 16),
|
||||
ElevatedButton(
|
||||
onPressed: () {
|
||||
ref.invalidate(favoritesProvider);
|
||||
},
|
||||
child: const Text('Retry'),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
void _showClearAllDialog(BuildContext context, WidgetRef ref) {
|
||||
showDialog(
|
||||
context: context,
|
||||
builder: (context) => AlertDialog(
|
||||
title: const Text('Clear All Favorites'),
|
||||
content: const Text('Are you sure you want to remove all favorites?'),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.pop(context),
|
||||
child: const Text('Cancel'),
|
||||
),
|
||||
TextButton(
|
||||
onPressed: () {
|
||||
ref.read(favoritesProvider.notifier).clearAll();
|
||||
Navigator.pop(context);
|
||||
},
|
||||
child: const Text('Clear', style: TextStyle(color: Colors.red)),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### 4. Add/Remove Favorites Programmatically
|
||||
|
||||
```dart
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:worker/features/favorites/presentation/providers/favorites_provider.dart';
|
||||
|
||||
class ProductDetailPage extends ConsumerWidget {
|
||||
final String productId;
|
||||
|
||||
const ProductDetailPage({super.key, required this.productId});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final isFavorited = ref.watch(isFavoriteProvider(productId));
|
||||
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text('Product Details'),
|
||||
actions: [
|
||||
IconButton(
|
||||
icon: Icon(
|
||||
isFavorited ? Icons.favorite : Icons.favorite_border,
|
||||
color: isFavorited ? Colors.red : Colors.white,
|
||||
),
|
||||
onPressed: () async {
|
||||
final notifier = ref.read(favoritesProvider.notifier);
|
||||
|
||||
if (isFavorited) {
|
||||
await notifier.removeFavorite(productId);
|
||||
if (context.mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(content: Text('Removed from favorites')),
|
||||
);
|
||||
}
|
||||
} else {
|
||||
await notifier.addFavorite(productId);
|
||||
if (context.mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(content: Text('Added to favorites')),
|
||||
);
|
||||
}
|
||||
}
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
body: Center(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
const Text('Product Details'),
|
||||
const SizedBox(height: 32),
|
||||
|
||||
// Custom button with loading state
|
||||
ElevatedButton.icon(
|
||||
icon: Icon(isFavorited ? Icons.favorite : Icons.favorite_border),
|
||||
label: Text(isFavorited ? 'Remove from Favorites' : 'Add to Favorites'),
|
||||
onPressed: () {
|
||||
ref.read(favoritesProvider.notifier).toggleFavorite(productId);
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### 5. Filter Products to Show Only Favorites
|
||||
|
||||
```dart
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:worker/features/favorites/presentation/providers/favorites_provider.dart';
|
||||
import 'package:worker/features/products/presentation/providers/products_provider.dart';
|
||||
|
||||
// Create a derived provider for favorite products
|
||||
@riverpod
|
||||
Future<List<Product>> favoriteProducts(Ref ref) async {
|
||||
// Get all products
|
||||
final products = await ref.watch(productsProvider.future);
|
||||
|
||||
// Get favorite IDs
|
||||
final favoriteIds = await ref.watch(favoritesProvider.future);
|
||||
|
||||
// Filter products
|
||||
return products.where((p) => favoriteIds.contains(p.id)).toList();
|
||||
}
|
||||
|
||||
// Usage in widget
|
||||
class FavoriteProductsList extends ConsumerWidget {
|
||||
const FavoriteProductsList({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final favoriteProductsAsync = ref.watch(favoriteProductsProvider);
|
||||
|
||||
return favoriteProductsAsync.when(
|
||||
data: (products) => ListView.builder(
|
||||
itemCount: products.length,
|
||||
itemBuilder: (context, index) {
|
||||
final product = products[index];
|
||||
return ListTile(
|
||||
title: Text(product.name),
|
||||
subtitle: Text('\$${product.price}'),
|
||||
);
|
||||
},
|
||||
),
|
||||
loading: () => const CircularProgressIndicator(),
|
||||
error: (error, stack) => Text('Error: $error'),
|
||||
);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### 6. Refresh Favorites from Database
|
||||
|
||||
```dart
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:worker/features/favorites/presentation/providers/favorites_provider.dart';
|
||||
|
||||
class FavoritesPageWithRefresh extends ConsumerWidget {
|
||||
const FavoritesPageWithRefresh({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final favoritesAsync = ref.watch(favoritesProvider);
|
||||
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text('My Favorites'),
|
||||
actions: [
|
||||
IconButton(
|
||||
icon: const Icon(Icons.refresh),
|
||||
onPressed: () {
|
||||
// Refresh favorites from database
|
||||
ref.read(favoritesProvider.notifier).refresh();
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
body: RefreshIndicator(
|
||||
onRefresh: () async {
|
||||
// Pull to refresh
|
||||
await ref.read(favoritesProvider.notifier).refresh();
|
||||
},
|
||||
child: favoritesAsync.when(
|
||||
data: (favorites) => ListView.builder(
|
||||
itemCount: favorites.length,
|
||||
itemBuilder: (context, index) {
|
||||
final productId = favorites.elementAt(index);
|
||||
return ListTile(title: Text('Product: $productId'));
|
||||
},
|
||||
),
|
||||
loading: () => const Center(child: CircularProgressIndicator()),
|
||||
error: (error, stack) => Center(child: Text('Error: $error')),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Advanced Usage
|
||||
|
||||
### 7. Handle Loading States with Custom UI
|
||||
|
||||
```dart
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:worker/features/favorites/presentation/providers/favorites_provider.dart';
|
||||
|
||||
class FavoriteButtonWithLoadingState extends ConsumerWidget {
|
||||
final String productId;
|
||||
|
||||
const FavoriteButtonWithLoadingState({
|
||||
super.key,
|
||||
required this.productId,
|
||||
});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final favoritesAsync = ref.watch(favoritesProvider);
|
||||
|
||||
return favoritesAsync.when(
|
||||
data: (favorites) {
|
||||
final isFavorited = favorites.contains(productId);
|
||||
|
||||
return IconButton(
|
||||
icon: Icon(
|
||||
isFavorited ? Icons.favorite : Icons.favorite_border,
|
||||
color: isFavorited ? Colors.red : Colors.grey,
|
||||
),
|
||||
onPressed: () {
|
||||
ref.read(favoritesProvider.notifier).toggleFavorite(productId);
|
||||
},
|
||||
);
|
||||
},
|
||||
loading: () => const SizedBox(
|
||||
width: 24,
|
||||
height: 24,
|
||||
child: CircularProgressIndicator(strokeWidth: 2),
|
||||
),
|
||||
error: (error, stack) => IconButton(
|
||||
icon: const Icon(Icons.error, color: Colors.grey),
|
||||
onPressed: () {
|
||||
ref.invalidate(favoritesProvider);
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### 8. Sync Favorites on App Resume
|
||||
|
||||
```dart
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:worker/features/favorites/presentation/providers/favorites_provider.dart';
|
||||
|
||||
class FavoritesAppLifecycleManager extends ConsumerStatefulWidget {
|
||||
final Widget child;
|
||||
|
||||
const FavoritesAppLifecycleManager({
|
||||
super.key,
|
||||
required this.child,
|
||||
});
|
||||
|
||||
@override
|
||||
ConsumerState<FavoritesAppLifecycleManager> createState() =>
|
||||
_FavoritesAppLifecycleManagerState();
|
||||
}
|
||||
|
||||
class _FavoritesAppLifecycleManagerState
|
||||
extends ConsumerState<FavoritesAppLifecycleManager>
|
||||
with WidgetsBindingObserver {
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
WidgetsBinding.instance.addObserver(this);
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
WidgetsBinding.instance.removeObserver(this);
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
void didChangeAppLifecycleState(AppLifecycleState state) {
|
||||
if (state == AppLifecycleState.resumed) {
|
||||
// Refresh favorites when app resumes
|
||||
ref.read(favoritesProvider.notifier).refresh();
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return widget.child;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Integration with Auth Provider (TODO)
|
||||
|
||||
Once the authentication provider is available, update the `currentUserId` provider:
|
||||
|
||||
```dart
|
||||
// In favorites_provider.dart
|
||||
|
||||
/// Provides the current logged-in user's ID
|
||||
@riverpod
|
||||
String currentUserId(Ref ref) {
|
||||
// Replace this with actual auth integration:
|
||||
final authState = ref.watch(authProvider);
|
||||
return authState.user?.id ?? 'guest';
|
||||
|
||||
// Or throw an error if user is not logged in:
|
||||
// final user = ref.watch(authProvider).user;
|
||||
// if (user == null) throw Exception('User not logged in');
|
||||
// return user.id;
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Testing Examples
|
||||
|
||||
### Unit Test Example
|
||||
|
||||
```dart
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:worker/features/favorites/presentation/providers/favorites_provider.dart';
|
||||
|
||||
void main() {
|
||||
test('should add favorite to state', () async {
|
||||
final container = ProviderContainer();
|
||||
addTearDown(container.dispose);
|
||||
|
||||
// Initial state should be empty
|
||||
final initialState = await container.read(favoritesProvider.future);
|
||||
expect(initialState, isEmpty);
|
||||
|
||||
// Add favorite
|
||||
await container.read(favoritesProvider.notifier).addFavorite('product_1');
|
||||
|
||||
// Check if favorite was added
|
||||
final updatedState = await container.read(favoritesProvider.future);
|
||||
expect(updatedState, contains('product_1'));
|
||||
});
|
||||
|
||||
test('isFavorite should return true for favorited product', () async {
|
||||
final container = ProviderContainer();
|
||||
addTearDown(container.dispose);
|
||||
|
||||
// Add favorite
|
||||
await container.read(favoritesProvider.notifier).addFavorite('product_1');
|
||||
|
||||
// Check if product is favorited
|
||||
final isFavorited = container.read(isFavoriteProvider('product_1'));
|
||||
expect(isFavorited, isTrue);
|
||||
});
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Performance Tips
|
||||
|
||||
1. **Use `isFavoriteProvider` for individual checks** - This prevents rebuilding all favorite-dependent widgets when the favorites list changes.
|
||||
|
||||
2. **Use `.select()` for specific data** - If you only need a specific piece of data:
|
||||
```dart
|
||||
final hasFavorites = ref.watch(
|
||||
favoritesProvider.select((async) => async.value?.isNotEmpty ?? false)
|
||||
);
|
||||
```
|
||||
|
||||
3. **Avoid unnecessary rebuilds** - Wrap expensive widgets with `RepaintBoundary` or use `Consumer` to isolate rebuilds.
|
||||
|
||||
4. **Batch operations** - If adding/removing multiple favorites, consider implementing a batch operation method.
|
||||
|
||||
---
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
### Favorites not persisting
|
||||
- Ensure Hive box is properly initialized in `main.dart`
|
||||
- Check that the favorite box is opened: `Hive.openBox<FavoriteModel>(HiveBoxNames.favoriteBox)`
|
||||
|
||||
### State not updating
|
||||
- Verify you're using `ref.read()` for mutations and `ref.watch()` for listening
|
||||
- Check console logs for error messages
|
||||
|
||||
### Performance issues
|
||||
- Use `isFavoriteProvider` instead of watching the entire `favoritesProvider`
|
||||
- Implement pagination for large favorite lists
|
||||
- Consider compacting the Hive box periodically
|
||||
|
||||
---
|
||||
|
||||
## Next Steps
|
||||
|
||||
1. **Integrate with Auth Provider** - Replace hardcoded userId with actual user from auth state
|
||||
2. **Add Remote Sync** - Implement API calls to sync favorites with backend
|
||||
3. **Analytics** - Track favorite actions for user insights
|
||||
4. **Recommendations** - Use favorite data to recommend similar products
|
||||
@@ -0,0 +1,151 @@
|
||||
import 'package:hive_ce_flutter/hive_flutter.dart';
|
||||
import 'package:worker/core/constants/storage_constants.dart';
|
||||
import 'package:worker/features/favorites/data/models/favorite_model.dart';
|
||||
|
||||
/// Favorites Local DataSource
|
||||
///
|
||||
/// Handles all local database operations for favorites using Hive.
|
||||
/// Supports multi-user functionality by filtering favorites by userId.
|
||||
class FavoritesLocalDataSource {
|
||||
/// Get the Hive box for favorites
|
||||
Box<dynamic> get _box {
|
||||
return Hive.box<dynamic>(HiveBoxNames.favoriteBox);
|
||||
}
|
||||
|
||||
/// Get all favorites for a specific user
|
||||
///
|
||||
/// Returns a list of [FavoriteModel] filtered by [userId].
|
||||
/// If the box is not open or an error occurs, returns an empty list.
|
||||
Future<List<FavoriteModel>> getAllFavorites(String userId) async {
|
||||
try {
|
||||
final favorites = _box.values
|
||||
.whereType<FavoriteModel>()
|
||||
.where((fav) => fav.userId == userId)
|
||||
.toList();
|
||||
|
||||
// Sort by creation date (newest first)
|
||||
favorites.sort((a, b) => b.createdAt.compareTo(a.createdAt));
|
||||
|
||||
return favorites;
|
||||
} catch (e) {
|
||||
debugPrint('[FavoritesLocalDataSource] Error getting favorites: $e');
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
/// Add a favorite to the database
|
||||
///
|
||||
/// Adds a new [FavoriteModel] to the Hive box.
|
||||
/// Uses the favoriteId as the key for efficient lookup.
|
||||
Future<void> addFavorite(FavoriteModel favorite) async {
|
||||
try {
|
||||
await _box.put(favorite.favoriteId, favorite);
|
||||
debugPrint('[FavoritesLocalDataSource] Added favorite: ${favorite.favoriteId} for user: ${favorite.userId}');
|
||||
} catch (e) {
|
||||
debugPrint('[FavoritesLocalDataSource] Error adding favorite: $e');
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
/// Remove a favorite from the database
|
||||
///
|
||||
/// Removes a favorite by finding it with the combination of [productId] and [userId].
|
||||
/// Returns true if the favorite was found and removed, false otherwise.
|
||||
Future<bool> removeFavorite(String productId, String userId) async {
|
||||
try {
|
||||
// Find the favorite by productId and userId
|
||||
final favorites = _box.values
|
||||
.whereType<FavoriteModel>()
|
||||
.where((fav) => fav.productId == productId && fav.userId == userId)
|
||||
.toList();
|
||||
|
||||
if (favorites.isEmpty) {
|
||||
debugPrint('[FavoritesLocalDataSource] Favorite not found: productId=$productId, userId=$userId');
|
||||
return false;
|
||||
}
|
||||
|
||||
final favorite = favorites.first;
|
||||
await _box.delete(favorite.favoriteId);
|
||||
debugPrint('[FavoritesLocalDataSource] Removed favorite: ${favorite.favoriteId} for user: $userId');
|
||||
return true;
|
||||
} catch (e) {
|
||||
debugPrint('[FavoritesLocalDataSource] Error removing favorite: $e');
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
/// Check if a product is favorited by a user
|
||||
///
|
||||
/// Returns true if the product is in the user's favorites, false otherwise.
|
||||
bool isFavorite(String productId, String userId) {
|
||||
try {
|
||||
return _box.values
|
||||
.whereType<FavoriteModel>()
|
||||
.any((fav) => fav.productId == productId && fav.userId == userId);
|
||||
} catch (e) {
|
||||
debugPrint('[FavoritesLocalDataSource] Error checking favorite: $e');
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// Clear all favorites for a specific user
|
||||
///
|
||||
/// Removes all favorites for the given [userId].
|
||||
/// Useful for logout or data cleanup scenarios.
|
||||
Future<void> clearFavorites(String userId) async {
|
||||
try {
|
||||
final favoriteIds = _box.values
|
||||
.whereType<FavoriteModel>()
|
||||
.where((fav) => fav.userId == userId)
|
||||
.map((fav) => fav.favoriteId)
|
||||
.toList();
|
||||
|
||||
await _box.deleteAll(favoriteIds);
|
||||
debugPrint('[FavoritesLocalDataSource] Cleared ${favoriteIds.length} favorites for user: $userId');
|
||||
} catch (e) {
|
||||
debugPrint('[FavoritesLocalDataSource] Error clearing favorites: $e');
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
/// Get the count of favorites for a user
|
||||
///
|
||||
/// Returns the total number of favorites for the given [userId].
|
||||
int getFavoriteCount(String userId) {
|
||||
try {
|
||||
return _box.values
|
||||
.whereType<FavoriteModel>()
|
||||
.where((fav) => fav.userId == userId)
|
||||
.length;
|
||||
} catch (e) {
|
||||
debugPrint('[FavoritesLocalDataSource] Error getting favorite count: $e');
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/// Check if the favorites box is open
|
||||
///
|
||||
/// Returns true if the box is open and ready to use.
|
||||
bool isBoxOpen() {
|
||||
return Hive.isBoxOpen(HiveBoxNames.favoriteBox);
|
||||
}
|
||||
|
||||
/// Compact the favorites box to reduce storage space
|
||||
///
|
||||
/// Should be called periodically to optimize database size.
|
||||
Future<void> compact() async {
|
||||
try {
|
||||
if (isBoxOpen()) {
|
||||
await _box.compact();
|
||||
debugPrint('[FavoritesLocalDataSource] Favorites box compacted');
|
||||
}
|
||||
} catch (e) {
|
||||
debugPrint('[FavoritesLocalDataSource] Error compacting favorites box: $e');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Debug print helper that works in both Flutter and Dart
|
||||
void debugPrint(String message) {
|
||||
print('[FavoritesLocalDataSource] $message');
|
||||
}
|
||||
120
lib/features/favorites/data/models/favorite_model.dart
Normal file
120
lib/features/favorites/data/models/favorite_model.dart
Normal file
@@ -0,0 +1,120 @@
|
||||
import 'package:hive_ce/hive.dart';
|
||||
|
||||
import 'package:worker/core/constants/storage_constants.dart';
|
||||
import 'package:worker/features/favorites/domain/entities/favorite.dart';
|
||||
|
||||
part 'favorite_model.g.dart';
|
||||
|
||||
/// Favorite Model
|
||||
///
|
||||
/// Hive CE model for storing user's favorite products locally.
|
||||
/// Maps to the 'favorites' table in the database.
|
||||
///
|
||||
/// Type ID: 28
|
||||
@HiveType(typeId: HiveTypeIds.favoriteModel)
|
||||
class FavoriteModel extends HiveObject {
|
||||
FavoriteModel({
|
||||
required this.favoriteId,
|
||||
required this.productId,
|
||||
required this.userId,
|
||||
required this.createdAt,
|
||||
});
|
||||
|
||||
/// Favorite ID (Primary Key)
|
||||
@HiveField(0)
|
||||
final String favoriteId;
|
||||
|
||||
/// Product ID (Foreign Key)
|
||||
@HiveField(1)
|
||||
final String productId;
|
||||
|
||||
/// User ID (Foreign Key)
|
||||
@HiveField(2)
|
||||
final String userId;
|
||||
|
||||
/// Created timestamp
|
||||
@HiveField(3)
|
||||
final DateTime createdAt;
|
||||
|
||||
// =========================================================================
|
||||
// JSON SERIALIZATION
|
||||
// =========================================================================
|
||||
|
||||
/// Create FavoriteModel from JSON
|
||||
factory FavoriteModel.fromJson(Map<String, dynamic> json) {
|
||||
return FavoriteModel(
|
||||
favoriteId: json['favorite_id'] as String,
|
||||
productId: json['product_id'] as String,
|
||||
userId: json['user_id'] as String,
|
||||
createdAt: DateTime.parse(json['created_at'] as String),
|
||||
);
|
||||
}
|
||||
|
||||
/// Convert FavoriteModel to JSON
|
||||
Map<String, dynamic> toJson() {
|
||||
return {
|
||||
'favorite_id': favoriteId,
|
||||
'product_id': productId,
|
||||
'user_id': userId,
|
||||
'created_at': createdAt.toIso8601String(),
|
||||
};
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// COPY WITH
|
||||
// =========================================================================
|
||||
|
||||
/// Create a copy with updated fields
|
||||
FavoriteModel copyWith({
|
||||
String? favoriteId,
|
||||
String? productId,
|
||||
String? userId,
|
||||
DateTime? createdAt,
|
||||
}) {
|
||||
return FavoriteModel(
|
||||
favoriteId: favoriteId ?? this.favoriteId,
|
||||
productId: productId ?? this.productId,
|
||||
userId: userId ?? this.userId,
|
||||
createdAt: createdAt ?? this.createdAt,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return 'FavoriteModel(favoriteId: $favoriteId, productId: $productId, userId: $userId)';
|
||||
}
|
||||
|
||||
@override
|
||||
bool operator ==(Object other) {
|
||||
if (identical(this, other)) return true;
|
||||
|
||||
return other is FavoriteModel && other.favoriteId == favoriteId;
|
||||
}
|
||||
|
||||
@override
|
||||
int get hashCode => favoriteId.hashCode;
|
||||
|
||||
// =========================================================================
|
||||
// ENTITY CONVERSION
|
||||
// =========================================================================
|
||||
|
||||
/// Convert FavoriteModel to Favorite entity
|
||||
Favorite toEntity() {
|
||||
return Favorite(
|
||||
favoriteId: favoriteId,
|
||||
productId: productId,
|
||||
userId: userId,
|
||||
createdAt: createdAt,
|
||||
);
|
||||
}
|
||||
|
||||
/// Create FavoriteModel from Favorite entity
|
||||
factory FavoriteModel.fromEntity(Favorite favorite) {
|
||||
return FavoriteModel(
|
||||
favoriteId: favorite.favoriteId,
|
||||
productId: favorite.productId,
|
||||
userId: favorite.userId,
|
||||
createdAt: favorite.createdAt,
|
||||
);
|
||||
}
|
||||
}
|
||||
50
lib/features/favorites/data/models/favorite_model.g.dart
Normal file
50
lib/features/favorites/data/models/favorite_model.g.dart
Normal file
@@ -0,0 +1,50 @@
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
|
||||
part of 'favorite_model.dart';
|
||||
|
||||
// **************************************************************************
|
||||
// TypeAdapterGenerator
|
||||
// **************************************************************************
|
||||
|
||||
class FavoriteModelAdapter extends TypeAdapter<FavoriteModel> {
|
||||
@override
|
||||
final typeId = 28;
|
||||
|
||||
@override
|
||||
FavoriteModel read(BinaryReader reader) {
|
||||
final numOfFields = reader.readByte();
|
||||
final fields = <int, dynamic>{
|
||||
for (int i = 0; i < numOfFields; i++) reader.readByte(): reader.read(),
|
||||
};
|
||||
return FavoriteModel(
|
||||
favoriteId: fields[0] as String,
|
||||
productId: fields[1] as String,
|
||||
userId: fields[2] as String,
|
||||
createdAt: fields[3] as DateTime,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
void write(BinaryWriter writer, FavoriteModel obj) {
|
||||
writer
|
||||
..writeByte(4)
|
||||
..writeByte(0)
|
||||
..write(obj.favoriteId)
|
||||
..writeByte(1)
|
||||
..write(obj.productId)
|
||||
..writeByte(2)
|
||||
..write(obj.userId)
|
||||
..writeByte(3)
|
||||
..write(obj.createdAt);
|
||||
}
|
||||
|
||||
@override
|
||||
int get hashCode => typeId.hashCode;
|
||||
|
||||
@override
|
||||
bool operator ==(Object other) =>
|
||||
identical(this, other) ||
|
||||
other is FavoriteModelAdapter &&
|
||||
runtimeType == other.runtimeType &&
|
||||
typeId == other.typeId;
|
||||
}
|
||||
72
lib/features/favorites/domain/entities/favorite.dart
Normal file
72
lib/features/favorites/domain/entities/favorite.dart
Normal file
@@ -0,0 +1,72 @@
|
||||
/// Domain Entity: Favorite
|
||||
///
|
||||
/// Pure business entity representing a user's favorite product.
|
||||
/// This entity is framework-independent and contains only business logic.
|
||||
library;
|
||||
|
||||
/// Favorite Entity
|
||||
///
|
||||
/// Represents a product that a user has marked as favorite.
|
||||
/// Used across all layers but originates in the domain layer.
|
||||
class Favorite {
|
||||
/// Unique identifier for the favorite entry
|
||||
final String favoriteId;
|
||||
|
||||
/// Reference to the product that was favorited
|
||||
final String productId;
|
||||
|
||||
/// Reference to the user who favorited the product
|
||||
final String userId;
|
||||
|
||||
/// Timestamp when the product was favorited
|
||||
final DateTime createdAt;
|
||||
|
||||
const Favorite({
|
||||
required this.favoriteId,
|
||||
required this.productId,
|
||||
required this.userId,
|
||||
required this.createdAt,
|
||||
});
|
||||
|
||||
/// Copy with method for creating modified copies
|
||||
Favorite copyWith({
|
||||
String? favoriteId,
|
||||
String? productId,
|
||||
String? userId,
|
||||
DateTime? createdAt,
|
||||
}) {
|
||||
return Favorite(
|
||||
favoriteId: favoriteId ?? this.favoriteId,
|
||||
productId: productId ?? this.productId,
|
||||
userId: userId ?? this.userId,
|
||||
createdAt: createdAt ?? this.createdAt,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return 'Favorite(favoriteId: $favoriteId, productId: $productId, '
|
||||
'userId: $userId, createdAt: $createdAt)';
|
||||
}
|
||||
|
||||
@override
|
||||
bool operator ==(Object other) {
|
||||
if (identical(this, other)) return true;
|
||||
|
||||
return other is Favorite &&
|
||||
other.favoriteId == favoriteId &&
|
||||
other.productId == productId &&
|
||||
other.userId == userId &&
|
||||
other.createdAt == createdAt;
|
||||
}
|
||||
|
||||
@override
|
||||
int get hashCode {
|
||||
return Object.hash(
|
||||
favoriteId,
|
||||
productId,
|
||||
userId,
|
||||
createdAt,
|
||||
);
|
||||
}
|
||||
}
|
||||
464
lib/features/favorites/presentation/pages/favorites_page.dart
Normal file
464
lib/features/favorites/presentation/pages/favorites_page.dart
Normal file
@@ -0,0 +1,464 @@
|
||||
/// Page: Favorites Page
|
||||
///
|
||||
/// Displays all favorited products in a grid layout.
|
||||
/// Allows users to view and manage their favorite products.
|
||||
library;
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
import 'package:shimmer/shimmer.dart';
|
||||
import 'package:worker/core/constants/ui_constants.dart';
|
||||
import 'package:worker/core/theme/colors.dart';
|
||||
import 'package:worker/features/favorites/presentation/providers/favorites_provider.dart';
|
||||
import 'package:worker/features/favorites/presentation/widgets/favorite_product_card.dart';
|
||||
import 'package:worker/features/products/domain/entities/product.dart';
|
||||
|
||||
/// Favorites Page
|
||||
///
|
||||
/// Shows all products that the user has marked as favorites.
|
||||
/// Features:
|
||||
/// - Grid layout of favorite products
|
||||
/// - Pull-to-refresh
|
||||
/// - Empty state when no favorites
|
||||
/// - Error state with retry
|
||||
/// - Clear all functionality
|
||||
class FavoritesPage extends ConsumerWidget {
|
||||
const FavoritesPage({super.key});
|
||||
|
||||
/// Show confirmation dialog before clearing all favorites
|
||||
Future<void> _showClearAllDialog(BuildContext context, WidgetRef ref, int count) async {
|
||||
final confirmed = await showDialog<bool>(
|
||||
context: context,
|
||||
builder: (context) => AlertDialog(
|
||||
title: const Text('Xóa tất cả yêu thích?'),
|
||||
content: Text(
|
||||
'Bạn có chắc muốn xóa toàn bộ $count sản phẩm khỏi danh sách yêu thích?',
|
||||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.of(context).pop(false),
|
||||
child: const Text('Hủy'),
|
||||
),
|
||||
ElevatedButton(
|
||||
onPressed: () => Navigator.of(context).pop(true),
|
||||
style: ElevatedButton.styleFrom(
|
||||
backgroundColor: AppColors.danger,
|
||||
foregroundColor: AppColors.white,
|
||||
),
|
||||
child: const Text('Xóa tất cả'),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
|
||||
if (confirmed == true && context.mounted) {
|
||||
// Clear all favorites
|
||||
await ref.read(favoritesProvider.notifier).clearAll();
|
||||
|
||||
// Show snackbar
|
||||
if (context.mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(
|
||||
content: Text('Đã xóa tất cả yêu thích'),
|
||||
duration: Duration(seconds: 2),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
// Watch favorites and products
|
||||
final favoriteProductsAsync = ref.watch(favoriteProductsProvider);
|
||||
final favoriteCount = ref.watch(favoriteCountProvider);
|
||||
|
||||
return Scaffold(
|
||||
backgroundColor: const Color(0xFFF4F6F8),
|
||||
appBar: AppBar(
|
||||
// backgroundColor: AppColors.white,
|
||||
foregroundColor: Colors.black,
|
||||
elevation: 1,
|
||||
leading: IconButton(
|
||||
icon: const Icon(Icons.arrow_back),
|
||||
onPressed: () => context.pop(),
|
||||
),
|
||||
title: const Text('Yêu thích'),
|
||||
actions: [
|
||||
// Count badge
|
||||
if (favoriteCount > 0)
|
||||
Center(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 8.0),
|
||||
child: Text(
|
||||
'($favoriteCount)',
|
||||
style: const TextStyle(
|
||||
fontSize: 16.0,
|
||||
fontWeight: FontWeight.w600,
|
||||
color: AppColors.primaryBlue,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
|
||||
// Clear all button
|
||||
if (favoriteCount > 0)
|
||||
IconButton(
|
||||
icon: const Icon(Icons.delete_outline),
|
||||
tooltip: 'Xóa tất cả',
|
||||
onPressed: () => _showClearAllDialog(context, ref, favoriteCount),
|
||||
),
|
||||
],
|
||||
),
|
||||
body: SafeArea(
|
||||
child: favoriteProductsAsync.when(
|
||||
data: (products) {
|
||||
if (products.isEmpty) {
|
||||
return const _EmptyState();
|
||||
}
|
||||
|
||||
return RefreshIndicator(
|
||||
onRefresh: () async {
|
||||
ref.invalidate(favoritesProvider);
|
||||
ref.invalidate(favoriteProductsProvider);
|
||||
},
|
||||
child: _FavoritesGrid(products: products),
|
||||
);
|
||||
},
|
||||
loading: () => const _LoadingState(),
|
||||
error: (error, stackTrace) => _ErrorState(
|
||||
error: error,
|
||||
onRetry: () {
|
||||
ref.invalidate(favoritesProvider);
|
||||
ref.invalidate(favoriteProductsProvider);
|
||||
},
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// EMPTY STATE
|
||||
// ============================================================================
|
||||
|
||||
/// Empty State Widget
|
||||
///
|
||||
/// Displayed when there are no favorite products.
|
||||
class _EmptyState extends StatelessWidget {
|
||||
const _EmptyState();
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Center(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(AppSpacing.xl),
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
// Large icon
|
||||
Icon(
|
||||
Icons.favorite_border,
|
||||
size: 80.0,
|
||||
color: AppColors.grey500.withValues(alpha: 0.5),
|
||||
),
|
||||
|
||||
const SizedBox(height: AppSpacing.lg),
|
||||
|
||||
// Heading
|
||||
const Text(
|
||||
'Chưa có sản phẩm yêu thích',
|
||||
style: TextStyle(
|
||||
fontSize: 18.0,
|
||||
fontWeight: FontWeight.w600,
|
||||
color: AppColors.grey900,
|
||||
),
|
||||
textAlign: TextAlign.center,
|
||||
),
|
||||
|
||||
const SizedBox(height: AppSpacing.sm),
|
||||
|
||||
// Subtext
|
||||
Text(
|
||||
'Thêm sản phẩm vào danh sách yêu thích để xem lại sau',
|
||||
style: TextStyle(
|
||||
fontSize: 14.0,
|
||||
color: AppColors.grey500,
|
||||
),
|
||||
textAlign: TextAlign.center,
|
||||
),
|
||||
|
||||
const SizedBox(height: AppSpacing.xl),
|
||||
|
||||
// Explore Products Button
|
||||
ElevatedButton(
|
||||
onPressed: () {
|
||||
// Navigate to products page
|
||||
context.pushReplacement('/products');
|
||||
},
|
||||
style: ElevatedButton.styleFrom(
|
||||
backgroundColor: AppColors.primaryBlue,
|
||||
foregroundColor: AppColors.white,
|
||||
padding: const EdgeInsets.symmetric(
|
||||
horizontal: AppSpacing.xl,
|
||||
vertical: AppSpacing.md,
|
||||
),
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(AppRadius.button),
|
||||
),
|
||||
),
|
||||
child: const Text(
|
||||
'Khám phá sản phẩm',
|
||||
style: TextStyle(
|
||||
fontSize: 16.0,
|
||||
fontWeight: FontWeight.w600,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// LOADING STATE
|
||||
// ============================================================================
|
||||
|
||||
/// Loading State Widget
|
||||
///
|
||||
/// Displayed while favorites are being loaded.
|
||||
class _LoadingState extends StatelessWidget {
|
||||
const _LoadingState();
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return GridView.builder(
|
||||
padding: const EdgeInsets.all(AppSpacing.md),
|
||||
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
|
||||
crossAxisCount: 2,
|
||||
crossAxisSpacing: 12.0,
|
||||
mainAxisSpacing: 12.0,
|
||||
childAspectRatio: 0.62,
|
||||
),
|
||||
itemCount: 6, // Show 6 shimmer cards
|
||||
itemBuilder: (context, index) => _ShimmerCard(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// Shimmer Card for Loading State
|
||||
class _ShimmerCard extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Card(
|
||||
elevation: ProductCardSpecs.elevation,
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(ProductCardSpecs.borderRadius),
|
||||
),
|
||||
child: Shimmer.fromColors(
|
||||
baseColor: AppColors.grey100,
|
||||
highlightColor: AppColors.grey50,
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
// Image placeholder
|
||||
Expanded(
|
||||
child: Container(
|
||||
decoration: BoxDecoration(
|
||||
color: AppColors.grey100,
|
||||
borderRadius: const BorderRadius.vertical(
|
||||
top: Radius.circular(ProductCardSpecs.borderRadius),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
|
||||
// Info placeholder
|
||||
Padding(
|
||||
padding: const EdgeInsets.all(AppSpacing.sm),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
// Name placeholder
|
||||
Container(
|
||||
height: 14.0,
|
||||
width: double.infinity,
|
||||
decoration: BoxDecoration(
|
||||
color: AppColors.grey100,
|
||||
borderRadius: BorderRadius.circular(4.0),
|
||||
),
|
||||
),
|
||||
|
||||
const SizedBox(height: 8.0),
|
||||
|
||||
// SKU placeholder
|
||||
Container(
|
||||
height: 12.0,
|
||||
width: 80.0,
|
||||
decoration: BoxDecoration(
|
||||
color: AppColors.grey100,
|
||||
borderRadius: BorderRadius.circular(4.0),
|
||||
),
|
||||
),
|
||||
|
||||
const SizedBox(height: 8.0),
|
||||
|
||||
// Price placeholder
|
||||
Container(
|
||||
height: 16.0,
|
||||
width: 100.0,
|
||||
decoration: BoxDecoration(
|
||||
color: AppColors.grey100,
|
||||
borderRadius: BorderRadius.circular(4.0),
|
||||
),
|
||||
),
|
||||
|
||||
const SizedBox(height: 12.0),
|
||||
|
||||
// Button placeholder
|
||||
Container(
|
||||
height: 36.0,
|
||||
width: double.infinity,
|
||||
decoration: BoxDecoration(
|
||||
color: AppColors.grey100,
|
||||
borderRadius: BorderRadius.circular(AppRadius.button),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// ERROR STATE
|
||||
// ============================================================================
|
||||
|
||||
/// Error State Widget
|
||||
///
|
||||
/// Displayed when there's an error loading favorites.
|
||||
class _ErrorState extends StatelessWidget {
|
||||
final Object error;
|
||||
final VoidCallback onRetry;
|
||||
|
||||
const _ErrorState({
|
||||
required this.error,
|
||||
required this.onRetry,
|
||||
});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Center(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(AppSpacing.xl),
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
// Error icon
|
||||
Icon(
|
||||
Icons.error_outline,
|
||||
size: 80.0,
|
||||
color: AppColors.danger.withValues(alpha: 0.7),
|
||||
),
|
||||
|
||||
const SizedBox(height: AppSpacing.lg),
|
||||
|
||||
// Title
|
||||
const Text(
|
||||
'Có lỗi xảy ra',
|
||||
style: TextStyle(
|
||||
fontSize: 18.0,
|
||||
fontWeight: FontWeight.w600,
|
||||
color: AppColors.grey900,
|
||||
),
|
||||
textAlign: TextAlign.center,
|
||||
),
|
||||
|
||||
const SizedBox(height: AppSpacing.sm),
|
||||
|
||||
// Error message
|
||||
Text(
|
||||
error.toString(),
|
||||
style: const TextStyle(
|
||||
fontSize: 14.0,
|
||||
color: AppColors.grey500,
|
||||
),
|
||||
textAlign: TextAlign.center,
|
||||
maxLines: 3,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
),
|
||||
|
||||
const SizedBox(height: AppSpacing.xl),
|
||||
|
||||
// Retry Button
|
||||
ElevatedButton.icon(
|
||||
onPressed: onRetry,
|
||||
style: ElevatedButton.styleFrom(
|
||||
backgroundColor: AppColors.primaryBlue,
|
||||
foregroundColor: AppColors.white,
|
||||
padding: const EdgeInsets.symmetric(
|
||||
horizontal: AppSpacing.xl,
|
||||
vertical: AppSpacing.md,
|
||||
),
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(AppRadius.button),
|
||||
),
|
||||
),
|
||||
icon: const Icon(Icons.refresh),
|
||||
label: const Text(
|
||||
'Thử lại',
|
||||
style: TextStyle(
|
||||
fontSize: 16.0,
|
||||
fontWeight: FontWeight.w600,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// FAVORITES GRID
|
||||
// ============================================================================
|
||||
|
||||
/// Favorites Grid Widget
|
||||
///
|
||||
/// Displays favorite products in a grid layout.
|
||||
class _FavoritesGrid extends StatelessWidget {
|
||||
final List<Product> products;
|
||||
|
||||
const _FavoritesGrid({
|
||||
required this.products,
|
||||
});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return GridView.builder(
|
||||
padding: const EdgeInsets.all(AppSpacing.md),
|
||||
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
|
||||
crossAxisCount: GridSpecs.productGridColumns,
|
||||
crossAxisSpacing: 12.0,
|
||||
mainAxisSpacing: 12.0,
|
||||
childAspectRatio: 0.62, // Same as products page
|
||||
),
|
||||
itemCount: products.length,
|
||||
itemBuilder: (context, index) {
|
||||
final product = products[index];
|
||||
return RepaintBoundary(
|
||||
child: FavoriteProductCard(product: product),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,273 @@
|
||||
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
||||
import 'package:worker/features/favorites/data/datasources/favorites_local_datasource.dart';
|
||||
import 'package:worker/features/favorites/data/models/favorite_model.dart';
|
||||
import 'package:worker/features/products/data/datasources/products_local_datasource.dart';
|
||||
import 'package:worker/features/products/data/repositories/products_repository_impl.dart';
|
||||
import 'package:worker/features/products/domain/entities/product.dart';
|
||||
import 'package:worker/features/products/domain/usecases/get_products.dart';
|
||||
|
||||
part 'favorites_provider.g.dart';
|
||||
|
||||
// ============================================================================
|
||||
// DATASOURCE PROVIDER
|
||||
// ============================================================================
|
||||
|
||||
/// Provides instance of FavoritesLocalDataSource
|
||||
@riverpod
|
||||
FavoritesLocalDataSource favoritesLocalDataSource(Ref ref) {
|
||||
return FavoritesLocalDataSource();
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// CURRENT USER ID PROVIDER
|
||||
// ============================================================================
|
||||
|
||||
/// Provides the current logged-in user's ID
|
||||
///
|
||||
/// TODO: Replace with actual auth provider integration
|
||||
/// For now, using hardcoded userId for development
|
||||
@riverpod
|
||||
String currentUserId(Ref ref) {
|
||||
// TODO: Integrate with actual auth provider when available
|
||||
// Example: return ref.watch(authProvider).user?.id ?? 'user_001';
|
||||
return 'user_001';
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// MAIN FAVORITES PROVIDER
|
||||
// ============================================================================
|
||||
|
||||
/// Manages the favorites state for the current user
|
||||
///
|
||||
/// Uses a Set<String> to store product IDs for efficient lookup.
|
||||
/// Data is persisted to Hive for offline access.
|
||||
@riverpod
|
||||
class Favorites extends _$Favorites {
|
||||
late FavoritesLocalDataSource _dataSource;
|
||||
late String _userId;
|
||||
|
||||
@override
|
||||
Future<Set<String>> build() async {
|
||||
_dataSource = ref.read(favoritesLocalDataSourceProvider);
|
||||
_userId = ref.read(currentUserIdProvider);
|
||||
|
||||
// Load favorites from Hive
|
||||
return await _loadFavorites();
|
||||
}
|
||||
|
||||
// ==========================================================================
|
||||
// PRIVATE METHODS
|
||||
// ==========================================================================
|
||||
|
||||
/// Load favorites from Hive database
|
||||
Future<Set<String>> _loadFavorites() async {
|
||||
try {
|
||||
final favorites = await _dataSource.getAllFavorites(_userId);
|
||||
final productIds = favorites.map((fav) => fav.productId).toSet();
|
||||
|
||||
debugPrint('Loaded ${productIds.length} favorites for user: $_userId');
|
||||
return productIds;
|
||||
} catch (e) {
|
||||
debugPrint('Error loading favorites: $e');
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
/// Generate a unique favorite ID
|
||||
String _generateFavoriteId(String productId) {
|
||||
// Using format: userId_productId_timestamp
|
||||
final timestamp = DateTime.now().millisecondsSinceEpoch;
|
||||
return '${_userId}_${productId}_$timestamp';
|
||||
}
|
||||
|
||||
// ==========================================================================
|
||||
// PUBLIC METHODS
|
||||
// ==========================================================================
|
||||
|
||||
/// Add a product to favorites
|
||||
///
|
||||
/// Creates a new favorite entry and persists it to Hive.
|
||||
/// If the product is already favorited, this operation is a no-op.
|
||||
Future<void> addFavorite(String productId) async {
|
||||
try {
|
||||
// Check if already favorited
|
||||
final currentState = state.value ?? <String>{};
|
||||
if (currentState.contains(productId)) {
|
||||
debugPrint('Product $productId is already favorited');
|
||||
return;
|
||||
}
|
||||
|
||||
// Create favorite model
|
||||
final favorite = FavoriteModel(
|
||||
favoriteId: _generateFavoriteId(productId),
|
||||
productId: productId,
|
||||
userId: _userId,
|
||||
createdAt: DateTime.now(),
|
||||
);
|
||||
|
||||
// Persist to Hive
|
||||
await _dataSource.addFavorite(favorite);
|
||||
|
||||
// Update state
|
||||
final newState = <String>{...currentState, productId};
|
||||
state = AsyncValue.data(newState);
|
||||
|
||||
debugPrint('Added favorite: $productId');
|
||||
} catch (e, stackTrace) {
|
||||
debugPrint('Error adding favorite: $e');
|
||||
state = AsyncValue.error(e, stackTrace);
|
||||
}
|
||||
}
|
||||
|
||||
/// Remove a product from favorites
|
||||
///
|
||||
/// Removes the favorite entry from Hive.
|
||||
/// If the product is not favorited, this operation is a no-op.
|
||||
Future<void> removeFavorite(String productId) async {
|
||||
try {
|
||||
// Check if favorited
|
||||
final currentState = state.value ?? <String>{};
|
||||
if (!currentState.contains(productId)) {
|
||||
debugPrint('Product $productId is not favorited');
|
||||
return;
|
||||
}
|
||||
|
||||
// Remove from Hive
|
||||
await _dataSource.removeFavorite(productId, _userId);
|
||||
|
||||
// Update state
|
||||
final newState = <String>{...currentState};
|
||||
newState.remove(productId);
|
||||
state = AsyncValue.data(newState);
|
||||
|
||||
debugPrint('Removed favorite: $productId');
|
||||
} catch (e, stackTrace) {
|
||||
debugPrint('Error removing favorite: $e');
|
||||
state = AsyncValue.error(e, stackTrace);
|
||||
}
|
||||
}
|
||||
|
||||
/// Toggle favorite status for a product
|
||||
///
|
||||
/// If the product is favorited, it will be removed.
|
||||
/// If the product is not favorited, it will be added.
|
||||
Future<void> toggleFavorite(String productId) async {
|
||||
final currentState = state.value ?? <String>{};
|
||||
|
||||
if (currentState.contains(productId)) {
|
||||
await removeFavorite(productId);
|
||||
} else {
|
||||
await addFavorite(productId);
|
||||
}
|
||||
}
|
||||
|
||||
/// Refresh favorites from database
|
||||
///
|
||||
/// Useful for syncing state after external changes or on app resume.
|
||||
Future<void> refresh() async {
|
||||
state = const AsyncValue.loading();
|
||||
state = await AsyncValue.guard(() async {
|
||||
return await _loadFavorites();
|
||||
});
|
||||
}
|
||||
|
||||
/// Clear all favorites for the current user
|
||||
///
|
||||
/// Removes all favorite entries from Hive.
|
||||
Future<void> clearAll() async {
|
||||
try {
|
||||
await _dataSource.clearFavorites(_userId);
|
||||
state = const AsyncValue.data({});
|
||||
debugPrint('Cleared all favorites for user: $_userId');
|
||||
} catch (e, stackTrace) {
|
||||
debugPrint('Error clearing favorites: $e');
|
||||
state = AsyncValue.error(e, stackTrace);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// HELPER PROVIDERS
|
||||
// ============================================================================
|
||||
|
||||
/// Check if a specific product is favorited
|
||||
///
|
||||
/// Returns true if the product is in the user's favorites, false otherwise.
|
||||
/// Safe to use in build methods - will return false during loading/error states.
|
||||
@riverpod
|
||||
bool isFavorite(Ref ref, String productId) {
|
||||
final favoritesAsync = ref.watch(favoritesProvider);
|
||||
|
||||
return favoritesAsync.when(
|
||||
data: (favorites) => favorites.contains(productId),
|
||||
loading: () => false,
|
||||
error: (_, __) => false,
|
||||
);
|
||||
}
|
||||
|
||||
/// Get the total count of favorites
|
||||
///
|
||||
/// Returns the number of products in the user's favorites.
|
||||
/// Safe to use in build methods - will return 0 during loading/error states.
|
||||
@riverpod
|
||||
int favoriteCount(Ref ref) {
|
||||
final favoritesAsync = ref.watch(favoritesProvider);
|
||||
|
||||
return favoritesAsync.when(
|
||||
data: (favorites) => favorites.length,
|
||||
loading: () => 0,
|
||||
error: (_, __) => 0,
|
||||
);
|
||||
}
|
||||
|
||||
/// Get all favorite product IDs as a list
|
||||
///
|
||||
/// Useful for filtering product lists or bulk operations.
|
||||
/// Returns an empty list during loading/error states.
|
||||
@riverpod
|
||||
List<String> favoriteProductIds(Ref ref) {
|
||||
final favoritesAsync = ref.watch(favoritesProvider);
|
||||
|
||||
return favoritesAsync.when(
|
||||
data: (favorites) => favorites.toList(),
|
||||
loading: () => <String>[],
|
||||
error: (_, __) => <String>[],
|
||||
);
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// FAVORITE PRODUCTS PROVIDER
|
||||
// ============================================================================
|
||||
|
||||
/// Get actual Product entities for favorited product IDs
|
||||
///
|
||||
/// Combines favorites state with products data to return full Product objects.
|
||||
/// This is useful for displaying favorite products with complete information.
|
||||
@riverpod
|
||||
Future<List<Product>> favoriteProducts(Ref ref) async {
|
||||
final favoriteIds = ref.watch(favoriteProductIdsProvider);
|
||||
|
||||
if (favoriteIds.isEmpty) {
|
||||
return [];
|
||||
}
|
||||
|
||||
// Import products provider to get all products
|
||||
const productsRepository = ProductsRepositoryImpl(
|
||||
localDataSource: ProductsLocalDataSourceImpl(),
|
||||
);
|
||||
|
||||
const getProductsUseCase = GetProducts(productsRepository);
|
||||
final allProducts = await getProductsUseCase();
|
||||
|
||||
// Filter to only include favorited products
|
||||
return allProducts.where((product) => favoriteIds.contains(product.productId)).toList();
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// DEBUG UTILITIES
|
||||
// ============================================================================
|
||||
|
||||
/// Debug print helper
|
||||
void debugPrint(String message) {
|
||||
print('[FavoritesProvider] $message');
|
||||
}
|
||||
@@ -0,0 +1,443 @@
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
|
||||
part of 'favorites_provider.dart';
|
||||
|
||||
// **************************************************************************
|
||||
// RiverpodGenerator
|
||||
// **************************************************************************
|
||||
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
// ignore_for_file: type=lint, type=warning
|
||||
/// Provides instance of FavoritesLocalDataSource
|
||||
|
||||
@ProviderFor(favoritesLocalDataSource)
|
||||
const favoritesLocalDataSourceProvider = FavoritesLocalDataSourceProvider._();
|
||||
|
||||
/// Provides instance of FavoritesLocalDataSource
|
||||
|
||||
final class FavoritesLocalDataSourceProvider
|
||||
extends
|
||||
$FunctionalProvider<
|
||||
FavoritesLocalDataSource,
|
||||
FavoritesLocalDataSource,
|
||||
FavoritesLocalDataSource
|
||||
>
|
||||
with $Provider<FavoritesLocalDataSource> {
|
||||
/// Provides instance of FavoritesLocalDataSource
|
||||
const FavoritesLocalDataSourceProvider._()
|
||||
: super(
|
||||
from: null,
|
||||
argument: null,
|
||||
retry: null,
|
||||
name: r'favoritesLocalDataSourceProvider',
|
||||
isAutoDispose: true,
|
||||
dependencies: null,
|
||||
$allTransitiveDependencies: null,
|
||||
);
|
||||
|
||||
@override
|
||||
String debugGetCreateSourceHash() => _$favoritesLocalDataSourceHash();
|
||||
|
||||
@$internal
|
||||
@override
|
||||
$ProviderElement<FavoritesLocalDataSource> $createElement(
|
||||
$ProviderPointer pointer,
|
||||
) => $ProviderElement(pointer);
|
||||
|
||||
@override
|
||||
FavoritesLocalDataSource create(Ref ref) {
|
||||
return favoritesLocalDataSource(ref);
|
||||
}
|
||||
|
||||
/// {@macro riverpod.override_with_value}
|
||||
Override overrideWithValue(FavoritesLocalDataSource value) {
|
||||
return $ProviderOverride(
|
||||
origin: this,
|
||||
providerOverride: $SyncValueProvider<FavoritesLocalDataSource>(value),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
String _$favoritesLocalDataSourceHash() =>
|
||||
r'2f6ff99042b7cc1087d8cfdad517f448952c25be';
|
||||
|
||||
/// Provides the current logged-in user's ID
|
||||
///
|
||||
/// TODO: Replace with actual auth provider integration
|
||||
/// For now, using hardcoded userId for development
|
||||
|
||||
@ProviderFor(currentUserId)
|
||||
const currentUserIdProvider = CurrentUserIdProvider._();
|
||||
|
||||
/// Provides the current logged-in user's ID
|
||||
///
|
||||
/// TODO: Replace with actual auth provider integration
|
||||
/// For now, using hardcoded userId for development
|
||||
|
||||
final class CurrentUserIdProvider
|
||||
extends $FunctionalProvider<String, String, String>
|
||||
with $Provider<String> {
|
||||
/// Provides the current logged-in user's ID
|
||||
///
|
||||
/// TODO: Replace with actual auth provider integration
|
||||
/// For now, using hardcoded userId for development
|
||||
const CurrentUserIdProvider._()
|
||||
: super(
|
||||
from: null,
|
||||
argument: null,
|
||||
retry: null,
|
||||
name: r'currentUserIdProvider',
|
||||
isAutoDispose: true,
|
||||
dependencies: null,
|
||||
$allTransitiveDependencies: null,
|
||||
);
|
||||
|
||||
@override
|
||||
String debugGetCreateSourceHash() => _$currentUserIdHash();
|
||||
|
||||
@$internal
|
||||
@override
|
||||
$ProviderElement<String> $createElement($ProviderPointer pointer) =>
|
||||
$ProviderElement(pointer);
|
||||
|
||||
@override
|
||||
String create(Ref ref) {
|
||||
return currentUserId(ref);
|
||||
}
|
||||
|
||||
/// {@macro riverpod.override_with_value}
|
||||
Override overrideWithValue(String value) {
|
||||
return $ProviderOverride(
|
||||
origin: this,
|
||||
providerOverride: $SyncValueProvider<String>(value),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
String _$currentUserIdHash() => r'7f968e463454a4ad87bce0442f62ecc24a6f756e';
|
||||
|
||||
/// Manages the favorites state for the current user
|
||||
///
|
||||
/// Uses a Set<String> to store product IDs for efficient lookup.
|
||||
/// Data is persisted to Hive for offline access.
|
||||
|
||||
@ProviderFor(Favorites)
|
||||
const favoritesProvider = FavoritesProvider._();
|
||||
|
||||
/// Manages the favorites state for the current user
|
||||
///
|
||||
/// Uses a Set<String> to store product IDs for efficient lookup.
|
||||
/// Data is persisted to Hive for offline access.
|
||||
final class FavoritesProvider
|
||||
extends $AsyncNotifierProvider<Favorites, Set<String>> {
|
||||
/// Manages the favorites state for the current user
|
||||
///
|
||||
/// Uses a Set<String> to store product IDs for efficient lookup.
|
||||
/// Data is persisted to Hive for offline access.
|
||||
const FavoritesProvider._()
|
||||
: super(
|
||||
from: null,
|
||||
argument: null,
|
||||
retry: null,
|
||||
name: r'favoritesProvider',
|
||||
isAutoDispose: true,
|
||||
dependencies: null,
|
||||
$allTransitiveDependencies: null,
|
||||
);
|
||||
|
||||
@override
|
||||
String debugGetCreateSourceHash() => _$favoritesHash();
|
||||
|
||||
@$internal
|
||||
@override
|
||||
Favorites create() => Favorites();
|
||||
}
|
||||
|
||||
String _$favoritesHash() => r'fccd46f5cd1bbf2b58a13ea90c6d1644ece767b0';
|
||||
|
||||
/// Manages the favorites state for the current user
|
||||
///
|
||||
/// Uses a Set<String> to store product IDs for efficient lookup.
|
||||
/// Data is persisted to Hive for offline access.
|
||||
|
||||
abstract class _$Favorites extends $AsyncNotifier<Set<String>> {
|
||||
FutureOr<Set<String>> build();
|
||||
@$mustCallSuper
|
||||
@override
|
||||
void runBuild() {
|
||||
final created = build();
|
||||
final ref = this.ref as $Ref<AsyncValue<Set<String>>, Set<String>>;
|
||||
final element =
|
||||
ref.element
|
||||
as $ClassProviderElement<
|
||||
AnyNotifier<AsyncValue<Set<String>>, Set<String>>,
|
||||
AsyncValue<Set<String>>,
|
||||
Object?,
|
||||
Object?
|
||||
>;
|
||||
element.handleValue(ref, created);
|
||||
}
|
||||
}
|
||||
|
||||
/// Check if a specific product is favorited
|
||||
///
|
||||
/// Returns true if the product is in the user's favorites, false otherwise.
|
||||
/// Safe to use in build methods - will return false during loading/error states.
|
||||
|
||||
@ProviderFor(isFavorite)
|
||||
const isFavoriteProvider = IsFavoriteFamily._();
|
||||
|
||||
/// Check if a specific product is favorited
|
||||
///
|
||||
/// Returns true if the product is in the user's favorites, false otherwise.
|
||||
/// Safe to use in build methods - will return false during loading/error states.
|
||||
|
||||
final class IsFavoriteProvider extends $FunctionalProvider<bool, bool, bool>
|
||||
with $Provider<bool> {
|
||||
/// Check if a specific product is favorited
|
||||
///
|
||||
/// Returns true if the product is in the user's favorites, false otherwise.
|
||||
/// Safe to use in build methods - will return false during loading/error states.
|
||||
const IsFavoriteProvider._({
|
||||
required IsFavoriteFamily super.from,
|
||||
required String super.argument,
|
||||
}) : super(
|
||||
retry: null,
|
||||
name: r'isFavoriteProvider',
|
||||
isAutoDispose: true,
|
||||
dependencies: null,
|
||||
$allTransitiveDependencies: null,
|
||||
);
|
||||
|
||||
@override
|
||||
String debugGetCreateSourceHash() => _$isFavoriteHash();
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return r'isFavoriteProvider'
|
||||
''
|
||||
'($argument)';
|
||||
}
|
||||
|
||||
@$internal
|
||||
@override
|
||||
$ProviderElement<bool> $createElement($ProviderPointer pointer) =>
|
||||
$ProviderElement(pointer);
|
||||
|
||||
@override
|
||||
bool create(Ref ref) {
|
||||
final argument = this.argument as String;
|
||||
return isFavorite(ref, argument);
|
||||
}
|
||||
|
||||
/// {@macro riverpod.override_with_value}
|
||||
Override overrideWithValue(bool value) {
|
||||
return $ProviderOverride(
|
||||
origin: this,
|
||||
providerOverride: $SyncValueProvider<bool>(value),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
bool operator ==(Object other) {
|
||||
return other is IsFavoriteProvider && other.argument == argument;
|
||||
}
|
||||
|
||||
@override
|
||||
int get hashCode {
|
||||
return argument.hashCode;
|
||||
}
|
||||
}
|
||||
|
||||
String _$isFavoriteHash() => r'8d69e5efe981a3717eebdd7ee192fd75afe722d5';
|
||||
|
||||
/// Check if a specific product is favorited
|
||||
///
|
||||
/// Returns true if the product is in the user's favorites, false otherwise.
|
||||
/// Safe to use in build methods - will return false during loading/error states.
|
||||
|
||||
final class IsFavoriteFamily extends $Family
|
||||
with $FunctionalFamilyOverride<bool, String> {
|
||||
const IsFavoriteFamily._()
|
||||
: super(
|
||||
retry: null,
|
||||
name: r'isFavoriteProvider',
|
||||
dependencies: null,
|
||||
$allTransitiveDependencies: null,
|
||||
isAutoDispose: true,
|
||||
);
|
||||
|
||||
/// Check if a specific product is favorited
|
||||
///
|
||||
/// Returns true if the product is in the user's favorites, false otherwise.
|
||||
/// Safe to use in build methods - will return false during loading/error states.
|
||||
|
||||
IsFavoriteProvider call(String productId) =>
|
||||
IsFavoriteProvider._(argument: productId, from: this);
|
||||
|
||||
@override
|
||||
String toString() => r'isFavoriteProvider';
|
||||
}
|
||||
|
||||
/// Get the total count of favorites
|
||||
///
|
||||
/// Returns the number of products in the user's favorites.
|
||||
/// Safe to use in build methods - will return 0 during loading/error states.
|
||||
|
||||
@ProviderFor(favoriteCount)
|
||||
const favoriteCountProvider = FavoriteCountProvider._();
|
||||
|
||||
/// Get the total count of favorites
|
||||
///
|
||||
/// Returns the number of products in the user's favorites.
|
||||
/// Safe to use in build methods - will return 0 during loading/error states.
|
||||
|
||||
final class FavoriteCountProvider extends $FunctionalProvider<int, int, int>
|
||||
with $Provider<int> {
|
||||
/// Get the total count of favorites
|
||||
///
|
||||
/// Returns the number of products in the user's favorites.
|
||||
/// Safe to use in build methods - will return 0 during loading/error states.
|
||||
const FavoriteCountProvider._()
|
||||
: super(
|
||||
from: null,
|
||||
argument: null,
|
||||
retry: null,
|
||||
name: r'favoriteCountProvider',
|
||||
isAutoDispose: true,
|
||||
dependencies: null,
|
||||
$allTransitiveDependencies: null,
|
||||
);
|
||||
|
||||
@override
|
||||
String debugGetCreateSourceHash() => _$favoriteCountHash();
|
||||
|
||||
@$internal
|
||||
@override
|
||||
$ProviderElement<int> $createElement($ProviderPointer pointer) =>
|
||||
$ProviderElement(pointer);
|
||||
|
||||
@override
|
||||
int create(Ref ref) {
|
||||
return favoriteCount(ref);
|
||||
}
|
||||
|
||||
/// {@macro riverpod.override_with_value}
|
||||
Override overrideWithValue(int value) {
|
||||
return $ProviderOverride(
|
||||
origin: this,
|
||||
providerOverride: $SyncValueProvider<int>(value),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
String _$favoriteCountHash() => r'1f147fe5ef28b1477034bd567cfc05ab3e8e90db';
|
||||
|
||||
/// Get all favorite product IDs as a list
|
||||
///
|
||||
/// Useful for filtering product lists or bulk operations.
|
||||
/// Returns an empty list during loading/error states.
|
||||
|
||||
@ProviderFor(favoriteProductIds)
|
||||
const favoriteProductIdsProvider = FavoriteProductIdsProvider._();
|
||||
|
||||
/// Get all favorite product IDs as a list
|
||||
///
|
||||
/// Useful for filtering product lists or bulk operations.
|
||||
/// Returns an empty list during loading/error states.
|
||||
|
||||
final class FavoriteProductIdsProvider
|
||||
extends $FunctionalProvider<List<String>, List<String>, List<String>>
|
||||
with $Provider<List<String>> {
|
||||
/// Get all favorite product IDs as a list
|
||||
///
|
||||
/// Useful for filtering product lists or bulk operations.
|
||||
/// Returns an empty list during loading/error states.
|
||||
const FavoriteProductIdsProvider._()
|
||||
: super(
|
||||
from: null,
|
||||
argument: null,
|
||||
retry: null,
|
||||
name: r'favoriteProductIdsProvider',
|
||||
isAutoDispose: true,
|
||||
dependencies: null,
|
||||
$allTransitiveDependencies: null,
|
||||
);
|
||||
|
||||
@override
|
||||
String debugGetCreateSourceHash() => _$favoriteProductIdsHash();
|
||||
|
||||
@$internal
|
||||
@override
|
||||
$ProviderElement<List<String>> $createElement($ProviderPointer pointer) =>
|
||||
$ProviderElement(pointer);
|
||||
|
||||
@override
|
||||
List<String> create(Ref ref) {
|
||||
return favoriteProductIds(ref);
|
||||
}
|
||||
|
||||
/// {@macro riverpod.override_with_value}
|
||||
Override overrideWithValue(List<String> value) {
|
||||
return $ProviderOverride(
|
||||
origin: this,
|
||||
providerOverride: $SyncValueProvider<List<String>>(value),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
String _$favoriteProductIdsHash() =>
|
||||
r'a6814af9a1775b908b4101e64ce3056e1534b561';
|
||||
|
||||
/// Get actual Product entities for favorited product IDs
|
||||
///
|
||||
/// Combines favorites state with products data to return full Product objects.
|
||||
/// This is useful for displaying favorite products with complete information.
|
||||
|
||||
@ProviderFor(favoriteProducts)
|
||||
const favoriteProductsProvider = FavoriteProductsProvider._();
|
||||
|
||||
/// Get actual Product entities for favorited product IDs
|
||||
///
|
||||
/// Combines favorites state with products data to return full Product objects.
|
||||
/// This is useful for displaying favorite products with complete information.
|
||||
|
||||
final class FavoriteProductsProvider
|
||||
extends
|
||||
$FunctionalProvider<
|
||||
AsyncValue<List<Product>>,
|
||||
List<Product>,
|
||||
FutureOr<List<Product>>
|
||||
>
|
||||
with $FutureModifier<List<Product>>, $FutureProvider<List<Product>> {
|
||||
/// Get actual Product entities for favorited product IDs
|
||||
///
|
||||
/// Combines favorites state with products data to return full Product objects.
|
||||
/// This is useful for displaying favorite products with complete information.
|
||||
const FavoriteProductsProvider._()
|
||||
: super(
|
||||
from: null,
|
||||
argument: null,
|
||||
retry: null,
|
||||
name: r'favoriteProductsProvider',
|
||||
isAutoDispose: true,
|
||||
dependencies: null,
|
||||
$allTransitiveDependencies: null,
|
||||
);
|
||||
|
||||
@override
|
||||
String debugGetCreateSourceHash() => _$favoriteProductsHash();
|
||||
|
||||
@$internal
|
||||
@override
|
||||
$FutureProviderElement<List<Product>> $createElement(
|
||||
$ProviderPointer pointer,
|
||||
) => $FutureProviderElement(pointer);
|
||||
|
||||
@override
|
||||
FutureOr<List<Product>> create(Ref ref) {
|
||||
return favoriteProducts(ref);
|
||||
}
|
||||
}
|
||||
|
||||
String _$favoriteProductsHash() => r'cb3af4f84591c94e9eed3322b167fd8050a40aa1';
|
||||
@@ -0,0 +1,242 @@
|
||||
/// Widget: Favorite Product Card
|
||||
///
|
||||
/// Displays a favorited product in a card format with image, name, price,
|
||||
/// and favorite toggle button.
|
||||
library;
|
||||
|
||||
import 'package:cached_network_image/cached_network_image.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
import 'package:intl/intl.dart';
|
||||
import 'package:shimmer/shimmer.dart';
|
||||
import 'package:worker/core/constants/ui_constants.dart';
|
||||
import 'package:worker/core/theme/colors.dart';
|
||||
import 'package:worker/features/favorites/presentation/providers/favorites_provider.dart';
|
||||
import 'package:worker/features/products/domain/entities/product.dart';
|
||||
|
||||
/// Favorite Product Card Widget
|
||||
///
|
||||
/// Displays product information in a card format with a favorite toggle button.
|
||||
/// Used in the favorites grid view.
|
||||
class FavoriteProductCard extends ConsumerWidget {
|
||||
final Product product;
|
||||
|
||||
const FavoriteProductCard({
|
||||
super.key,
|
||||
required this.product,
|
||||
});
|
||||
|
||||
String _formatPrice(double price) {
|
||||
final formatter = NumberFormat('#,###', 'vi_VN');
|
||||
return '${formatter.format(price)}đ';
|
||||
}
|
||||
|
||||
/// Show confirmation dialog before removing from favorites
|
||||
Future<void> _showRemoveDialog(BuildContext context, WidgetRef ref) async {
|
||||
final confirmed = await showDialog<bool>(
|
||||
context: context,
|
||||
builder: (context) => AlertDialog(
|
||||
title: const Text('Xóa khỏi yêu thích?'),
|
||||
content: const Text(
|
||||
'Bạn có chắc muốn xóa sản phẩm này khỏi danh sách yêu thích?',
|
||||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.of(context).pop(false),
|
||||
child: const Text('Hủy'),
|
||||
),
|
||||
ElevatedButton(
|
||||
onPressed: () => Navigator.of(context).pop(true),
|
||||
style: ElevatedButton.styleFrom(
|
||||
backgroundColor: AppColors.danger,
|
||||
foregroundColor: AppColors.white,
|
||||
),
|
||||
child: const Text('Xóa'),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
|
||||
if (confirmed == true && context.mounted) {
|
||||
// Remove from favorites
|
||||
await ref.read(favoritesProvider.notifier).removeFavorite(product.productId);
|
||||
|
||||
// Show snackbar
|
||||
if (context.mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(
|
||||
content: Text('Đã xóa khỏi yêu thích'),
|
||||
duration: Duration(seconds: 2),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
return Card(
|
||||
elevation: ProductCardSpecs.elevation,
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(ProductCardSpecs.borderRadius),
|
||||
),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
// Product Image with Favorite Button
|
||||
Expanded(
|
||||
child: Stack(
|
||||
children: [
|
||||
// Image
|
||||
ClipRRect(
|
||||
borderRadius: const BorderRadius.vertical(
|
||||
top: Radius.circular(ProductCardSpecs.borderRadius),
|
||||
),
|
||||
child: CachedNetworkImage(
|
||||
imageUrl: product.imageUrl,
|
||||
width: double.infinity,
|
||||
height: double.infinity,
|
||||
fit: BoxFit.cover,
|
||||
memCacheWidth: ImageSpecs.productImageCacheWidth,
|
||||
memCacheHeight: ImageSpecs.productImageCacheHeight,
|
||||
placeholder: (context, url) => Shimmer.fromColors(
|
||||
baseColor: AppColors.grey100,
|
||||
highlightColor: AppColors.grey50,
|
||||
child: Container(
|
||||
color: AppColors.grey100,
|
||||
),
|
||||
),
|
||||
errorWidget: (context, url, error) => Container(
|
||||
color: AppColors.grey100,
|
||||
child: const Icon(
|
||||
Icons.image_not_supported,
|
||||
size: 48.0,
|
||||
color: AppColors.grey500,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
|
||||
// Favorite Button (top-right)
|
||||
Positioned(
|
||||
top: AppSpacing.sm,
|
||||
right: AppSpacing.sm,
|
||||
child: Container(
|
||||
decoration: BoxDecoration(
|
||||
color: AppColors.white,
|
||||
shape: BoxShape.circle,
|
||||
boxShadow: [
|
||||
BoxShadow(
|
||||
color: Colors.black.withValues(alpha: 0.15),
|
||||
blurRadius: 8,
|
||||
offset: const Offset(0, 2),
|
||||
),
|
||||
],
|
||||
),
|
||||
child: IconButton(
|
||||
icon: const Icon(
|
||||
Icons.favorite,
|
||||
color: AppColors.danger,
|
||||
size: 20.0,
|
||||
),
|
||||
padding: const EdgeInsets.all(AppSpacing.sm),
|
||||
constraints: const BoxConstraints(
|
||||
minWidth: 36,
|
||||
minHeight: 36,
|
||||
),
|
||||
onPressed: () => _showRemoveDialog(context, ref),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
|
||||
// Product Info
|
||||
Padding(
|
||||
padding: const EdgeInsets.all(AppSpacing.sm),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
// Product Name
|
||||
Text(
|
||||
product.name,
|
||||
style: const TextStyle(
|
||||
fontSize: 14.0,
|
||||
fontWeight: FontWeight.w600,
|
||||
height: 1.3,
|
||||
),
|
||||
maxLines: 2,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
),
|
||||
|
||||
const SizedBox(height: AppSpacing.xs),
|
||||
|
||||
// SKU (if available)
|
||||
if (product.erpnextItemCode != null)
|
||||
Text(
|
||||
'Mã: ${product.erpnextItemCode}',
|
||||
style: const TextStyle(
|
||||
fontSize: 12.0,
|
||||
color: AppColors.grey500,
|
||||
),
|
||||
maxLines: 1,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
),
|
||||
|
||||
const SizedBox(height: AppSpacing.xs),
|
||||
|
||||
// Price
|
||||
Text(
|
||||
_formatPrice(product.effectivePrice),
|
||||
style: const TextStyle(
|
||||
fontSize: 16.0,
|
||||
fontWeight: FontWeight.bold,
|
||||
color: AppColors.primaryBlue,
|
||||
),
|
||||
),
|
||||
|
||||
const SizedBox(height: AppSpacing.sm),
|
||||
|
||||
// View Details Button
|
||||
SizedBox(
|
||||
width: double.infinity,
|
||||
height: 36.0,
|
||||
child: OutlinedButton(
|
||||
onPressed: () {
|
||||
// Navigate to product detail
|
||||
context.push('/products/${product.productId}');
|
||||
},
|
||||
style: OutlinedButton.styleFrom(
|
||||
foregroundColor: AppColors.primaryBlue,
|
||||
side: const BorderSide(
|
||||
color: AppColors.primaryBlue,
|
||||
width: 1.5,
|
||||
),
|
||||
elevation: 0,
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(AppRadius.button),
|
||||
),
|
||||
padding: const EdgeInsets.symmetric(
|
||||
horizontal: AppSpacing.sm,
|
||||
),
|
||||
),
|
||||
child: const Text(
|
||||
'Xem chi tiết',
|
||||
style: TextStyle(
|
||||
fontSize: 12.0,
|
||||
fontWeight: FontWeight.w600,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -142,8 +142,7 @@ class HomePage extends ConsumerWidget {
|
||||
QuickAction(
|
||||
icon: Icons.favorite,
|
||||
label: 'Yêu thích',
|
||||
onTap: () =>
|
||||
_showComingSoon(context, 'Yêu thích', l10n),
|
||||
onTap: () => context.push(RouteNames.favorites),
|
||||
),
|
||||
],
|
||||
),
|
||||
|
||||
@@ -5,18 +5,20 @@ library;
|
||||
|
||||
import 'package:cached_network_image/cached_network_image.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:intl/intl.dart';
|
||||
import 'package:shimmer/shimmer.dart';
|
||||
import 'package:worker/core/constants/ui_constants.dart';
|
||||
import 'package:worker/core/theme/colors.dart';
|
||||
import 'package:worker/features/favorites/presentation/providers/favorites_provider.dart';
|
||||
import 'package:worker/features/products/domain/entities/product.dart';
|
||||
import 'package:worker/generated/l10n/app_localizations.dart';
|
||||
|
||||
/// Product Card Widget
|
||||
///
|
||||
/// Displays product information in a card format.
|
||||
/// Includes image, name, price, stock status, and add to cart button.
|
||||
class ProductCard extends StatelessWidget {
|
||||
/// Includes image, name, price, stock status, favorite toggle, and add to cart button.
|
||||
class ProductCard extends ConsumerWidget {
|
||||
final Product product;
|
||||
final VoidCallback? onTap;
|
||||
final VoidCallback? onAddToCart;
|
||||
@@ -34,8 +36,9 @@ class ProductCard extends StatelessWidget {
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final l10n = AppLocalizations.of(context)!;
|
||||
final isFavorited = ref.watch(isFavoriteProvider(product.productId));
|
||||
|
||||
return Card(
|
||||
elevation: ProductCardSpecs.elevation,
|
||||
@@ -131,6 +134,56 @@ class ProductCard extends StatelessWidget {
|
||||
),
|
||||
),
|
||||
),
|
||||
|
||||
// Favorite Button (bottom-left corner)
|
||||
Positioned(
|
||||
bottom: AppSpacing.sm,
|
||||
left: AppSpacing.sm,
|
||||
child: Material(
|
||||
color: Colors.transparent,
|
||||
child: InkWell(
|
||||
onTap: () {
|
||||
ref
|
||||
.read(favoritesProvider.notifier)
|
||||
.toggleFavorite(product.productId);
|
||||
|
||||
// Show feedback
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text(
|
||||
isFavorited
|
||||
? 'Đã xóa khỏi yêu thích'
|
||||
: 'Đã thêm vào yêu thích',
|
||||
),
|
||||
duration: const Duration(seconds: 1),
|
||||
behavior: SnackBarBehavior.floating,
|
||||
),
|
||||
);
|
||||
},
|
||||
borderRadius: BorderRadius.circular(20),
|
||||
child: Container(
|
||||
width: 36,
|
||||
height: 36,
|
||||
decoration: BoxDecoration(
|
||||
color: AppColors.white,
|
||||
shape: BoxShape.circle,
|
||||
boxShadow: [
|
||||
BoxShadow(
|
||||
color: Colors.black.withOpacity(0.15),
|
||||
blurRadius: 6,
|
||||
offset: const Offset(0, 2),
|
||||
),
|
||||
],
|
||||
),
|
||||
child: Icon(
|
||||
isFavorited ? Icons.favorite : Icons.favorite_border,
|
||||
color: isFavorited ? AppColors.danger : AppColors.grey500,
|
||||
size: 20,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
|
||||
@@ -13,6 +13,7 @@ import 'package:worker/features/cart/data/models/cart_item_model.dart';
|
||||
import 'package:worker/features/cart/data/models/cart_model.dart';
|
||||
import 'package:worker/features/chat/data/models/chat_room_model.dart';
|
||||
import 'package:worker/features/chat/data/models/message_model.dart';
|
||||
import 'package:worker/features/favorites/data/models/favorite_model.dart';
|
||||
import 'package:worker/features/home/data/models/member_card_model.dart';
|
||||
import 'package:worker/features/home/data/models/promotion_model.dart';
|
||||
import 'package:worker/features/loyalty/data/models/gift_catalog_model.dart';
|
||||
@@ -48,6 +49,7 @@ extension HiveRegistrar on HiveInterface {
|
||||
registerAdapter(DesignStatusAdapter());
|
||||
registerAdapter(EntrySourceAdapter());
|
||||
registerAdapter(EntryTypeAdapter());
|
||||
registerAdapter(FavoriteModelAdapter());
|
||||
registerAdapter(GiftCatalogModelAdapter());
|
||||
registerAdapter(GiftCategoryAdapter());
|
||||
registerAdapter(GiftStatusAdapter());
|
||||
@@ -103,6 +105,7 @@ extension IsolatedHiveRegistrar on IsolatedHiveInterface {
|
||||
registerAdapter(DesignStatusAdapter());
|
||||
registerAdapter(EntrySourceAdapter());
|
||||
registerAdapter(EntryTypeAdapter());
|
||||
registerAdapter(FavoriteModelAdapter());
|
||||
registerAdapter(GiftCatalogModelAdapter());
|
||||
registerAdapter(GiftCategoryAdapter());
|
||||
registerAdapter(GiftStatusAdapter());
|
||||
|
||||
Reference in New Issue
Block a user