Skip to content
This repository has been archived by the owner on Jun 23, 2024. It is now read-only.

Commit

Permalink
Refresh token if expires in the next 5 minutes
Browse files Browse the repository at this point in the history
Show artist name in artist page
  • Loading branch information
AlvaroRojas committed Jun 8, 2023
1 parent 78cd6ed commit bf6e073
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 10 deletions.
4 changes: 3 additions & 1 deletion lib/APIs/spotify_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ class SpotifyApi {
String? refreshToken,
}) async {
Map<String, String>? body;
Map<String, String>? headers;
if (code != null) {
body = {
'grant_type': 'authorization_code',
Expand All @@ -93,6 +94,7 @@ class SpotifyApi {
'refresh_token': refreshToken,
'client_id': clientID
};
headers = {'Content-Type': 'application/x-www-form-urlencoded'};
}

if (body == null) {
Expand All @@ -101,7 +103,7 @@ class SpotifyApi {

try {
final Uri path = Uri.parse(requestToken);
final response = await post(path, body: body);
final response = await post(path, body: body, headers: headers);

if (response.statusCode == 200) {
final Map result = jsonDecode(response.body) as Map;
Expand Down
9 changes: 5 additions & 4 deletions lib/CustomWidgets/song_tile_trailing_menu.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,10 @@ import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:share_plus/share_plus.dart';
import 'package:soundal/APIs/spotify_api.dart';
import 'package:soundal/CustomWidgets/add_playlist.dart';
import 'package:soundal/Helpers/add_mediaitem_to_queue.dart';
import 'package:soundal/Helpers/mediaitem_converter.dart';
import 'package:soundal/Helpers/spotify_helper.dart';
import 'package:soundal/Screens/Common/song_list.dart';
import 'package:soundal/Screens/Search/albums.dart';
import 'package:soundal/Screens/Search/artists.dart';
import 'package:soundal/Screens/Search/search.dart';
import 'package:soundal/Services/youtube_services.dart';
Expand Down Expand Up @@ -224,7 +221,11 @@ class _SongTileTrailingMenuState extends State<SongTileTrailingMenu> {
PageRouteBuilder(
opaque: false,
pageBuilder: (_, __, ___) => ArtistSearchPage(
data: {'id': value.toString()},
data: {
'id': value.toString(),
'title': (widget.data['artist'].split(', ') as List)[
widget.data['spotify_artist_id'].indexOf(value) as int]
},
),
),
);
Expand Down
2 changes: 1 addition & 1 deletion lib/Helpers/spotify_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ Future<String?> retriveAccessToken() async {
return null;
} else {
final currentTime = DateTime.now().millisecondsSinceEpoch / 1000;
if ((currentTime + 60) >= expiredAt) {
if ((currentTime + 60 * 5) >= expiredAt) {
final List<String> data =
await SpotifyApi().getAccessToken(refreshToken: refreshToken);
if (data.isNotEmpty) {
Expand Down
6 changes: 3 additions & 3 deletions lib/Screens/Home/spotify.dart
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ class _SpotifyHomePageState extends State<SpotifyHomePage>
'imagesList': [element],
'source': 'spotify',
};
Hive.box('settings').put('playlistNames', playlistNames);
Hive.box('settings').put('playlistDetails', playlistDetails);
await Hive.box('settings').put('playlistNames', playlistNames);
await Hive.box('settings').put('playlistDetails', playlistDetails);
} else {
if (playlistDetails[element['title']]['imagesList'][0]
['snapshot_id'] !=
Expand All @@ -127,7 +127,7 @@ class _SpotifyHomePageState extends State<SpotifyHomePage>
'imagesList': [element],
'source': 'spotify',
};
Hive.box('settings').put('playlistDetails', playlistDetails);
await Hive.box('settings').put('playlistDetails', playlistDetails);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Screens/Search/albums.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:intl/intl.dart';
import 'package:soundal/APIs/api.dart';
import 'package:soundal/APIs/spotify_api.dart';
import 'package:soundal/CustomWidgets/bouncy_sliver_scroll_view.dart';
import 'package:soundal/CustomWidgets/copy_clipboard.dart';
Expand Down Expand Up @@ -154,6 +153,7 @@ class _AlbumSearchPageState extends State<AlbumSearchPage> {
temp.addAll(List<Map>.from(value['artists'] as List)),
for (final element in temp)
{
element['title'] = element['name'],
if ((element['images'] as List).isNotEmpty)
{
element['image'] = element['images'].first['url'],
Expand Down

0 comments on commit bf6e073

Please sign in to comment.