π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Simplify Auth theme persistence
Neeraj Guptac1523307b10d
diff --git a/mobile/apps/auth/lib/app/view/app.dart b/mobile/apps/auth/lib/app/view/app.dartindex 508b973e81..864cac204f 100644--- a/mobile/apps/auth/lib/app/view/app.dart+++ b/mobile/apps/auth/lib/app/view/app.dart@@ -1,12 +1,12 @@import 'dart:async';-import 'package:adaptive_theme/adaptive_theme.dart';import 'package:ente_accounts/services/user_service.dart';import 'package:ente_auth/core/configuration.dart';import 'package:ente_auth/ente_theme_data.dart';import 'package:ente_auth/l10n/l10n.dart';import 'package:ente_auth/locale.dart';import 'package:ente_auth/onboarding/view/onboarding_page.dart';+import 'package:ente_auth/services/auth_theme_preferences.dart';import 'package:ente_auth/services/authenticator_service.dart';import 'package:ente_auth/services/update_service.dart';import 'package:ente_auth/ui/home_page.dart';@@ -22,11 +22,11 @@ import 'package:flutter_localizations/flutter_localizations.dart';class App extends StatefulWidget {final Locale? locale;- final AdaptiveThemeMode savedThemeMode;+ final ThemeMode savedThemeMode;const App({super.key,this.locale = const Locale("en"),- this.savedThemeMode = AdaptiveThemeMode.system,+ this.savedThemeMode = ThemeMode.system,});static void setLocale(BuildContext context, Locale newLocale) {@@ -34,6 +34,16 @@ class App extends StatefulWidget {state.setLocale(newLocale);}+ static ThemeMode themeModeOf(BuildContext context) {+ return context.findAncestorStateOfType<_AppState>()!._themeMode;+ }++ static Future<void> setThemeMode(BuildContext context, ThemeMode themeMode) {+ return context.findAncestorStateOfType<_AppState>()!.setThemeMode(+ themeMode,+ );+ }+@overrideState<App> createState() => _AppState();}@@ -42,6 +52,7 @@ class _AppState extends State<App> with WidgetsBindingObserver {static final Logger _renderErrorLogger = Logger('RenderError');late StreamSubscription<SignedOutEvent> _signedOutEvent;late StreamSubscription<SignedInEvent> _signedInEvent;+ late ThemeMode _themeMode;Locale? locale;void setLocale(Locale newLocale) {setState(() {@@ -49,9 +60,19 @@ class _AppState extends State<App> with WidgetsBindingObserver {});}+ Future<void> setThemeMode(ThemeMode themeMode) async {+ await AuthThemePreferences.setThemeMode(themeMode);+ if (mounted) {+ setState(() {+ _themeMode = themeMode;+ });+ }+ }+@overridevoid initState() {WidgetsBinding.instance.addObserver(this);+ _themeMode = widget.savedThemeMode;_signedOutEvent = Bus.instance.on<SignedOutEvent>().listen((event) {if (mounted) {@@ -102,40 +123,27 @@ class _AppState extends State<App> with WidgetsBindingObserver {@overrideWidget build(BuildContext context) {- return AdaptiveTheme(- light: lightThemeData,- dark: darkThemeData,- initial: widget.savedThemeMode,- builder: (lightTheme, darkTheme) => Builder(- builder: (context) => MaterialApp(- title: "ente",- themeMode: _themeMode(AdaptiveTheme.of(context).mode),- theme: lightTheme,- darkTheme: darkTheme,- debugShowCheckedModeBanner: false,- locale: locale,- supportedLocales: appSupportedLocales,- localeListResolutionCallback: localResolutionCallBack,- localizationsDelegates: const [- AppLocalizations.delegate,- StringsLocalizations.delegate,- GlobalMaterialLocalizations.delegate,- GlobalCupertinoLocalizations.delegate,- GlobalWidgetsLocalizations.delegate,- ],- routes: _getRoutes,- builder: _materialAppBuilder,- ),- ),+ return MaterialApp(+ title: "ente",+ themeMode: _themeMode,+ theme: lightThemeData,+ darkTheme: darkThemeData,+ debugShowCheckedModeBanner: false,+ locale: locale,+ supportedLocales: appSupportedLocales,+ localeListResolutionCallback: localResolutionCallBack,+ localizationsDelegates: const [+ AppLocalizations.delegate,+ StringsLocalizations.delegate,+ GlobalMaterialLocalizations.delegate,+ GlobalCupertinoLocalizations.delegate,+ GlobalWidgetsLocalizations.delegate,+ ],+ routes: _getRoutes,+ builder: _materialAppBuilder,);}- ThemeMode _themeMode(AdaptiveThemeMode mode) {- if (mode.isLight) return ThemeMode.light;- if (mode.isDark) return ThemeMode.dark;- return ThemeMode.system;- }-Map<String, WidgetBuilder> get _getRoutes {return {"/": (context) =>diff --git a/mobile/apps/auth/lib/main.dart b/mobile/apps/auth/lib/main.dartindex 99994253e0..c8a8f19b50 100644--- a/mobile/apps/auth/lib/main.dart+++ b/mobile/apps/auth/lib/main.dart@@ -1,7 +1,6 @@import 'dart:async';import 'dart:io';-import 'package:adaptive_theme/adaptive_theme.dart';import 'package:ente_accounts/services/user_service.dart';import 'package:ente_auth/app/view/app.dart';import 'package:ente_auth/core/configuration.dart';@@ -103,8 +102,7 @@ void main() async {Future<void> _runInForeground() async {AppThemeConfig.initialize(EnteApp.auth);- final adaptiveThemeMode = await AuthThemePreferences.getThemeMode();- final savedThemeMode = _themeMode(adaptiveThemeMode);+ final savedThemeMode = await AuthThemePreferences.getThemeMode();final configuration = Configuration.instance;return await _runWithLogs(() async {_logger.info("Starting app in foreground");@@ -118,8 +116,7 @@ Future<void> _runInForeground() async {unawaited(UpdateService.instance.showUpdateNotification());runApp(AppLock(- builder: (args) =>- App(locale: locale, savedThemeMode: adaptiveThemeMode),+ builder: (args) => App(locale: locale, savedThemeMode: savedThemeMode),lockScreen: LockScreen(configuration),enabled: await LockScreenSettings.instance.shouldShowLockScreen(),locale: locale,@@ -138,13 +135,6 @@ Future<void> _runInForeground() async {});}-ThemeMode _themeMode(AdaptiveThemeMode? savedThemeMode) {- if (savedThemeMode == null) return ThemeMode.system;- if (savedThemeMode.isLight) return ThemeMode.light;- if (savedThemeMode.isDark) return ThemeMode.dark;- return ThemeMode.system;-}-Future _runWithLogs(Function() function, {String prefix = ""}) async {String dir = "";try {diff --git a/mobile/apps/auth/lib/services/auth_theme_preferences.dart b/mobile/apps/auth/lib/services/auth_theme_preferences.dartindex 8d231d8c85..88a32682f5 100644--- a/mobile/apps/auth/lib/services/auth_theme_preferences.dart+++ b/mobile/apps/auth/lib/services/auth_theme_preferences.dart@@ -1,6 +1,5 @@import "dart:convert";-import "package:adaptive_theme/adaptive_theme.dart";import "package:flutter/material.dart";import "package:shared_preferences/shared_preferences.dart";@@ -8,61 +7,53 @@ class AuthThemePreferences {AuthThemePreferences._();static const _authThemeModeKey = "ente_auth_theme_mode";+ static const _adaptiveThemePrefKey = "adaptive_theme_preferences";static const _themeModeKey = "theme_mode";- static const _defaultThemeModeKey = "default_theme_mode";- static Future<AdaptiveThemeMode> getThemeMode() async {+ static Future<ThemeMode> getThemeMode() async {final authThemeMode = await _getAuthThemeMode();if (authThemeMode != null) {return authThemeMode;}- final savedThemeMode = await AdaptiveTheme.getThemeMode();- if (savedThemeMode != null) {- return savedThemeMode;+ final migratedThemeMode = await _getMigratedThemeMode();+ if (migratedThemeMode != null) {+ await _setAuthThemeMode(migratedThemeMode);+ return migratedThemeMode;}- return await _getLegacyThemeMode() ?? AdaptiveThemeMode.system;+ return ThemeMode.system;}- static Future<void> setThemeMode(- AdaptiveThemeManager<ThemeData> adaptiveTheme,- AdaptiveThemeMode themeMode,- ) async {- adaptiveTheme.setThemeMode(themeMode);- await Future.wait([- _setAuthThemeMode(themeMode),- _setAdaptiveThemeMode(themeMode),- _setLegacyThemeMode(themeMode),- ]);- }+ static Future<void> setThemeMode(ThemeMode themeMode) =>+ _setAuthThemeMode(themeMode);- static Future<AdaptiveThemeMode?> _getAuthThemeMode() async {- final prefs = SharedPreferencesAsync();- return _themeModeFromIndex(await prefs.getInt(_authThemeModeKey));+ static Future<ThemeMode?> _getAuthThemeMode() async {+ final prefs = await SharedPreferences.getInstance();+ return _themeModeFromIndex(prefs.getInt(_authThemeModeKey));}- static Future<void> _setAuthThemeMode(AdaptiveThemeMode themeMode) async {- final prefs = SharedPreferencesAsync();- await prefs.setInt(_authThemeModeKey, themeMode.index);+ static Future<void> _setAuthThemeMode(ThemeMode themeMode) async {+ final prefs = await SharedPreferences.getInstance();+ await prefs.setInt(_authThemeModeKey, _themeModeIndex(themeMode));}- static Future<void> _setAdaptiveThemeMode(AdaptiveThemeMode themeMode) async {+ static Future<ThemeMode?> _getMigratedThemeMode() async {final prefs = SharedPreferencesAsync();- await prefs.setString(AdaptiveTheme.prefKey, _themeModeJson(themeMode));+ return _parseThemeMode(await prefs.getString(_adaptiveThemePrefKey)) ??+ await _getLegacyAdaptiveThemeMode();}- static Future<AdaptiveThemeMode?> _getLegacyThemeMode() async {+ static Future<ThemeMode?> _getLegacyAdaptiveThemeMode() async {final prefs = await SharedPreferences.getInstance();- final themeDataString = prefs.getString(AdaptiveTheme.prefKey);+ return _parseThemeMode(prefs.getString(_adaptiveThemePrefKey));+ }++ static ThemeMode? _parseThemeMode(String? themeDataString) {if (themeDataString == null || themeDataString.isEmpty) {return null;}- return _parseThemeMode(themeDataString);- }-- static AdaptiveThemeMode? _parseThemeMode(String themeDataString) {try {final decoded = json.decode(themeDataString);if (decoded is! Map<String, dynamic>) {@@ -76,25 +67,27 @@ class AuthThemePreferences {}}- static AdaptiveThemeMode? _themeModeFromIndex(Object? themeModeIndex) {- if (themeModeIndex is! int ||- themeModeIndex < 0 ||- themeModeIndex >= AdaptiveThemeMode.values.length) {- return null;+ static ThemeMode? _themeModeFromIndex(Object? themeModeIndex) {+ switch (themeModeIndex) {+ case 0:+ return ThemeMode.light;+ case 1:+ return ThemeMode.dark;+ case 2:+ return ThemeMode.system;+ default:+ return null;}-- return AdaptiveThemeMode.values[themeModeIndex];}- static Future<void> _setLegacyThemeMode(AdaptiveThemeMode themeMode) async {- final prefs = await SharedPreferences.getInstance();- await prefs.setString(AdaptiveTheme.prefKey, _themeModeJson(themeMode));- }-- static String _themeModeJson(AdaptiveThemeMode themeMode) {- return json.encode({- _themeModeKey: themeMode.index,- _defaultThemeModeKey: AdaptiveThemeMode.system.index,- });+ static int _themeModeIndex(ThemeMode themeMode) {+ switch (themeMode) {+ case ThemeMode.light:+ return 0;+ case ThemeMode.dark:+ return 1;+ case ThemeMode.system:+ return 2;+ }}}diff --git a/mobile/apps/auth/lib/ui/settings/theme_switch_widget.dart b/mobile/apps/auth/lib/ui/settings/theme_switch_widget.dartindex 89b2dc22a6..f5c31e3d8c 100644--- a/mobile/apps/auth/lib/ui/settings/theme_switch_widget.dart+++ b/mobile/apps/auth/lib/ui/settings/theme_switch_widget.dart@@ -1,7 +1,6 @@-import 'package:adaptive_theme/adaptive_theme.dart';+import 'package:ente_auth/app/view/app.dart';import 'package:ente_auth/ente_theme_data.dart';import 'package:ente_auth/l10n/l10n.dart';-import 'package:ente_auth/services/auth_theme_preferences.dart';import 'package:ente_auth/theme/ente_theme.dart';import 'package:ente_auth/ui/components/captioned_text_widget.dart';import 'package:ente_auth/ui/components/expandable_menu_item_widget.dart';@@ -33,29 +32,29 @@ class _ThemeSwitchWidgetState extends State<ThemeSwitchWidget> {return Column(children: [sectionOptionSpacing,- _menuItem(context, AdaptiveThemeMode.light),+ _menuItem(context, ThemeMode.light),sectionOptionSpacing,- _menuItem(context, AdaptiveThemeMode.dark),+ _menuItem(context, ThemeMode.dark),sectionOptionSpacing,- _menuItem(context, AdaptiveThemeMode.system),+ _menuItem(context, ThemeMode.system),sectionOptionSpacing,],);}- String _name(BuildContext ctx, AdaptiveThemeMode mode) {+ String _name(BuildContext ctx, ThemeMode mode) {switch (mode) {- case AdaptiveThemeMode.light:+ case ThemeMode.light:return ctx.l10n.lightTheme;- case AdaptiveThemeMode.dark:+ case ThemeMode.dark:return ctx.l10n.darkTheme;- case AdaptiveThemeMode.system:+ case ThemeMode.system:return ctx.l10n.systemTheme;}}- Widget _menuItem(BuildContext context, AdaptiveThemeMode themeMode) {- final currentThemeMode = AdaptiveTheme.of(context).mode;+ Widget _menuItem(BuildContext context, ThemeMode themeMode) {+ final currentThemeMode = App.themeModeOf(context);return MenuItemWidget(captionedTextWidget: CaptionedTextWidget(title: _name(context, themeMode),@@ -66,20 +65,13 @@ class _ThemeSwitchWidgetState extends State<ThemeSwitchWidget> {trailingIcon: currentThemeMode == themeMode ? Icons.check : null,trailingExtraMargin: 4,onTap: () async {- final adaptiveTheme = AdaptiveTheme.of(context);final appLock = AppLock.of(context);- await AuthThemePreferences.setThemeMode(adaptiveTheme, themeMode);- appLock?.setThemeMode(_themeMode(themeMode));+ await App.setThemeMode(context, themeMode);+ appLock?.setThemeMode(themeMode);if (mounted) {setState(() {});}},);}-- ThemeMode _themeMode(AdaptiveThemeMode themeMode) {- if (themeMode.isLight) return ThemeMode.light;- if (themeMode.isDark) return ThemeMode.dark;- return ThemeMode.system;- }}diff --git a/mobile/apps/auth/pubspec.lock b/mobile/apps/auth/pubspec.lockindex 27bb4293ed..2eb23128a2 100644--- a/mobile/apps/auth/pubspec.lock+++ b/mobile/apps/auth/pubspec.lock@@ -9,14 +9,6 @@ packages:url: "https://pub.dev"source: hostedversion: "85.0.0"- adaptive_theme:- dependency: "direct main"- description:- name: adaptive_theme- sha256: "5caccff82e40ef6d3ebb28caaa091ab1865b0e35bd2ab2ddccf49cd336331012"- url: "https://pub.dev"- source: hosted- version: "3.7.2"analyzer:dependency: transitivedescription:diff --git a/mobile/apps/auth/pubspec.yaml b/mobile/apps/auth/pubspec.yamlindex a11729b995..092e28c9ea 100644--- a/mobile/apps/auth/pubspec.yaml+++ b/mobile/apps/auth/pubspec.yaml@@ -7,7 +7,6 @@ environment:sdk: ">=3.10.0 <4.0.0"dependencies:- adaptive_theme: 3.7.2app_links: 6.4.1archive: 4.0.7auto_size_text: 3.0.0diff --git a/mobile/apps/auth/test/services/auth_theme_preferences_test.dart b/mobile/apps/auth/test/services/auth_theme_preferences_test.dartindex 05535a8b53..53c0a97a41 100644--- a/mobile/apps/auth/test/services/auth_theme_preferences_test.dart+++ b/mobile/apps/auth/test/services/auth_theme_preferences_test.dart@@ -1,6 +1,5 @@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";@@ -8,6 +7,9 @@ import "package:shared_preferences/shared_preferences.dart";import "package:shared_preferences_platform_interface/in_memory_shared_preferences_async.dart";import "package:shared_preferences_platform_interface/shared_preferences_async_platform_interface.dart";+const _authThemeModeKey = "ente_auth_theme_mode";+const _adaptiveThemePrefKey = "adaptive_theme_preferences";+void main() {setUp(() {SharedPreferencesAsyncPlatform.instance =@@ -16,65 +18,88 @@ void main() {});test("returns system when no theme preference exists", () async {- expect(await AuthThemePreferences.getThemeMode(), AdaptiveThemeMode.system);+ expect(await AuthThemePreferences.getThemeMode(), ThemeMode.system);});- test("prefers adaptive theme async preference", () async {- await _setAsyncThemeMode(AdaptiveThemeMode.dark);- await _setLegacyThemeMode(AdaptiveThemeMode.light);+ test("prefers Auth theme preference", () async {+ await _setAuthThemeMode(ThemeMode.dark);+ await _setAsyncThemeMode(ThemeMode.light);+ await _setLegacyThemeMode(ThemeMode.light);- expect(await AuthThemePreferences.getThemeMode(), AdaptiveThemeMode.dark);+ expect(await AuthThemePreferences.getThemeMode(), ThemeMode.dark);});- test("falls back to legacy adaptive theme preference", () async {- await _setLegacyThemeMode(AdaptiveThemeMode.light);+ test("migrates adaptive theme async preference", () async {+ await _setAsyncThemeMode(ThemeMode.dark);+ await _setLegacyThemeMode(ThemeMode.light);- expect(await AuthThemePreferences.getThemeMode(), AdaptiveThemeMode.light);+ expect(await AuthThemePreferences.getThemeMode(), ThemeMode.dark);+ expect(await _getAuthThemeMode(), ThemeMode.dark);});- 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);+ test("migrates legacy adaptive theme preference", () async {+ await _setLegacyThemeMode(ThemeMode.light);++ expect(await AuthThemePreferences.getThemeMode(), ThemeMode.light);+ expect(await _getAuthThemeMode(), ThemeMode.light);});++ test("keeps Auth theme preference when adaptive value resets", () async {+ await AuthThemePreferences.setThemeMode(ThemeMode.light);+ await _setAsyncThemeMode(ThemeMode.system);+ await _setLegacyThemeMode(ThemeMode.system);++ expect(await AuthThemePreferences.getThemeMode(), ThemeMode.light);+ });+}++Future<void> _setAuthThemeMode(ThemeMode themeMode) async {+ final prefs = await SharedPreferences.getInstance();+ await prefs.setInt(_authThemeModeKey, _themeModeIndex(themeMode));}-Future<void> _setAsyncThemeMode(AdaptiveThemeMode themeMode) async {+Future<ThemeMode?> _getAuthThemeMode() async {+ final prefs = await SharedPreferences.getInstance();+ return _themeModeFromIndex(prefs.getInt(_authThemeModeKey));+}++Future<void> _setAsyncThemeMode(ThemeMode themeMode) async {final prefs = SharedPreferencesAsync();- await prefs.setString(AdaptiveTheme.prefKey, _themeModeJson(themeMode));+ await prefs.setString(_adaptiveThemePrefKey, _themeModeJson(themeMode));}-Future<void> _setLegacyThemeMode(AdaptiveThemeMode themeMode) async {- SharedPreferences.setMockInitialValues({- AdaptiveTheme.prefKey: _themeModeJson(themeMode),- });+Future<void> _setLegacyThemeMode(ThemeMode themeMode) async {+ final prefs = await SharedPreferences.getInstance();+ await prefs.setString(_adaptiveThemePrefKey, _themeModeJson(themeMode));}-String _themeModeJson(AdaptiveThemeMode themeMode) {+String _themeModeJson(ThemeMode themeMode) {return json.encode({- "theme_mode": themeMode.index,- "default_theme_mode": AdaptiveThemeMode.system.index,+ "theme_mode": _themeModeIndex(themeMode),+ "default_theme_mode": _themeModeIndex(ThemeMode.system),});}++ThemeMode? _themeModeFromIndex(Object? themeModeIndex) {+ switch (themeModeIndex) {+ case 0:+ return ThemeMode.light;+ case 1:+ return ThemeMode.dark;+ case 2:+ return ThemeMode.system;+ default:+ return null;+ }+}++int _themeModeIndex(ThemeMode themeMode) {+ switch (themeMode) {+ case ThemeMode.light:+ return 0;+ case ThemeMode.dark:+ return 1;+ case ThemeMode.system:+ return 2;+ }+}