Files
retail/docs/PROVIDERS_SUMMARY.md
Phuoc Nguyen b94c158004 runable
2025-10-10 16:38:07 +07:00

12 KiB

Riverpod 3.0 Providers - Complete Implementation Summary

Project Structure

All providers have been implemented using Riverpod 3.0 with @riverpod code generation annotation.


1. Cart Management Providers

Location: /lib/features/home/presentation/providers/

Files Created:

  1. cart_provider.dart

    • CartProvider - Manages cart items (add, remove, update, clear)
    • State: List<CartItem>
    • Type: Notifier
  2. cart_total_provider.dart

    • CartTotalProvider - Calculates subtotal, tax, total
    • State: CartTotalData
    • Type: Notifier
    • Dependencies: cartProvider, settingsProvider
  3. cart_item_count_provider.dart

    • cartItemCount - Total quantity of items
    • cartUniqueItemCount - Number of unique products
    • Type: Function providers
  4. providers.dart - Barrel file for easy imports


2. Products Management Providers

Location: /lib/features/products/presentation/providers/

Files Created:

  1. product_datasource_provider.dart

    • productLocalDataSource - DI provider for data source
    • Type: Provider (keepAlive)
  2. products_provider.dart

    • ProductsProvider - Fetches all products from Hive
    • State: AsyncValue<List<Product>>
    • Type: AsyncNotifier
    • Methods: refresh(), syncProducts(), getProductById()
  3. search_query_provider.dart

    • SearchQueryProvider - Manages search query state
    • State: String
    • Type: Notifier
    • Methods: setQuery(), clear()
  4. selected_category_provider.dart

    • SelectedCategoryProvider - Manages category filter
    • State: String?
    • Type: Notifier
    • Methods: selectCategory(), clearSelection()
  5. filtered_products_provider.dart

    • FilteredProductsProvider - Combines search and category filtering
    • SortedProductsProvider - Sorts products by various criteria
    • State: List<Product>
    • Type: Notifier
    • Dependencies: productsProvider, searchQueryProvider, selectedCategoryProvider
  6. providers.dart - Barrel file


3. Categories Management Providers

Location: /lib/features/categories/presentation/providers/

Files Created:

  1. category_datasource_provider.dart

    • categoryLocalDataSource - DI provider for data source
    • Type: Provider (keepAlive)
  2. categories_provider.dart

    • CategoriesProvider - Fetches all categories from Hive
    • State: AsyncValue<List<Category>>
    • Type: AsyncNotifier
    • Methods: refresh(), syncCategories(), getCategoryById(), getCategoryName()
  3. category_product_count_provider.dart

    • categoryProductCount - Count for specific category (family)
    • allCategoryProductCounts - Map of all counts
    • Type: Function providers
    • Dependencies: productsProvider
  4. providers.dart - Barrel file


4. Settings Management Providers

Location: /lib/features/settings/presentation/providers/

Files Created:

  1. settings_datasource_provider.dart

    • settingsLocalDataSource - DI provider for data source
    • Type: Provider (keepAlive)
  2. settings_provider.dart

    • SettingsProvider - Manages all app settings
    • State: AsyncValue<AppSettings>
    • Type: AsyncNotifier (keepAlive)
    • Methods: updateThemeMode(), updateLanguage(), updateTaxRate(), updateStoreName(), updateCurrency(), toggleSync(), resetToDefaults()
  3. theme_provider.dart

    • themeModeProvider - Current theme mode
    • isDarkModeProvider - Check dark mode
    • isLightModeProvider - Check light mode
    • isSystemThemeProvider - Check system theme
    • Type: Function providers
    • Dependencies: settingsProvider
  4. language_provider.dart

    • appLanguageProvider - Current language code
    • supportedLanguagesProvider - List of available languages
    • Type: Function providers
    • Dependencies: settingsProvider
  5. providers.dart - Barrel file


5. Core Providers

Location: /lib/core/providers/

