Last active
April 18, 2019 12:07
-
-
Save halgatewood/d8bfffe18cf5a42f385c to your computer and use it in GitHub Desktop.
Completely Dynamic Apple TV Top Shelf using a Remote JSON file, Alamofire, Semaphore and SwiftyJSON
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ServiceProvider.swift | |
// BT Top Shelf | |
// | |
// Created by HalBook on 9/28/15. | |
// Copyright © 2015 HalGatewood.com. All rights reserved. | |
// | |
import Foundation | |
import TVServices | |
import Alamofire | |
class ServiceProvider: NSObject, TVTopShelfProvider { | |
override init() { | |
super.init() | |
} | |
var pingURL = "https://bibletalk.tv/appletv-topshelf.json" | |
var items: [TVContentItem] = [] | |
// MARK: - TVTopShelfProvider protocol | |
var topShelfStyle: TVTopShelfContentStyle { | |
// Return desired Top Shelf style. | |
return .Sectioned | |
} | |
var topShelfItems: [TVContentItem] { | |
// START SEMAPHORE | |
let semaphore = dispatch_semaphore_create(0) | |
// REQUEST REMOTE JSON | |
Alamofire.request( .GET, self.pingURL ) | |
.responseJSON { response in | |
var ContentItems = [TVContentItem](); | |
let json = JSON(response.result.value!) | |
let sections = json["sections"].arrayValue | |
// LOOP ARRAY OF SECTIONS | |
for section in sections | |
{ | |
let items = section["items"].arrayValue | |
ContentItems = [TVContentItem](); | |
// LOOP ITEMS | |
for item in items | |
{ | |
// PASS IN THE IMAGE SIZE | |
var image_shape : TVContentItemImageShape = .Square | |
switch item["image_size"].string! | |
{ | |
case "HDTV": image_shape = .HDTV | |
case "ExtraWide": image_shape = .ExtraWide | |
case "Wide": image_shape = .Wide | |
case "SDTV": image_shape = .SDTV | |
case "Poster": image_shape = .SDTV | |
case "None": image_shape = .None | |
default : image_shape = .Square | |
} | |
// APPEND THE LATEST ITEM | |
ContentItems.append( self.buildShelfItem( item["slug"].string!, Title: item["title"].string!, Image: item["image"].string!, ImageSize: image_shape ) ) | |
} | |
// SET THE SECTION DETAILS | |
let sectionItem = TVContentItem(contentIdentifier: TVContentIdentifier(identifier: section["contentIdentifier"].string!, container: nil)!) | |
sectionItem!.title = section["title"].string! | |
sectionItem!.topShelfItems = ContentItems | |
// SET THE ITEM, IN THIS CASE THE WHOLE SECTION | |
self.items.append(sectionItem!) | |
} | |
// TELL THE SEMAPHORE WE'RE DONE | |
dispatch_semaphore_signal(semaphore) | |
} | |
// WAIT FOR THE SIGNAL | |
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER) | |
// RETURN | |
return self.items | |
} | |
func buildShelfItem( Slug: String, Title: String = "", Image: String, ImageSize: TVContentItemImageShape = .Square) -> TVContentItem | |
{ | |
// BUILDS THE ITEM, YOU WILL NEED TO MODIFY THE displayURL, etc. | |
let item = TVContentItem(contentIdentifier: TVContentIdentifier(identifier: Slug, container: nil)!) | |
item!.imageURL = NSURL(string:Image) | |
item!.imageShape = ImageSize | |
item!.displayURL = NSURL(string: "bibletalktv:///" + Slug); | |
item!.title = Title | |
return item! | |
} | |
} |
Thank you for sharing.
I've used this and it works fine. However, I noticed that the top shelf does not self update when my endpoint updates.
After long research and tests, I've found the issue. At line 21 var items: [TVContentItem] = []
if I move this inside the AlamoFire callback and configure AlamoFire to not cache responses then the self update starts working.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this to your podfile. I hope this will helps you.
target 'MyAppExtensionName'
do
pod 'Alamofire', '~> 4.0'
end