Skip to content

Commit

Permalink
fix: various compiler errors
Browse files Browse the repository at this point in the history
  • Loading branch information
angristan committed Aug 2, 2021
1 parent ff4a54f commit 05a2f09
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 64 deletions.
106 changes: 53 additions & 53 deletions firstfm/Models/Spotify/API/SpotifyToken.swift
Expand Up @@ -11,79 +11,79 @@ struct SpotifyCredentialsResponse: Codable {
}
}

func renewSpotifyToken(completion: @escaping (String) -> Void) {
let authURL = "https://accounts.spotify.com/api/token"
// swiftlint:disable force_cast
let spotifyAPIToken = Bundle.main.object(forInfoDictionaryKey: "SPOTIFY_API_TOKEN") as! String
class SpotifyAPIService {

@AppStorage("spotify_tmp_token") var spotifyToken: String?
@AppStorage("spotify_expires_at") var spotifyExpiresAt: String?

if let queryURL = URL(string: authURL) {
var request = URLRequest(url: queryURL)
func renewSpotifyToken(completion: @escaping (String) -> Void) {
let authURL = "https://accounts.spotify.com/api/token"
// swiftlint:disable force_cast
let spotifyAPIToken = Bundle.main.object(forInfoDictionaryKey: "SPOTIFY_API_TOKEN") as! String

request.setValue("Basic \(spotifyAPIToken)",
forHTTPHeaderField: "Authorization")
if let queryURL = URL(string: authURL) {
var request = URLRequest(url: queryURL)

let data: Data = "grant_type=client_credentials".data(using: .utf8)!
request.setValue("Basic \(spotifyAPIToken)",
forHTTPHeaderField: "Authorization")

request.httpMethod = "POST"
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
request.httpBody = data
let data: Data = "grant_type=client_credentials".data(using: .utf8)!

URLSession.shared.dataTask(with: request, completionHandler: { data, response, error in
do {
if let response = response {
let nsHTTPResponse = response as? HTTPURLResponse
if let statusCode = nsHTTPResponse?.statusCode {
print("renewSpotifyToken status code = \(statusCode)")
request.httpMethod = "POST"
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
request.httpBody = data

URLSession.shared.dataTask(with: request, completionHandler: { data, response, error in
do {
if let response = response {
let nsHTTPResponse = response as? HTTPURLResponse
if let statusCode = nsHTTPResponse?.statusCode {
print("renewSpotifyToken status code = \(statusCode)")
}
// TODO
}
// TODO
}
if let error = error {
if let error = error {
print(error)
// TODO
completion("")
}
if let data = data {
do {
let jsonResponse = try JSONDecoder().decode(SpotifyCredentialsResponse.self, from: data)
self.spotifyToken = jsonResponse.accessToken
self.spotifyExpiresAt = String(Int64(Date().timeIntervalSince1970) + Int64(jsonResponse.expiresIn))
completion(jsonResponse.accessToken)
}
}
} catch {
print(error)
// TODO
completion("")
}
if let data = data {
do {
let jsonResponse = try JSONDecoder().decode(SpotifyCredentialsResponse.self, from: data)
spotifyToken = jsonResponse.accessToken
spotifyExpiresAt = String(Int64(Date().timeIntervalSince1970) + Int64(jsonResponse.expiresIn))
completion(jsonResponse.accessToken)
}
}
} catch {
print(error)
// TODO
}
}).resume()
}).resume()
}
}
}

func getSpotifyToken(completion: @escaping (String) -> Void) {
@AppStorage("spotify_tmp_token") var spotifyToken: String?
@AppStorage("spotify_expires_at") var spotifyExpiresAt: String?

let currentTimeSeconds = Int64(Date().timeIntervalSince1970)

if spotifyToken == nil || Int64(spotifyExpiresAt ?? "0") == 0 {
renewSpotifyToken { renewedToken in
completion(renewedToken)
}
func getSpotifyToken(completion: @escaping (String) -> Void) {
let currentTimeSeconds = Int64(Date().timeIntervalSince1970)

} else {
// We want the refresh token to be valid for at least 30s
if Int64(spotifyExpiresAt ?? "0") != 0 && currentTimeSeconds + 30 > Int64(spotifyExpiresAt ?? "0") ?? 0 {
if spotifyToken == nil || Int64(spotifyExpiresAt ?? "0") == 0 {
renewSpotifyToken { renewedToken in
completion(renewedToken)
}

} else {
// We want the refresh token to be valid for at least 30s
if Int64(spotifyExpiresAt ?? "0") != 0 && currentTimeSeconds + 30 > Int64(spotifyExpiresAt ?? "0") ?? 0 {
renewSpotifyToken { renewedToken in
completion(renewedToken)

}
}
}
}

if let token = spotifyToken {
completion(token)
if let token = spotifyToken {
completion(token)
}
completion("")
}
completion("")
}
2 changes: 1 addition & 1 deletion firstfm/Models/Spotify/SpotifyImage.swift
Expand Up @@ -34,7 +34,7 @@ struct SpotifyImage: Codable {

request.setValue("application/json", forHTTPHeaderField: "Content-Type")

getSpotifyToken { spotifyToken in
SpotifyAPIService().getSpotifyToken { spotifyToken in
request.setValue("Bearer \(spotifyToken)", forHTTPHeaderField: "Authorization")
URLSession.shared.dataTask(with: request, completionHandler: { data, response, error in
do {
Expand Down
6 changes: 3 additions & 3 deletions firstfm/ViewModel/ArtistViewModel.swift
Expand Up @@ -41,7 +41,7 @@ class ArtistViewModel: ObservableObject {
DispatchQueue.main.async {
FloatingNotificationBanner(title: "Failed to load albums", subtitle: error?.localizedDescription, style: .danger).show()
// Force refresh, otherwise pull loader doesn't dismiss itself
self.albums = self.albums
(self.albums = self.albums)
}
}

Expand Down Expand Up @@ -76,7 +76,7 @@ class ArtistViewModel: ObservableObject {
DispatchQueue.main.async {
FloatingNotificationBanner(title: "Failed to load tracks", subtitle: error?.localizedDescription, style: .danger).show()
// Force refresh, otherwise pull loader doesn't dismiss itself
self.tracks = self.tracks
(self.tracks = self.tracks)
}
}

Expand Down Expand Up @@ -110,7 +110,7 @@ class ArtistViewModel: ObservableObject {
DispatchQueue.main.async {
FloatingNotificationBanner(title: "Failed to artist info", subtitle: error?.localizedDescription, style: .danger).show()
// Force refresh, otherwise pull loader doesn't dismiss itself
self.tracks = self.tracks
(self.tracks = self.tracks)
}
}

Expand Down
4 changes: 2 additions & 2 deletions firstfm/ViewModel/ChartViewModel.swift
Expand Up @@ -19,7 +19,7 @@ class ChartViewModel: ObservableObject {
DispatchQueue.main.async {
FloatingNotificationBanner(title: "Failed to load charts", subtitle: error?.localizedDescription, style: .danger).show()
// Force refresh, otherwise pull loader doesn't dismiss itself
self.artists = self.artists
(self.artists = self.artists)
self.isLoading = false
}
}
Expand Down Expand Up @@ -59,7 +59,7 @@ class ChartViewModel: ObservableObject {
DispatchQueue.main.async {
FloatingNotificationBanner(title: "Failed to load charts", subtitle: error?.localizedDescription, style: .danger).show()
// Force refresh, otherwise pull loader doesn't dismiss itself
self.tracks = self.tracks
(self.tracks = self.tracks)
self.isLoading = false
}
}
Expand Down
8 changes: 4 additions & 4 deletions firstfm/ViewModel/ProfileViewModel.swift
Expand Up @@ -89,7 +89,7 @@ class ProfileViewModel: ObservableObject {
DispatchQueue.main.async {
FloatingNotificationBanner(title: "Failed to load scrobbles", subtitle: error?.localizedDescription, style: .danger).show()
// Force refresh, otherwise pull loader doesn't dismiss itself
self.scrobbles = self.scrobbles
(self.scrobbles = self.scrobbles)
}
}

Expand Down Expand Up @@ -156,7 +156,7 @@ class ProfileViewModel: ObservableObject {
DispatchQueue.main.async {
FloatingNotificationBanner(title: "Failed to load artists", subtitle: error?.localizedDescription, style: .danger).show()
// Force refresh, otherwise pull loader doesn't dismiss itself
self.topArtists = self.topArtists
(self.topArtists = self.topArtists)
}
}

Expand Down Expand Up @@ -212,7 +212,7 @@ class ProfileViewModel: ObservableObject {
DispatchQueue.main.async {
FloatingNotificationBanner(title: "Failed to load tracks", subtitle: error?.localizedDescription, style: .danger).show()
// Force refresh, otherwise pull loader doesn't dismiss itself
self.topTracks = self.topTracks
(self.topTracks = self.topTracks)
}
}

Expand Down Expand Up @@ -268,7 +268,7 @@ class ProfileViewModel: ObservableObject {
DispatchQueue.main.async {
FloatingNotificationBanner(title: "Failed to load albums", subtitle: error?.localizedDescription, style: .danger).show()
// Force refresh, otherwise pull loader doesn't dismiss itself
self.topAlbums = self.topAlbums
(self.topAlbums = self.topAlbums)
}
}

Expand Down
2 changes: 1 addition & 1 deletion firstfm/ViewModel/ScrobblesViewModel.swift
Expand Up @@ -25,7 +25,7 @@ class ScrobblesViewModel: ObservableObject {
DispatchQueue.main.async {
FloatingNotificationBanner(title: "Failed to load scrobbles", subtitle: error?.localizedDescription, style: .danger).show()
// Force refresh, otherwise pull loader doesn't dismiss itself
self.scrobbles = self.scrobbles
(self.scrobbles = self.scrobbles)
}
}

Expand Down

0 comments on commit 05a2f09

Please sign in to comment.