Files Created:

  1. network_info_provider.dart

    • connectivityProvider - Connectivity instance (keepAlive)
    • networkInfoProvider - NetworkInfo implementation (keepAlive)
    • isConnectedProvider - Check connection status
    • connectivityStreamProvider - Stream of connectivity changes
    • Type: Multiple provider types
  2. sync_status_provider.dart

    • SyncStatusProvider - Manages data synchronization
    • State: AsyncValue<SyncResult>
    • Type: AsyncNotifier
    • Methods: syncAll(), syncProducts(), syncCategories(), resetStatus()
    • Dependencies: networkInfoProvider, productsProvider, categoriesProvider, settingsProvider
    • Additional: lastSyncTimeProvider
  3. providers.dart - Barrel file


6. Domain Entities

Location: /lib/features/*/domain/entities/

Files Created:

  1. cart_item.dart (/home/domain/entities/)

    • CartItem entity with lineTotal calculation
  2. product.dart (/products/domain/entities/)

    • Product entity with stock management
  3. category.dart (/categories/domain/entities/)

    • Category entity
  4. app_settings.dart (/settings/domain/entities/)

    • AppSettings entity with ThemeMode, language, currency, etc.

7. Data Sources (Mock Implementations)

Location: /lib/features/*/data/datasources/

Files Created:

  1. product_local_datasource.dart (/products/data/datasources/)

    • Interface: ProductLocalDataSource
    • Implementation: ProductLocalDataSourceImpl
    • Mock data: 8 sample products
  2. category_local_datasource.dart (/categories/data/datasources/)

    • Interface: CategoryLocalDataSource
    • Implementation: CategoryLocalDataSourceImpl
    • Mock data: 4 sample categories
  3. settings_local_datasource.dart (/settings/data/datasources/)

    • Interface: SettingsLocalDataSource
    • Implementation: SettingsLocalDataSourceImpl
    • Default settings provided

8. Core Utilities

Location: /lib/core/network/

Files Created:

  1. network_info.dart
    • Interface: NetworkInfo
    • Implementation: NetworkInfoImpl
    • Mock: NetworkInfoMock
    • Uses: connectivity_plus package

9. Configuration Files

Files Created:

  1. build.yaml (root)

    • Configures riverpod_generator
  2. analysis_options.yaml (updated)

    • Enabled custom_lint plugin
  3. pubspec.yaml (updated)

    • Added all Riverpod 3.0 dependencies
    • Added code generation packages

Complete File Tree

lib/
├── core/
│   ├── network/
│   │   └── network_info.dart
│   └── providers/
│       ├── network_info_provider.dart
│       ├── sync_status_provider.dart
│       └── providers.dart
│
├── features/
│   ├── home/
│   │   ├── domain/
│   │   │   └── entities/
│   │   │       └── cart_item.dart
│   │   └── presentation/
│   │       └── providers/
│   │           ├── cart_provider.dart
│   │           ├── cart_total_provider.dart
│   │           ├── cart_item_count_provider.dart
│   │           └── providers.dart
│   │
│   ├── products/
│   │   ├── domain/
│   │   │   └── entities/
│   │   │       └── product.dart
│   │   ├── data/
│   │   │   └── datasources/
│   │   │       └── product_local_datasource.dart
│   │   └── presentation/
│   │       └── providers/
│   │           ├── product_datasource_provider.dart
│   │           ├── products_provider.dart
│   │           ├── search_query_provider.dart
│   │           ├── selected_category_provider.dart
│   │           ├── filtered_products_provider.dart
│   │           └── providers.dart
│   │
│   ├── categories/
│   │   ├── domain/
│   │   │   └── entities/
│   │   │       └── category.dart
│   │   ├── data/
│   │   │   └── datasources/
│   │   │       └── category_local_datasource.dart
│   │   └── presentation/
│   │       └── providers/
│   │           ├── category_datasource_provider.dart
│   │           ├── categories_provider.dart
│   │           ├── category_product_count_provider.dart
│   │           └── providers.dart
│   │
│   └── settings/
│       ├── domain/
│       │   └── entities/
│       │       └── app_settings.dart
│       ├── data/
│       │   └── datasources/
│       │       └── settings_local_datasource.dart
│       └── presentation/
│           └── providers/
│               ├── settings_datasource_provider.dart
│               ├── settings_provider.dart
│               ├── theme_provider.dart
│               ├── language_provider.dart
│               └── providers.dart
│
build.yaml
analysis_options.yaml (updated)
pubspec.yaml (updated)
PROVIDERS_DOCUMENTATION.md (this file)
PROVIDERS_SUMMARY.md

