Bläddra i källkod

move two shared pref logic to user service

ashilkn 2 år sedan
förälder
incheckning
c7d6542219

+ 0 - 18
lib/core/configuration.dart

@@ -40,7 +40,6 @@ import 'package:photos/services/memories_service.dart';
 import 'package:photos/services/search_service.dart';
 // ignore: import_of_legacy_library_into_null_safe
 import 'package:photos/services/sync_service.dart';
-import 'package:photos/services/user_service.dart';
 import 'package:photos/utils/crypto_util.dart';
 import 'package:photos/utils/validator_util.dart';
 import 'package:shared_preferences/shared_preferences.dart';
@@ -69,8 +68,6 @@ class Configuration {
   static const keyShouldShowLockScreen = "should_show_lock_screen";
   static const keyHasSelectedAnyBackupFolder =
       "has_selected_any_folder_for_backup";
-  static const keyHasEnabledTwoFactor = "has_enabled_two_factor";
-
   static const lastTempFolderClearTimeKey = "last_temp_folder_clear_time";
   static const nameKey = "name";
   static const secretKeyKey = "secret_key";
@@ -151,7 +148,6 @@ class Configuration {
       await _migrateSecurityStorageToFirstUnlock();
     }
     SuperLogging.setUserID(await _getOrCreateAnonymousUserID());
-    setTwoFactor(fetchTwoFactorStatus: true);
   }
 
   Future<void> logout({bool autoLogout = false}) async {
@@ -612,20 +608,6 @@ class Configuration {
     return _preferences.getBool(hasSelectedAllFoldersForBackupKey) ?? false;
   }
 
-  Future<void> setTwoFactor({
-    bool value = false,
-    bool fetchTwoFactorStatus = false,
-  }) async {
-    if (fetchTwoFactorStatus) {
-      value = await UserService.instance.fetchTwoFactorStatus();
-    }
-    _preferences.setBool(keyHasEnabledTwoFactor, value);
-  }
-
-  bool? hasEnabledTwoFactor() {
-    return _preferences.getBool(keyHasEnabledTwoFactor);
-  }
-
   Future<void> setSelectAllFoldersForBackup(bool value) async {
     await _preferences.setBool(hasSelectedAllFoldersForBackupKey, value);
   }

+ 2 - 2
lib/events/two_factor_status_change_event.dart

@@ -1,10 +1,10 @@
-import 'package:photos/core/configuration.dart';
 import 'package:photos/events/event.dart';
+import 'package:photos/services/user_service.dart';
 
 class TwoFactorStatusChangeEvent extends Event {
   final bool status;
 
   TwoFactorStatusChangeEvent(this.status) {
-    Configuration.instance.setTwoFactor(value: status);
+    UserService.instance.setTwoFactor(value: status);
   }
 }

+ 21 - 1
lib/services/user_service.dart

@@ -34,12 +34,16 @@ import 'package:photos/utils/crypto_util.dart';
 import 'package:photos/utils/dialog_util.dart';
 import 'package:photos/utils/navigation_util.dart';
 import 'package:photos/utils/toast_util.dart';
+import 'package:shared_preferences/shared_preferences.dart';
 
 class UserService {
+  static const keyHasEnabledTwoFactor = "has_enabled_two_factor";
   final _dio = Network.instance.getDio();
   final _enteDio = Network.instance.enteDio;
   final _logger = Logger((UserService).toString());
   final _config = Configuration.instance;
+  SharedPreferences _preferences;
+
   ValueNotifier<String> emailValueNotifier;
 
   UserService._privateConstructor();
@@ -49,6 +53,8 @@ class UserService {
   Future<void> init() async {
     emailValueNotifier =
         ValueNotifier<String>(Configuration.instance.getEmail());
+    _preferences = await SharedPreferences.getInstance();
+    setTwoFactor(fetchTwoFactorStatus: true);
   }
 
   Future<void> sendOtt(
@@ -716,7 +722,7 @@ class UserService {
   Future<bool> fetchTwoFactorStatus() async {
     try {
       final response = await _enteDio.get("/users/two-factor/status");
-      Configuration.instance.setTwoFactor(value: response.data["status"]);
+      setTwoFactor(value: response.data["status"]);
       return response.data["status"];
     } catch (e) {
       _logger.severe("Failed to fetch 2FA status", e);
@@ -784,4 +790,18 @@ class UserService {
       await Configuration.instance.setToken(response.data["token"]);
     }
   }
+
+  Future<void> setTwoFactor({
+    bool value = false,
+    bool fetchTwoFactorStatus = false,
+  }) async {
+    if (fetchTwoFactorStatus) {
+      value = await UserService.instance.fetchTwoFactorStatus();
+    }
+    _preferences.setBool(keyHasEnabledTwoFactor, value);
+  }
+
+  bool hasEnabledTwoFactor() {
+    return _preferences.getBool(keyHasEnabledTwoFactor);
+  }
 }

+ 3 - 2
lib/ui/settings/security_section_widget.dart

@@ -69,7 +69,7 @@ class _SecuritySectionWidgetState extends State<SecuritySectionWidget> {
               title: "Two-factor",
             ),
             trailingSwitch: ToggleSwitchWidget(
-              value: () => _config.hasEnabledTwoFactor(),
+              value: () => UserService.instance.hasEnabledTwoFactor(),
               onChanged: () async {
                 final hasAuthenticated = await LocalAuthenticationService
                     .instance
@@ -77,7 +77,8 @@ class _SecuritySectionWidgetState extends State<SecuritySectionWidget> {
                   context,
                   "Please authenticate to configure two-factor authentication",
                 );
-                final isTwoFactorEnabled = _config.hasEnabledTwoFactor();
+                final isTwoFactorEnabled =
+                    UserService.instance.hasEnabledTwoFactor();
                 if (hasAuthenticated) {
                   if (isTwoFactorEnabled) {
                     await _disableTwoFactor();