Skip to content

Commit

Permalink
fixed Spotube doesn't remember theme setting (#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
KRTirtho committed Apr 5, 2022
1 parent d62f3ce commit 46efefe
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 67 deletions.
6 changes: 2 additions & 4 deletions lib/components/Settings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import 'package:spotube/components/Shared/PageWindowTitleBar.dart';
import 'package:spotube/models/LocalStorageKeys.dart';
import 'package:spotube/models/SpotifyMarkets.dart';
import 'package:spotube/provider/Auth.dart';
import 'package:spotube/provider/ThemeProvider.dart';
import 'package:spotube/provider/UserPreferences.dart';

class Settings extends HookConsumerWidget {
Expand All @@ -20,7 +19,6 @@ class Settings extends HookConsumerWidget {
@override
Widget build(BuildContext context, ref) {
final UserPreferences preferences = ref.watch(userPreferencesProvider);
final ThemeMode theme = ref.watch(themeProvider);
final Auth auth = ref.watch(authProvider);
var geniusAccessToken = useState<String?>(null);
TextEditingController textEditingController = useTextEditingController();
Expand Down Expand Up @@ -115,7 +113,7 @@ class Settings extends HookConsumerWidget {
children: [
const Text("Theme"),
DropdownButton<ThemeMode>(
value: theme,
value: preferences.themeMode,
items: const [
DropdownMenuItem(
child: Text(
Expand All @@ -136,7 +134,7 @@ class Settings extends HookConsumerWidget {
],
onChanged: (value) {
if (value != null) {
ref.read(themeProvider.notifier).state = value;
preferences.setThemeMode(value);
}
},
)
Expand Down
22 changes: 3 additions & 19 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@ import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:go_router/go_router.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:hotkey_manager/hotkey_manager.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:spotube/models/GoRouteDeclarations.dart';
import 'package:spotube/models/LocalStorageKeys.dart';
import 'package:spotube/models/Logger.dart';
import 'package:spotube/provider/AudioPlayer.dart';
import 'package:spotube/provider/ThemeProvider.dart';
import 'package:spotube/provider/UserPreferences.dart';
import 'package:spotube/provider/YouTube.dart';
import 'package:just_audio_background/just_audio_background.dart';

Expand Down Expand Up @@ -44,25 +42,11 @@ class MyApp extends HookConsumerWidget {
MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context, ref) {
var themeMode = ref.watch(themeProvider);
var themeMode =
ref.watch(userPreferencesProvider.select((s) => s.themeMode));
var player = ref.watch(audioPlayerProvider);
var youtube = ref.watch(youtubeProvider);
useEffect(() {
SharedPreferences.getInstance().then((localStorage) {
String? themeMode = localStorage.getString(LocalStorageKeys.themeMode);
var themeNotifier = ref.read(themeProvider.notifier);

switch (themeMode) {
case "light":
themeNotifier.state = ThemeMode.light;
break;
case "dark":
themeNotifier.state = ThemeMode.dark;
break;
default:
themeNotifier.state = ThemeMode.system;
}
});
return () {
player.dispose();
youtube.close();
Expand Down
6 changes: 0 additions & 6 deletions lib/provider/ThemeProvider.dart

This file was deleted.

87 changes: 49 additions & 38 deletions lib/provider/UserPreferences.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'dart:convert';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:hotkey_manager/hotkey_manager.dart';
import 'package:shared_preferences/shared_preferences.dart';
Expand All @@ -10,15 +10,18 @@ import 'package:spotube/models/Logger.dart';
import 'package:spotube/models/generated_secrets.dart';

class UserPreferences extends ChangeNotifier {
ThemeMode themeMode;
String recommendationMarket;
bool saveTrackLyrics;
String geniusAccessToken;
SharedPreferences? localStorage;
HotKey? nextTrackHotKey;
HotKey? prevTrackHotKey;
HotKey? playPauseHotKey;
UserPreferences({
required this.geniusAccessToken,
required this.recommendationMarket,
required this.themeMode,
this.saveTrackLyrics = false,
this.nextTrackHotKey,
this.prevTrackHotKey,
Expand All @@ -29,9 +32,8 @@ class UserPreferences extends ChangeNotifier {

final logger = getLogger(UserPreferences);

Future<HotKey?> _getHotKeyFromLocalStorage(
SharedPreferences preferences, String key) async {
String? str = preferences.getString(key);
Future<HotKey?> _getHotKeyFromLocalStorage(String key) async {
String? str = localStorage?.getString(key);
if (str != null) {
Map<String, dynamic> json = await jsonDecode(str);
if (json.isEmpty) {
Expand All @@ -44,21 +46,33 @@ class UserPreferences extends ChangeNotifier {

Future<void> onInit() async {
try {
SharedPreferences localStorage = await SharedPreferences.getInstance();
localStorage = await SharedPreferences.getInstance();
String? accessToken =
localStorage.getString(LocalStorageKeys.geniusAccessToken);
localStorage?.getString(LocalStorageKeys.geniusAccessToken);

saveTrackLyrics =
localStorage.getBool(LocalStorageKeys.saveTrackLyrics) ?? false;
localStorage?.getBool(LocalStorageKeys.saveTrackLyrics) ?? false;

final themeModeRaw = localStorage?.getString(LocalStorageKeys.themeMode);
switch (themeModeRaw) {
case "light":
themeMode = ThemeMode.light;
break;
case "dark":
themeMode = ThemeMode.dark;
break;
default:
themeMode = ThemeMode.system;
}

recommendationMarket =
localStorage.getString(LocalStorageKeys.recommendationMarket) ?? 'US';
localStorage?.getString(LocalStorageKeys.recommendationMarket) ??
'US';
geniusAccessToken = accessToken != null && accessToken.isNotEmpty
? accessToken
: getRandomElement(lyricsSecrets);

nextTrackHotKey ??= (await _getHotKeyFromLocalStorage(
localStorage,
LocalStorageKeys.nextTrackHotKey,
)) ??
HotKey(
Expand All @@ -67,7 +81,6 @@ class UserPreferences extends ChangeNotifier {
);

prevTrackHotKey ??= (await _getHotKeyFromLocalStorage(
localStorage,
LocalStorageKeys.prevTrackHotKey,
)) ??
HotKey(
Expand All @@ -76,7 +89,6 @@ class UserPreferences extends ChangeNotifier {
);

playPauseHotKey ??= (await _getHotKeyFromLocalStorage(
localStorage,
LocalStorageKeys.playPauseHotKey,
)) ??
HotKey(
Expand All @@ -89,20 +101,21 @@ class UserPreferences extends ChangeNotifier {
}
}

void setThemeMode(ThemeMode mode) {
themeMode = mode;
localStorage?.setString(LocalStorageKeys.themeMode, mode.name);
notifyListeners();
}

void setSaveTrackLyrics(bool shouldSave) {
saveTrackLyrics = shouldSave;
SharedPreferences.getInstance().then((value) {
value.setBool(LocalStorageKeys.saveTrackLyrics, shouldSave);
notifyListeners();
});
localStorage?.setBool(LocalStorageKeys.saveTrackLyrics, shouldSave);
notifyListeners();
}

void setRecommendationMarket(String country) {
recommendationMarket = country;
SharedPreferences.getInstance().then((value) {
value.setString(LocalStorageKeys.recommendationMarket, country);
notifyListeners();
});
localStorage?.setString(LocalStorageKeys.recommendationMarket, country);
}

void setGeniusAccessToken(String token) {
Expand All @@ -112,38 +125,36 @@ class UserPreferences extends ChangeNotifier {

void setNextTrackHotKey(HotKey? value) {
nextTrackHotKey = value;
SharedPreferences.getInstance().then((preferences) {
preferences.setString(
LocalStorageKeys.nextTrackHotKey,
jsonEncode(value?.toJson() ?? {}),
);
});
localStorage?.setString(
LocalStorageKeys.nextTrackHotKey,
jsonEncode(value?.toJson() ?? {}),
);
notifyListeners();
}

void setPrevTrackHotKey(HotKey? value) {
prevTrackHotKey = value;
SharedPreferences.getInstance().then((preferences) {
preferences.setString(
LocalStorageKeys.prevTrackHotKey,
jsonEncode(value?.toJson() ?? {}),
);
});
localStorage?.setString(
LocalStorageKeys.prevTrackHotKey,
jsonEncode(value?.toJson() ?? {}),
);
notifyListeners();
}

void setPlayPauseHotKey(HotKey? value) {
playPauseHotKey = value;
SharedPreferences.getInstance().then((preferences) {
preferences.setString(
LocalStorageKeys.playPauseHotKey,
jsonEncode(value?.toJson() ?? {}),
);
});
localStorage?.setString(
LocalStorageKeys.playPauseHotKey,
jsonEncode(value?.toJson() ?? {}),
);
notifyListeners();
}
}

final userPreferencesProvider = ChangeNotifierProvider(
(_) => UserPreferences(geniusAccessToken: "", recommendationMarket: 'US'),
(_) => UserPreferences(
geniusAccessToken: "",
recommendationMarket: 'US',
themeMode: ThemeMode.system,
),
);

0 comments on commit 46efefe

Please sign in to comment.