session-ios/Signal/src/ViewControllers/LongTextViewController.swift

172 lines
5.8 KiB
Swift
Raw Normal View History

//
// Copyright (c) 2019 Open Whisper Systems. All rights reserved.
//
import Foundation
import SignalServiceKit
import SignalMessaging
@objc
public protocol LongTextViewDelegate {
@objc
func longTextViewMessageWasDeleted(_ longTextViewController: LongTextViewController)
}
@objc
public class LongTextViewController: OWSViewController {
// MARK: - Dependencies
var uiDatabaseConnection: YapDatabaseConnection {
return OWSPrimaryStorage.shared().uiDatabaseConnection
}
// MARK: - Properties
@objc
weak var delegate: LongTextViewDelegate?
let viewItem: ConversationViewItem
2018-08-22 21:38:17 +02:00
var messageTextView: UITextView!
2019-03-15 02:24:15 +01:00
var displayableText: DisplayableText? {
return viewItem.displayableBodyText
}
var fullText: String {
return displayableText?.fullText ?? ""
}
// MARK: Initializers
@available(*, unavailable, message:"use other constructor instead.")
public required init?(coder aDecoder: NSCoder) {
2018-08-27 16:21:03 +02:00
notImplemented()
}
2018-05-25 23:28:36 +02:00
@objc
public required init(viewItem: ConversationViewItem) {
self.viewItem = viewItem
super.init(nibName: nil, bundle: nil)
}
// MARK: View Lifecycle
public override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.title = NSLocalizedString("LONG_TEXT_VIEW_TITLE",
comment: "Title for the 'long text message' view.")
createViews()
2018-08-22 21:38:17 +02:00
self.messageTextView.contentOffset = CGPoint(x: 0, y: self.messageTextView.contentInset.top)
NotificationCenter.default.addObserver(self,
selector: #selector(uiDatabaseDidUpdate),
name: .OWSUIDatabaseConnectionDidUpdate,
object: OWSPrimaryStorage.shared().dbNotificationObject)
}
// MARK: - DB
@objc internal func uiDatabaseDidUpdate(notification: NSNotification) {
AssertIsOnMainThread()
guard let notifications = notification.userInfo?[OWSUIDatabaseConnectionNotificationsKey] as? [Notification] else {
owsFailDebug("notifications was unexpectedly nil")
return
}
guard let uniqueId = self.viewItem.interaction.uniqueId else {
Logger.error("Message is missing uniqueId.")
return
}
guard self.uiDatabaseConnection.hasChange(forKey: uniqueId,
inCollection: TSInteraction.collection(),
in: notifications) else {
Logger.debug("No relevant changes.")
return
}
do {
try uiDatabaseConnection.read { transaction in
guard TSInteraction.fetch(uniqueId: uniqueId, transaction: transaction) != nil else {
Logger.error("Message was deleted")
throw LongTextViewError.messageWasDeleted
}
}
} catch LongTextViewError.messageWasDeleted {
DispatchQueue.main.async {
self.delegate?.longTextViewMessageWasDeleted(self)
}
} catch {
owsFailDebug("unexpected error: \(error)")
}
}
enum LongTextViewError: Error {
case messageWasDeleted
}
// MARK: - Create Views
private func createViews() {
2018-08-08 15:29:23 +02:00
view.backgroundColor = Theme.backgroundColor
2018-08-15 23:09:59 +02:00
let messageTextView = OWSTextView()
self.messageTextView = messageTextView
messageTextView.font = UIFont.ows_dynamicTypeBody
2018-08-08 15:29:23 +02:00
messageTextView.backgroundColor = Theme.backgroundColor
messageTextView.isOpaque = true
messageTextView.isEditable = false
messageTextView.isSelectable = true
messageTextView.isScrollEnabled = true
messageTextView.showsHorizontalScrollIndicator = false
messageTextView.showsVerticalScrollIndicator = true
messageTextView.isUserInteractionEnabled = true
2018-08-08 15:29:23 +02:00
messageTextView.textColor = Theme.primaryColor
2019-03-15 02:24:15 +01:00
if let displayableText = displayableText {
messageTextView.text = fullText
messageTextView.ensureShouldLinkifyText(displayableText.shouldAllowLinkification)
} else {
owsFailDebug("displayableText was unexpectedly nil")
messageTextView.text = ""
}
2019-03-30 15:35:53 +01:00
let linkTextAttributes: [NSAttributedString.Key: Any] = [
NSAttributedString.Key.foregroundColor: Theme.primaryColor,
NSAttributedString.Key.underlineColor: Theme.primaryColor,
NSAttributedString.Key.underlineStyle: NSUnderlineStyle.single.rawValue
2018-08-22 23:07:05 +02:00
]
2019-03-30 15:35:53 +01:00
messageTextView.linkTextAttributes = linkTextAttributes
2018-08-22 23:07:05 +02:00
view.addSubview(messageTextView)
2018-08-22 21:38:17 +02:00
messageTextView.autoPinEdge(toSuperviewEdge: .top)
messageTextView.autoPinEdge(toSuperviewEdge: .leading)
messageTextView.autoPinEdge(toSuperviewEdge: .trailing)
messageTextView.textContainerInset = UIEdgeInsets(top: 0, leading: 16, bottom: 0, trailing: 16)
let footer = UIToolbar()
view.addSubview(footer)
2018-08-22 21:38:17 +02:00
footer.autoPinWidthToSuperview()
footer.autoPinEdge(.top, to: .bottom, of: messageTextView)
footer.autoPin(toBottomLayoutGuideOf: self, withInset: 0)
footer.items = [
UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil),
UIBarButtonItem(barButtonSystemItem: .action, target: self, action: #selector(shareButtonPressed)),
UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
]
}
// MARK: - Actions
2018-05-25 18:54:25 +02:00
@objc func shareButtonPressed() {
2019-03-15 02:24:15 +01:00
AttachmentSharing.showShareUI(forText: fullText)
}
}