feat: add admin page
This commit is contained in:
@@ -0,0 +1,112 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
|
||||
import '../services/admin_pin_service.dart';
|
||||
|
||||
class AdminPinViewModel extends ChangeNotifier {
|
||||
final AdminPinService adminPinService;
|
||||
|
||||
AdminPinViewModel({required this.adminPinService});
|
||||
|
||||
bool _isPinSet = false;
|
||||
bool _isUnlocked = false;
|
||||
bool _isLoading = false;
|
||||
bool _hasLoaded = false;
|
||||
String? _errorMessage;
|
||||
|
||||
bool get isPinSet => _isPinSet;
|
||||
|
||||
bool get isUnlocked => _isUnlocked;
|
||||
|
||||
bool get isLoading => _isLoading;
|
||||
|
||||
bool get hasLoaded => _hasLoaded;
|
||||
|
||||
String? get errorMessage => _errorMessage;
|
||||
|
||||
Future<void> ensureLoaded() async {
|
||||
if (_hasLoaded || _isLoading) return;
|
||||
|
||||
await load();
|
||||
}
|
||||
|
||||
Future<void> load() async {
|
||||
if (_isLoading) return;
|
||||
|
||||
_isLoading = true;
|
||||
_errorMessage = null;
|
||||
notifyListeners();
|
||||
|
||||
try {
|
||||
_isPinSet = await adminPinService.hasPin();
|
||||
} catch (e) {
|
||||
debugPrint('AdminPinViewModel: load error: $e');
|
||||
_errorMessage = 'Could not check PIN status.';
|
||||
} finally {
|
||||
_hasLoaded = true;
|
||||
_isLoading = false;
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
|
||||
Future<bool> setPin(String pin) async {
|
||||
_errorMessage = null;
|
||||
|
||||
try {
|
||||
await adminPinService.setPin(pin);
|
||||
_isPinSet = true;
|
||||
_isUnlocked = true;
|
||||
notifyListeners();
|
||||
return true;
|
||||
} catch (e) {
|
||||
debugPrint('AdminPinViewModel: setPin error: $e');
|
||||
_errorMessage = 'Could not save PIN.';
|
||||
notifyListeners();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Future<bool> verify(String pin) async {
|
||||
_errorMessage = null;
|
||||
|
||||
try {
|
||||
final matches = await adminPinService.verifyPin(pin);
|
||||
|
||||
if (matches) {
|
||||
_isUnlocked = true;
|
||||
notifyListeners();
|
||||
return true;
|
||||
}
|
||||
|
||||
_errorMessage = 'Incorrect PIN.';
|
||||
notifyListeners();
|
||||
return false;
|
||||
} catch (e) {
|
||||
debugPrint('AdminPinViewModel: verify error: $e');
|
||||
_errorMessage = 'Could not verify PIN.';
|
||||
notifyListeners();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Future<bool> changePin({
|
||||
required String currentPin,
|
||||
required String newPin,
|
||||
}) async {
|
||||
final currentMatches = await adminPinService.verifyPin(currentPin);
|
||||
|
||||
if (!currentMatches) {
|
||||
_errorMessage = 'Current PIN is incorrect.';
|
||||
notifyListeners();
|
||||
return false;
|
||||
}
|
||||
|
||||
return setPin(newPin);
|
||||
}
|
||||
|
||||
void lock() {
|
||||
if (!_isUnlocked) return;
|
||||
|
||||
_isUnlocked = false;
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user