Skip to content

Commit

Permalink
Live events migrated to new model
Browse files Browse the repository at this point in the history
  • Loading branch information
insidegui committed Oct 4, 2015
1 parent 43a78fb commit ba5ee61
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 17 deletions.
4 changes: 2 additions & 2 deletions WWDC/LiveEventBannerViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class LiveEventBannerViewController: NSViewController {
}
}

var event: LiveEvent? {
var event: LiveSession? {
didSet {
updateUI()
}
Expand Down Expand Up @@ -69,7 +69,7 @@ class LiveEventBannerViewController: NSViewController {
} else {
titleLabel.stringValue = "\(event.title)"
}
descriptionLabel.stringValue = event.description
descriptionLabel.stringValue = event.summary
} else {
view.hidden = true
NSNotificationCenter.defaultCenter().postNotificationName(LiveEventBannerVisibilityChangedNotification, object: nil)
Expand Down
79 changes: 69 additions & 10 deletions WWDC/LiveEventObserver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ private let _sharedInstance = LiveEventObserver()

class LiveEventObserver: NSObject, NSUserNotificationCenterDelegate {

var nextEvent: LiveEvent? {
var nextEvent: LiveSession? {
didSet {
NSNotificationCenter.defaultCenter().postNotificationName(LiveEventNextInfoChangedNotification, object: nil)
}
}
private var lastEventFound: LiveEvent?
private var lastEventFound: LiveSession?
private var timer: NSTimer?
private var liveEventPlayerController: VideoWindowController?

Expand All @@ -36,10 +36,10 @@ class LiveEventObserver: NSObject, NSUserNotificationCenterDelegate {
}

func checkNow() {
DataStore.SharedStore.checkForLiveEvent { available, event in
checkForLiveEvent { available, event in
dispatch_async(dispatch_get_main_queue()) {
if !available && self.liveEventPlayerController != nil {
// self.liveEventPlayerController?.close()
self.liveEventPlayerController?.close()
return
}

Expand All @@ -51,22 +51,22 @@ class LiveEventObserver: NSObject, NSUserNotificationCenterDelegate {
}
}

DataStore.SharedStore.fetchNextLiveEvent { available, event in
fetchNextLiveEvent { available, event in
dispatch_async(dispatch_get_main_queue()) {
self.nextEvent = event
}
}
}

func playEvent(event: LiveEvent) {
func playEvent(event: LiveSession) {
if Preferences.SharedPreferences().autoplayLiveEvents || overrideAutoplayPreference {
doPlayEvent(event)
}

// showNotification(event)
showNotification(event)
}

private func doPlayEvent(event: LiveEvent) {
private func doPlayEvent(event: LiveSession) {
// we already have a live event playing, just return
if liveEventPlayerController != nil {
NSNotificationCenter.defaultCenter().postNotificationName(LiveEventTitleAvailableNotification, object: event.title)
Expand All @@ -75,7 +75,7 @@ class LiveEventObserver: NSObject, NSUserNotificationCenterDelegate {

NSNotificationCenter.defaultCenter().postNotificationName(LiveEventWillStartPlayingNotification, object: nil)

liveEventPlayerController = VideoWindowController(event: event, videoURL: event.stream!.absoluteString)
liveEventPlayerController = VideoWindowController(event: event, videoURL: event.streamURL!.absoluteString)
liveEventPlayerController?.showWindow(nil)
}

Expand All @@ -94,7 +94,7 @@ class LiveEventObserver: NSObject, NSUserNotificationCenterDelegate {
}
}

func showNotification(event: LiveEvent) {
func showNotification(event: LiveSession) {
let notification = NSUserNotification()
notification.title = "\(event.title) is live!"
notification.informativeText = "Watch \(event.title) right now!"
Expand All @@ -111,4 +111,63 @@ class LiveEventObserver: NSObject, NSUserNotificationCenterDelegate {
}
}

private let _liveServiceURL = "http://wwdc.guilhermerambo.me/live.json"
private let _liveNextServiceURL = "http://wwdc.guilhermerambo.me/next.json"

private var liveURL: NSURL {
get {
sranddev()
// adds a random number as a parameter to completely prevent any caching
return NSURL(string: "\(_liveServiceURL)?t=\(rand())&s=\(NSDate.timeIntervalSinceReferenceDate())")!
}
}

private var liveNextURL: NSURL {
get {
sranddev()
// adds a random number as a parameter to completely prevent any caching
return NSURL(string: "\(_liveNextServiceURL)?t=\(rand())&s=\(NSDate.timeIntervalSinceReferenceDate())")!
}
}

let URLSession2 = NSURLSession(configuration: NSURLSessionConfiguration.ephemeralSessionConfiguration())

func checkForLiveEvent(completionHandler: (Bool, LiveSession?) -> ()) {
let task = URLSession2.dataTaskWithURL(liveURL) { data, response, error in
if data == nil {
completionHandler(false, nil)
return
}

let jsonData = JSON(data: data!)
let event = LiveSession(jsonObject: jsonData)

if event.isLiveRightNow {
completionHandler(true, event)
} else {
completionHandler(false, nil)
}
}
task.resume()
}

func fetchNextLiveEvent(completionHandler: (Bool, LiveSession?) -> ()) {
let task = URLSession2.dataTaskWithURL(liveNextURL) { data, response, error in
if data == nil {
completionHandler(false, nil)
return
}

let jsonData = JSON(data: data!)
let event = LiveSession(jsonObject: jsonData)

if event.title != "" {
completionHandler(true, event)
} else {
completionHandler(false, nil)
}
}
task.resume()
}

}
46 changes: 44 additions & 2 deletions WWDC/Models.swift
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,49 @@ class LiveSession {
var title = ""
var summary = ""
var startsAt: NSDate?
var streamURL = ""
var streamURL: NSURL?
var alternateStreamURL = ""
var current = false
var isLiveRightNow = false

private struct Keys {
static let id = "id"
static let title = "title"
static let description = "description"
static let stream = "stream"
static let stream2 = "stream_elcapitan"
static let startsAt = "starts_at"
static let isLiveRightNow = "isLiveRightNow"
}

private let _dateTimezone = "GMT"
private let _dateFormat = "yyyy-MM-dd'T'HH:mm:ss'Z'ZZZZ"

init(jsonObject: JSON) {
id = jsonObject[Keys.id].intValue

if let title = jsonObject[Keys.title].string {
self.title = title
} else {
self.title = ""
}

if let description = jsonObject[Keys.description].string {
self.summary = description
} else {
self.summary = ""
}

if let streamURL = jsonObject[Keys.stream].string {
self.streamURL = NSURL(string: streamURL)
}

isLiveRightNow = jsonObject[Keys.isLiveRightNow].boolValue

let formatter = NSDateFormatter()
formatter.dateFormat = _dateFormat
if let startsAtString = jsonObject[Keys.startsAt].string {
let startsAtWithZone = startsAtString+_dateTimezone
startsAt = formatter.dateFromString(startsAtWithZone)
}
}
}
2 changes: 1 addition & 1 deletion WWDC/VideoWindowController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ class VideoWindowController: NSWindowController {
}

private func loadEventVideo() {
if let url = NSURL(string: event!.streamURL) {
if let url = event?.streamURL {
asset = AVURLAsset(URL: url, options: nil)
asset.loadValuesAsynchronouslyForKeys(["playable"]) {
dispatch_async(dispatch_get_main_queue()) {
Expand Down
2 changes: 0 additions & 2 deletions WWDC/WWDCDatabase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ typealias SessionsUpdatedCallback = () -> Void

private struct Constants {
static let internalServiceURL = "http://wwdc.guilhermerambo.me/index.json"
static let liveServiceURL = "http://wwdc.guilhermerambo.me/live.json"
static let liveNextServiceURL = "http://wwdc.guilhermerambo.me/next.json"
static let asciiServiceBaseURL = "http://asciiwwdc.com/"
}

Expand Down

0 comments on commit ba5ee61

Please sign in to comment.