Skip to content
This repository was archived by the owner on Oct 30, 2024. It is now read-only.

Commit 106638a

Browse files
committed
Added voting to Apple Watch, Fixed more comments loading on AW
1 parent 420e122 commit 106638a

File tree

9 files changed

+222
-46
lines changed

9 files changed

+222
-46
lines changed

Slide for Apple Watch Extension/CommentRepliesController.swift

+21-3
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,30 @@ import Foundation
1010
import WatchConnectivity
1111
import WatchKit
1212

13-
class CommentRepliesController: WKInterfaceController {
13+
class CommentRepliesController: Votable {
1414
public var modelContext: CommentsRowController?
15+
1516
@IBOutlet var originalBody: WKInterfaceLabel!
1617
@IBOutlet var commentsTable: WKInterfaceTable!
1718
@IBOutlet var originalTitle: WKInterfaceLabel!
18-
19+
@IBOutlet var upvoteButton: WKInterfaceButton!
20+
@IBOutlet var downvoteButton: WKInterfaceButton!
21+
1922
override init() {
2023
super.init()
2124
self.setTitle("Back")
2225
}
23-
26+
@IBAction func didUpvote() {
27+
(WKExtension.shared().visibleInterfaceController as? Votable)?.sharedUp = upvoteButton
28+
(WKExtension.shared().visibleInterfaceController as? Votable)?.sharedDown = downvoteButton
29+
(WKExtension.shared().visibleInterfaceController as? Votable)?.doVote(id: modelContext!.fullname!, upvote: true, downvote: false)
30+
}
31+
@IBAction func didDownvote() {
32+
(WKExtension.shared().visibleInterfaceController as? Votable)?.sharedUp = upvoteButton
33+
(WKExtension.shared().visibleInterfaceController as? Votable)?.sharedDown = downvoteButton
34+
(WKExtension.shared().visibleInterfaceController as? Votable)?.doVote(id: modelContext!.fullname!, upvote: false, downvote: true)
35+
}
36+
2437
override func contextForSegue(withIdentifier segueIdentifier: String, in table: WKInterfaceTable, rowIndex: Int) -> Any? {
2538
return table.rowController(at: rowIndex)
2639
}
@@ -31,6 +44,9 @@ class CommentRepliesController: WKInterfaceController {
3144
self.modelContext = myModel
3245
self.originalTitle.setAttributedText(myModel.attributedTitle)
3346
self.originalBody.setAttributedText(myModel.attributedBody)
47+
48+
upvoteButton.setBackgroundColor((myModel.dictionary["upvoted"] ?? false) as! Bool ? UIColor.init(hexString: "#FF5700") : UIColor.gray)
49+
downvoteButton.setBackgroundColor((myModel.dictionary["downvoted"] ?? false) as! Bool ? UIColor.init(hexString: "#9494FF") : UIColor.gray)
3450

3551
WCSession.default.sendMessage(["comments": myModel.submissionId!, "context": myModel.id!], replyHandler: { (message) in
3652
self.comments = message["comments"] as? [NSDictionary] ?? []
@@ -42,6 +58,8 @@ class CommentRepliesController: WKInterfaceController {
4258

4359
var comments = [NSDictionary]()
4460
func beginLoadingTable() {
61+
WKInterfaceDevice.current().play(.success)
62+
4563
commentsTable.insertRows(at: IndexSet(integersIn: 0 ..< comments.count), withRowType: "CommentsRowController")
4664
if comments.count > 0 {
4765
for index in 0...(comments.count - 1) {

Slide for Apple Watch Extension/CommentsRowController.swift

+5-1
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,23 @@ public class CommentsRowController: NSObject {
1616
var author: String!
1717
var time: String!
1818
var id: String!
19+
var fullname: String!
1920
var body: String!
2021
var submissionId: String!
22+
var dictionary: NSDictionary!
2123

2224
var attributedTitle: NSAttributedString!
2325
var attributedBody: NSAttributedString!
2426
@IBOutlet var titleLabel: WKInterfaceLabel!
2527
@IBOutlet var bodyLabel: WKInterfaceLabel!
2628

2729
func setData(dictionary: NSDictionary) {
30+
self.dictionary = dictionary
2831
let titleFont = UIFont.systemFont(ofSize: 14)
2932
let subtitleFont = UIFont.boldSystemFont(ofSize: 10)
3033
//let attributedTitle = NSMutableAttributedString(string: dictionary["title"] as! String, attributes: [NSAttributedString.Key.font: titleFont, NSAttributedString.Key.foregroundColor: UIColor.white])
3134
id = dictionary["context"] as? String ?? ""
35+
fullname = dictionary["id"] as? String ?? ""
3236
submissionId = dictionary["submission"] as? String ?? ""
3337

3438
let spacer = NSMutableAttributedString.init(string: " ")
@@ -64,7 +68,7 @@ public class CommentsRowController: NSObject {
6468
infoString.append(scoreString)
6569
self.attributedTitle = infoString
6670
titleLabel.setAttributedText(infoString)
67-
if let html = (dictionary["body"] as! String).replacingOccurrences(of: "<div class=\"md\">", with: "").replacingOccurrences(of: "</p>\n</div>", with: "").data(using: String.Encoding.unicode) {
71+
if let html = (dictionary["body"] as! String).replacingOccurrences(of: "<div class=\"md\">", with: "").replacingOccurrences(of: "</p>\n</div>", with: "</p>").replacingOccurrences(of: "<p>", with: "").replacingOccurrences(of: "</p>", with: "</br>").data(using: String.Encoding.unicode) {
6872
do {
6973
var attributedText = try NSMutableAttributedString(data: html, options: [NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.html], documentAttributes: nil)
7074
attributedText.addAttributes([NSAttributedString.Key.font: UIFont.systemFont(ofSize: 11), NSAttributedString.Key.foregroundColor: UIColor.white], range: NSRange(location: 0, length: attributedText.length))

Slide for Apple Watch Extension/InterfaceController.swift

+1-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import Foundation
1010
import WatchConnectivity
1111
import WatchKit
1212

13-
class InterfaceController: WKInterfaceController {
13+
class InterfaceController: Votable {
1414
@IBOutlet weak var table: WKInterfaceTable!
1515
@IBOutlet weak var loadingImage: WKInterfaceImage!
1616

@@ -96,7 +96,6 @@ class InterfaceController: WKInterfaceController {
9696
for index in last...(links.count - 1) {
9797
let item = links[index]
9898
if let rowController = table.rowController(at: index) as? SubmissionRowController {
99-
rowController.parent = self
10099
rowController.setData(dictionary: item, color: UIColor(hexString: self.subs[item["subreddit"] as? String ?? ""] ?? "#ffffff"))
101100
}
102101
}

Slide for Apple Watch Extension/PostActionMenuController.swift

+25-13
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import Foundation
1010
import WatchConnectivity
1111
import WatchKit
1212

13-
class PostActionMenuController: WKInterfaceController {
13+
class PostActionMenuController: Votable {
1414
@IBOutlet weak var bannerImage: WKInterfaceImage!
1515
@IBOutlet var commentTable: WKInterfaceTable!
1616
@IBOutlet weak var titleLabel: WKInterfaceLabel!
@@ -21,7 +21,20 @@ class PostActionMenuController: WKInterfaceController {
2121
public var parent: InterfaceController?
2222
@IBOutlet var thumbImage: WKInterfaceImage!
2323
@IBOutlet var thumbGroup: WKInterfaceGroup!
24+
@IBOutlet var upvoteButton: WKInterfaceButton!
25+
@IBOutlet var downvoteButton: WKInterfaceButton!
2426
@IBOutlet var linkInfo: WKInterfaceLabel!
27+
var id: String?
28+
@IBAction func didUpvote() {
29+
(WKExtension.shared().visibleInterfaceController as? Votable)?.sharedUp = upvoteButton
30+
(WKExtension.shared().visibleInterfaceController as? Votable)?.sharedDown = downvoteButton
31+
(WKExtension.shared().visibleInterfaceController as? Votable)?.doVote(id: id!, upvote: true, downvote: false)
32+
}
33+
@IBAction func didDownvote() {
34+
(WKExtension.shared().visibleInterfaceController as? Votable)?.sharedUp = upvoteButton
35+
(WKExtension.shared().visibleInterfaceController as? Votable)?.sharedDown = downvoteButton
36+
(WKExtension.shared().visibleInterfaceController as? Votable)?.doVote(id: id!, upvote: false, downvote: true)
37+
}
2538

2639
@IBAction func openComments() {
2740
// if !(self.parent?.isPro ?? true) {
@@ -49,16 +62,7 @@ class PostActionMenuController: WKInterfaceController {
4962
})
5063
// }
5164
}
52-
@IBAction func doUpvote() {
53-
WCSession.default.sendMessage(["upvote": modelContext!.id!], replyHandler: { (result) in
54-
if result["failed"] == nil {
55-
self.dismiss()
56-
}
57-
}, errorHandler: { (error) in
58-
print(error)
59-
})
60-
}
61-
65+
6266
override init() {
6367
super.init()
6468
self.setTitle("Back")
@@ -68,20 +72,26 @@ class PostActionMenuController: WKInterfaceController {
6872
super.awake(withContext: context)
6973
let myModel = context as! SubmissionRowController //make the model
7074
self.modelContext = myModel
71-
self.parent = (context as? SubmissionRowController)?.parent
7275
titleLabel.setAttributedText(myModel.titleText)
7376
if myModel.thumbnail == nil && myModel.largeimage != nil {
7477
bannerImage.setImage(myModel.largeimage)
7578
bannerImage.setHidden(false)
7679
thumbGroup.setHidden(true)
77-
} else {
80+
} else if myModel.thumbnail != nil {
7881
thumbImage.setImage(myModel.thumbnail)
7982
bannerImage.setHidden(true)
8083
thumbGroup.setHidden(false)
84+
} else {
85+
bannerImage.setHidden(true)
86+
thumbGroup.setHidden(true)
8187
}
8288
imageGroup.setCornerRadius(5)
8389

90+
upvoteButton.setBackgroundColor((myModel.dictionary["upvoted"] ?? false) as! Bool ? UIColor.init(hexString: "#FF5700") : UIColor.gray)
91+
downvoteButton.setBackgroundColor((myModel.dictionary["downvoted"] ?? false) as! Bool ? UIColor.init(hexString: "#9494FF") : UIColor.gray)
92+
8493
scoreLabel.setText(myModel.scoreText)
94+
id = myModel.id
8595
commentLabel.setText(myModel.commentText)
8696
WCSession.default.sendMessage(["comments": myModel.id!], replyHandler: { (message) in
8797
self.comments = message["comments"] as? [NSDictionary] ?? []
@@ -93,6 +103,8 @@ class PostActionMenuController: WKInterfaceController {
93103

94104
var comments = [NSDictionary]()
95105
func beginLoadingTable() {
106+
WKInterfaceDevice.current().play(.success)
107+
96108
commentTable.insertRows(at: IndexSet(integersIn: 0 ..< comments.count), withRowType: "CommentsRowController")
97109

98110
if comments.count > 0 {

Slide for Apple Watch Extension/SubmissionRowController.swift

+13-5
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ import WatchKit
1414
public class SubmissionRowController: NSObject {
1515

1616
var titleText: NSAttributedString?
17-
var parent: InterfaceController?
1817
var thumbnail: UIImage?
1918
var largeimage: UIImage?
2019
var id: String?
2120
var sub: String?
2221
var scoreText: String!
2322
var commentText: String!
23+
var dictionary: NSDictionary!
2424

2525
@IBOutlet weak var imageGroup: WKInterfaceGroup!
2626
@IBOutlet var bannerImage: WKInterfaceImage!
@@ -34,20 +34,25 @@ public class SubmissionRowController: NSObject {
3434
@IBOutlet var upvote: WKInterfaceButton!
3535
@IBOutlet var downvote: WKInterfaceButton!
3636
@IBOutlet var readlater: WKInterfaceButton!
37+
3738
@IBAction func didUpvote() {
39+
(WKExtension.shared().visibleInterfaceController as? Votable)?.sharedUp = upvote
40+
(WKExtension.shared().visibleInterfaceController as? Votable)?.sharedDown = downvote
41+
(WKExtension.shared().visibleInterfaceController as? Votable)?.doVote(id: id!, upvote: true, downvote: false)
3842
}
3943
@IBAction func didDownvote() {
40-
}
41-
@IBAction func didSaveLater() {
44+
(WKExtension.shared().visibleInterfaceController as? Votable)?.sharedUp = upvote
45+
(WKExtension.shared().visibleInterfaceController as? Votable)?.sharedDown = downvote
46+
(WKExtension.shared().visibleInterfaceController as? Votable)?.doVote(id: id!, upvote: false, downvote: true)
4247
}
4348

44-
@IBAction func didSelect() {
45-
self.parent?.presentController(withName: "DetailView", context: self)
49+
@IBAction func didSaveLater() {
4650
}
4751

4852
func setData(dictionary: NSDictionary, color: UIColor) {
4953
largeimage = nil
5054
thumbnail = nil
55+
self.dictionary = dictionary
5156

5257
let titleFont = UIFont.systemFont(ofSize: 14)
5358
let subtitleFont = UIFont.boldSystemFont(ofSize: 10)
@@ -215,6 +220,9 @@ public class SubmissionRowController: NSObject {
215220
}
216221
}).resume()
217222
}
223+
} else if type == .SELF || type == .NONE {
224+
thumbGroup.setHidden(true)
225+
bigImage.setHidden(true)
218226
} else {
219227
if dictionary["spoiler"] as? Bool ?? false {
220228
self.bannerImage.setImage(UIImage(named: "reports")?.getCopy(withSize: CGSize(width: 25, height: 25)))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//
2+
// Votable.swift
3+
// Slide for Apple Watch Extension
4+
//
5+
// Created by Carlos Crane on 3/5/20.
6+
// Copyright © 2020 Haptic Apps. All rights reserved.
7+
//
8+
9+
import Foundation
10+
import WatchConnectivity
11+
import WatchKit
12+
13+
class Votable: WKInterfaceController {
14+
weak var sharedUp: WKInterfaceButton?
15+
weak var sharedDown: WKInterfaceButton?
16+
func doVote(id: String, upvote: Bool, downvote: Bool) {
17+
print("Doing vote")
18+
WCSession.default.sendMessage(["vote": id, "upvote": upvote, "downvote": downvote], replyHandler: { (result) in
19+
if result["failed"] == nil {
20+
WKInterfaceDevice.current().play(.success)
21+
self.sharedUp?.setBackgroundColor((result["upvoted"] ?? false) as! Bool ? UIColor.init(hexString: "#FF5700") : UIColor.gray)
22+
self.sharedDown?.setBackgroundColor((result["downvoted"] ?? false) as! Bool ? UIColor.init(hexString: "#9494FF") : UIColor.gray)
23+
} else {
24+
WKInterfaceDevice.current().play(.failure)
25+
}
26+
}, errorHandler: { (error) in
27+
print(error)
28+
})
29+
}
30+
}

0 commit comments

Comments
 (0)