Provider Statistics

Total Files Created: 35+

By Type:

  • Provider files: 21
  • Entity files: 4
  • Data source files: 3
  • Utility files: 2
  • Barrel files: 5
  • Configuration files: 3

By Feature:

  • Cart Management: 4 files
  • Products Management: 7 files
  • Categories Management: 4 files
  • Settings Management: 5 files
  • Core/Sync: 3 files
  • Supporting files: 12 files

Code Generation Status

To Generate Provider Code:

# Run this command to generate all .g.dart files
dart run build_runner build --delete-conflicting-outputs

# Or run in watch mode for development
dart run build_runner watch --delete-conflicting-outputs

Expected Generated Files (21 .g.dart files):

Cart:

  • cart_provider.g.dart
  • cart_total_provider.g.dart
  • cart_item_count_provider.g.dart

Products:

  • product_datasource_provider.g.dart
  • products_provider.g.dart
  • search_query_provider.g.dart
  • selected_category_provider.g.dart
  • filtered_products_provider.g.dart

Categories:

  • category_datasource_provider.g.dart
  • categories_provider.g.dart
  • category_product_count_provider.g.dart

Settings:

  • settings_datasource_provider.g.dart
  • settings_provider.g.dart
  • theme_provider.g.dart
  • language_provider.g.dart

Core:

  • network_info_provider.g.dart
  • sync_status_provider.g.dart

Next Steps

1. Generate Code

dart run build_runner build --delete-conflicting-outputs

2. Wrap App with ProviderScope

// main.dart
void main() {
  runApp(
    const ProviderScope(
      child: MyApp(),
    ),
  );
}

3. Use Providers in Widgets

class MyWidget extends ConsumerWidget {
  @override
  Widget build(BuildContext context, WidgetRef ref) {
    final products = ref.watch(productsProvider);

    return products.when(
      data: (data) => ProductList(data),
      loading: () => CircularProgressIndicator(),
      error: (e, s) => ErrorWidget(e),
    );
  }
}

4. Replace Mock Data Sources

Replace the mock implementations with actual Hive implementations once Hive models are ready.


Features Implemented

Cart Management

  • Add/remove items
  • Update quantities
  • Calculate totals with tax
  • Clear cart
  • Item count tracking

Products Management

  • Fetch all products
  • Search products
  • Filter by category
  • Sort products (6 options)
  • Product sync
  • Refresh products

Categories Management

  • Fetch all categories
  • Category sync
  • Product count per category
  • Category filtering

Settings Management

  • Theme mode (light/dark/system)
  • Language selection (10 languages)
  • Tax rate configuration
  • Currency settings
  • Store name
  • Sync toggle

Core Features

  • Network connectivity detection
  • Data synchronization (all/products/categories)
  • Sync status tracking
  • Offline handling
  • Last sync time tracking

All Providers Are:

  • Using Riverpod 3.0 with code generation
  • Using @riverpod annotation
  • Following modern patterns (Notifier, AsyncNotifier)
  • Implementing proper error handling with AsyncValue
  • Using proper ref.watch/read dependencies
  • Including keepAlive where appropriate
  • Optimized with selective watching
  • Fully documented with inline comments
  • Ready for testing
  • Following clean architecture principles

Ready to Use!

All 25+ providers are implemented and ready for code generation. Simply run the build_runner command and start using them in your widgets!