From 9f850a6f301c29a5b33e1d331506fc3744b15bc9 Mon Sep 17 00:00:00 2001 From: exttex Date: Tue, 6 Apr 2021 09:57:38 +0200 Subject: [PATCH] Minor fixes, so I can leave for another 2 months --- lib/api/spotify.dart | 39 ++++++++++++++++++++++++++++++++++++- lib/main.dart | 2 +- lib/settings.dart | 26 ++++++++++++++++++++----- lib/settings.g.dart | 28 +++++++++++++++++++++++++- lib/ui/importer_screen.dart | 12 ++++++++++++ pubspec.yaml | 2 +- 6 files changed, 100 insertions(+), 9 deletions(-) diff --git a/lib/api/spotify.dart b/lib/api/spotify.dart index 8b4979d..d0cfed1 100644 --- a/lib/api/spotify.dart +++ b/lib/api/spotify.dart @@ -1,5 +1,6 @@ import 'package:freezer/api/deezer.dart'; import 'package:freezer/api/importer.dart'; +import 'package:freezer/settings.dart'; import 'package:html/parser.dart'; import 'package:html/dom.dart' as dom; import 'package:http/http.dart' as http; @@ -141,6 +142,24 @@ class SpotifyAPIWrapper { SpotifyApi spotify; User me; + //Try authorize with saved credentials + Future trySaved() async { + print(settings.spotifyCredentials); + if (settings.spotifyClientId == null || settings.spotifyClientSecret == null || settings.spotifyCredentials == null) return false; + final credentials = SpotifyApiCredentials( + settings.spotifyClientId, + settings.spotifyClientSecret, + accessToken: settings.spotifyCredentials.accessToken, + refreshToken: settings.spotifyCredentials.refreshToken, + scopes: settings.spotifyCredentials.scopes, + expiration: settings.spotifyCredentials.expiration + ); + spotify = SpotifyApi(credentials); + me = await spotify.me.get(); + await _save(); + return true; + } + Future authorize(String clientId, String clientSecret) async { //Spotify SpotifyApiCredentials credentials = SpotifyApiCredentials(clientId, clientSecret); @@ -171,6 +190,24 @@ class SpotifyAPIWrapper { //Create spotify spotify = SpotifyApi.fromAuthCodeGrant(grant, responseUri); me = await spotify.me.get(); + + //Save + await _save(); + } + + Future _save() async { + //Save credentials + final spotifyCredentials = await spotify.getCredentials(); + final saveCredentials = SpotifyCredentialsSave( + accessToken: spotifyCredentials.accessToken, + refreshToken: spotifyCredentials.refreshToken, + scopes: spotifyCredentials.scopes, + expiration: spotifyCredentials.expiration + ); + settings.spotifyClientSecret = spotifyCredentials.clientId; + settings.spotifyClientSecret = spotifyCredentials.clientSecret; + settings.spotifyCredentials = saveCredentials; + await settings.save(); } //Cancel authorization @@ -180,4 +217,4 @@ class SpotifyAPIWrapper { _server = null; } } -} \ No newline at end of file +} diff --git a/lib/main.dart b/lib/main.dart index a09e83e..4082d4b 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -372,7 +372,7 @@ class _MainScreenState extends State with SingleTickerProviderStateM //Fix statusbar SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle( - statusBarColor: Colors.transparent + statusBarColor: Colors.transparent, )); }, selectedItemColor: Theme.of(context).primaryColor, diff --git a/lib/settings.dart b/lib/settings.dart index c37d380..c41a1d7 100644 --- a/lib/settings.dart +++ b/lib/settings.dart @@ -132,6 +132,8 @@ class Settings { String spotifyClientId; @JsonKey(defaultValue: null) String spotifyClientSecret; + @JsonKey(defaultValue: null) + SpotifyCredentialsSave spotifyCredentials; Settings({this.downloadPath, this.arl}); @@ -238,18 +240,18 @@ class Settings { static const deezerBottom = Color(0xFF1b1714); TextTheme get _textTheme => (font == 'Deezer') ? null - : GoogleFonts.getTextTheme(font, this.isDark ? ThemeData.dark().textTheme : ThemeData.light().textTheme); + : GoogleFonts.getTextTheme(font, isDark ? ThemeData.dark().textTheme : ThemeData.light().textTheme); String get _fontFamily => (font == 'Deezer') ? 'MabryPro' : null; //Overrides for the non-deprecated buttons to look like the old ones - static final outlinedButtonTheme = OutlinedButtonThemeData( + OutlinedButtonThemeData get outlinedButtonTheme => OutlinedButtonThemeData( style: ButtonStyle( - foregroundColor: MaterialStateProperty.all(Colors.white), + foregroundColor: MaterialStateProperty.all(isDark ? Colors.white : Colors.black), ) ); - static final textButtonTheme = TextButtonThemeData( + TextButtonThemeData get textButtonTheme => TextButtonThemeData( style: ButtonStyle( - foregroundColor: MaterialStateProperty.all(Colors.white), + foregroundColor: MaterialStateProperty.all(isDark ? Colors.white : Colors.black), ) ); @@ -335,4 +337,18 @@ enum Themes { Dark, Deezer, Black +} + +@JsonSerializable() +class SpotifyCredentialsSave { + String accessToken; + String refreshToken; + List scopes; + DateTime expiration; + + SpotifyCredentialsSave({this.accessToken, this.refreshToken, this.scopes, this.expiration}); + + //JSON + factory SpotifyCredentialsSave.fromJson(Map json) => _$SpotifyCredentialsSaveFromJson(json); + Map toJson() => _$SpotifyCredentialsSaveToJson(this); } \ No newline at end of file diff --git a/lib/settings.g.dart b/lib/settings.g.dart index 13d1d4b..275cd86 100644 --- a/lib/settings.g.dart +++ b/lib/settings.g.dart @@ -78,7 +78,11 @@ Settings _$SettingsFromJson(Map json) { ..lastFMUsername = json['lastFMUsername'] as String ..lastFMPassword = json['lastFMPassword'] as String ..spotifyClientId = json['spotifyClientId'] as String - ..spotifyClientSecret = json['spotifyClientSecret'] as String; + ..spotifyClientSecret = json['spotifyClientSecret'] as String + ..spotifyCredentials = json['spotifyCredentials'] == null + ? null + : SpotifyCredentialsSave.fromJson( + json['spotifyCredentials'] as Map); } Map _$SettingsToJson(Settings instance) => { @@ -123,6 +127,7 @@ Map _$SettingsToJson(Settings instance) => { 'lastFMPassword': instance.lastFMPassword, 'spotifyClientId': instance.spotifyClientId, 'spotifyClientSecret': instance.spotifyClientSecret, + 'spotifyCredentials': instance.spotifyCredentials, }; T _$enumDecode( @@ -170,3 +175,24 @@ const _$ThemesEnumMap = { Themes.Deezer: 'Deezer', Themes.Black: 'Black', }; + +SpotifyCredentialsSave _$SpotifyCredentialsSaveFromJson( + Map json) { + return SpotifyCredentialsSave( + accessToken: json['accessToken'] as String, + refreshToken: json['refreshToken'] as String, + scopes: (json['scopes'] as List)?.map((e) => e as String)?.toList(), + expiration: json['expiration'] == null + ? null + : DateTime.parse(json['expiration'] as String), + ); +} + +Map _$SpotifyCredentialsSaveToJson( + SpotifyCredentialsSave instance) => + { + 'accessToken': instance.accessToken, + 'refreshToken': instance.refreshToken, + 'scopes': instance.scopes, + 'expiration': instance.expiration?.toIso8601String(), + }; diff --git a/lib/ui/importer_screen.dart b/lib/ui/importer_screen.dart index 5b8077b..cc2c4a8 100644 --- a/lib/ui/importer_screen.dart +++ b/lib/ui/importer_screen.dart @@ -326,6 +326,18 @@ class _SpotifyImporterV2State extends State { void initState() { _clientId = settings.spotifyClientId; _clientSecret = settings.spotifyClientSecret; + + //Try saved + spotify = SpotifyAPIWrapper(); + spotify.trySaved().then((r) { + if (r) { + Navigator.of(context).pushReplacement(MaterialPageRoute( + builder: (context) => SpotifyImporterV2Main(spotify) + )); + } + }); + + super.initState(); } diff --git a/pubspec.yaml b/pubspec.yaml index 799ab3a..9f81226 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -15,7 +15,7 @@ publish_to: 'none' # Remove this line if you wish to publish to pub.dev # In iOS, build-name is used as CFBundleShortVersionString while build-number used as CFBundleVersion. # Read more about iOS versioning at # https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html -version: 0.6.10+1 +version: 0.6.11+1 environment: sdk: ">=2.8.0 <3.0.0"