Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Persist Auth theme selection independently

Neeraj Gupta35ede0c3bf69
diff --git a/mobile/apps/auth/lib/services/auth_theme_preferences.dart b/mobile/apps/auth/lib/services/auth_theme_preferences.dart
index 43b515dad9..8d231d8c85 100644
--- a/mobile/apps/auth/lib/services/auth_theme_preferences.dart
+++ b/mobile/apps/auth/lib/services/auth_theme_preferences.dart
@@ -7,10 +7,16 @@ import "package:shared_preferences/shared_preferences.dart";
class AuthThemePreferences {
AuthThemePreferences._();
+ static const _authThemeModeKey = "ente_auth_theme_mode";
static const _themeModeKey = "theme_mode";
static const _defaultThemeModeKey = "default_theme_mode";
static Future<AdaptiveThemeMode> getThemeMode() async {
+ final authThemeMode = await _getAuthThemeMode();
+ if (authThemeMode != null) {
+ return authThemeMode;
+ }
+
final savedThemeMode = await AdaptiveTheme.getThemeMode();
if (savedThemeMode != null) {
return savedThemeMode;
@@ -24,8 +30,26 @@ class AuthThemePreferences {
AdaptiveThemeMode themeMode,
) async {
adaptiveTheme.setThemeMode(themeMode);
- await adaptiveTheme.persist();
- await _setLegacyThemeMode(themeMode);
+ await Future.wait([
+ _setAuthThemeMode(themeMode),
+ _setAdaptiveThemeMode(themeMode),
+ _setLegacyThemeMode(themeMode),
+ ]);
+ }
+
+ static Future<AdaptiveThemeMode?> _getAuthThemeMode() async {
+ final prefs = SharedPreferencesAsync();
+ return _themeModeFromIndex(await prefs.getInt(_authThemeModeKey));
+ }
+
+ static Future<void> _setAuthThemeMode(AdaptiveThemeMode themeMode) async {
+ final prefs = SharedPreferencesAsync();
+ await prefs.setInt(_authThemeModeKey, themeMode.index);
+ }
+
+ static Future<void> _setAdaptiveThemeMode(AdaptiveThemeMode themeMode) async {
+ final prefs = SharedPreferencesAsync();
+ await prefs.setString(AdaptiveTheme.prefKey, _themeModeJson(themeMode));
}
static Future<AdaptiveThemeMode?> _getLegacyThemeMode() async {
@@ -46,26 +70,31 @@ class AuthThemePreferences {
}
final themeModeIndex = decoded[_themeModeKey];
- if (themeModeIndex is! int ||
- themeModeIndex < 0 ||
- themeModeIndex >= AdaptiveThemeMode.values.length) {
- return null;
- }
-
- return AdaptiveThemeMode.values[themeModeIndex];
+ return _themeModeFromIndex(themeModeIndex);
} on FormatException {
return null;
}
}
+ static AdaptiveThemeMode? _themeModeFromIndex(Object? themeModeIndex) {
+ if (themeModeIndex is! int ||
+ themeModeIndex < 0 ||
+ themeModeIndex >= AdaptiveThemeMode.values.length) {
+ return null;
+ }
+
+ return AdaptiveThemeMode.values[themeModeIndex];
+ }
+
static Future<void> _setLegacyThemeMode(AdaptiveThemeMode themeMode) async {
final prefs = await SharedPreferences.getInstance();
- await prefs.setString(
- AdaptiveTheme.prefKey,
- json.encode({
- _themeModeKey: themeMode.index,
- _defaultThemeModeKey: AdaptiveThemeMode.system.index,
- }),
- );
+ await prefs.setString(AdaptiveTheme.prefKey, _themeModeJson(themeMode));
+ }
+
+ static String _themeModeJson(AdaptiveThemeMode themeMode) {
+ return json.encode({
+ _themeModeKey: themeMode.index,
+ _defaultThemeModeKey: AdaptiveThemeMode.system.index,
+ });
}
}
diff --git a/mobile/apps/auth/test/services/auth_theme_preferences_test.dart b/mobile/apps/auth/test/services/auth_theme_preferences_test.dart
index 7db4e1e05e..05535a8b53 100644
--- a/mobile/apps/auth/test/services/auth_theme_preferences_test.dart
+++ b/mobile/apps/auth/test/services/auth_theme_preferences_test.dart
@@ -2,6 +2,7 @@ import "dart:convert";
import "package:adaptive_theme/adaptive_theme.dart";
import "package:ente_auth/services/auth_theme_preferences.dart";
+import "package:flutter/material.dart";
import "package:flutter_test/flutter_test.dart";
import "package:shared_preferences/shared_preferences.dart";
import "package:shared_preferences_platform_interface/in_memory_shared_preferences_async.dart";
@@ -30,6 +31,34 @@ void main() {
expect(await AuthThemePreferences.getThemeMode(), AdaptiveThemeMode.light);
});
+
+ testWidgets("keeps Auth theme preference when adaptive value resets", (
+ tester,
+ ) async {
+ late BuildContext adaptiveThemeContext;
+ await tester.pumpWidget(
+ AdaptiveTheme(
+ light: ThemeData.light(),
+ dark: ThemeData.dark(),
+ initial: AdaptiveThemeMode.system,
+ builder: (_, _) => Builder(
+ builder: (context) {
+ adaptiveThemeContext = context;
+ return const SizedBox.shrink();
+ },
+ ),
+ ),
+ );
+
+ await AuthThemePreferences.setThemeMode(
+ AdaptiveTheme.of(adaptiveThemeContext),
+ AdaptiveThemeMode.light,
+ );
+ await _setAsyncThemeMode(AdaptiveThemeMode.system);
+ await _setLegacyThemeMode(AdaptiveThemeMode.system);
+
+ expect(await AuthThemePreferences.getThemeMode(), AdaptiveThemeMode.light);
+ });
}
Future<void> _setAsyncThemeMode(AdaptiveThemeMode themeMode) async {