cb_prestige_qr/lib/features/scan/presentation/manager/scan_controller.dart
2026-04-09 13:17:03 +06:30

71 lines
1.8 KiB
Dart

import 'package:riverpod_annotation/riverpod_annotation.dart';
import 'package:cb_prestige_qr/features/scan/scan_providers.dart';
part 'scan_controller.g.dart';
class ScanState {
const ScanState({
this.isScanning = true,
this.isProcessing = false,
this.scannedValue,
this.errorMessage,
});
final bool isScanning;
final bool isProcessing;
final String? scannedValue;
final String? errorMessage;
static const _sentinel = Object();
ScanState copyWith({
bool? isScanning,
bool? isProcessing,
Object? scannedValue = _sentinel,
Object? errorMessage = _sentinel,
}) {
return ScanState(
isScanning: isScanning ?? this.isScanning,
isProcessing: isProcessing ?? this.isProcessing,
scannedValue:
identical(scannedValue, _sentinel) ? this.scannedValue : scannedValue as String?,
errorMessage:
identical(errorMessage, _sentinel) ? this.errorMessage : errorMessage as String?,
);
}
}
@riverpod
class ScanController extends _$ScanController {
@override
ScanState build() => const ScanState();
Future<void> onBarcodeDetected(String value) async {
if (!state.isScanning || state.isProcessing) return;
state = state.copyWith(
isScanning: false,
isProcessing: true,
errorMessage: null,
);
try {
final result = await ref.read(processScanUseCaseProvider)(value);
state = state.copyWith(
isProcessing: false,
scannedValue: result.rawValue,
);
} catch (e) {
state = state.copyWith(
isProcessing: false,
isScanning: true,
scannedValue: null,
errorMessage: e.toString(),
);
}
}
void resumeScanning() {
state = const ScanState(isScanning: true, isProcessing: false, scannedValue: null, errorMessage: null);
}
}