Skip to content
Permalink
ff963a53f9
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
193 lines (138 sloc) 6.98 KB
//
// AddStationsViewController.swift
// ZRadiov2
//
// Created by Kenny Payes on 3/31/20.
// Copyright © 2020 Kenny Payes. All rights reserved.
//
import UIKit
class AddStationsViewController: UIViewController {
@IBOutlet weak var newStationName: UITextField!
@IBOutlet weak var newStationLink: UITextField!
@IBOutlet weak var removeStationName: UITextField!
@IBOutlet weak var changeStation: UITextField!
let s = StationStorage()
var stations = [String:String]()
var stationsList = [[String]]()
@IBOutlet weak var addTextField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
stations = s.stationList
let dictKeys = [String](stations.keys)
let dictvalues = [String](stations.values)
for i in 0...(dictKeys.count - 1){
stationsList.append([dictKeys[i], dictvalues[i]])
}
//print("\n addStations: \(stationsList) \n")
// Do any additional setup after loading the view.
}
@IBAction func addStationsButton(_ sender: Any) {
if newStationName.text?.trimmingCharacters(in: .whitespaces) != "" || newStationLink.text?.trimmingCharacters(in: .whitespaces) != "" {
var testContent:String?
testContent = "[ \n"
for i in 0...(stationsList.count - 1){
let stationElement:String? = " { \n \"name\" : \"\(stationsList[i][0])\", \n \"url\" : \"\(stationsList[i][1])\" \n "
testContent = testContent! + stationElement! + "}, \n"
}
testContent = testContent! + "{ \n \"name\" : \"\(String(newStationName.text!))\", \n \"url\" : \"\(String(newStationLink.text!))\" \n } \n "
testContent = testContent! + "]"
let url = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!.appendingPathComponent("stations.json")
do{
try testContent!.write(to: url, atomically: true, encoding: String.Encoding.utf8)
print("File Created!")
} catch { print("error writing to file...") }
print(url)
let fileName = "stations.json"
var filePath = ""
let dirs = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.allDomainsMask, true)
if dirs.count > 0{
let dir = dirs[0]
filePath = dir.appending("/" + fileName)
print("local path: = \(filePath)" )
}else{
print("Could not find filePath")
}
let fileman = FileManager.default.fileExists(atPath: filePath)
print(fileman)
newStationLink.text = ""
newStationName.text = ""
}
s.reloadData()
stations = s.stationList
stationsList.removeAll()
let dictKeys = [String](stations.keys)
let dictvalues = [String](stations.values)
for i in 0...(dictKeys.count - 1){
stationsList.append([dictKeys[i], dictvalues[i]])
}
}
@IBAction func removePressed(_ sender: Any) {
for i in 0...(stationsList.count - 1){
if stationsList[i][0] == removeStationName.text {
print("Before removing \(stationsList)")
stationsList.remove(at: i)
print("After removing \(stationsList)")
var testContent:String?
testContent = "[ \n"
var count = 0
for i in 0...(stationsList.count - 1){
if count != 0{ testContent = testContent! + ", \n" } else {testContent = testContent! + "\n" }
count += 1
let stationElement:String? = " { \n \"name\" : \"\(stationsList[i][0])\", \n \"url\" : \"\(stationsList[i][1])\" \n "
testContent = testContent! + stationElement! + "} \n"
}
testContent = testContent! + "]"
print(testContent!)
let url = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!.appendingPathComponent("stations.json")
do{
try testContent!.write(to: url, atomically: true, encoding: String.Encoding.utf8)
print("File rewritten!")
} catch { print("error writing to file...") }
/*
print(url)
let fileName = "stations.json"
var filePath = ""
let dirs = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.allDomainsMask, true)
if dirs.count > 0{
let dir = dirs[0]
filePath = dir.appending("/" + fileName)
print("local path: = \(filePath)" )
}else{
print("Could not find filePath")
}
let fileman = FileManager.default.fileExists(atPath: filePath)
print(fileman)
*/
removeStationName.text = ""
break
}
}
s.reloadData()
stations = s.stationList
stationsList.removeAll()
let dictKeys = [String](stations.keys)
let dictvalues = [String](stations.values)
for i in 0...(dictKeys.count - 1){
stationsList.append([dictKeys[i], dictvalues[i]])
}
}
@IBAction func changePressed(_ sender: Any) {
for i in 0...(stationsList.count - 1){
if stationsList[i][0] == changeStation.text {
let testContent = "[{\"name\" : \"\(stationsList[i][0])\", \n \"url\" : \"\(stationsList[i][1])\"} ]"
let url = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!.appendingPathComponent("defaultStation.json")
do{
try testContent.write(to: url, atomically: true, encoding: String.Encoding.utf8)
print("File rewritten!")
} catch { print("error writing to file...") }
break
}
}
changeStation.text = ""
}
@IBAction func linkPressed(_ sender: Any) {
if let url = URL(string: "http://www.radiosure.com/stations/") {
UIApplication.shared.open(url)
}
}
}