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 pathPostCommentViewController.swift
143 lines (115 loc) · 5.83 KB
/
PostCommentViewController.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
//
// PostCommentViewController.swift
// reddift
//
// Created by sonson on 2016/11/21.
// Copyright © 2016年 sonson. All rights reserved.
//
import reddift
import Foundation
let PostCommentViewControllerDidSendCommentName = Notification.Name(rawValue: "PostCommentViewControllerDidSendCommentName")
class PostCommentViewController: UIViewController, FromFieldViewDelegate {
let thing: Thing
let textView = UITextView(frame: CGRect.zero)
var bottomSpaceConstraint: NSLayoutConstraint?
let fromFieldView = FromFieldView(frame: CGRect.zero)
let fieldHeight = CGFloat(44)
static func controller(with thing: Thing) -> UINavigationController {
let controller = PostCommentViewController(with: thing)
let navigationController = UINavigationController(rootViewController: controller)
return navigationController
}
required init?(coder aDecoder: NSCoder) {
self.thing = Link(id: "")
super.init(coder: aDecoder)
}
init(with thing: Thing) {
self.thing = thing
super.init(nibName: nil, bundle: nil)
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
textView.contentOffset = CGPoint(x: 0, y: -108)
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
}
override func viewDidLoad() {
let views = [
"textView": textView,
"accountBaseView": fromFieldView
]
super.viewDidLoad()
self.navigationItem.leftBarButtonItem = UIBarButtonItem(image: #imageLiteral(resourceName: "backOnBarButton"), style: .plain, target: self, action: #selector(PostCommentViewController.close(sender:)))
self.navigationItem.rightBarButtonItem = UIBarButtonItem(image: #imageLiteral(resourceName: "send"), style: .plain, target: self, action: #selector(PostCommentViewController.send(sender:)))
self.view.addSubview(textView)
textView.addSubview(fromFieldView)
textView.translatesAutoresizingMaskIntoConstraints = false
self.view.backgroundColor = UIColor.white
fromFieldView.backgroundColor = UIColor.white
var contentInset = textView.contentInset
contentInset.top = fieldHeight
textView.contentInset = contentInset
fromFieldView.frame = CGRect(x: 0, y: -fieldHeight, width: self.view.frame.size.width, height: fieldHeight)
textView.setNeedsLayout()
textView.alwaysBounceVertical = true
self.view.addConstraints (
NSLayoutConstraint.constraints(withVisualFormat: "H:|-0-[textView]-0-|", options: NSLayoutFormatOptions(), metrics: nil, views: views)
)
let topConstraint = NSLayoutConstraint(item: self.view, attribute: .top, relatedBy: .equal, toItem: textView, attribute: .top, multiplier: 1, constant: 0)
let bottomSpaceConstraint = NSLayoutConstraint(item: self.view, attribute: .bottom, relatedBy: .equal, toItem: textView, attribute: .bottom, multiplier: 1, constant: 0)
self.bottomSpaceConstraint = bottomSpaceConstraint
self.view.addConstraint(topConstraint)
self.view.addConstraint(bottomSpaceConstraint)
NotificationCenter.default.addObserver(self, selector: #selector(PostCommentViewController.keyboardWillChangeFrame(notification:)), name: .UIKeyboardWillHide, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(PostCommentViewController.keyboardWillChangeFrame(notification:)), name: .UIKeyboardDidChangeFrame, object: nil)
let str = "test\n **bold** \n"
textView.text = str
setupAccountView()
fromFieldView.delegate = self
}
func didTapFromFieldView(sender: FromFieldView) {
if let s = UIApplication.shared.keyWindow?.rootViewController?.storyboard {
let nav = s.instantiateViewController(withIdentifier: "AccountListNavigationController")
self.present(nav, animated: true, completion: nil)
}
}
func setupAccountView() {
}
@objc func close(sender: Any) {
self.dismiss(animated: true, completion: nil)
}
@objc func send(sender: Any) {
do {
try UIApplication.appDelegate()?.session?.postComment(self.textView.text, parentName: thing.name, completion: { (result) in
switch result {
case .failure(let error):
print(error)
case .success(let comment):
print(comment)
DispatchQueue.main.async {
let userInfo: [String: Any] = [
"newComment": comment,
"parent": self.thing
]
self.dismiss(animated: true, completion: nil)
NotificationCenter.default.post(name: PostCommentViewControllerDidSendCommentName, object: nil, userInfo: userInfo)
}
}
})
} catch {
// let nserror = NSError(domain: "", code: 0, userInfo: [NSLocalizedDescriptionKey:"\(error)"])
// postNotification(name: LinkContainerCellarDidLoadName, userInfo: [LinkContainerCellar.errorKey: nserror, LinkContainerCellar.providerKey: self])
}
// self.dismiss(animated: true, completion: nil)
}
@objc func keyboardWillChangeFrame(notification: Notification) {
if let userInfo = notification.userInfo {
if let rect = userInfo[UIKeyboardFrameEndUserInfoKey] as? CGRect, let bottomSpaceConstraint = self.bottomSpaceConstraint {
print(rect)
let h = self.view.frame.size.height - rect.origin.y
bottomSpaceConstraint.constant = h
}
}
}
}