This repository was archived by the owner on Oct 30, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 88
/
Copy pathInterfaceController.swift
182 lines (158 loc) · 6.24 KB
/
InterfaceController.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
//
// InterfaceController.swift
// Slide for Apple Watch Extension
//
// Created by Carlos Crane on 9/23/18.
// Copyright © 2018 Haptic Apps. All rights reserved.
//
import Foundation
import WatchConnectivity
import WatchKit
class InterfaceController: Votable {
@IBOutlet weak var table: WKInterfaceTable!
@IBOutlet weak var loadingImage: WKInterfaceImage!
override func awake(withContext context: Any?) {
super.awake(withContext: context)
// Configure interface objects here.
}
override func contextForSegue(withIdentifier segueIdentifier: String, in table: WKInterfaceTable, rowIndex: Int) -> Any? {
return table.rowController(at: rowIndex)
}
var links = [NSDictionary]()
var subs = [String: String]()
var subsOrdered = [String]()
var page = 1
var last = 0
var currentSub = ""
var isPro = false
var isNew = false
override func willActivate() {
// setTitle("WILL ACTIVATE")
// This method is called when watch view controller is about to be visible to user
super.willActivate()
let watchSession = WCSession.default
watchSession.delegate = self
watchSession.activate()
}
@IBAction func gotosub() {
presentTextInputController(withSuggestions: subsOrdered, allowedInputMode: .plain) { (subs) in
self.getSubmissions((subs ?? ["all"])[0] as! String, reset: true)
}
}
@IBAction func sortHot() {
self.getSubmissions(currentSub, reset: true)
}
@IBAction func sortNew() {
self.getSubmissions(currentSub, reset: true, new: true)
}
func getSubmissions(_ subreddit: String, reset: Bool, new: Bool = false) {
isNew = new
currentSub = subreddit
if reset {
self.page = 1
self.last = 0
self.loadingImage.setHidden(false)
self.links.removeAll()
DispatchQueue.main.async {
self.setTitle("\(subreddit)")
self.table.setNumberOfRows(0, withRowType: "SubmissionRowController")
}
}
// self.setTitle("getting submissions")
WCSession.default.sendMessage(["links": subreddit, "reset": reset, "new": isNew], replyHandler: { (message) in
self.loadingImage.setHidden(true)
if let newLinks = message["links"] as? [NSDictionary] {
self.links.append(contentsOf: newLinks)
}
self.beginLoadingTable()
}, errorHandler: { (error) in
print(error)
})
}
override func didDeactivate() {
// setTitle("DEACTIVATE")
// This method is called when watch view controller is no longer visible
super.didDeactivate()
}
func beginLoadingTable() {
page += 1
if page > 1 {
table.removeRows(at: IndexSet(integer: last))
}
table.insertRows(at: IndexSet(integersIn: last ..< links.count), withRowType: "SubmissionRowController")
for index in last...(links.count - 1) {
let item = links[index]
if let rowController = table.rowController(at: index) as? SubmissionRowController {
rowController.setData(dictionary: item, color: UIColor(hexString: self.subs[item["subreddit"] as? String ?? ""] ?? "#ffffff"))
}
}
last = links.count
table.insertRows(at: IndexSet(integer: links.count), withRowType: "MoreRowController")
if let rowController = table.rowController(at: links.count) as? MoreRowController {
rowController.loadButton.setTitle("Load page \(page)")
rowController.completion = {[weak self] in
rowController.progressImage.setImageNamed("Activity")
rowController.progressImage.startAnimatingWithImages(in: NSRange(location: 0, length: 15), duration: 1.0, repeatCount: 0)
rowController.loadButton.setTitle("Loading...")
if let strongSelf = self {
strongSelf.getSubmissions(strongSelf.currentSub, reset: false, new: strongSelf.isNew)
}
}
}
}
var tries = 0
var letThrough = false
func loadData(_ session: WCSession) {
// setTitle("HB \(tries)")
tries += 1
if session.isReachable {
if !letThrough {
letThrough = true
return
}
checkTimer?.invalidate()
checkTimer = nil
} else if checkTimer == nil {
DispatchQueue.main.async {
self.checkTimer = Timer.scheduledTimer(withTimeInterval: 1, repeats: true, block: { (_) in
self.loadData(session)
})
}
return
} else {
return
}
// setTitle("LOADING")
letThrough = true
session.sendMessage(["sublist": true], replyHandler: { (message) in
// self.setTitle("got subs")
self.subs = message["subs"] as? [String: String] ?? [String: String]()
self.subsOrdered = message["orderedsubs"] as? [String] ?? [String]()
self.isPro = message["pro"] as? Bool ?? false
if self.subsOrdered.count > 0 {
self.getSubmissions(self.subsOrdered[0], reset: true)
}
}, errorHandler: { (error) in
self.setTitle("Error connecting...")
print(error)
})
}
var checkTimer: Timer?
}
extension InterfaceController: WCSessionDelegate {
func session(_ session: WCSession, didReceiveApplicationContext applicationContext: [String: Any]) {
DispatchQueue.main.async {
self.setTitle(applicationContext["title"] as? String)
self.beginLoadingTable()
}
}
func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) {
// setTitle("DID ACTIVATE")
if subs.isEmpty {
loadingImage.setHidden(false)
loadingImage.setImageNamed("Activity")
loadingImage.startAnimatingWithImages(in: NSRange(location: 0, length: 15), duration: 1.0, repeatCount: 0)
loadData(WCSession.default)
}
}
}