diff --git a/lib/components/Lyrics.dart b/lib/components/Lyrics.dart index 220ea347..9bcdf6ae 100644 --- a/lib/components/Lyrics.dart +++ b/lib/components/Lyrics.dart @@ -3,6 +3,7 @@ import 'package:provider/provider.dart'; import 'package:spotube/helpers/artist-to-string.dart'; import 'package:spotube/helpers/getLyrics.dart'; import 'package:spotube/provider/Playback.dart'; +import 'package:spotube/provider/UserPreferences.dart'; class Lyrics extends StatefulWidget { const Lyrics({Key? key}) : super(key: key); @@ -12,19 +13,20 @@ class Lyrics extends StatefulWidget { } class _LyricsState extends State { - Map? _lyrics; + Map _lyrics = {}; @override Widget build(BuildContext context) { Playback playback = context.watch(); + UserPreferences userPreferences = context.watch(); if (playback.currentTrack != null && - playback.currentTrack!.id != _lyrics?["id"]) { + userPreferences.geniusAccessToken != null && + playback.currentTrack!.id != _lyrics["id"]) { getLyrics( playback.currentTrack!.name!, artistsToString(playback.currentTrack!.artists ?? []), - apiKey: - "O6K9JcMNsVD36lRJM6wvl0YsfjrtHFFfAwYHZqxxTNg2xBuMxcaJXrYbpR6kVipN", + apiKey: userPreferences.geniusAccessToken, optimizeQuery: true, ).then((lyrics) { if (lyrics != null) { @@ -35,7 +37,7 @@ class _LyricsState extends State { }); } - if (_lyrics == null && playback.currentTrack != null) { + if (_lyrics["lyrics"] == null && playback.currentTrack != null) { return const Expanded( child: Center( child: CircularProgressIndicator.adaptive(), @@ -48,13 +50,13 @@ class _LyricsState extends State { children: [ Center( child: Text( - playback.currentTrack!.name!, + playback.currentTrack?.name ?? "", style: Theme.of(context).textTheme.headline3, ), ), Center( child: Text( - artistsToString(playback.currentTrack!.artists ?? []), + artistsToString(playback.currentTrack?.artists ?? []), style: Theme.of(context).textTheme.headline5, ), ), @@ -62,9 +64,9 @@ class _LyricsState extends State { child: SingleChildScrollView( child: Center( child: Text( - _lyrics == null && playback.currentTrack == null + _lyrics["lyrics"] == null && playback.currentTrack == null ? "No Track being played currently" - : _lyrics!["lyrics"]!, + : _lyrics["lyrics"]!, style: Theme.of(context).textTheme.headline6, ), ), diff --git a/lib/components/Settings.dart b/lib/components/Settings.dart index dfcee303..c45233e4 100644 --- a/lib/components/Settings.dart +++ b/lib/components/Settings.dart @@ -2,7 +2,9 @@ import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:spotube/components/PageWindowTitleBar.dart'; +import 'package:spotube/models/LocalStorageKeys.dart'; import 'package:spotube/provider/Auth.dart'; +import 'package:spotube/provider/UserPreferences.dart'; class Settings extends StatefulWidget { const Settings({Key? key}) : super(key: key); @@ -12,8 +14,30 @@ class Settings extends StatefulWidget { } class _SettingsState extends State { + TextEditingController? _textEditingController; + String? _geniusAccessToken; + + @override + void initState() { + super.initState(); + _textEditingController = TextEditingController(); + _textEditingController?.addListener(() { + setState(() { + _geniusAccessToken = _textEditingController?.value.text; + }); + }); + } + + @override + void dispose() { + _textEditingController?.dispose(); + super.dispose(); + } + @override Widget build(BuildContext context) { + UserPreferences preferences = context.watch(); + return Scaffold( body: Column( children: [ @@ -25,6 +49,51 @@ class _SettingsState extends State { ), ), const SizedBox(height: 10), + Padding( + padding: const EdgeInsets.all(10), + child: Row( + children: [ + Expanded( + flex: 2, + child: Text( + "Genius Access Token", + style: Theme.of(context).textTheme.subtitle1, + ), + ), + Expanded( + flex: 1, + child: TextField( + controller: _textEditingController, + decoration: InputDecoration( + hintText: preferences.geniusAccessToken, + ), + ), + ), + Padding( + padding: const EdgeInsets.all(8.0), + child: ElevatedButton( + onPressed: _geniusAccessToken != null + ? () async { + SharedPreferences localStorage = + await SharedPreferences.getInstance(); + preferences + .setGeniusAccessToken(_geniusAccessToken); + localStorage.setString( + LocalStorageKeys.geniusAccessToken, + _geniusAccessToken!); + setState(() { + _geniusAccessToken = null; + }); + _textEditingController?.text = ""; + } + : null, + child: const Text("Save"), + ), + ) + ], + ), + ), + const SizedBox(height: 10), Builder(builder: (context) { var auth = context.read(); return ElevatedButton( diff --git a/lib/main.dart b/lib/main.dart index 09e26b52..eb0b795c 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -10,6 +10,7 @@ import 'package:spotube/provider/Auth.dart'; import 'package:spotube/provider/Playback.dart'; import 'package:spotube/provider/PlayerDI.dart'; import 'package:spotube/provider/SpotifyDI.dart'; +import 'package:spotube/provider/UserPreferences.dart'; void main() { runApp(MyApp()); @@ -69,7 +70,12 @@ class MyApp extends StatelessWidget { "--script-opts=ytdl_hook-ytdl_path=yt-dlp", ], )), - ) + ), + ChangeNotifierProvider( + create: (context) { + return UserPreferences(); + }, + ), ], child: MaterialApp( debugShowCheckedModeBanner: false, diff --git a/lib/models/LocalStorageKeys.dart b/lib/models/LocalStorageKeys.dart index 65bbad25..c6d7790f 100644 --- a/lib/models/LocalStorageKeys.dart +++ b/lib/models/LocalStorageKeys.dart @@ -3,5 +3,6 @@ abstract class LocalStorageKeys { static String clientSecret = 'client_secret'; static String accessToken = 'access_token'; static String refreshToken = 'refresh_token'; - static String expiration = " expiration"; + static String expiration = "expiration"; + static String geniusAccessToken = "genius_access_token"; } diff --git a/lib/provider/UserPreferences.dart b/lib/provider/UserPreferences.dart new file mode 100644 index 00000000..0fe10c5e --- /dev/null +++ b/lib/provider/UserPreferences.dart @@ -0,0 +1,25 @@ +import 'package:flutter/cupertino.dart'; +import 'package:shared_preferences/shared_preferences.dart'; +import 'package:spotube/models/LocalStorageKeys.dart'; + +class UserPreferences extends ChangeNotifier { + String? _geniusAccessToken; + UserPreferences({String? geniusAccessToken}) { + if (geniusAccessToken == null) { + SharedPreferences.getInstance().then((localStorage) { + String? accessToken = + localStorage.getString(LocalStorageKeys.geniusAccessToken); + _geniusAccessToken ??= accessToken; + }); + } else { + _geniusAccessToken = geniusAccessToken; + } + } + + get geniusAccessToken => _geniusAccessToken; + + setGeniusAccessToken(String? token) { + _geniusAccessToken = token; + notifyListeners(); + } +}