Skip to content

Commit

Permalink
Added support for RadioAPI (primitive version)
Browse files Browse the repository at this point in the history
 - This version hits the /broadcast endpoint and tries to parse the current_show.
   If there is none, then we assume it's Spud. This avoids the complicated decoding
  of a dual-typed field (current_show, which is either false or a struct).
 - Updates the UI to show the current DJ's name.
  • Loading branch information
joemcmahon committed Dec 24, 2023
1 parent 2ed0ee0 commit 58cbe57
Show file tree
Hide file tree
Showing 6 changed files with 162 additions and 4 deletions.
6 changes: 5 additions & 1 deletion RadioSpiral.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
5FDEE0221F72FF980064333C /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 5FDEE0211F72FF980064333C /* LaunchScreen.storyboard */; };
6258DCD822D93A3500166C65 /* LogoShareView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6258DCD722D93A3500166C65 /* LogoShareView.swift */; };
6258DCDA22D93A5400166C65 /* LogoShareView.xib in Resources */ = {isa = PBXBuildFile; fileRef = 6258DCD922D93A5400166C65 /* LogoShareView.xib */; };
710189282B366D3300E8A04C /* RadioAPI.swift in Sources */ = {isa = PBXBuildFile; fileRef = 710189272B366D3300E8A04C /* RadioAPI.swift */; };
715C87A62B23E141003FC57A /* SnapshotHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 715C87A52B23E141003FC57A /* SnapshotHelper.swift */; };
7165121C2B35519B00473770 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = CF72ACE621F714D000461EED /* Main.storyboard */; };
9409E11C1ABF6FEA00312E2B /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9409E11B1ABF6FEA00312E2B /* AppDelegate.swift */; };
Expand Down Expand Up @@ -106,6 +107,7 @@
5FDEE0211F72FF980064333C /* LaunchScreen.storyboard */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; path = LaunchScreen.storyboard; sourceTree = "<group>"; };
6258DCD722D93A3500166C65 /* LogoShareView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LogoShareView.swift; sourceTree = "<group>"; };
6258DCD922D93A5400166C65 /* LogoShareView.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = LogoShareView.xib; sourceTree = "<group>"; };
710189272B366D3300E8A04C /* RadioAPI.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RadioAPI.swift; sourceTree = "<group>"; };
715C87A52B23E141003FC57A /* SnapshotHelper.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = SnapshotHelper.swift; path = fastlane/SnapshotHelper.swift; sourceTree = SOURCE_ROOT; };
9409E1161ABF6FEA00312E2B /* RadioSpiral.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = RadioSpiral.app; sourceTree = BUILT_PRODUCTS_DIR; };
9409E11A1ABF6FEA00312E2B /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
Expand Down Expand Up @@ -287,6 +289,7 @@
children = (
94AC70AD1AD05C6200652982 /* RadioStation.swift */,
CE963ECB29135A6F004F299E /* StationsManager.swift */,
710189272B366D3300E8A04C /* RadioAPI.swift */,
);
path = Model;
sourceTree = "<group>";
Expand Down Expand Up @@ -398,7 +401,7 @@
BuildIndependentTargetsInParallel = YES;
LastSwiftMigration = 0700;
LastSwiftUpdateCheck = 0710;
LastUpgradeCheck = 1500;
LastUpgradeCheck = 1510;
ORGANIZATIONNAME = matthewfecher.com;
TargetAttributes = {
2C5545B91C1124DE00728469 = {
Expand Down Expand Up @@ -497,6 +500,7 @@
CE6036192A48BA2500E15E15 /* UITableViewCell+reuseIdentifier.swift in Sources */,
CE6036162A47B88600E15E15 /* StationTableViewCell.swift in Sources */,
CE9EE8DE293BB41300F62041 /* BaseController.swift in Sources */,
710189282B366D3300E8A04C /* RadioAPI.swift in Sources */,
53113F39230C720900462C0E /* ShareActivity.swift in Sources */,
9409E1401ABF78B000312E2B /* NowPlayingViewController.swift in Sources */,
CE963ECC29135A6F004F299E /* StationsManager.swift in Sources */,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1500"
LastUpgradeVersion = "1510"
version = "1.7">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1500"
LastUpgradeVersion = "1510"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1500"
LastUpgradeVersion = "1510"
version = "1.7">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
144 changes: 144 additions & 0 deletions RadioSpiral/Model/RadioAPI.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
//
// RadioAPI.swift
// RadioSpiral
//
// Created by Joe McMahon on 12/22/23.
// Copyright © 2023 matthewfecher.com. All rights reserved.
//

import Foundation

struct Show: Codable {
let id: Int
let title: String
let name: String
let slug: String
let url: String
let genres: [String]
let languages: [String]
let hosts: [Host]
let producers: [String] // You can replace this with the actual type if needed
let avatar_url: String
let avatar_id: String
let image_url: String
let image_id: String
}

struct Host: Codable {
let name: String
let url: String
}

struct ShowDetails: Codable {
let override: Int
let id: String
let name: String
let slug: String
let date: String
let day: String
let start: String
let end: String
let url: String
let split: Bool
let show: Show
}

struct Broadcast: Codable {
let current_show: ShowDetails
let next_show: ShowDetails
let current_playlist: Bool
let now_playing: NowPlaying
let instance: Int
}

struct NowPlaying: Codable {
let text: String
let title: String
let artist: String
}

struct Endpoints: Codable {
let station: String
let broadcast: String
let schedule: String
let shows: String
let genres: String
let languages: String
let episodes: String
let hosts: String
let producers: String
}

struct Root: Codable {
let broadcast: Broadcast
let timezone: String
let stream_url: String
let stream_format: String
let fallback_url: String
let fallback_format: String
let station_url: String
let schedule_url: String
let language: String
let timestamp: String
let date_time: String
let updated: String
let success: Bool
let endpoints: Endpoints
}


struct RadioAPI {

// Define the URL for the broadcast endpoint
private static let endpointURL = URL(string: "https://radiospiral.net/wp-json/radio/broadcast/")!
static var djName: String = ""
static var showName: String = ""

// Define a struct to represent the JSON response
struct BroadcastResponse: Codable {
let current_show: String
}

static var result: String = ""

// Define a function to fetch the current show from the API
static func getCurrentDJ(completion: @escaping (Result<String, Error>) -> Void) {

// Create a data task to make the HTTP request
let task = URLSession.shared.dataTask(with: endpointURL) { (data, response, error) in
// Check for errors
if let error = error {
completion(.failure(error))
return
}

// Check for a valid HTTP response
guard let httpResponse = response as? HTTPURLResponse, (200...299).contains(httpResponse.statusCode) else {
completion(.failure(NSError(domain: "Invalid HTTP response", code: 0, userInfo: nil)))
return
}

// Check for valid data
guard let data = data else {
completion(.failure(NSError(domain: "No data received", code: 0, userInfo: nil)))
return
}

result = String(decoding: data, as: UTF8.self)
do {
// Decode the JSON response
let decoder = JSONDecoder()
let broadcastResponse = try decoder.decode(BroadcastResponse.self, from: data)

// Extract and pass the current show value to the completion handler
completion(.success(broadcastResponse.current_show))
} catch {
// Handle decoding errors within the closure
completion(.failure(error))
}
}

// Start the data task
task.resume()
}
}
10 changes: 10 additions & 0 deletions RadioSpiral/ViewControllers/NowPlayingViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,16 @@ class NowPlayingViewController: UIViewController {
} else {
playingLive.text = ""
}
RadioAPI.getCurrentDJ { result in
DispatchQueue.main.async {
switch result {
case .success(let currentDJ):
self.djName.text = currentDJ
case .failure(_):
self.djName.text = "Spud the Ambient Robot"
}
}
}
shouldAnimateSongLabel(animate)
return
}
Expand Down

0 comments on commit 58cbe57

Please sign in to comment.