QuickSSH/lib/services/settings_controller.dart

72 lines
2.0 KiB
Dart

import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:local_auth/local_auth.dart';
class SettingsController extends ChangeNotifier {
bool _vibrationEnabled = true;
bool get vibrationEnabled => _vibrationEnabled;
bool _biometricEnabled = false;
bool get biometricEnabled => _biometricEnabled;
bool _adsEnabled = true;
bool get adsEnabled => _adsEnabled;
final LocalAuthentication auth = LocalAuthentication();
late Future<void> initializationFuture;
SettingsController() {
initializationFuture = _loadSettings();
}
Future<void> _loadSettings() async {
final prefs = await SharedPreferences.getInstance();
_vibrationEnabled = prefs.getBool('vibration_enabled') ?? true;
_biometricEnabled = prefs.getBool('biometric_enabled') ?? false;
_adsEnabled = prefs.getBool('ads_enabled') ?? true;
notifyListeners();
}
Future<void> toggleVibration(bool value) async {
_vibrationEnabled = value;
final prefs = await SharedPreferences.getInstance();
await prefs.setBool('vibration_enabled', value);
notifyListeners();
}
Future<void> toggleBiometric(bool value) async {
_biometricEnabled = value;
final prefs = await SharedPreferences.getInstance();
await prefs.setBool('biometric_enabled', value);
notifyListeners();
}
Future<void> toggleAds(bool value) async {
_adsEnabled = value;
final prefs = await SharedPreferences.getInstance();
await prefs.setBool('ads_enabled', value);
notifyListeners();
}
Future<bool> checkSecurity() async {
await initializationFuture;
if (!_biometricEnabled) return true;
bool canCheck = await auth.canCheckBiometrics;
bool isSupported = await auth.isDeviceSupported();
if (!canCheck && !isSupported) return true;
try {
return await auth.authenticate(
localizedReason: 'Unlock QuickSSH',
biometricOnly: false,
);
} catch (e) {
return false;
}
}
}