import 'package:flutter_riverpod/flutter_riverpod.dart'; import '../../../app/providers.dart'; import '../../../domain/entities/customer.dart'; /// Outcome of the mobile-number lookup on the Existing Customer screen. sealed class CustomerLookupState { const CustomerLookupState(); } class LookupIdle extends CustomerLookupState { const LookupIdle(); } class LookupSearching extends CustomerLookupState { const LookupSearching(); } class LookupFound extends CustomerLookupState { const LookupFound(this.customer); final Customer customer; } class LookupNotFound extends CustomerLookupState { const LookupNotFound(this.mobile); final String mobile; } class LookupError extends CustomerLookupState { const LookupError(this.message); final String message; } class CustomerLookupController extends StateNotifier { CustomerLookupController(this._ref) : super(const LookupIdle()); final Ref _ref; Future search(String mobile) async { final digits = mobile.replaceAll(RegExp(r'\D'), ''); if (digits.length != 10) { state = const LookupIdle(); return; } state = const LookupSearching(); try { final customer = await _ref.read(customerRepositoryProvider).findByMobile(digits); state = customer != null ? LookupFound(customer) : LookupNotFound(digits); } catch (e) { state = LookupError(e.toString()); } } void reset() => state = const LookupIdle(); } final customerLookupProvider = StateNotifierProvider( (ref) => CustomerLookupController(ref), ); final recentCustomersProvider = FutureProvider>( (ref) => ref.watch(customerRepositoryProvider).recent(limit: 6), );