11import 'package:shared_preferences/shared_preferences.dart' ;
22
3+ import '../storage/project_settings_store.dart' ;
4+
35class SecurityPreferencesService {
6+ SecurityPreferencesService ({ProjectSettingsStore ? projectSettingsStore})
7+ : _projectSettingsStore = projectSettingsStore ?? ProjectSettingsStore ();
8+
49 static const String _lockOnBackgroundKey = 'security.lock_on_background' ;
510 static const String _inactivityTimeoutSecondsKey =
611 'security.inactivity_timeout_seconds' ;
712 static const String _sessionDirtyKey = 'security.session_dirty' ;
813 static const String _biometricEnabledKey = 'security.biometric_enabled' ;
14+ static const List <String > _lockOnBackgroundPath = [
15+ 'security' ,
16+ 'lockOnBackground' ,
17+ ];
18+ static const List <String > _inactivityTimeoutPath = [
19+ 'security' ,
20+ 'inactivityTimeoutSeconds' ,
21+ ];
22+ static const List <String > _biometricEnabledPath = [
23+ 'security' ,
24+ 'biometricEnabled' ,
25+ ];
926
1027 static const Duration defaultInactivityTimeout = Duration (minutes: 5 );
1128
29+ final ProjectSettingsStore _projectSettingsStore;
30+
1231 Future <bool > lockOnBackground () async {
32+ final settings = await _projectSettingsStore.read ();
33+ final stored = ProjectSettingsStore .boolAt (settings, _lockOnBackgroundPath);
34+ if (stored != null ) {
35+ return stored;
36+ }
37+
1338 final prefs = await SharedPreferences .getInstance ();
14- return prefs.getBool (_lockOnBackgroundKey) ?? true ;
39+ final legacy = prefs.getBool (_lockOnBackgroundKey);
40+ if (legacy != null ) {
41+ if (await _projectSettingsStore.hasProjectContainer ()) {
42+ await _writeBool (
43+ _lockOnBackgroundPath,
44+ legacy,
45+ removeLegacyKey: _lockOnBackgroundKey,
46+ );
47+ }
48+ return legacy;
49+ }
50+ return true ;
1551 }
1652
1753 Future <void > setLockOnBackground (bool value) async {
18- final prefs = await SharedPreferences .getInstance ();
19- await prefs.setBool (_lockOnBackgroundKey, value);
54+ await _writeBool (
55+ _lockOnBackgroundPath,
56+ value,
57+ removeLegacyKey: _lockOnBackgroundKey,
58+ );
2059 }
2160
2261 Future <Duration > inactivityTimeout () async {
62+ final settings = await _projectSettingsStore.read ();
63+ final storedSeconds = ProjectSettingsStore .intAt (
64+ settings,
65+ _inactivityTimeoutPath,
66+ );
67+ if (storedSeconds != null && storedSeconds > 0 ) {
68+ return Duration (seconds: storedSeconds);
69+ }
70+
2371 final prefs = await SharedPreferences .getInstance ();
24- final seconds = prefs.getInt (_inactivityTimeoutSecondsKey);
25- if (seconds == null || seconds <= 0 ) {
72+ final legacySeconds = prefs.getInt (_inactivityTimeoutSecondsKey);
73+ if (legacySeconds == null || legacySeconds <= 0 ) {
2674 return defaultInactivityTimeout;
2775 }
28- return Duration (seconds: seconds);
76+ await _writeInt (
77+ _inactivityTimeoutPath,
78+ legacySeconds,
79+ removeLegacyKey: _inactivityTimeoutSecondsKey,
80+ );
81+ return Duration (seconds: legacySeconds);
2982 }
3083
3184 Future <void > setInactivityTimeout (Duration duration) async {
32- final prefs = await SharedPreferences .getInstance ();
33- await prefs.setInt (
34- _inactivityTimeoutSecondsKey,
85+ await _writeInt (
86+ _inactivityTimeoutPath,
3587 duration.inSeconds,
88+ removeLegacyKey: _inactivityTimeoutSecondsKey,
3689 );
3790 }
3891
@@ -47,12 +100,58 @@ class SecurityPreferencesService {
47100 }
48101
49102 Future <bool > biometricEnabled () async {
103+ final settings = await _projectSettingsStore.read ();
104+ final stored = ProjectSettingsStore .boolAt (settings, _biometricEnabledPath);
105+ if (stored != null ) {
106+ return stored;
107+ }
108+
50109 final prefs = await SharedPreferences .getInstance ();
51- return prefs.getBool (_biometricEnabledKey) ?? false ;
110+ final legacy = prefs.getBool (_biometricEnabledKey);
111+ if (legacy != null ) {
112+ if (await _projectSettingsStore.hasProjectContainer ()) {
113+ await _writeBool (
114+ _biometricEnabledPath,
115+ legacy,
116+ removeLegacyKey: _biometricEnabledKey,
117+ );
118+ }
119+ return legacy;
120+ }
121+ return false ;
52122 }
53123
54124 Future <void > setBiometricEnabled (bool value) async {
125+ await _writeBool (
126+ _biometricEnabledPath,
127+ value,
128+ removeLegacyKey: _biometricEnabledKey,
129+ );
130+ }
131+
132+ Future <void > _writeBool (
133+ List <String > path,
134+ bool value, {
135+ required String removeLegacyKey,
136+ }) async {
137+ await _projectSettingsStore.update ((settings) {
138+ ProjectSettingsStore .setPath (settings, path, value);
139+ return settings;
140+ });
141+ final prefs = await SharedPreferences .getInstance ();
142+ await prefs.remove (removeLegacyKey);
143+ }
144+
145+ Future <void > _writeInt (
146+ List <String > path,
147+ int value, {
148+ required String removeLegacyKey,
149+ }) async {
150+ await _projectSettingsStore.update ((settings) {
151+ ProjectSettingsStore .setPath (settings, path, value);
152+ return settings;
153+ });
55154 final prefs = await SharedPreferences .getInstance ();
56- await prefs.setBool (_biometricEnabledKey, value );
155+ await prefs.remove (removeLegacyKey );
57156 }
58157}
0 commit comments