-
Notifications
You must be signed in to change notification settings - Fork 0
/
VideoCollection.swift
83 lines (71 loc) · 3.07 KB
/
VideoCollection.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
//
// VideoCollection.swift
// FitGeek
//
// Created by MU IT Program on 3/31/16.
// Copyright © 2016 Teddy Ivanov. All rights reserved.
//
import Foundation
class VideoCollection {
//class name with constructing an instance of this class.
static let sharedInstance = VideoCollection(fileName: "videos")
private init(fileName:String) {
loadFromJSONFile(fileName)
}
//All ways to do the same thing
var videos:[Video] = Array<Video>()
//var photos = Array<Photo>()
//var photos: Array<Photo>() = Array<Photo>()
//It is inside the application, it can only be read cant write to it.
//This is different has its own file system. They refer to the sandbox. the sandbox restricts apps to writing to each other. Apps can read and write from their own sandbox
private func loadFromJSONFile(fileName: String) {
let bundle = NSBundle.mainBundle()
//Compound if let state, like 2 if let put together.
if let path = bundle.pathForResource(fileName, ofType: "json"), jsonData = NSData(contentsOfFile: path) {
parseJson(jsonData)
}
}
private func parseJson(jsonData: NSData) {
videos = Array<Video>()
var jsonResultWrapped: NSDictionary?
do {
//Used to parse Json, Its job to parse atleast the top level object. Dicitionary is key value pairs
jsonResultWrapped = try NSJSONSerialization.JSONObjectWithData(jsonData, options: NSJSONReadingOptions.MutableContainers) as? NSDictionary
} catch {
return
}
//Can check in a where condition if another piece of data is in the state you want it to be in, used to avoid too many if lets
if let jsonResult = jsonResultWrapped where jsonResult.count > 0 {
if let status = jsonResult["status"] as? String where status == "ok" {
if let photoList = jsonResult["photos"] as? NSArray {
//in is another condition word that runs through the entire NSAarry.
for photo in photoList{
if let fileName = photo["image"] as? String,
title = photo["title"] as? String,
description = photo["description"] as? String,
latitude = photo["latitude"] as? Double,
longitude = photo["longitude"] as? Double,
date = photo["date"] as? String {
self.videos.append(Video(fileName: fileName, title: title, description: description,
latitude: latitude, longitude: longitude, dateString: date))
}
}
}
}
}
}
func filter(searchText: String) -> Array<Video> {
if searchText.isEmpty {
return videos
}
var filteredPhotos = Array<Video>()
for photo in videos {
if photo.title.rangeOfString(searchText, options: .CaseInsensitiveSearch) != nil {
filteredPhotos.append(photo)
} else if photo.description.rangeOfString(searchText, options: .CaseInsensitiveSearch) != nil {
filteredPhotos.append(photo)
}
}
return filteredPhotos
}
}