Skip to content

Commit d647d5e

Browse files
committed
Full Lyrics support
1 parent 50b835e commit d647d5e

File tree

5 files changed

+114
-11
lines changed

5 files changed

+114
-11
lines changed

lib/components/Lyrics.dart

+11-9
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import 'package:provider/provider.dart';
33
import 'package:spotube/helpers/artist-to-string.dart';
44
import 'package:spotube/helpers/getLyrics.dart';
55
import 'package:spotube/provider/Playback.dart';
6+
import 'package:spotube/provider/UserPreferences.dart';
67

78
class Lyrics extends StatefulWidget {
89
const Lyrics({Key? key}) : super(key: key);
@@ -12,19 +13,20 @@ class Lyrics extends StatefulWidget {
1213
}
1314

1415
class _LyricsState extends State<Lyrics> {
15-
Map<String, String>? _lyrics;
16+
Map<String, String> _lyrics = {};
1617

1718
@override
1819
Widget build(BuildContext context) {
1920
Playback playback = context.watch<Playback>();
21+
UserPreferences userPreferences = context.watch<UserPreferences>();
2022

2123
if (playback.currentTrack != null &&
22-
playback.currentTrack!.id != _lyrics?["id"]) {
24+
userPreferences.geniusAccessToken != null &&
25+
playback.currentTrack!.id != _lyrics["id"]) {
2326
getLyrics(
2427
playback.currentTrack!.name!,
2528
artistsToString(playback.currentTrack!.artists ?? []),
26-
apiKey:
27-
"O6K9JcMNsVD36lRJM6wvl0YsfjrtHFFfAwYHZqxxTNg2xBuMxcaJXrYbpR6kVipN",
29+
apiKey: userPreferences.geniusAccessToken,
2830
optimizeQuery: true,
2931
).then((lyrics) {
3032
if (lyrics != null) {
@@ -35,7 +37,7 @@ class _LyricsState extends State<Lyrics> {
3537
});
3638
}
3739

38-
if (_lyrics == null && playback.currentTrack != null) {
40+
if (_lyrics["lyrics"] == null && playback.currentTrack != null) {
3941
return const Expanded(
4042
child: Center(
4143
child: CircularProgressIndicator.adaptive(),
@@ -48,23 +50,23 @@ class _LyricsState extends State<Lyrics> {
4850
children: [
4951
Center(
5052
child: Text(
51-
playback.currentTrack!.name!,
53+
playback.currentTrack?.name ?? "",
5254
style: Theme.of(context).textTheme.headline3,
5355
),
5456
),
5557
Center(
5658
child: Text(
57-
artistsToString(playback.currentTrack!.artists ?? []),
59+
artistsToString(playback.currentTrack?.artists ?? []),
5860
style: Theme.of(context).textTheme.headline5,
5961
),
6062
),
6163
Expanded(
6264
child: SingleChildScrollView(
6365
child: Center(
6466
child: Text(
65-
_lyrics == null && playback.currentTrack == null
67+
_lyrics["lyrics"] == null && playback.currentTrack == null
6668
? "No Track being played currently"
67-
: _lyrics!["lyrics"]!,
69+
: _lyrics["lyrics"]!,
6870
style: Theme.of(context).textTheme.headline6,
6971
),
7072
),

lib/components/Settings.dart

+69
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ import 'package:flutter/material.dart';
22
import 'package:provider/provider.dart';
33
import 'package:shared_preferences/shared_preferences.dart';
44
import 'package:spotube/components/PageWindowTitleBar.dart';
5+
import 'package:spotube/models/LocalStorageKeys.dart';
56
import 'package:spotube/provider/Auth.dart';
7+
import 'package:spotube/provider/UserPreferences.dart';
68

79
class Settings extends StatefulWidget {
810
const Settings({Key? key}) : super(key: key);
@@ -12,8 +14,30 @@ class Settings extends StatefulWidget {
1214
}
1315

1416
class _SettingsState extends State<Settings> {
17+
TextEditingController? _textEditingController;
18+
String? _geniusAccessToken;
19+
20+
@override
21+
void initState() {
22+
super.initState();
23+
_textEditingController = TextEditingController();
24+
_textEditingController?.addListener(() {
25+
setState(() {
26+
_geniusAccessToken = _textEditingController?.value.text;
27+
});
28+
});
29+
}
30+
31+
@override
32+
void dispose() {
33+
_textEditingController?.dispose();
34+
super.dispose();
35+
}
36+
1537
@override
1638
Widget build(BuildContext context) {
39+
UserPreferences preferences = context.watch<UserPreferences>();
40+
1741
return Scaffold(
1842
body: Column(
1943
children: [
@@ -25,6 +49,51 @@ class _SettingsState extends State<Settings> {
2549
),
2650
),
2751
const SizedBox(height: 10),
52+
Padding(
53+
padding: const EdgeInsets.all(10),
54+
child: Row(
55+
children: [
56+
Expanded(
57+
flex: 2,
58+
child: Text(
59+
"Genius Access Token",
60+
style: Theme.of(context).textTheme.subtitle1,
61+
),
62+
),
63+
Expanded(
64+
flex: 1,
65+
child: TextField(
66+
controller: _textEditingController,
67+
decoration: InputDecoration(
68+
hintText: preferences.geniusAccessToken,
69+
),
70+
),
71+
),
72+
Padding(
73+
padding: const EdgeInsets.all(8.0),
74+
child: ElevatedButton(
75+
onPressed: _geniusAccessToken != null
76+
? () async {
77+
SharedPreferences localStorage =
78+
await SharedPreferences.getInstance();
79+
preferences
80+
.setGeniusAccessToken(_geniusAccessToken);
81+
localStorage.setString(
82+
LocalStorageKeys.geniusAccessToken,
83+
_geniusAccessToken!);
84+
setState(() {
85+
_geniusAccessToken = null;
86+
});
87+
_textEditingController?.text = "";
88+
}
89+
: null,
90+
child: const Text("Save"),
91+
),
92+
)
93+
],
94+
),
95+
),
96+
const SizedBox(height: 10),
2897
Builder(builder: (context) {
2998
var auth = context.read<Auth>();
3099
return ElevatedButton(

lib/main.dart

+7-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import 'package:spotube/provider/Auth.dart';
1010
import 'package:spotube/provider/Playback.dart';
1111
import 'package:spotube/provider/PlayerDI.dart';
1212
import 'package:spotube/provider/SpotifyDI.dart';
13+
import 'package:spotube/provider/UserPreferences.dart';
1314

1415
void main() {
1516
runApp(MyApp());
@@ -69,7 +70,12 @@ class MyApp extends StatelessWidget {
6970
"--script-opts=ytdl_hook-ytdl_path=yt-dlp",
7071
],
7172
)),
72-
)
73+
),
74+
ChangeNotifierProvider<UserPreferences>(
75+
create: (context) {
76+
return UserPreferences();
77+
},
78+
),
7379
],
7480
child: MaterialApp(
7581
debugShowCheckedModeBanner: false,

lib/models/LocalStorageKeys.dart

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ abstract class LocalStorageKeys {
33
static String clientSecret = 'client_secret';
44
static String accessToken = 'access_token';
55
static String refreshToken = 'refresh_token';
6-
static String expiration = " expiration";
6+
static String expiration = "expiration";
7+
static String geniusAccessToken = "genius_access_token";
78
}

lib/provider/UserPreferences.dart

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import 'package:flutter/cupertino.dart';
2+
import 'package:shared_preferences/shared_preferences.dart';
3+
import 'package:spotube/models/LocalStorageKeys.dart';
4+
5+
class UserPreferences extends ChangeNotifier {
6+
String? _geniusAccessToken;
7+
UserPreferences({String? geniusAccessToken}) {
8+
if (geniusAccessToken == null) {
9+
SharedPreferences.getInstance().then((localStorage) {
10+
String? accessToken =
11+
localStorage.getString(LocalStorageKeys.geniusAccessToken);
12+
_geniusAccessToken ??= accessToken;
13+
});
14+
} else {
15+
_geniusAccessToken = geniusAccessToken;
16+
}
17+
}
18+
19+
get geniusAccessToken => _geniusAccessToken;
20+
21+
setGeniusAccessToken(String? token) {
22+
_geniusAccessToken = token;
23+
notifyListeners();
24+
}
25+
}

0 commit comments

Comments
 (0)