72 lines
1.7 KiB
Dart
72 lines
1.7 KiB
Dart
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),
|
|
);
|