Skip to content
Permalink
63086ad466
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
137 lines (104 sloc) 3.99 KB
//
// FirstViewController.swift
// ZRadiov2
//
// Created by Kenny Payes on 3/30/20.
// Copyright © 2020 Kenny Payes. All rights reserved.
//
import UIKit
import Foundation
import AVFoundation
class RadioViewController: UIViewController,UITableViewDataSource, UITableViewDelegate {
let tabBar = MainTabBarController()
let s = StationStorage()
var stations = [String:String]()
var stationList = [[String]]()
@IBOutlet weak var playImage: UIImageView!
@IBOutlet weak var customLabel: UILabel!
@IBOutlet weak var currentlyPlaying: UILabel!
@IBOutlet weak var tableView: UITableView!
var radioOn: Bool?
var radio: AVPlayer?
let refreshControl = UIRefreshControl()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
stationList = createStationsList()
refreshControl.addTarget(self, action: #selector(RadioViewController.handleRefresh), for: .valueChanged)
tableView.refreshControl = refreshControl
tableView.delegate = self
tableView.dataSource = self
radio = AVPlayer(url:self.tabBar.url!)
radioOn = false
playImage.image = UIImage(systemName: "play.fill")
customLabel.text = String(self.tabBar.currentLabel!)
currentlyPlaying.text = String(self.tabBar.currentURL!)
}
@objc func handleRefresh(){
stationList.removeAll()
stationList = createStationsList()
Timer.scheduledTimer(withTimeInterval: 3.0, repeats: false) { (timer) in
self.tableView.reloadData()
self.refreshControl.endRefreshing()
}
}
@IBAction func buttonPressed(_ sender: Any) {
if radioOn!{
radioOn = false
radio!.pause()
playImage.image = UIImage(systemName: "play.fill")
}
else{
radioOn = true
radio!.play()
playImage.image = UIImage(systemName: "pause.fill")
}
}
func createStationsList() -> [[String]] {
s.reloadData()
stations = s.stationList
let dictKeys = [String](stations.keys)
let dictvalues = [String](stations.values)
var stationsList = [[String]]()
for i in 0...(dictKeys.count - 1){
stationsList.append([dictKeys[i], dictvalues[i]])
}
return stationsList
}
func changeStation(url: String) -> Void{
radio?.pause()
radioOn = false
radio?.replaceCurrentItem(with: AVPlayerItem(url: URL(string: url)!))
radio?.play()
radioOn = true
print("\(url)) \n")
}
func changeLabel(label: String) -> Void {
customLabel?.text = label
}
func changeUrl(url: String) -> Void {
currentlyPlaying?.text = url
}
@IBAction func change(_ sender: Any) {
changeStation(url: (currentlyPlaying?.text)!)
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return stationList.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cellLabel") as! SavedStationsCell
cell.setLabel(label: stationList[indexPath.row][0])
cell.setUrlPassed(url: stationList[indexPath.row][1])
cell.cellButton.tag = indexPath.row
cell.cellButton.addTarget(self, action:#selector(self.buttonTapped) , for: .touchUpInside)
return cell
}
@objc func buttonTapped(sender:UIButton){
let index: Int = sender.tag
print(index)
changeStation(url: stationList[sender.tag][1])
changeUrl(url: stationList[sender.tag][1])
changeLabel(label: stationList[sender.tag][0])
playImage.image = UIImage(systemName: "pause.fill")
}
}