|
|
@@ -11,7 +11,7 @@ import SnapKit
|
|
|
|
|
|
|
|
|
class LNFeedCommentInputPanel: LNPopupView {
|
|
|
- private let inputField = LNAutoSizeTextView()
|
|
|
+ private let textView = LNAutoSizeTextView()
|
|
|
private var hideSend: Constraint?
|
|
|
private let sendView = UIView()
|
|
|
var handler: ((String) -> Void)?
|
|
|
@@ -25,7 +25,7 @@ class LNFeedCommentInputPanel: LNPopupView {
|
|
|
override func popup(_ targetView: UIView? = nil) {
|
|
|
super.popup(targetView)
|
|
|
|
|
|
- inputField.becomeFirstResponder()
|
|
|
+ textView.becomeFirstResponder()
|
|
|
}
|
|
|
|
|
|
required init?(coder: NSCoder) {
|
|
|
@@ -33,8 +33,20 @@ class LNFeedCommentInputPanel: LNPopupView {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+extension LNFeedCommentInputPanel {
|
|
|
+ private func toSendComment() {
|
|
|
+ dismiss()
|
|
|
+ let cleanedText = (textView.text ?? "").replacingOccurrences(of: "\n", with: "").trimmingCharacters(in: .whitespacesAndNewlines)
|
|
|
+ guard !cleanedText.isEmpty else { return }
|
|
|
+ handler?(cleanedText)
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
extension LNFeedCommentInputPanel: UITextViewDelegate {
|
|
|
func textViewDidChange(_ textView: UITextView) {
|
|
|
+ if textView.text.count > LNFeedManager.feedCommentMaxInput {
|
|
|
+ textView.text = String(textView.text.prefix(LNFeedManager.feedCommentMaxInput))
|
|
|
+ }
|
|
|
let showSend = !textView.text.isEmpty
|
|
|
hideSend?.update(priority: showSend ? .low : .high)
|
|
|
UIView.animate(withDuration: 0.2) { [weak self] in
|
|
|
@@ -43,16 +55,21 @@ extension LNFeedCommentInputPanel: UITextViewDelegate {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
|
|
|
- let currentText = textField.text ?? ""
|
|
|
-
|
|
|
- guard let range = Range(range, in: currentText) else { return false }
|
|
|
- let newText = currentText.replacingCharacters(in: range, with: string)
|
|
|
- if newText.count < currentText.count {
|
|
|
+ func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
|
|
|
+ if text == "\n" {
|
|
|
+ toSendComment()
|
|
|
+ return false
|
|
|
+ }
|
|
|
+ let currentText = textView.text ?? ""
|
|
|
+ guard let swiftRange = Range(range, in: currentText) else {
|
|
|
return true
|
|
|
}
|
|
|
+ let newText = currentText.replacingCharacters(in: swiftRange, with: text)
|
|
|
|
|
|
- return newText.count <= LNFeedManager.feedCommentMaxInput
|
|
|
+ if newText.count > LNFeedManager.feedCommentMaxInput {
|
|
|
+ return false
|
|
|
+ }
|
|
|
+ return true
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -82,12 +99,13 @@ extension LNFeedCommentInputPanel {
|
|
|
hideSend = make.trailing.equalToSuperview().offset(-12).priority(.high).constraint
|
|
|
}
|
|
|
|
|
|
- inputField.backgroundColor = .clear
|
|
|
- inputField.delegate = self
|
|
|
- inputField.font = .body_m
|
|
|
- inputField.textColor = .text_5
|
|
|
- holder.addSubview(inputField)
|
|
|
- inputField.snp.makeConstraints { make in
|
|
|
+ textView.backgroundColor = .clear
|
|
|
+ textView.delegate = self
|
|
|
+ textView.font = .body_m
|
|
|
+ textView.textColor = .text_5
|
|
|
+ textView.returnKeyType = .send
|
|
|
+ holder.addSubview(textView)
|
|
|
+ textView.snp.makeConstraints { make in
|
|
|
make.horizontalEdges.equalToSuperview().inset(10)
|
|
|
make.centerY.equalToSuperview()
|
|
|
make.top.greaterThanOrEqualToSuperview()
|
|
|
@@ -110,9 +128,7 @@ extension LNFeedCommentInputPanel {
|
|
|
sendButton.contentEdgeInsets = .init(top: 0, left: 16, bottom: 0, right: 16)
|
|
|
sendButton.addAction(UIAction(handler: { [weak self] _ in
|
|
|
guard let self else { return }
|
|
|
- guard let text = inputField.text else { return }
|
|
|
- dismiss()
|
|
|
- handler?(text.trimmingCharacters(in: .whitespacesAndNewlines))
|
|
|
+ toSendComment()
|
|
|
}), for: .touchUpInside)
|
|
|
sendView.addSubview(sendButton)
|
|
|
sendButton.snp.makeConstraints { make in
|