feat: add alot
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
|
||||
import '../models/app_error.dart';
|
||||
import '../services/admin_pin_service.dart';
|
||||
import '../services/pin_lock_service.dart';
|
||||
|
||||
class AdminPinViewModel extends ChangeNotifier {
|
||||
final AdminPinService adminPinService;
|
||||
@@ -11,7 +13,7 @@ class AdminPinViewModel extends ChangeNotifier {
|
||||
bool _isUnlocked = false;
|
||||
bool _isLoading = false;
|
||||
bool _hasLoaded = false;
|
||||
String? _errorMessage;
|
||||
AppError? _error;
|
||||
|
||||
bool get isPinSet => _isPinSet;
|
||||
|
||||
@@ -21,7 +23,7 @@ class AdminPinViewModel extends ChangeNotifier {
|
||||
|
||||
bool get hasLoaded => _hasLoaded;
|
||||
|
||||
String? get errorMessage => _errorMessage;
|
||||
AppError? get error => _error;
|
||||
|
||||
Future<void> ensureLoaded() async {
|
||||
if (_hasLoaded || _isLoading) return;
|
||||
@@ -33,14 +35,14 @@ class AdminPinViewModel extends ChangeNotifier {
|
||||
if (_isLoading) return;
|
||||
|
||||
_isLoading = true;
|
||||
_errorMessage = null;
|
||||
_error = null;
|
||||
notifyListeners();
|
||||
|
||||
try {
|
||||
_isPinSet = await adminPinService.hasPin();
|
||||
} catch (e) {
|
||||
debugPrint('AdminPinViewModel: load error: $e');
|
||||
_errorMessage = 'Could not check PIN status.';
|
||||
_error = AppError.checkPinStatus;
|
||||
} finally {
|
||||
_hasLoaded = true;
|
||||
_isLoading = false;
|
||||
@@ -49,7 +51,7 @@ class AdminPinViewModel extends ChangeNotifier {
|
||||
}
|
||||
|
||||
Future<bool> setPin(String pin) async {
|
||||
_errorMessage = null;
|
||||
_error = null;
|
||||
|
||||
try {
|
||||
await adminPinService.setPin(pin);
|
||||
@@ -59,14 +61,14 @@ class AdminPinViewModel extends ChangeNotifier {
|
||||
return true;
|
||||
} catch (e) {
|
||||
debugPrint('AdminPinViewModel: setPin error: $e');
|
||||
_errorMessage = 'Could not save PIN.';
|
||||
_error = AppError.savePin;
|
||||
notifyListeners();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Future<bool> verify(String pin) async {
|
||||
_errorMessage = null;
|
||||
_error = null;
|
||||
|
||||
try {
|
||||
final matches = await adminPinService.verifyPin(pin);
|
||||
@@ -77,12 +79,16 @@ class AdminPinViewModel extends ChangeNotifier {
|
||||
return true;
|
||||
}
|
||||
|
||||
_errorMessage = 'Incorrect PIN.';
|
||||
_error = AppError.incorrectPin;
|
||||
notifyListeners();
|
||||
return false;
|
||||
} on PinLockedOutException {
|
||||
_error = AppError.tooManyPinAttempts;
|
||||
notifyListeners();
|
||||
return false;
|
||||
} catch (e) {
|
||||
debugPrint('AdminPinViewModel: verify error: $e');
|
||||
_errorMessage = 'Could not verify PIN.';
|
||||
_error = AppError.verifyPin;
|
||||
notifyListeners();
|
||||
return false;
|
||||
}
|
||||
@@ -92,17 +98,23 @@ class AdminPinViewModel extends ChangeNotifier {
|
||||
required String currentPin,
|
||||
required String newPin,
|
||||
}) async {
|
||||
final currentMatches = await adminPinService.verifyPin(currentPin);
|
||||
|
||||
if (!currentMatches) {
|
||||
_errorMessage = 'Current PIN is incorrect.';
|
||||
notifyListeners();
|
||||
return false;
|
||||
}
|
||||
if (!await _verifyCurrentPin(currentPin)) return false;
|
||||
|
||||
return setPin(newPin);
|
||||
}
|
||||
|
||||
Future<bool> _verifyCurrentPin(String pin) async {
|
||||
try {
|
||||
if (await adminPinService.verifyPin(pin)) return true;
|
||||
_error = AppError.currentPinIncorrect;
|
||||
} on PinLockedOutException {
|
||||
_error = AppError.tooManyPinAttempts;
|
||||
}
|
||||
|
||||
notifyListeners();
|
||||
return false;
|
||||
}
|
||||
|
||||
void lock() {
|
||||
if (!_isUnlocked) return;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user