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
133 lines (115 loc) · 4.61 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
//
// InterfaceController.swift
// Slide for Apple Watch Extension
//
// Created by Carlos Crane on 9/23/18.
// Copyright © 2018 Haptic Apps. All rights reserved.
//
import WatchKit
import WatchConnectivity
import Foundation
class InterfaceController: WKInterfaceController {
@IBOutlet weak var table: WKInterfaceTable!
@IBOutlet weak var loadingImage: WKInterfaceImage!
override func awake(withContext context: Any?) {
super.awake(withContext: context)
// Configure interface objects here.
}
@IBAction func onMenuItemPlayTap() {
print("Play Tapped")
}
var links = [NSDictionary]()
var subs = [String: String]()
var subsOrdered = [String]()
override func willActivate() {
// 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()
if subs.isEmpty {
loadingImage.setImageNamed("Activity")
loadingImage.startAnimatingWithImages(in: NSRange(location: 0, length: 15), duration: 1.0, repeatCount: 0)
watchSession.sendMessage(["sublist": true], replyHandler: { (message) in
self.subs = message["subs"] as? [String: String] ?? [String: String]()
if self.subs.count > 0 {
self.subsOrdered = Array(self.subs.keys).sorted(by: { (a, b) -> Bool in
return a.lowercased() < b.lowercased()
})
self.subsOrdered = self.subsOrdered.filter({ (a) -> Bool in
!a.startsWith("/")
})
self.getSubmissions(self.subsOrdered[0])
}
}) { (error) in
print(error)
}
}
}
@IBAction func gotosub() {
presentTextInputController(withSuggestions: subsOrdered, allowedInputMode: .plain) { (subs) in
self.getSubmissions((subs ?? ["all"])[0] as! String)
}
}
func getSubmissions(_ subreddit: String) {
self.loadingImage.setHidden(false)
DispatchQueue.main.async {
self.setTitle("r/\(subreddit)")
self.table.setNumberOfRows(0, withRowType: "SubmissionRowController")
}
WCSession.default.sendMessage(["links": subreddit], replyHandler: { (message) in
self.loadingImage.setHidden(true)
if let links = message["links"] as? [NSDictionary] {
self.links = links
}
self.beginLoadingTable()
}) { (error) in
print(error)
}
}
override func didDeactivate() {
// This method is called when watch view controller is no longer visible
super.didDeactivate()
}
func beginLoadingTable() {
table.setNumberOfRows(links.count, withRowType: "SubmissionRowController")
var index = 0
for item in links {
if let rowController = table.rowController(at: index) as? SubmissionRowController {
rowController.parent = self
rowController.setData(dictionary: item, color: UIColor(hexString: self.subs[item["subreddit"] as? String ?? ""] ?? "#ffffff"))
}
index += 1
}
}
}
extension InterfaceController: WCSessionDelegate {
func session(_ session: WCSession, didReceiveApplicationContext applicationContext: [String: Any]) {
print("Doing links")
DispatchQueue.main.async {
self.setTitle(applicationContext["title"] as? String)
self.beginLoadingTable()
}
}
func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) {
}
}
extension UIColor {
convenience init(hexString: String) {
let hex = hexString.trimmingCharacters(in: CharacterSet.alphanumerics.inverted)
var int = UInt32()
Scanner(string: hex).scanHexInt32(&int)
let a, r, g, b: UInt32
switch hex.count {
case 3: // RGB (12-bit)
(a, r, g, b) = (255, (int >> 8) * 17, (int >> 4 & 0xF) * 17, (int & 0xF) * 17)
case 6: // RGB (24-bit)
(a, r, g, b) = (255, int >> 16, int >> 8 & 0xFF, int & 0xFF)
case 8: // ARGB (32-bit)
(a, r, g, b) = (int >> 24, int >> 16 & 0xFF, int >> 8 & 0xFF, int & 0xFF)
default:
(a, r, g, b) = (255, 0, 0, 0)
}
self.init(red: CGFloat(r) / 255, green: CGFloat(g) / 255, blue: CGFloat(b) / 255, alpha: CGFloat(a) / 255)
}
}