import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; import 'package:local_auth/local_auth.dart'; import 'package:flutter_secure_storage/flutter_secure_storage.dart'; import '../services/notification_service.dart'; import '../providers/subscription_provider.dart'; import 'package:hive_flutter/hive_flutter.dart'; import 'package:flutter/foundation.dart' show kIsWeb; class AppLockProvider extends ChangeNotifier { final Box _appLockBox; final LocalAuthentication _localAuth = LocalAuthentication(); final FlutterSecureStorage _secureStorage = const FlutterSecureStorage(); static const String _isEnabledKey = 'app_lock_enabled'; bool _isEnabled = false; bool _isLocked = false; bool _isBiometricEnabled = false; bool _isBiometricAvailable = false; AppLockProvider(this._appLockBox) { _init(); } bool get isEnabled => _isEnabled; bool get isLocked => _isLocked; bool get isBiometricEnabled => _isBiometricEnabled; bool get isBiometricAvailable => _isBiometricAvailable; Future _init() async { if (!kIsWeb) { _isBiometricAvailable = await _localAuth.canCheckBiometrics; } else { _isBiometricAvailable = false; } _isLocked = _appLockBox.get('isLocked', defaultValue: false) ?? false; await _loadState(); notifyListeners(); } Future _loadState() async { final value = await _secureStorage.read(key: _isEnabledKey); _isEnabled = value == 'true'; _isLocked = _appLockBox.get('isLocked', defaultValue: false) ?? false; await _loadSettings(); notifyListeners(); } Future _loadSettings() async { try { final biometricEnabled = await _secureStorage.read(key: 'biometric_enabled'); _isBiometricEnabled = biometricEnabled == 'true'; notifyListeners(); } catch (e) { debugPrint('설정 로드 중 오류 발생: $e'); } } Future authenticate() async { if (kIsWeb) { _isLocked = false; notifyListeners(); return true; } try { final canCheck = await _checkBiometrics(); if (!canCheck) { _isLocked = false; notifyListeners(); return true; } final authenticated = await _localAuth.authenticate( localizedReason: '생체 인증을 사용하여 앱 잠금을 해제하세요.', options: const AuthenticationOptions( stickyAuth: true, biometricOnly: true, ), ); if (authenticated) { _isLocked = false; await _appLockBox.put('isLocked', false); notifyListeners(); } return authenticated; } catch (e) { _isLocked = false; notifyListeners(); return true; } } Future _checkBiometrics() async { if (kIsWeb) return false; try { return await _localAuth.canCheckBiometrics; } catch (e) { return false; } } Future toggleBiometricAuth() async { if (kIsWeb) { _isBiometricEnabled = false; await _appLockBox.put('biometric_enabled', false); notifyListeners(); return; } try { final canCheck = await _checkBiometrics(); if (!canCheck) { _isBiometricEnabled = false; await _appLockBox.put('biometric_enabled', false); notifyListeners(); return; } _isBiometricEnabled = !_isBiometricEnabled; await _secureStorage.write( key: 'biometric_enabled', value: _isBiometricEnabled.toString(), ); await _appLockBox.put('biometric_enabled', _isBiometricEnabled); notifyListeners(); } catch (e) { _isBiometricEnabled = false; await _appLockBox.put('biometric_enabled', false); notifyListeners(); } } void lock() { if (_isBiometricEnabled) { _isLocked = true; notifyListeners(); } } Future unlock() async { _isLocked = false; await _appLockBox.put('isLocked', false); notifyListeners(); } Future enable() async { _isEnabled = true; await _secureStorage.write(key: _isEnabledKey, value: 'true'); notifyListeners(); } Future disable() async { _isEnabled = false; await _secureStorage.write(key: _isEnabledKey, value: 'false'); notifyListeners(); } Future refreshNotifications(BuildContext context) async { final subscriptionProvider = Provider.of( context, listen: false, ); for (var subscription in subscriptionProvider.subscriptions) { await NotificationService.scheduleSubscriptionNotification(subscription); } } }