second commit
This commit is contained in:
71
lib/presentation/customer/providers/customer_providers.dart
Normal file
71
lib/presentation/customer/providers/customer_providers.dart
Normal file
@@ -0,0 +1,71 @@
|
||||
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<CustomerLookupState> {
|
||||
CustomerLookupController(this._ref) : super(const LookupIdle());
|
||||
|
||||
final Ref _ref;
|
||||
|
||||
Future<void> 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<CustomerLookupController, CustomerLookupState>(
|
||||
(ref) => CustomerLookupController(ref),
|
||||
);
|
||||
|
||||
final recentCustomersProvider = FutureProvider<List<Customer>>(
|
||||
(ref) => ref.watch(customerRepositoryProvider).recent(limit: 6),
|
||||
);
|
||||
Reference in New Issue
Block a user