Skip to content

Commit

Permalink
Only set system board for android 12+ devices
Browse files Browse the repository at this point in the history
  • Loading branch information
veloce committed Feb 25, 2024
1 parent bc3956a commit 8210b17
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
18 changes: 17 additions & 1 deletion lib/src/app_dependencies.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:convert';

import 'package:device_info_plus/device_info_plus.dart';
import 'package:fast_immutable_collections/fast_immutable_collections.dart';
import 'package:flutter/foundation.dart';
Expand All @@ -11,7 +13,9 @@ import 'package:lichess_mobile/src/model/auth/session_storage.dart';
import 'package:lichess_mobile/src/model/common/http.dart';
import 'package:lichess_mobile/src/model/common/service/sound_service.dart';
import 'package:lichess_mobile/src/model/common/socket.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/utils/color_palette.dart';
import 'package:lichess_mobile/src/utils/string.dart';
import 'package:logging/logging.dart';
import 'package:package_info_plus/package_info_plus.dart';
Expand Down Expand Up @@ -58,10 +62,22 @@ Future<AppDependencies> appDependencies(

final db = await openDb(databaseFactory, dbPath);

// Clear secure storage on first run because it is not deleted on app uninstall
if (prefs.getBool('first_run') ?? true) {
// Clear secure storage on first run because it is not deleted on app uninstall
await secureStorage.deleteAll();

// on android 12+ set the default board theme as system
if (getCorePalette() != null) {
prefs.setString(
BoardPreferences.prefKey,
jsonEncode(
BoardPrefs.defaults.copyWith(
boardTheme: BoardTheme.system,
),
),
);
}

await prefs.setBool('first_run', false);
}

Expand Down
14 changes: 7 additions & 7 deletions lib/src/model/settings/board_preferences.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ import 'package:riverpod_annotation/riverpod_annotation.dart';
part 'board_preferences.freezed.dart';
part 'board_preferences.g.dart';

const _prefKey = 'preferences.board';

@Riverpod(keepAlive: true)
class BoardPreferences extends _$BoardPreferences {
static const String prefKey = 'preferences.board';

@override
BoardPrefs build() {
final prefs = ref.watch(sharedPreferencesProvider);
final stored = prefs.getString(_prefKey);
final stored = prefs.getString(prefKey);
return stored != null
? BoardPrefs.fromJson(
jsonDecode(stored) as Map<String, dynamic>,
Expand All @@ -29,8 +29,8 @@ class BoardPreferences extends _$BoardPreferences {
return _save(state.copyWith(pieceSet: pieceSet));
}

Future<void> setBoardTheme(BoardTheme boardTheme) {
return _save(state.copyWith(boardTheme: boardTheme));
Future<void> setBoardTheme(BoardTheme boardTheme) async {
await _save(state.copyWith(boardTheme: boardTheme));
}

Future<void> toggleHapticFeedback() {
Expand Down Expand Up @@ -66,7 +66,7 @@ class BoardPreferences extends _$BoardPreferences {
Future<void> _save(BoardPrefs newState) async {
final prefs = ref.read(sharedPreferencesProvider);
await prefs.setString(
_prefKey,
prefKey,
jsonEncode(newState.toJson()),
);
state = newState;
Expand All @@ -91,7 +91,7 @@ class BoardPrefs with _$BoardPrefs {

static const defaults = BoardPrefs(
pieceSet: PieceSet.staunty,
boardTheme: BoardTheme.system,
boardTheme: BoardTheme.brown,
hapticFeedback: true,
showLegalMoves: true,
boardHighlights: true,
Expand Down

0 comments on commit 8210b17

Please sign in to comment.