-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDataLayer.swift
96 lines (82 loc) · 3.32 KB
/
DataLayer.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
84
85
86
87
88
89
90
91
92
93
94
95
96
//
// Data.swift
// SmartStream
//
// Created by Jerry on 3/8/16.
// Copyright © 2016 SmartStream. All rights reserved.
//
import UIKit
class DataLayer: NSObject {
class func createChannel(withDictionary dictionary: NSDictionary, completion: (error: NSError?, channel: Channel?) -> ()) {
// let channelID = generateID()
let topics = dictionary["topics"] as! [String]
let filters = dictionary["filters"] as! Filters
let filtersDict = filtersToDictionary(filters)
let title = dictionary["title"] as! String
// let itemDictionary = Mock.NewChannelItem.init(topics: topics).items
// let owner = Mock.NewUser().user
let channelDictionary =
["channel": [
"title": title,
"filters": filtersDict,
"topics": topics
]
] as NSDictionary
ChannelClient.sharedInstance.createChannel(channelDictionary) { (channel, error) -> () in
if error != nil {
debugPrint(error)
completion(error: error!, channel: nil)
} else {
completion(error: nil, channel: channel!)
}
}
}
class func updateChannel(withDictionary dictionary: NSDictionary, completion: (error: NSError?, channel: Channel?) -> ()) {
let channel_id = dictionary["channel_id"] as! String
let topics = dictionary["topics"] as! [String]
let filters = dictionary["filters"] as! Filters
let filtersDict = filtersToDictionary(filters)
let title = dictionary["title"] as! String
let channelDictionary =
["channel": [
"title": title,
"filters": filtersDict,
"topics": topics
]
] as NSDictionary
ChannelClient.sharedInstance.updateChannel(channel_id, channelDict: channelDictionary) { (channel, error) -> () in
if error != nil {
completion(error: error!, channel: nil)
} else {
completion(error: nil, channel: channel!)
}
}
}
class func deleteChannel(withChannelId channel_id: String, completion: (error: NSError?, channelId: String?) -> ()) {
ChannelClient.sharedInstance.deleteChannel(channel_id) { (channelId, error) -> () in
if error != nil {
completion(error: error!, channelId: nil)
} else {
completion(error: nil, channelId: channelId)
}
}
}
class func forkChannels(withChannelIDs channel_ids: [String], completion: (error: NSError?, channels: [Channel]?) -> ()) {
ChannelClient.sharedInstance.forkChannels(channel_ids) { (channels, error) in
if error != nil {
completion(error: error!, channels: nil)
} else {
completion(error: nil, channels: channels!)
}
}
}
static func filtersToDictionary(filters: Filters) -> NSDictionary {
let dictionary = ["max_duration": filters.max_duration!] as NSDictionary
return dictionary
}
// static func generateID() -> String {
// let date = NSDate()
// let uniqueID = String(UInt64(floor(date.timeIntervalSince1970)))
// return uniqueID
// }
}