You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
137 lines
5.0 KiB
137 lines
5.0 KiB
//
|
|
// ComposeViewController.swift
|
|
// elpha-ios
|
|
//
|
|
// Created by Dwayne Harris on 11/12/18.
|
|
// Copyright © 2018 Elpha. All rights reserved.
|
|
//
|
|
|
|
import Kingfisher
|
|
import UIKit
|
|
|
|
class ComposeViewController: UIViewController {
|
|
@IBOutlet var replyToView: UIView!
|
|
@IBOutlet var replyToAvatarImageView: UIImageView!
|
|
@IBOutlet var replyToDisplayNameLabel: UILabel!
|
|
@IBOutlet var replyToUsernameLabel: UILabel!
|
|
@IBOutlet var replyToContentTextView: UITextViewFixed!
|
|
@IBOutlet var statusTextView: UITextView!
|
|
@IBOutlet var statusCharacterCountLabel: UILabel!
|
|
@IBOutlet var contentWarningTextField: UITextField!
|
|
@IBOutlet var bottomConstraint: NSLayoutConstraint!
|
|
|
|
let characterLimit = 500
|
|
let composeAccessoryViewHeight: CGFloat = 55.0
|
|
var feedbackGenerator: UINotificationFeedbackGenerator? = nil
|
|
var replyToStatus: StatusMO? = nil
|
|
var composeAccessoryView: ComposeAccessoryView? = nil
|
|
|
|
override func viewDidLoad() {
|
|
super.viewDidLoad()
|
|
|
|
feedbackGenerator = UINotificationFeedbackGenerator()
|
|
|
|
statusTextView.layer.cornerRadius = 10
|
|
statusTextView.layer.masksToBounds = true
|
|
|
|
replyToAvatarImageView.setRoundedCorners()
|
|
|
|
composeAccessoryView = ComposeAccessoryView(frame: CGRect(x: 0.0, y: 0.0, width: self.view.bounds.size.width, height: composeAccessoryViewHeight))
|
|
composeAccessoryView!.delegate = self
|
|
composeAccessoryView!.selectedVisibility = .public
|
|
|
|
statusTextView.delegate = self
|
|
statusTextView.inputAccessoryView = composeAccessoryView
|
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(keyboardNotification(notification:)), name: UIResponder.keyboardWillChangeFrameNotification, object: nil)
|
|
|
|
if let replyToStatus = replyToStatus {
|
|
replyToView.isHidden = false
|
|
|
|
if let reblog = replyToStatus.reblog {
|
|
self.setupReplyTo(status: reblog)
|
|
} else {
|
|
self.setupReplyTo(status: replyToStatus)
|
|
}
|
|
} else {
|
|
replyToView.isHidden = true
|
|
}
|
|
}
|
|
|
|
deinit {
|
|
NotificationCenter.default.removeObserver(self)
|
|
}
|
|
|
|
override func viewDidAppear(_ animated: Bool) {
|
|
super.viewDidAppear(animated)
|
|
statusTextView.becomeFirstResponder()
|
|
}
|
|
|
|
func setupReplyTo(status: StatusMO) {
|
|
if let account = status.account {
|
|
let options: KingfisherOptionsInfo = SettingsManager.automaticallyPlayGIFs ? [] : [.onlyLoadFirstFrame]
|
|
replyToAvatarImageView.kf.setImage(with: account.avatarURL!, options: options)
|
|
replyToDisplayNameLabel.text = account.displayName
|
|
replyToUsernameLabel.text = "@\(account.acct!)"
|
|
}
|
|
|
|
replyToContentTextView.attributedText = status.content?.htmlAttributed(size: 13.0)
|
|
}
|
|
|
|
@objc func keyboardNotification(notification: NSNotification) {
|
|
if let userInfo = notification.userInfo {
|
|
let endFrame = (userInfo[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue
|
|
let endFrameY = endFrame?.origin.y ?? 0
|
|
|
|
if endFrameY >= UIScreen.main.bounds.size.height {
|
|
bottomConstraint.constant = 0
|
|
} else {
|
|
bottomConstraint.constant = (endFrame?.size.height ?? 0.0) + 10
|
|
}
|
|
}
|
|
}
|
|
|
|
@IBAction func dismissTapped(_ sender: Any) {
|
|
dismiss(animated: true)
|
|
}
|
|
}
|
|
|
|
extension ComposeViewController: UITextViewDelegate {
|
|
func textViewDidChange(_ textView: UITextView) {
|
|
let characters = statusTextView.text!.count
|
|
statusCharacterCountLabel.text = String(characterLimit - characters)
|
|
}
|
|
}
|
|
|
|
extension ComposeViewController: ComposeAccessoryViewDelegate {
|
|
func attachmentTapped() {
|
|
AlertManager.shared.show(message: "Attachment support coming soon", category: .normal)
|
|
}
|
|
|
|
func visibilityTapped() {
|
|
print("Visibility tapped")
|
|
}
|
|
|
|
func tootTapped() {
|
|
if let content = statusTextView.text {
|
|
if !content.isEmpty && content.count <= characterLimit {
|
|
feedbackGenerator?.prepare()
|
|
|
|
MastodonAPI.postStatus(content: content, replyToID: replyToStatus?.id, spoilerText: contentWarningTextField.text) { error in
|
|
guard error == nil else {
|
|
self.feedbackGenerator?.notificationOccurred(.error)
|
|
AlertManager.shared.show(message: "Couldn't toot!", category: .error)
|
|
return
|
|
}
|
|
|
|
self.feedbackGenerator?.notificationOccurred(.success)
|
|
AlertManager.shared.show(message: "Toot!")
|
|
|
|
self.statusTextView.text = ""
|
|
self.contentWarningTextField.text = ""
|
|
self.dismiss(animated: true)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|