Skip to content

Commit

Permalink
More work on custom theme
Browse files Browse the repository at this point in the history
  • Loading branch information
veloce committed Dec 17, 2024
1 parent cb303f4 commit c0db0f1
Show file tree
Hide file tree
Showing 6 changed files with 437 additions and 321 deletions.
34 changes: 14 additions & 20 deletions lib/src/app.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import 'package:lichess_mobile/src/navigation.dart';
import 'package:lichess_mobile/src/network/connectivity.dart';
import 'package:lichess_mobile/src/network/http.dart';
import 'package:lichess_mobile/src/network/socket.dart';
import 'package:lichess_mobile/src/styles/lichess_colors.dart';
import 'package:lichess_mobile/src/styles/styles.dart';
import 'package:lichess_mobile/src/utils/screen.dart';

Expand Down Expand Up @@ -140,27 +139,22 @@ class _AppState extends ConsumerState<Application> {
final dynamicColorScheme =
brightness == Brightness.light ? fixedLightScheme : fixedDarkScheme;

ColorScheme colorScheme;
if (generalPrefs.customThemeEnabled) {
if (generalPrefs.customThemeSeed != null) {
colorScheme = ColorScheme.fromSeed(
seedColor: generalPrefs.customThemeSeed!,
brightness: brightness,
);
} else if (dynamicColorScheme != null) {
colorScheme = dynamicColorScheme;
} else {
colorScheme = ColorScheme.fromSeed(
seedColor: LichessColors.primary[500]!,
brightness: brightness,
);
}
} else {
colorScheme = ColorScheme.fromSeed(
final ColorScheme colorScheme = switch (generalPrefs.appThemeSeed) {
AppThemeSeed.color => ColorScheme.fromSeed(
seedColor: generalPrefs.customThemeSeed ?? kDefaultSeedColor,
brightness: brightness,
),
AppThemeSeed.board => ColorScheme.fromSeed(
seedColor: boardTheme.colors.darkSquare,
brightness: brightness,
);
}
),
AppThemeSeed.system =>
dynamicColorScheme ??
ColorScheme.fromSeed(
seedColor: boardTheme.colors.darkSquare,
brightness: brightness,
),
};

final cupertinoThemeData = CupertinoThemeData(
primaryColor: colorScheme.primary,
Expand Down
2 changes: 2 additions & 0 deletions lib/src/constants.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ const kClueLessDeviation = 230;

// UI

const kDefaultSeedColor = Color(0xFFD64F00);

const kGoldenRatio = 1.61803398875;

/// Flex golden ratio base (flex has to be an int).
Expand Down
44 changes: 26 additions & 18 deletions lib/src/init.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ Future<void> setupFirstLaunch() async {
final appVersion = Version.parse(pInfo.version);
final installedVersion = prefs.getString('installed_version');

// TODO remove this migration code after a few releases
if (installedVersion != null && Version.parse(installedVersion) <= Version(0, 13, 9)) {
_migrateThemeSettings();
}

if (installedVersion == null || Version.parse(installedVersion) != appVersion) {
prefs.setString('installed_version', appVersion.canonicalizedVersion);
}
Expand All @@ -49,6 +54,26 @@ Future<void> setupFirstLaunch() async {
}
}

Future<void> _migrateThemeSettings() async {
final prefs = LichessBinding.instance.sharedPreferences;
try {
final stored = LichessBinding.instance.sharedPreferences.getString(
PrefCategory.general.storageKey,
);
if (stored == null) {
return;
}
final generalPrefs = GeneralPrefs.fromJson(jsonDecode(stored) as Map<String, dynamic>);
final migrated = generalPrefs.copyWith(
// ignore: deprecated_member_use_from_same_package
appThemeSeed: generalPrefs.systemColors == true ? AppThemeSeed.system : AppThemeSeed.board,
);
await prefs.setString(PrefCategory.general.storageKey, jsonEncode(migrated.toJson()));
} catch (e) {
_logger.warning('Failed to migrate theme settings: $e');
}
}

Future<void> initializeLocalNotifications(Locale locale) async {
final l10n = await AppLocalizations.delegate.load(locale);
await FlutterLocalNotificationsPlugin().initialize(
Expand Down Expand Up @@ -86,27 +111,10 @@ Future<void> preloadPieceImages() async {
///
/// This is meant to be called once during app initialization.
Future<void> androidDisplayInitialization(WidgetsBinding widgetsBinding) async {
final prefs = LichessBinding.instance.sharedPreferences;

// On android 12+ get core palette and set the board theme to system if it is not set
// On android 12+ set core palette and make system board
try {
await DynamicColorPlugin.getCorePalette().then((value) {
setCorePalette(value);

if (getCorePalette() != null) {
if (prefs.getString(PrefCategory.general.storageKey) == null) {
prefs.setString(
PrefCategory.general.storageKey,
jsonEncode(GeneralPrefs.defaults.copyWith(customThemeEnabled: true)),
);
}
if (prefs.getString(PrefCategory.board.storageKey) == null) {
prefs.setString(
PrefCategory.board.storageKey,
jsonEncode(BoardPrefs.defaults.copyWith(boardTheme: BoardTheme.system)),
);
}
}
});
} catch (e) {
_logger.fine('Device does not support core palette: $e');
Expand Down
23 changes: 22 additions & 1 deletion lib/src/model/settings/general_preferences.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import 'dart:ui' show Color, Locale;

import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:lichess_mobile/src/model/settings/board_preferences.dart';
import 'package:lichess_mobile/src/model/settings/preferences_storage.dart';
import 'package:lichess_mobile/src/utils/json.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
Expand Down Expand Up @@ -54,6 +53,10 @@ class GeneralPreferences extends _$GeneralPreferences with PreferencesStorage<Ge
Future<void> setCustomThemeSeed(Color? color) {
return save(state.copyWith(customThemeSeed: color));
}

Future<void> setAppThemeSeed(AppThemeSeed seed) {
return save(state.copyWith(appThemeSeed: seed));
}
}

@Freezed(fromJson: true, toJson: true)
Expand All @@ -71,6 +74,12 @@ class GeneralPrefs with _$GeneralPrefs implements Serializable {
/// Custom theme seed color
@ColorConverter() Color? customThemeSeed,

@Deprecated('Use appThemeSeed instead') bool? systemColors,

/// App theme seed
@JsonKey(unknownEnumValue: AppThemeSeed.color, defaultValue: AppThemeSeed.color)
required AppThemeSeed appThemeSeed,

/// Locale to use in the app, use system locale if null
@LocaleConverter() Locale? locale,
}) = _GeneralPrefs;
Expand All @@ -81,13 +90,25 @@ class GeneralPrefs with _$GeneralPrefs implements Serializable {
soundTheme: SoundTheme.standard,
masterVolume: 0.8,
customThemeEnabled: false,
appThemeSeed: AppThemeSeed.color,
);

factory GeneralPrefs.fromJson(Map<String, dynamic> json) {
return _$GeneralPrefsFromJson(json);
}
}

enum AppThemeSeed {
/// The app theme is based on the user's system theme (only available on Android 10+).
system,

/// The app theme is based on the chessboard.
board,

/// The app theme is based on a specific color.
color,
}

/// Describes the background theme of the app.
enum BackgroundThemeMode {
/// Use either the light or dark theme based on what the user has selected in
Expand Down
2 changes: 0 additions & 2 deletions lib/src/view/settings/settings_tab_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import 'package:lichess_mobile/src/model/account/account_repository.dart';
import 'package:lichess_mobile/src/model/auth/auth_controller.dart';
import 'package:lichess_mobile/src/model/auth/auth_session.dart';
import 'package:lichess_mobile/src/model/common/preloaded_data.dart';
import 'package:lichess_mobile/src/model/settings/board_preferences.dart';
import 'package:lichess_mobile/src/model/settings/general_preferences.dart';
import 'package:lichess_mobile/src/navigation.dart';
import 'package:lichess_mobile/src/styles/lichess_icons.dart';
Expand Down Expand Up @@ -82,7 +81,6 @@ class _Body extends ConsumerWidget {
});

final generalPrefs = ref.watch(generalPreferencesProvider);
final boardPrefs = ref.watch(boardPreferencesProvider);
final authController = ref.watch(authControllerProvider);
final userSession = ref.watch(authSessionProvider);
final packageInfo = ref.read(preloadedDataProvider).requireValue.packageInfo;
Expand Down
Loading

0 comments on commit c0db0f1

Please sign in to comment.