This repository was archived by the owner on Sep 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathThingContainable.swift
111 lines (98 loc) · 3.06 KB
/
ThingContainable.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
//
// ThingContainable.swift
// reddift
//
// Created by sonson on 2016/09/29.
// Copyright © 2016年 sonson. All rights reserved.
//
import reddift
import Foundation
let ThingContainableDidUpdate = Notification.Name(rawValue: "ThingContainableDidUpdate")
class ThingContainable {
internal var intrinsicThing: Thing
var thing: Thing {
get {
return intrinsicThing
}
}
init(with aThing: Thing) {
intrinsicThing = aThing
}
var cellIdentifier: String {
return "InvisibleCell"
}
var height: CGFloat {
return 0
}
var isHidden: Bool = false
// update
func update() {
do {
try UIApplication.appDelegate()?.session?.getInfo([self.thing.name], completion: { (result) in
switch result {
case .failure(let error):
print(error)
case .success(let listing):
if listing.children.count == 1 {
self.intrinsicThing = listing.children[0]
DispatchQueue.main.async(execute: { () -> Void in
NotificationCenter.default.post(name: ThingContainableDidUpdate, object: nil, userInfo: ["contents": self])
})
}
}
})
} catch { print(error) }
}
// hide
func hide() {
do {
try UIApplication.appDelegate()?.session?.setHide(true, name: self.thing.name, completion: { (result) in
switch result {
case .failure(let error):
print(error)
case .success:
self.update()
}
})
} catch { print(error) }
}
// report
func report() {
do {
try UIApplication.appDelegate()?.session?.report(self.thing, reason: "", otherReason: "", completion: { (result) in
switch result {
case .failure(let error):
print(error)
case .success(let obj):
print(obj)
}
})
} catch { print(error) }
}
// vote
func vote(voteDirection: VoteDirection) {
do {
try UIApplication.appDelegate()?.session?.setVote(voteDirection, name: self.thing.name, completion: { (result) in
switch result {
case .failure(let error):
print(error)
case .success:
self.update()
}
})
} catch { print(error) }
}
// save
func save(saved: Bool) {
do {
try UIApplication.appDelegate()?.session?.setSave(saved, name: self.thing.name, completion: { (result) in
switch result {
case .failure(let error):
print(error)
case .success:
self.update()
}
})
} catch { print(error) }
}
}