Skip to content

Commit

Permalink
Make only one instance of BoardColorScheme
Browse files Browse the repository at this point in the history
  • Loading branch information
veloce committed Feb 21, 2024
1 parent c13c6a8 commit 92cb888
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 37 deletions.
39 changes: 2 additions & 37 deletions lib/src/model/settings/board_preferences.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import 'dart:convert';

import 'package:chessground/chessground.dart' hide BoardTheme;
import 'package:flutter/material.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:lichess_mobile/src/db/shared_preferences.dart';
import 'package:lichess_mobile/src/utils/color_palette.dart';
Expand Down Expand Up @@ -200,40 +199,6 @@ enum BoardTheme {
/// Get the [BoardColorScheme] based on system colors on Android 12+.
///
/// If the system colors are not available, the default brown theme is used.
static BoardColorScheme get systemColors {
final corePalette = getCorePalette();
if (corePalette == null) {
return brown.colors;
}
final darkSquare = Color(corePalette.secondary.get(60));
final lightSquare = Color(corePalette.primary.get(95));
return BoardColorScheme(
darkSquare: darkSquare,
lightSquare: lightSquare,
background: SolidColorBackground(
lightSquare: lightSquare,
darkSquare: darkSquare,
),
whiteCoordBackground: SolidColorBackground(
lightSquare: lightSquare,
darkSquare: darkSquare,
coordinates: true,
),
blackCoordBackground: SolidColorBackground(
lightSquare: lightSquare,
darkSquare: darkSquare,
coordinates: true,
orientation: Side.black,
),
lastMove: HighlightDetails(
solidColor: Color(corePalette.tertiary.get(80)).withOpacity(0.8),
),
selected: HighlightDetails(
solidColor: Color(corePalette.tertiary.get(80)).withOpacity(0.6),
),
validMoves: Color(corePalette.neutral.get(60)).withOpacity(0.45),
validPremoves:
Color(corePalette.neutralVariant.get(60)).withOpacity(0.45),
);
}
static BoardColorScheme get systemColors =>
getBoardColorScheme() ?? BoardColorScheme.brown;
}
42 changes: 42 additions & 0 deletions lib/src/utils/color_palette.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,54 @@
import 'dart:ui';

import 'package:chessground/chessground.dart';
import 'package:material_color_utilities/material_color_utilities.dart';

CorePalette? _corePalette;

BoardColorScheme? _boardColorScheme;

void setCorePalette(CorePalette? palette) {
_corePalette = palette;

if (palette != null) {
final darkSquare = Color(palette.secondary.get(60));
final lightSquare = Color(palette.primary.get(95));
_boardColorScheme = BoardColorScheme(
darkSquare: darkSquare,
lightSquare: lightSquare,
background: SolidColorBackground(
lightSquare: lightSquare,
darkSquare: darkSquare,
),
whiteCoordBackground: SolidColorBackground(
lightSquare: lightSquare,
darkSquare: darkSquare,
coordinates: true,
),
blackCoordBackground: SolidColorBackground(
lightSquare: lightSquare,
darkSquare: darkSquare,
coordinates: true,
orientation: Side.black,
),
lastMove: HighlightDetails(
solidColor: Color(palette.tertiary.get(80)).withOpacity(0.8),
),
selected: HighlightDetails(
solidColor: Color(palette.tertiary.get(80)).withOpacity(0.6),
),
validMoves: Color(palette.tertiary.get(30)).withOpacity(0.30),
validPremoves: Color(palette.tertiary.get(30)).withOpacity(0.30),
);
}
}

/// Get the core palette if available (android 12+ only).
CorePalette? getCorePalette() {
return _corePalette;
}

/// Get the board colors based on the core palette, if available (android 12+).
BoardColorScheme? getBoardColorScheme() {
return _boardColorScheme;
}

0 comments on commit 92cb888

Please sign in to comment.