Skip to content

Commit

Permalink
fix(settings): Fix reset colors
Browse files Browse the repository at this point in the history
  • Loading branch information
realth000 committed Aug 10, 2024
1 parent cffda12 commit 7fee74e
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 41 deletions.
2 changes: 1 addition & 1 deletion lib/features/settings/bloc/settings_event.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ final class SettingsValueChanged<T> extends SettingsEvent
const SettingsValueChanged(this.settings, this.value) : super();

/// Settings that changed.
final SettingsKeys settings;
final SettingsKeys<T> settings;

/// New value.
final T value;
Expand Down
12 changes: 6 additions & 6 deletions lib/features/settings/repositories/settings_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import 'package:tsdm_client/shared/providers/storage_provider/models/database/da
import 'package:tsdm_client/shared/providers/storage_provider/storage_provider.dart';
import 'package:tsdm_client/utils/logger.dart';

typedef _SK = SettingsKeys;
typedef _SK<T> = SettingsKeys<T>;

extension _ExtractExt on List<SettingsEntity> {
T extract<T>(SettingsKeys settings) {
T extract<T>(SettingsKeys<T> settings) {
assert(
T == settings.type,
'Settings value type and expected extract type MUST equal\n'
Expand All @@ -21,7 +21,7 @@ extension _ExtractExt on List<SettingsEntity> {

final v = firstWhereOrNull((e) => e.name == settings.name);
if (v == null) {
return settings.defaultValue as T;
return settings.defaultValue;
}
return (switch (T) {
int => v.intValue,
Expand Down Expand Up @@ -105,7 +105,7 @@ final class SettingsRepository with LoggerMixin {
}

/// Get settings [key] with value in type [T}.
Future<T> getValue<T>(SettingsKeys key) async {
Future<T> getValue<T>(SettingsKeys<T> key) async {
assert(
T == key.type,
'Settings value type and expected extract type MUST equal\n'
Expand All @@ -127,14 +127,14 @@ final class SettingsRepository with LoggerMixin {
}

/// Delete the settings record in database.
Future<void> deleteValue(SettingsKeys key) async {
Future<void> deleteValue<T>(SettingsKeys<T> key) async {
await _storage.deleteKey(key.name);
_state = _state.copyWithKey(key, null);
_controller.add(_state);
}

/// Save settings [key] with value [value].
Future<void> setValue<T>(SettingsKeys key, T value) async {
Future<void> setValue<T>(SettingsKeys<T> key, T value) async {
assert(
T == key.type,
'Settings value type and expected extract type MUST equal\n'
Expand Down
9 changes: 6 additions & 3 deletions lib/features/settings/view/settings_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -220,9 +220,12 @@ class _SettingsPageState extends State<SettingsPage> {
// Effect immediately.
context.read<ThemeCubit>().clearAccentColor();
// Set to -1 ( < 0) will clear accent color.
context
.read<SettingsBloc>()
.add(const SettingsValueChanged(SettingsKeys.accentColor, -1));
context.read<SettingsBloc>().add(
SettingsValueChanged(
SettingsKeys.accentColor,
SettingsKeys.accentColor.defaultValue,
),
);
return;
}
context.read<ThemeCubit>().setAccentColor(color.$1!);
Expand Down
60 changes: 30 additions & 30 deletions lib/shared/models/settings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ part of 'models.dart';

/// Keys for all settings.
// ignore_for_file: public_member_api_docs
enum SettingsKeys implements Comparable<SettingsKeys> {
enum SettingsKeys<T> implements Comparable<SettingsKeys<T>> {
/// Net client config: Accept.
netClientAccept(
netClientAccept<String>(
name: 'netClientAccept',
type: String,
defaultValue:
Expand All @@ -19,78 +19,78 @@ enum SettingsKeys implements Comparable<SettingsKeys> {
/// After debugging like this:
/// https://github.com/flutter/flutter/issues/32558#issuecomment-886022246
/// Remove "gzip" encoding in "Accept-Encoding" can fix this.
netClientAcceptEncoding(
netClientAcceptEncoding<String>(
name: 'netClientAcceptEncoding',
type: String,
defaultValue: 'deflate, br',
),

/// Net client config: Accept-Language.
netClientAcceptLanguage(
netClientAcceptLanguage<String>(
name: 'dioAcceptLanguage',
type: String,
defaultValue: 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6,zh-TW;q=0.5',
),

/// Net client config: User-Agent.
netClientUserAgent(
netClientUserAgent<String>(
name: 'dioUserAgent',
type: String,
defaultValue:
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36',
),

/// Window width config on desktop platforms.
windowWidth(
windowWidth<double>(
name: 'windowWidth',
type: double,
defaultValue: 600.0,
defaultValue: 600,
),

/// Window height config on desktop platforms.
windowHeight(
windowHeight<double>(
name: 'windowHeight',
type: double,
defaultValue: 800.0,
defaultValue: 800,
),

/// Window position config on desktop platforms.
windowPositionDx(
windowPositionDx<double>(
name: 'windowPositionX',
type: double,
defaultValue: 0.0,
defaultValue: 0,
),

/// Window position config on desktop platforms.
windowPositionDy(
windowPositionDy<double>(
name: 'windowPositionY',
type: double,
defaultValue: 0.0,
defaultValue: 0,
),

/// Window whether in the center of screen config on desktop platforms.
windowInCenter(
windowInCenter<bool>(
name: 'windowInCenter',
type: bool,
defaultValue: false,
),

/// Login user username.
loginUsername(
loginUsername<String>(
name: 'loginUsername',
type: String,
defaultValue: '',
),

/// Login user uid.
loginUid(
loginUid<int>(
name: 'loginUid',
type: int,
defaultValue: 0,
),

/// Login user email address.
loginEmail(
loginEmail<String>(
name: 'loginEmail',
type: String,
defaultValue: '',
Expand All @@ -101,7 +101,7 @@ enum SettingsKeys implements Comparable<SettingsKeys> {
/// 0: [ThemeMode.system]
/// 1: [ThemeMode.light]
/// 2: [ThemeMode.dark]
themeMode(
themeMode<int>(
name: 'ThemeMode',
type: int,
defaultValue: 0,
Expand All @@ -110,29 +110,29 @@ enum SettingsKeys implements Comparable<SettingsKeys> {
/// Locale
///
/// Empty means follow system locale.
locale(
locale<String>(
name: 'locale',
type: String,
defaultValue: '',
),

/// Default feeling when check in
checkinFeeling(
checkinFeeling<String>(
name: 'checkInFeeling',
type: String,
defaultValue: 'kx',
),

/// Default check in message when check in
checkinMessage(
checkinMessage<String>(
name: 'checkInMessage',
type: String,
defaultValue: '每日签到',
),

/// Show shortcut widget that to redirect to latest thread or subreddit in
/// forum card.
showShortcutInForumCard(
showShortcutInForumCard<bool>(
name: 'showShortcutInForumCard',
type: bool,
defaultValue: false,
Expand All @@ -141,14 +141,14 @@ enum SettingsKeys implements Comparable<SettingsKeys> {
/// Default accent color.
///
/// Less than zero represents default color.
accentColor(
accentColor<int>(
name: 'accentColor',
type: int,
defaultValue: 4280391411, // PrimaryColors.blue
),

/// Show badge or unread notice count on notice button.
showUnreadInfoHint(
showUnreadInfoHint<bool>(
name: 'showUnreadInfoHint',
type: bool,
defaultValue: true,
Expand All @@ -157,29 +157,29 @@ enum SettingsKeys implements Comparable<SettingsKeys> {
/// Only exit the app when user press back button twice or more.
///
/// Avoid accidentally exit the app.
doublePressExit(
doublePressExit<bool>(
name: 'doublePressExit',
type: bool,
defaultValue: true,
),

/// View latest posts in thread first, in other words, posts are sorted in
/// desc order.
threadReverseOrder(
threadReverseOrder<bool>(
name: 'threadReverseOrder',
type: bool,
defaultValue: false,
),

/// Center align the info row in thread card.
threadCardInfoRowAlignCenter(
threadCardInfoRowAlignCenter<bool>(
name: 'threadCardInfoRowAlignCenter',
type: bool,
defaultValue: false,
),

/// Show last replied author's username in info row in `ThreadCard`.
threadCardShowLastReplyAuthor(
threadCardShowLastReplyAuthor<bool>(
name: 'threadCardShowLastReplyAuthor',
type: bool,
defaultValue: true,
Expand All @@ -193,8 +193,8 @@ enum SettingsKeys implements Comparable<SettingsKeys> {

final String name;
final Type type;
final dynamic defaultValue;
final T defaultValue;

@override
int compareTo(SettingsKeys other) => name.compareTo(other.name);
int compareTo(SettingsKeys<dynamic> other) => name.compareTo(other.name);
}
2 changes: 1 addition & 1 deletion lib/shared/models/settings_map.dart
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class SettingsMap with SettingsMapMappable {
final bool threadCardInfoRowAlignCenter;
final bool threadCardShowLastReplyAuthor;

SettingsMap copyWithKey<T>(SettingsKeys key, T value) {
SettingsMap copyWithKey<T>(SettingsKeys<T> key, T value) {
assert(
T == key.type || T == Null,
'Settings value type and expected extract type MUST equal\n'
Expand Down

0 comments on commit 7fee74e

Please sign in to comment.