From f7510d7eae574f0a1f3edd8828153fe40a489756 Mon Sep 17 00:00:00 2001 From: Kingkor Roy Tirtho Date: Mon, 4 Apr 2022 20:21:32 +0600 Subject: [PATCH] recommendation country set from settings suport added --- lib/components/Home/Home.dart | 12 +- lib/components/Settings.dart | 21 ++++ lib/helpers/search-youtube.dart | 1 - lib/models/LocalStorageKeys.dart | 1 + lib/models/SpotifyMarkets.dart | 186 ++++++++++++++++++++++++++++++ lib/provider/UserPreferences.dart | 18 ++- 6 files changed, 233 insertions(+), 6 deletions(-) create mode 100644 lib/models/SpotifyMarkets.dart diff --git a/lib/components/Home/Home.dart b/lib/components/Home/Home.dart index 734fe4b0..a7b4ea6d 100644 --- a/lib/components/Home/Home.dart +++ b/lib/components/Home/Home.dart @@ -27,6 +27,7 @@ import 'package:spotube/models/Logger.dart'; import 'package:spotube/models/generated_secrets.dart'; import 'package:spotube/provider/Auth.dart'; import 'package:spotube/provider/SpotifyDI.dart'; +import 'package:spotube/provider/UserPreferences.dart'; List spotifyScopes = [ "playlist-modify-public", @@ -47,6 +48,9 @@ class Home extends HookConsumerWidget { @override Widget build(BuildContext context, ref) { Auth auth = ref.watch(authProvider); + String recommendationMarket = ref.watch(userPreferencesProvider.select( + (value) => (value.recommendationMarket), + )); final pagingController = usePagingController(firstPageKey: 0); @@ -67,9 +71,11 @@ class Home extends HookConsumerWidget { final listener = useCallback((int pageKey) async { final spotify = ref.read(spotifyProvider); + try { - Page categories = - await spotify.categories.list(country: "US").getPage(15, pageKey); + Page categories = await spotify.categories + .list(country: recommendationMarket) + .getPage(15, pageKey); final items = categories.items!.toList(); if (pageKey == 0) { @@ -88,7 +94,7 @@ class Home extends HookConsumerWidget { pagingController.error = e; logger.e("pagingController.addPageRequestListener", e, stack); } - }, []); + }, [recommendationMarket]); useEffect(() { if (localStorage == null) return null; diff --git a/lib/components/Settings.dart b/lib/components/Settings.dart index 0da0d989..95e4b830 100644 --- a/lib/components/Settings.dart +++ b/lib/components/Settings.dart @@ -9,6 +9,7 @@ import 'package:spotube/components/Settings/SettingsHotkeyTile.dart'; import 'package:spotube/components/Shared/Hyperlink.dart'; 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'; @@ -142,6 +143,26 @@ class Settings extends HookConsumerWidget { ], ), const SizedBox(height: 10), + Row( + mainAxisAlignment: MainAxisAlignment.spaceBetween, + children: [ + const Text("Market Place (Recommendation Country)"), + DropdownButton( + value: preferences.recommendationMarket, + items: spotifyMarkets + .map((country) => (DropdownMenuItem( + child: Text(country), + value: country, + ))) + .toList(), + onChanged: (value) { + if (value == null) return; + preferences.setRecommendationMarket(value as String); + }, + ), + ], + ), + const SizedBox(height: 10), if (auth.isAnonymous) Wrap( spacing: 20, diff --git a/lib/helpers/search-youtube.dart b/lib/helpers/search-youtube.dart index cedea1e6..24fa2e1d 100644 --- a/lib/helpers/search-youtube.dart +++ b/lib/helpers/search-youtube.dart @@ -1,5 +1,4 @@ import 'dart:io'; -import 'package:collection/collection.dart'; import 'package:spotify/spotify.dart'; import 'package:youtube_explode_dart/youtube_explode_dart.dart'; diff --git a/lib/models/LocalStorageKeys.dart b/lib/models/LocalStorageKeys.dart index 1af5088c..9d6104ef 100644 --- a/lib/models/LocalStorageKeys.dart +++ b/lib/models/LocalStorageKeys.dart @@ -1,4 +1,5 @@ abstract class LocalStorageKeys { + static String recommendationMarket = 'recommendation_market'; static String clientId = 'client_id'; static String clientSecret = 'client_secret'; static String accessToken = 'access_token'; diff --git a/lib/models/SpotifyMarkets.dart b/lib/models/SpotifyMarkets.dart new file mode 100644 index 00000000..e734800c --- /dev/null +++ b/lib/models/SpotifyMarkets.dart @@ -0,0 +1,186 @@ +final spotifyMarkets = [ + "AD", + "AE", + "AG", + "AL", + "AM", + "AO", + "AR", + "AT", + "AU", + "AZ", + "BA", + "BB", + "BD", + "BE", + "BF", + "BG", + "BH", + "BI", + "BJ", + "BN", + "BO", + "BR", + "BS", + "BT", + "BW", + "BY", + "BZ", + "CA", + "CD", + "CG", + "CH", + "CI", + "CL", + "CM", + "CO", + "CR", + "CV", + "CW", + "CY", + "CZ", + "DE", + "DJ", + "DK", + "DM", + "DO", + "DZ", + "EC", + "EE", + "EG", + "ES", + "FI", + "FJ", + "FM", + "FR", + "GA", + "GB", + "GD", + "GE", + "GH", + "GM", + "GN", + "GQ", + "GR", + "GT", + "GW", + "GY", + "HK", + "HN", + "HR", + "HT", + "HU", + "ID", + "IE", + "IL", + "IN", + "IQ", + "IS", + "IT", + "JM", + "JO", + "JP", + "KE", + "KG", + "KH", + "KI", + "KM", + "KN", + "KR", + "KW", + "KZ", + "LA", + "LB", + "LC", + "LI", + "LK", + "LR", + "LS", + "LT", + "LU", + "LV", + "LY", + "MA", + "MC", + "MD", + "ME", + "MG", + "MH", + "MK", + "ML", + "MN", + "MO", + "MR", + "MT", + "MU", + "MV", + "MW", + "MX", + "MY", + "MZ", + "NA", + "NE", + "NG", + "NI", + "NL", + "NO", + "NP", + "NR", + "NZ", + "OM", + "PA", + "PE", + "PG", + "PH", + "PK", + "PL", + "PS", + "PT", + "PW", + "PY", + "QA", + "RO", + "RS", + "RU", + "RW", + "SA", + "SB", + "SC", + "SE", + "SG", + "SI", + "SK", + "SL", + "SM", + "SN", + "SR", + "ST", + "SV", + "SZ", + "TD", + "TG", + "TH", + "TJ", + "TL", + "TN", + "TO", + "TR", + "TT", + "TV", + "TW", + "TZ", + "UA", + "UG", + "US", + "UY", + "UZ", + "VC", + "VE", + "VN", + "VU", + "WS", + "XK", + "ZA", + "ZM", + "ZW" +]; diff --git a/lib/provider/UserPreferences.dart b/lib/provider/UserPreferences.dart index 49ba2c8e..614606da 100644 --- a/lib/provider/UserPreferences.dart +++ b/lib/provider/UserPreferences.dart @@ -10,12 +10,14 @@ import 'package:spotube/models/Logger.dart'; import 'package:spotube/models/generated_secrets.dart'; class UserPreferences extends ChangeNotifier { + String recommendationMarket; String geniusAccessToken; HotKey? nextTrackHotKey; HotKey? prevTrackHotKey; HotKey? playPauseHotKey; UserPreferences({ required this.geniusAccessToken, + required this.recommendationMarket, this.nextTrackHotKey, this.prevTrackHotKey, this.playPauseHotKey, @@ -43,6 +45,9 @@ class UserPreferences extends ChangeNotifier { SharedPreferences localStorage = await SharedPreferences.getInstance(); String? accessToken = localStorage.getString(LocalStorageKeys.geniusAccessToken); + + recommendationMarket = + localStorage.getString(LocalStorageKeys.recommendationMarket) ?? 'US'; geniusAccessToken = accessToken != null && accessToken.isNotEmpty ? accessToken : getRandomElement(lyricsSecrets); @@ -79,6 +84,14 @@ class UserPreferences extends ChangeNotifier { } } + void setRecommendationMarket(String country) { + recommendationMarket = country; + SharedPreferences.getInstance().then((value) { + value.setString(LocalStorageKeys.recommendationMarket, country); + notifyListeners(); + }); + } + void setGeniusAccessToken(String token) { geniusAccessToken = token; notifyListeners(); @@ -118,5 +131,6 @@ class UserPreferences extends ChangeNotifier { } } -final userPreferencesProvider = - ChangeNotifierProvider((_) => UserPreferences(geniusAccessToken: "")); +final userPreferencesProvider = ChangeNotifierProvider( + (_) => UserPreferences(geniusAccessToken: "", recommendationMarket: 'US'), +);