Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recently played songs (Working) #96

Open
mannyd209 opened this issue Apr 4, 2018 · 9 comments
Open

Recently played songs (Working) #96

mannyd209 opened this issue Apr 4, 2018 · 9 comments

Comments

@mannyd209
Copy link

mannyd209 commented Apr 4, 2018

So I added a recently played icon button on the now playing page, I linked it to a tableview and got it working. I inputed the JSON recently played link in the station long description. Here is my code for the recently played tableviewcontroller:

import UIKit

//----------
//MARK: JSON
//----------

//The Initial Response From The JSON
struct Response: Codable {
    
    var playHistory: Album
    
}

//The Album Received Which Is An Array Of Song Data
struct Album: Codable {
    var song: [SongData]
    
}

//The SongData From The PlayHistory Album
struct SongData: Codable{
    
    var album: String
    var artist: String
    var cover: String
    var duration: String
    var programStartTS: String
    var title: String
}


class TableViewController: UITableViewController {
    
    //1. Create An Array To Store The SongData
    var songs = [SongData]()
    var currentStation: RadioStation!
    var downloadTask: URLSessionDownloadTask?
    
    override func viewDidLoad() { super.viewDidLoad()
        
        
        self.tableView.delegate = self
        self.tableView.dataSource = self
        
        //2. Load The JSON From The Main Bundle
        guard let urlText = URL (string: currentStation.longDesc)
            else { return }
        
        do{
            //a. Get The Data From The From The File
            let data = try Data(contentsOf: urlText)
            
            //b. Decode The Data To Our Structs
            let albumData = try JSONDecoder().decode(Response.self, from: data)
            
            //c. Append The Songs Array With The PlayHistory
            albumData.playHistory.song.forEach { songs.append($0) }
            
            //d. Test Some Data
            print("""
                **The First Album Details**
                Album = \(songs[0].album)
                Artist = \(songs[0].artist)
                Cover = \(songs[0].cover)
                Duration = \(songs[0].duration)
                Start = \(songs[0].programStartTS)
                Title = \(songs[0].title)
                """)
            
            //3. Load The Data
            DispatchQueue.main.async {
                self.tableView.reloadData()
            }
        }catch{
            
            print(error)
        }
        
    }
    
    //-----------------
    //MARK: UITableView
    //-----------------
    
    
    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return songs.count
    }
    
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
       
        
        //1. Create A Cell
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TableViewCell
        
        //2. Set It's Text
        cell.songTitle.text = songs[indexPath.row].title
        cell.artistLabel.text = songs[indexPath.row].artist
        
        //3. Get The Image
        //Start with placeholder image so it shows until image download completes
        cell.songCover.image = UIImage(named: "WhiteLogo.png")
        
        //Continue with your logic, no change really but could be shortened to:
        if let imageURL = URL(string: songs[indexPath.row].cover) {
            let request = URLSession.shared.dataTask(with: imageURL) { (imageData, response, error) in
                guard let imageData = imageData
                    else {
                        return
                }
                
                DispatchQueue.main.async {
                    cell.songCover.image = UIImage(data: imageData)
                }
            }
            
            request.resume()
        }
        return cell
        
    }
    
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        print("""
            **Album \(indexPath.row) Selected**
            Album = \(songs[indexPath.row].album)
            Artist = \(songs[indexPath.row].artist)
            Cover = \(songs[indexPath.row].cover)
            Duration = \(songs[indexPath.row].duration)
            Start = \(songs[indexPath.row].programStartTS)
            Title = \(songs[indexPath.row].title)
            """)
    }
    
}
@mannyd209
Copy link
Author

mannyd209 commented Apr 4, 2018

This is what my Recently Played tableview looks like now.

img_1283

@mannyd209
Copy link
Author

I got this working. I pushed the Current Station to my Recently Played View Controller. Then whatever station I am currently on it pulls its LongDesc link and uses that JSON link to parse my recently played tracks as shown above.

@mannyd209 mannyd209 changed the title Recently played songs. Recently played songs (Working) Apr 21, 2018
@urayoanm
Copy link

Thanks for share mannyd209!

@rezaataiy
Copy link

rezaataiy commented Sep 21, 2018

Can you please share the completed project with "recently played songs" tableviewcontroller?

@mannyd209
Copy link
Author

@rezaataiy are you still wanting to see to completed project code?

@mannyd209
Copy link
Author

@urayoanm No worries bro.

@mannyd209
Copy link
Author

@rezaataiy I put the completed code up above and this is how the data is seen in the app.

{"playHistory":
{"song":
[
{
"title":"LET'S GO",
"artist":"CALVIN HARRIS",
"album":"",
"cover":"https://cdnrf.securenetsystems.net/file_radio/album_art/H/1/5/51HudB3bXoL.jpg",
"duration":"219",
"programStartTS":"30 Jan 2020 15:50:47"
}
]
}
}

@ValentinOgier
Copy link

Hi there, I'd like to know if you can share the tutorial to add it ?

Thank you !

@bark1n9
Copy link

bark1n9 commented Jul 6, 2020

Hi,

Could you please share the completed project. I'm trying to implement this but I get nil crash.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants