Minor fixes, so I can leave for another 2 months

This commit is contained in:
exttex 2021-04-06 09:57:38 +02:00
parent 676d5d45cc
commit 9f850a6f30
6 changed files with 100 additions and 9 deletions

View File

@ -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<bool> 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;
}
}
}
}

View File

@ -372,7 +372,7 @@ class _MainScreenState extends State<MainScreen> with SingleTickerProviderStateM
//Fix statusbar
SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle(
statusBarColor: Colors.transparent
statusBarColor: Colors.transparent,
));
},
selectedItemColor: Theme.of(context).primaryColor,

View File

@ -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<String> scopes;
DateTime expiration;
SpotifyCredentialsSave({this.accessToken, this.refreshToken, this.scopes, this.expiration});
//JSON
factory SpotifyCredentialsSave.fromJson(Map<String, dynamic> json) => _$SpotifyCredentialsSaveFromJson(json);
Map<String, dynamic> toJson() => _$SpotifyCredentialsSaveToJson(this);
}

View File

@ -78,7 +78,11 @@ Settings _$SettingsFromJson(Map<String, dynamic> 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<String, dynamic>);
}
Map<String, dynamic> _$SettingsToJson(Settings instance) => <String, dynamic>{
@ -123,6 +127,7 @@ Map<String, dynamic> _$SettingsToJson(Settings instance) => <String, dynamic>{
'lastFMPassword': instance.lastFMPassword,
'spotifyClientId': instance.spotifyClientId,
'spotifyClientSecret': instance.spotifyClientSecret,
'spotifyCredentials': instance.spotifyCredentials,
};
T _$enumDecode<T>(
@ -170,3 +175,24 @@ const _$ThemesEnumMap = {
Themes.Deezer: 'Deezer',
Themes.Black: 'Black',
};
SpotifyCredentialsSave _$SpotifyCredentialsSaveFromJson(
Map<String, dynamic> 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<String, dynamic> _$SpotifyCredentialsSaveToJson(
SpotifyCredentialsSave instance) =>
<String, dynamic>{
'accessToken': instance.accessToken,
'refreshToken': instance.refreshToken,
'scopes': instance.scopes,
'expiration': instance.expiration?.toIso8601String(),
};

View File

@ -326,6 +326,18 @@ class _SpotifyImporterV2State extends State<SpotifyImporterV2> {
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();
}

View File

@ -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"