Skip to content

Commit

Permalink
added Offline Mode
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnCoates committed Oct 31, 2015
1 parent 545db76 commit 5b1be6c
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
6 changes: 6 additions & 0 deletions Aerial/Source/Models/AerialVideo.swift
Expand Up @@ -17,6 +17,12 @@ class AerialVideo {
let url:NSURL;
var arrayPosition:Int = 1;

var isAvailableOffline:Bool {
get {
return VideoCache.isVideoAvailableOffline(self);
}
}


init(id:String, name:String, type:String, timeOfDay:String, url:String) {
self.id = id;
Expand Down
18 changes: 18 additions & 0 deletions Aerial/Source/Models/Cache/VideoCache.swift
Expand Up @@ -63,6 +63,24 @@ class VideoCache {
}
}

static func isVideoAvailableOffline(video:AerialVideo) -> Bool {
guard let appCacheDirectory = VideoCache.cacheDirectory else {
return false;
}

guard let filename = video.url.lastPathComponent else {
NSLog("Aerial Error: Couldn't get filename from URL for cache.");
return false;
}

let videoCachePath = appCacheDirectory.stringByAppendingPathComponent(filename);

let fileManager = NSFileManager.defaultManager()


return fileManager.fileExistsAtPath(videoCachePath)
}

init(URL:NSURL) {
videoData = NSData()
loading = true
Expand Down
15 changes: 14 additions & 1 deletion Aerial/Source/Models/ManifestLoader.swift
Expand Up @@ -18,6 +18,7 @@ class ManifestLoader {
var callbacks = [manifestLoadCallback]();
var loadedManifest = [AerialVideo]();
var playedVideos = [AerialVideo]();
var offlineMode:Bool = false

func addCallback(callback:manifestLoadCallback) {
if (loadedManifest.count > 0) {
Expand All @@ -33,6 +34,7 @@ class ManifestLoader {
let shuffled = loadedManifest.shuffle();

for video in shuffled {
// check if this video id has been disabled in preferences
let possible = defaults.objectForKey(video.id);

if let possible = possible as? NSNumber {
Expand All @@ -41,6 +43,13 @@ class ManifestLoader {
}
}

// check if we're in offline mode
if offlineMode == true {
if video.isAvailableOffline == false {
continue;
}
}

return video;
}

Expand Down Expand Up @@ -71,7 +80,10 @@ class ManifestLoader {

};
let url = NSURL(string: "http://a1.phobos.apple.com/us/r1000/000/Features/atv/AutumnResources/videos/entries.json");
let task = NSURLSession.sharedSession().dataTaskWithURL(url!, completionHandler:completionHandler);
// use ephemeral session so when we load json offline it fails and puts us in offline mode
let configuration = NSURLSessionConfiguration.ephemeralSessionConfiguration()
let session = NSURLSession(configuration: configuration)
let task = session.dataTaskWithURL(url!, completionHandler:completionHandler);
task.resume();
}

Expand All @@ -81,6 +93,7 @@ class ManifestLoader {
return;
}

offlineMode = true;
readJSONFromData(savedJSON)
}

Expand Down

0 comments on commit 5b1be6c

Please sign in to comment.