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 pathCommentRepliesController.swift
73 lines (63 loc) · 3.08 KB
/
CommentRepliesController.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
//
// CommentRepliesController.swift
// Slide for Apple Watch Extension
//
// Created by Carlos Crane on 3/1/20.
// Copyright © 2020 Haptic Apps. All rights reserved.
//
import Foundation
import WatchConnectivity
import WatchKit
class CommentRepliesController: Votable {
public var modelContext: CommentsRowController?
@IBOutlet var originalBody: WKInterfaceLabel!
@IBOutlet var commentsTable: WKInterfaceTable!
@IBOutlet var originalTitle: WKInterfaceLabel!
@IBOutlet var upvoteButton: WKInterfaceButton!
@IBOutlet var downvoteButton: WKInterfaceButton!
override init() {
super.init()
self.setTitle("Back")
}
@IBAction func didUpvote() {
(WKExtension.shared().visibleInterfaceController as? Votable)?.sharedUp = upvoteButton
(WKExtension.shared().visibleInterfaceController as? Votable)?.sharedDown = downvoteButton
(WKExtension.shared().visibleInterfaceController as? Votable)?.doVote(id: modelContext!.fullname!, upvote: true, downvote: false)
}
@IBAction func didDownvote() {
(WKExtension.shared().visibleInterfaceController as? Votable)?.sharedUp = upvoteButton
(WKExtension.shared().visibleInterfaceController as? Votable)?.sharedDown = downvoteButton
(WKExtension.shared().visibleInterfaceController as? Votable)?.doVote(id: modelContext!.fullname!, upvote: false, downvote: true)
}
override func contextForSegue(withIdentifier segueIdentifier: String, in table: WKInterfaceTable, rowIndex: Int) -> Any? {
return table.rowController(at: rowIndex)
}
override func awake(withContext context: Any?) {
super.awake(withContext: context)
let myModel = context as! CommentsRowController // make the model
self.modelContext = myModel
self.originalTitle.setAttributedText(myModel.attributedTitle)
self.originalBody.setAttributedText(myModel.attributedBody)
upvoteButton.setBackgroundColor((myModel.dictionary["upvoted"] ?? false) as! Bool ? UIColor.init(hexString: "#FF5700") : UIColor.gray)
downvoteButton.setBackgroundColor((myModel.dictionary["downvoted"] ?? false) as! Bool ? UIColor.init(hexString: "#9494FF") : UIColor.gray)
WCSession.default.sendMessage(["comments": myModel.submissionId!, "context": myModel.id!], replyHandler: { (message) in
self.comments = message["comments"] as? [NSDictionary] ?? []
self.beginLoadingTable()
}, errorHandler: { (error) in
print(error)
})
}
var comments = [NSDictionary]()
func beginLoadingTable() {
WKInterfaceDevice.current().play(.success)
commentsTable.insertRows(at: IndexSet(integersIn: 0 ..< comments.count), withRowType: "CommentsRowController")
if comments.count > 0 {
for index in 0...(comments.count - 1) {
let item = comments[index]
if let rowController = commentsTable.rowController(at: index) as? CommentsRowController {
rowController.setData(dictionary: item)
}
}
}
}
}