Skip to content

Commit

Permalink
fix: system color scheme not persisting on restart when system color …
Browse files Browse the repository at this point in the history
…scheme changed
  • Loading branch information
KRTirtho committed Mar 23, 2023
1 parent 7131efa commit e04515d
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 37 deletions.
78 changes: 51 additions & 27 deletions lib/components/settings/color_scheme_picker_dialog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,42 @@ import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:spotube/provider/user_preferences_provider.dart';
import 'package:system_theme/system_theme.dart';

final Map<String, Color> colorsMap = {
"System": SystemTheme.accentColor.accent,
"Red": Colors.red,
"Pink": Colors.pink,
"Purple": Colors.purple,
"DeepPurple": Colors.deepPurple,
"Indigo": Colors.indigo,
"Blue": Colors.blue,
"LightBlue": Colors.lightBlue,
"Cyan": Colors.cyan,
"Teal": Colors.teal,
"Green": Colors.green,
"LightGreen": Colors.lightGreen,
"Yellow": Colors.yellow,
"Amber": Colors.amber,
"Orange": Colors.orange,
"DeepOrange": Colors.deepOrange,
"Brown": Colors.brown,
class SpotubeColor extends Color {
final String name;

const SpotubeColor(int color, {required this.name}) : super(color);

const SpotubeColor.from(int value, {required this.name}) : super(value);

factory SpotubeColor.fromString(String string) {
final slices = string.split(":");
return SpotubeColor(int.parse(slices.last), name: slices.first);
}

@override
String toString() {
return "$name:$value";
}
}

final Set<SpotubeColor> colorsMap = {
SpotubeColor(SystemTheme.accentColor.accent.value, name: "System"),
SpotubeColor(Colors.red.value, name: "Red"),
SpotubeColor(Colors.pink.value, name: "Pink"),
SpotubeColor(Colors.purple.value, name: "Purple"),
SpotubeColor(Colors.deepPurple.value, name: "DeepPurple"),
SpotubeColor(Colors.indigo.value, name: "Indigo"),
SpotubeColor(Colors.blue.value, name: "Blue"),
SpotubeColor(Colors.lightBlue.value, name: "LightBlue"),
SpotubeColor(Colors.cyan.value, name: "Cyan"),
SpotubeColor(Colors.teal.value, name: "Teal"),
SpotubeColor(Colors.green.value, name: "Green"),
SpotubeColor(Colors.lightGreen.value, name: "LightGreen"),
SpotubeColor(Colors.yellow.value, name: "Yellow"),
SpotubeColor(Colors.amber.value, name: "Amber"),
SpotubeColor(Colors.orange.value, name: "Orange"),
SpotubeColor(Colors.deepOrange.value, name: "DeepOrange"),
SpotubeColor(Colors.brown.value, name: "Brown"),
};

class ColorSchemePickerDialog extends HookConsumerWidget {
Expand All @@ -32,14 +50,20 @@ class ColorSchemePickerDialog extends HookConsumerWidget {
Widget build(BuildContext context, ref) {
final preferences = ref.watch(userPreferencesProvider);
final scheme = preferences.accentColorScheme;
final active = useState<String>(colorsMap.entries.firstWhere(
final active = useState<String>(colorsMap.firstWhere(
(element) {
return scheme.value == element.value.value;
return scheme.name == element.name;
},
).key);
).name);

onOk() {
preferences.setAccentColorScheme(colorsMap[active.value]!);
preferences.setAccentColorScheme(
colorsMap.firstWhere(
(element) {
return element.name == active.value;
},
),
);
Navigator.pop(context);
}

Expand All @@ -66,14 +90,14 @@ class ColorSchemePickerDialog extends HookConsumerWidget {
},
itemCount: colorsMap.length,
itemBuilder: (context, index) {
final color = colorsMap.entries.elementAt(index);
final color = colorsMap.elementAt(index);
return ColorTile(
color: color.value,
isActive: active.value == color.key,
color: color,
isActive: active.value == color.name,
onPressed: () {
active.value = color.key;
active.value = color.name;
},
tooltip: color.key,
tooltip: color.name,
);
},
),
Expand Down
2 changes: 0 additions & 2 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import 'package:package_info_plus/package_info_plus.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:spotube/collections/cache_keys.dart';
import 'package:spotube/collections/env.dart';
import 'package:spotube/components/settings/color_scheme_picker_dialog.dart';
import 'package:spotube/components/shared/dialogs/replace_downloaded_dialog.dart';
import 'package:spotube/entities/cache_track.dart';
import 'package:spotube/collections/routes.dart';
Expand Down Expand Up @@ -70,7 +69,6 @@ void main(List<String> rawArgs) async {

WidgetsFlutterBinding.ensureInitialized();
await SystemTheme.accentColor.load();
colorsMap["System"] = SystemTheme.accentColor.accent;
await QueryClient.initialize(cachePrefix: "oss.krtirtho.spotube");
Hive.registerAdapter(CacheTrackAdapter());
Hive.registerAdapter(CacheTrackEngagementAdapter());
Expand Down
16 changes: 8 additions & 8 deletions lib/provider/user_preferences_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import 'package:path_provider/path_provider.dart';
import 'package:spotube/components/settings/color_scheme_picker_dialog.dart';
import 'package:spotube/models/generated_secrets.dart';
import 'package:spotube/utils/persisted_change_notifier.dart';
import 'package:collection/collection.dart';
import 'package:spotube/utils/platform.dart';
import 'package:spotube/utils/primitive_utils.dart';
import 'package:path/path.dart' as path;
Expand All @@ -30,7 +29,7 @@ class UserPreferences extends PersistedChangeNotifier {
bool checkUpdate;
AudioQuality audioQuality;

Color accentColorScheme;
SpotubeColor accentColorScheme;
bool skipSponsorSegments;

String downloadLocation;
Expand All @@ -46,8 +45,8 @@ class UserPreferences extends PersistedChangeNotifier {
required this.themeMode,
required this.layoutMode,
required this.predownload,
required this.accentColorScheme,
this.saveTrackLyrics = false,
this.accentColorScheme = Colors.green,
this.checkUpdate = true,
this.audioQuality = AudioQuality.high,
this.skipSponsorSegments = true,
Expand Down Expand Up @@ -93,7 +92,7 @@ class UserPreferences extends PersistedChangeNotifier {
updatePersistence();
}

void setAccentColorScheme(Color color) {
void setAccentColorScheme(SpotubeColor color) {
accentColorScheme = color;
notifyListeners();
updatePersistence();
Expand Down Expand Up @@ -162,9 +161,9 @@ class UserPreferences extends PersistedChangeNotifier {
PrimitiveUtils.getRandomElement(lyricsSecrets);

themeMode = ThemeMode.values[map["themeMode"] ?? 0];
accentColorScheme = colorsMap.values
.firstWhereOrNull((e) => e.value == map["accentColorScheme"]) ??
accentColorScheme;
accentColorScheme = map["accentColorScheme"] != null
? SpotubeColor.fromString(map["accentColorScheme"])
: accentColorScheme;
audioQuality = map["audioQuality"] != null
? AudioQuality.values[map["audioQuality"]]
: audioQuality;
Expand All @@ -187,7 +186,7 @@ class UserPreferences extends PersistedChangeNotifier {
"recommendationMarket": recommendationMarket,
"geniusAccessToken": geniusAccessToken,
"themeMode": themeMode.index,
"accentColorScheme": accentColorScheme.value,
"accentColorScheme": accentColorScheme.toString(),
"checkUpdate": checkUpdate,
"audioQuality": audioQuality.index,
"skipSponsorSegments": skipSponsorSegments,
Expand All @@ -201,6 +200,7 @@ class UserPreferences extends PersistedChangeNotifier {

final userPreferencesProvider = ChangeNotifierProvider(
(_) => UserPreferences(
accentColorScheme: SpotubeColor(Colors.blue.value, name: "Blue"),
geniusAccessToken: "",
recommendationMarket: 'US',
themeMode: ThemeMode.system,
Expand Down

0 comments on commit e04515d

Please sign in to comment.