fix: pincode functionality
This commit is contained in:
@@ -1,16 +1,19 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
|
||||
import '../services/pin_lock_service.dart';
|
||||
import '../services/settings_service.dart';
|
||||
|
||||
class PinLockViewModel extends ChangeNotifier {
|
||||
final PinLockService pinLockService;
|
||||
final SettingsService? settingsService;
|
||||
|
||||
PinLockViewModel({required this.pinLockService});
|
||||
PinLockViewModel({required this.pinLockService, this.settingsService});
|
||||
|
||||
bool _isPinSet = false;
|
||||
bool _isUnlocked = false;
|
||||
bool _isLoading = false;
|
||||
bool _hasLoaded = false;
|
||||
bool _pinRequired = true;
|
||||
String? _errorMessage;
|
||||
|
||||
bool get isPinSet => _isPinSet;
|
||||
@@ -21,8 +24,16 @@ class PinLockViewModel extends ChangeNotifier {
|
||||
|
||||
bool get hasLoaded => _hasLoaded;
|
||||
|
||||
bool get pinRequired => _pinRequired;
|
||||
|
||||
String? get errorMessage => _errorMessage;
|
||||
|
||||
void setPinRequired(bool value) {
|
||||
if (_pinRequired == value) return;
|
||||
_pinRequired = value;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
Future<void> ensureLoaded() async {
|
||||
if (_hasLoaded || _isLoading) return;
|
||||
|
||||
@@ -39,6 +50,12 @@ class PinLockViewModel extends ChangeNotifier {
|
||||
|
||||
try {
|
||||
_isPinSet = await pinLockService.hasPin();
|
||||
|
||||
final svc = settingsService;
|
||||
if (svc != null) {
|
||||
final settings = await svc.getSettings();
|
||||
_pinRequired = settings.pinRequired;
|
||||
}
|
||||
} catch (_) {
|
||||
_errorMessage = 'Could not check PIN status.';
|
||||
} finally {
|
||||
@@ -58,6 +75,14 @@ class PinLockViewModel extends ChangeNotifier {
|
||||
await pinLockService.setPin(pin);
|
||||
_isPinSet = true;
|
||||
_isUnlocked = true;
|
||||
_pinRequired = true;
|
||||
|
||||
final svc = settingsService;
|
||||
if (svc != null) {
|
||||
final current = await svc.getSettings();
|
||||
await svc.saveSettings(current.copyWith(pinRequired: true));
|
||||
}
|
||||
|
||||
notifyListeners();
|
||||
return true;
|
||||
} catch (_) {
|
||||
@@ -118,6 +143,14 @@ class PinLockViewModel extends ChangeNotifier {
|
||||
|
||||
await pinLockService.clearPin();
|
||||
_isPinSet = false;
|
||||
_pinRequired = false;
|
||||
|
||||
final svc = settingsService;
|
||||
if (svc != null) {
|
||||
final current = await svc.getSettings();
|
||||
await svc.saveSettings(current.copyWith(pinRequired: false));
|
||||
}
|
||||
|
||||
notifyListeners();
|
||||
return true;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user