From 46efefe07c279f465c4c28a79789fa0122c2364b Mon Sep 17 00:00:00 2001 From: Kingkor Roy Tirtho Date: Tue, 5 Apr 2022 15:05:45 +0600 Subject: [PATCH] fixed Spotube doesn't remember theme setting (#54) --- lib/components/Settings.dart | 6 +-- lib/main.dart | 22 ++------ lib/provider/ThemeProvider.dart | 6 --- lib/provider/UserPreferences.dart | 87 +++++++++++++++++-------------- 4 files changed, 54 insertions(+), 67 deletions(-) delete mode 100644 lib/provider/ThemeProvider.dart diff --git a/lib/components/Settings.dart b/lib/components/Settings.dart index 8f01fe32c..d1187a37c 100644 --- a/lib/components/Settings.dart +++ b/lib/components/Settings.dart @@ -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 { @@ -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(null); TextEditingController textEditingController = useTextEditingController(); @@ -115,7 +113,7 @@ class Settings extends HookConsumerWidget { children: [ const Text("Theme"), DropdownButton( - value: theme, + value: preferences.themeMode, items: const [ DropdownMenuItem( child: Text( @@ -136,7 +134,7 @@ class Settings extends HookConsumerWidget { ], onChanged: (value) { if (value != null) { - ref.read(themeProvider.notifier).state = value; + preferences.setThemeMode(value); } }, ) diff --git a/lib/main.dart b/lib/main.dart index 87293f8c9..bc63f96ad 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -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'; @@ -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(); diff --git a/lib/provider/ThemeProvider.dart b/lib/provider/ThemeProvider.dart deleted file mode 100644 index 870c5aab0..000000000 --- a/lib/provider/ThemeProvider.dart +++ /dev/null @@ -1,6 +0,0 @@ -import 'package:flutter/material.dart'; -import 'package:flutter_riverpod/flutter_riverpod.dart'; - -var themeProvider = StateProvider((ref) { - return ThemeMode.system; -}); diff --git a/lib/provider/UserPreferences.dart b/lib/provider/UserPreferences.dart index 892f19f54..938446ff8 100644 --- a/lib/provider/UserPreferences.dart +++ b/lib/provider/UserPreferences.dart @@ -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'; @@ -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, @@ -29,9 +32,8 @@ class UserPreferences extends ChangeNotifier { final logger = getLogger(UserPreferences); - Future _getHotKeyFromLocalStorage( - SharedPreferences preferences, String key) async { - String? str = preferences.getString(key); + Future _getHotKeyFromLocalStorage(String key) async { + String? str = localStorage?.getString(key); if (str != null) { Map json = await jsonDecode(str); if (json.isEmpty) { @@ -44,21 +46,33 @@ class UserPreferences extends ChangeNotifier { Future 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( @@ -67,7 +81,6 @@ class UserPreferences extends ChangeNotifier { ); prevTrackHotKey ??= (await _getHotKeyFromLocalStorage( - localStorage, LocalStorageKeys.prevTrackHotKey, )) ?? HotKey( @@ -76,7 +89,6 @@ class UserPreferences extends ChangeNotifier { ); playPauseHotKey ??= (await _getHotKeyFromLocalStorage( - localStorage, LocalStorageKeys.playPauseHotKey, )) ?? HotKey( @@ -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) { @@ -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, + ), );