123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648 |
- //
- // ChatViewController.swift
- // deltachat-ios
- //
- // Created by Bastian van de Wetering on 08.11.17.
- // Copyright © 2017 Jonas Reinsch. All rights reserved.
- //
- import ALCameraViewController
- import MapKit
- import MessageInputBar
- import MessageKit
- import UIKit
- class ChatViewController: MessagesViewController {
- let outgoingAvatarOverlap: CGFloat = 17.5
- let loadCount = 30
- let chatId: Int
- let refreshControl = UIRefreshControl()
- var messageList: [MRMessage] = []
- var msgChangedObserver: Any?
- var incomingMsgObserver: Any?
- var disableWriting = false
- var previewView: UIView?
- init(chatId: Int, title: String? = nil) {
- self.chatId = chatId
- super.init(nibName: nil, bundle: nil)
- if let title = title {
- updateTitleView(title: title, subtitle: nil)
- }
- }
- @objc
- func loadMoreMessages() {
- DispatchQueue.global(qos: .userInitiated).asyncAfter(deadline: .now() + 1) {
- DispatchQueue.main.async {
- self.messageList = self.getMessageIds(self.loadCount, from: self.messageList.count) + self.messageList
- self.messagesCollectionView.reloadDataAndKeepOffset()
- self.refreshControl.endRefreshing()
- }
- }
- }
- func loadFirstMessages() {
- DispatchQueue.global(qos: .userInitiated).async {
- DispatchQueue.main.async {
- self.messageList = self.getMessageIds(self.loadCount)
- self.messagesCollectionView.reloadData()
- self.refreshControl.endRefreshing()
- self.messagesCollectionView.scrollToBottom(animated: false)
- }
- }
- }
- var textDraft: String? {
- // FIXME: need to free pointer
- if let draft = dc_get_draft(mailboxPointer, UInt32(chatId)) {
- if let text = dc_msg_get_text(draft) {
- let s = String(validatingUTF8: text)!
- return s
- }
- return nil
- }
- return nil
- }
- func getMessageIds(_ count: Int, from: Int? = nil) -> [MRMessage] {
- let c_messageIds = dc_get_chat_msgs(mailboxPointer, UInt32(chatId), 0, 0)
- let ids: [Int]
- if let from = from {
- ids = Utils.copyAndFreeArrayWithOffset(inputArray: c_messageIds, len: count, skipEnd: from)
- } else {
- ids = Utils.copyAndFreeArrayWithLen(inputArray: c_messageIds, len: count)
- }
- let markIds: [UInt32] = ids.map { return UInt32($0) }
- dc_markseen_msgs(mailboxPointer, UnsafePointer(markIds), Int32(ids.count))
- return ids.map {
- MRMessage(id: $0)
- }
- }
- required init?(coder _: NSCoder) {
- fatalError("init(coder:) has not been implemented")
- }
- override func viewWillAppear(_ animated: Bool) {
- super.viewWillAppear(animated)
- let cnt = Int(dc_get_fresh_msg_cnt(mailboxPointer, UInt32(chatId)))
- logger.info("updating count for chat \(cnt)")
- UIApplication.shared.applicationIconBadgeNumber = cnt
- if #available(iOS 11.0, *) {
- if disableWriting {
- navigationController?.navigationBar.prefersLargeTitles = true
- }
- }
- let nc = NotificationCenter.default
- msgChangedObserver = nc.addObserver(forName: dc_notificationChanged,
- object: nil, queue: OperationQueue.main) {
- notification in
- if let ui = notification.userInfo {
- if self.chatId == ui["chat_id"] as! Int {
- self.updateMessage(ui["message_id"] as! Int)
- }
- }
- }
- incomingMsgObserver = nc.addObserver(forName: dc_notificationIncoming,
- object: nil, queue: OperationQueue.main) {
- notification in
- if let ui = notification.userInfo {
- if self.chatId == ui["chat_id"] as! Int {
- let id = ui["message_id"] as! Int
- self.insertMessage(MRMessage(id: id))
- }
- }
- }
- }
- func setTextDraft() {
- if let text = self.messageInputBar.inputTextView.text {
- let draft = dc_msg_new(mailboxPointer, DC_MSG_TEXT)
- dc_msg_set_text(draft, text.cString(using: .utf8))
- dc_set_draft(mailboxPointer, UInt32(chatId), draft)
- // cleanup
- dc_msg_unref(draft)
- }
- }
- override func viewWillDisappear(_ animated: Bool) {
- super.viewWillDisappear(animated)
- if #available(iOS 11.0, *) {
- if disableWriting {
- navigationController?.navigationBar.prefersLargeTitles = false
- }
- }
- }
- override func viewDidDisappear(_ animated: Bool) {
- super.viewDidDisappear(animated)
- setTextDraft()
- let nc = NotificationCenter.default
- if let msgChangedObserver = self.msgChangedObserver {
- nc.removeObserver(msgChangedObserver)
- }
- if let incomingMsgObserver = self.incomingMsgObserver {
- nc.removeObserver(incomingMsgObserver)
- }
- }
- override var inputAccessoryView: UIView? {
- if disableWriting {
- return nil
- }
- return messageInputBar
- }
- override func viewDidLoad() {
- super.viewDidLoad()
- if !MRConfig.configured {
- // TODO: display message about nothing being configured
- return
- }
- let chat = MRChat(id: chatId)
- updateTitleView(title: chat.name, subtitle: chat.subtitle)
- configureMessageCollectionView()
- if !disableWriting {
- configureMessageInputBar()
- messageInputBar.inputTextView.text = textDraft
- messageInputBar.inputTextView.becomeFirstResponder()
- }
- loadFirstMessages()
- }
- func configureMessageCollectionView() {
- messagesCollectionView.messagesDataSource = self
- messagesCollectionView.messageCellDelegate = self
- scrollsToBottomOnKeyboardBeginsEditing = true // default false
- maintainPositionOnKeyboardFrameChanged = true // default false
- messagesCollectionView.addSubview(refreshControl)
- refreshControl.addTarget(self, action: #selector(loadMoreMessages), for: .valueChanged)
- let layout = messagesCollectionView.collectionViewLayout as? MessagesCollectionViewFlowLayout
- layout?.sectionInset = UIEdgeInsets(top: 1, left: 8, bottom: 1, right: 8)
- // Hide the outgoing avatar and adjust the label alignment to line up with the messages
- layout?.setMessageOutgoingAvatarSize(.zero)
- layout?.setMessageOutgoingMessageTopLabelAlignment(LabelAlignment(textAlignment: .right, textInsets: UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 8)))
- layout?.setMessageOutgoingMessageBottomLabelAlignment(LabelAlignment(textAlignment: .right, textInsets: UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 8)))
- // Set outgoing avatar to overlap with the message bubble
- layout?.setMessageIncomingMessageTopLabelAlignment(LabelAlignment(textAlignment: .left, textInsets: UIEdgeInsets(top: 0, left: 18, bottom: outgoingAvatarOverlap, right: 0)))
- layout?.setMessageIncomingAvatarSize(CGSize(width: 30, height: 30))
- layout?.setMessageIncomingMessagePadding(UIEdgeInsets(top: -outgoingAvatarOverlap, left: -18, bottom: outgoingAvatarOverlap, right: 18))
- layout?.setMessageIncomingAccessoryViewSize(CGSize(width: 30, height: 30))
- layout?.setMessageIncomingAccessoryViewPadding(HorizontalEdgeInsets(left: 8, right: 0))
- layout?.setMessageOutgoingAccessoryViewSize(CGSize(width: 30, height: 30))
- layout?.setMessageOutgoingAccessoryViewPadding(HorizontalEdgeInsets(left: 0, right: 8))
- messagesCollectionView.messagesLayoutDelegate = self
- messagesCollectionView.messagesDisplayDelegate = self
- }
- func configureMessageInputBar() {
- messageInputBar.delegate = self
- messageInputBar.inputTextView.tintColor = Constants.primaryColor
- messageInputBar.sendButton.tintColor = Constants.primaryColor
- messageInputBar.isTranslucent = true
- messageInputBar.separatorLine.isHidden = true
- messageInputBar.inputTextView.tintColor = Constants.primaryColor
- messageInputBar.delegate = self
- scrollsToBottomOnKeyboardBeginsEditing = true
- messageInputBar.inputTextView.backgroundColor = UIColor(red: 245 / 255, green: 245 / 255, blue: 245 / 255, alpha: 1)
- messageInputBar.inputTextView.placeholderTextColor = UIColor(red: 0.6, green: 0.6, blue: 0.6, alpha: 1)
- messageInputBar.inputTextView.textContainerInset = UIEdgeInsets(top: 8, left: 16, bottom: 8, right: 38)
- messageInputBar.inputTextView.placeholderLabelInsets = UIEdgeInsets(top: 8, left: 20, bottom: 8, right: 38)
- messageInputBar.inputTextView.layer.borderColor = UIColor(red: 200 / 255, green: 200 / 255, blue: 200 / 255, alpha: 1).cgColor
- messageInputBar.inputTextView.layer.borderWidth = 1.0
- messageInputBar.inputTextView.layer.cornerRadius = 16.0
- messageInputBar.inputTextView.layer.masksToBounds = true
- messageInputBar.inputTextView.scrollIndicatorInsets = UIEdgeInsets(top: 8, left: 0, bottom: 8, right: 0)
- configureInputBarItems()
- }
- private func configureInputBarItems() {
- messageInputBar.setLeftStackViewWidthConstant(to: 44, animated: false)
- messageInputBar.setRightStackViewWidthConstant(to: 36, animated: false)
- let sendButtonImage = UIImage(named: "paper_plane")?.withRenderingMode(.alwaysTemplate)
- messageInputBar.sendButton.image = sendButtonImage
- messageInputBar.sendButton.tintColor = UIColor(white: 1, alpha: 1)
- messageInputBar.sendButton.backgroundColor = UIColor(white: 0.9, alpha: 1)
- messageInputBar.sendButton.contentEdgeInsets = UIEdgeInsets(top: 6, left: 0, bottom: 6, right: 0)
- messageInputBar.sendButton.setSize(CGSize(width: 34, height: 34), animated: false)
- messageInputBar.sendButton.title = nil
- messageInputBar.sendButton.layer.cornerRadius = 18
- messageInputBar.textViewPadding.right = -40
- let leftItems = [
- InputBarButtonItem()
- .configure {
- $0.spacing = .fixed(0)
- $0.image = UIImage(named: "camera")?.withRenderingMode(.alwaysTemplate)
- $0.setSize(CGSize(width: 36, height: 36), animated: false)
- $0.tintColor = UIColor(white: 0.8, alpha: 1)
- }.onSelected {
- $0.tintColor = Constants.primaryColor
- }.onDeselected {
- $0.tintColor = UIColor(white: 0.8, alpha: 1)
- }.onTouchUpInside { _ in
- self.didPressPhotoButton()
- },
- ]
- messageInputBar.setStackViewItems(leftItems, forStack: .left, animated: false)
- // This just adds some more flare
- messageInputBar.sendButton
- .onEnabled { item in
- UIView.animate(withDuration: 0.3, animations: {
- item.backgroundColor = Constants.primaryColor
- })
- }.onDisabled { item in
- UIView.animate(withDuration: 0.3, animations: {
- item.backgroundColor = UIColor(white: 0.9, alpha: 1)
- })
- }
- }
- // MARK: - UICollectionViewDataSource
- public override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
- guard let messagesDataSource = messagesCollectionView.messagesDataSource else {
- fatalError("Ouch. nil data source for messages")
- }
- // guard !isSectionReservedForTypingBubble(indexPath.section) else {
- // return super.collectionView(collectionView, cellForItemAt: indexPath)
- // }
- let message = messagesDataSource.messageForItem(at: indexPath, in: messagesCollectionView)
- if case .custom = message.kind {
- let cell = messagesCollectionView.dequeueReusableCell(CustomCell.self, for: indexPath)
- cell.configure(with: message, at: indexPath, and: messagesCollectionView)
- return cell
- }
- return super.collectionView(collectionView, cellForItemAt: indexPath)
- }
- }
- // MARK: - MessagesDataSource
- extension ChatViewController: MessagesDataSource {
- func numberOfSections(in _: MessagesCollectionView) -> Int {
- return messageList.count
- }
- func currentSender() -> Sender {
- let currentSender = Sender(id: "1", displayName: "Alice")
- return currentSender
- }
- func messageForItem(at indexPath: IndexPath, in _: MessagesCollectionView) -> MessageType {
- return messageList[indexPath.section]
- }
- func avatar(for message: MessageType, at indexPath: IndexPath, in _: MessagesCollectionView) -> Avatar {
- let message = messageList[indexPath.section]
- let contact = message.fromContact
- return Avatar(image: contact.profileImage, initials: Utils.getInitials(inputName: contact.name))
- }
- func cellTopLabelAttributedText(for message: MessageType, at indexPath: IndexPath) -> NSAttributedString? {
- if isTimeLabelVisible(at: indexPath) {
- return NSAttributedString(string: MessageKitDateFormatter.shared.string(from: message.sentDate), attributes: [NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 10), NSAttributedString.Key.foregroundColor: UIColor.darkGray])
- }
- return nil
- }
- func messageTopLabelAttributedText(for message: MessageType, at indexPath: IndexPath) -> NSAttributedString? {
- if !isPreviousMessageSameSender(at: indexPath) {
- let name = message.sender.displayName
- return NSAttributedString(string: name, attributes: [NSAttributedString.Key.font: UIFont.preferredFont(forTextStyle: .caption1)])
- }
- return nil
- }
- func isTimeLabelVisible(at indexPath: IndexPath) -> Bool {
- // TODO: better heuristic when to show the time label
- return indexPath.section % 3 == 0 && !isPreviousMessageSameSender(at: indexPath)
- }
- func isPreviousMessageSameSender(at indexPath: IndexPath) -> Bool {
- guard indexPath.section - 1 >= 0 else { return false }
- return messageList[indexPath.section].fromContactId == messageList[indexPath.section - 1].fromContactId
- }
- func isInfoMessage(at indexPath: IndexPath) -> Bool {
- return messageList[indexPath.section].isInfo
- }
- func isNextMessageSameSender(at indexPath: IndexPath) -> Bool {
- guard indexPath.section + 1 < messageList.count else { return false }
- return messageList[indexPath.section].fromContactId == messageList[indexPath.section + 1].fromContactId
- }
- func messageBottomLabelAttributedText(for message: MessageType, at indexPath: IndexPath) -> NSAttributedString? {
- guard indexPath.section < messageList.count else { return nil }
- let m = messageList[indexPath.section]
- if !isNextMessageSameSender(at: indexPath), isFromCurrentSender(message: message) {
- return NSAttributedString(string: m.stateOutDescription(), attributes: [NSAttributedString.Key.font: UIFont.preferredFont(forTextStyle: .caption1)])
- }
- return nil
- }
- func updateMessage(_ messageId: Int) {
- if let index = messageList.firstIndex(where: { $0.id == messageId }) {
- dc_markseen_msgs(mailboxPointer, UnsafePointer([UInt32(messageId)]), 1)
- messageList[index] = MRMessage(id: messageId)
- // Reload section to update header/footer labels
- messagesCollectionView.performBatchUpdates({
- messagesCollectionView.reloadSections([index])
- if index > 0 {
- messagesCollectionView.reloadSections([index - 1])
- }
- if index < messageList.count - 1 {
- messagesCollectionView.reloadSections([index + 1])
- }
- }, completion: { [weak self] _ in
- if self?.isLastSectionVisible() == true {
- self?.messagesCollectionView.scrollToBottom(animated: true)
- }
- })
- } else {
- insertMessage(MRMessage(id: messageId))
- }
- }
- func insertMessage(_ message: MRMessage) {
- dc_markseen_msgs(mailboxPointer, UnsafePointer([UInt32(message.id)]), 1)
- messageList.append(message)
- // Reload last section to update header/footer labels and insert a new one
- messagesCollectionView.performBatchUpdates({
- messagesCollectionView.insertSections([messageList.count - 1])
- if messageList.count >= 2 {
- messagesCollectionView.reloadSections([messageList.count - 2])
- }
- }, completion: { [weak self] _ in
- if self?.isLastSectionVisible() == true {
- self?.messagesCollectionView.scrollToBottom(animated: true)
- }
- })
- }
- func isLastSectionVisible() -> Bool {
- guard !messageList.isEmpty else { return false }
- let lastIndexPath = IndexPath(item: 0, section: messageList.count - 1)
- return messagesCollectionView.indexPathsForVisibleItems.contains(lastIndexPath)
- }
- }
- // MARK: - MessagesDisplayDelegate
- extension ChatViewController: MessagesDisplayDelegate {
- // MARK: - Text Messages
- func textColor(for _: MessageType, at _: IndexPath, in _: MessagesCollectionView) -> UIColor {
- return .darkText
- }
- // MARK: - All Messages
- func backgroundColor(for message: MessageType, at _: IndexPath, in _: MessagesCollectionView) -> UIColor {
- return isFromCurrentSender(message: message) ? Constants.messagePrimaryColor : Constants.messageSecondaryColor
- }
- func messageStyle(for message: MessageType, at indexPath: IndexPath, in _: MessagesCollectionView) -> MessageStyle {
- if isInfoMessage(at: indexPath) {
- return .custom { view in
- view.style = .none
- view.backgroundColor = UIColor(alpha: 0, red: 0, green: 0, blue: 0)
- view.center.x = self.view.center.x
- }
- }
- var corners: UIRectCorner = []
- if isFromCurrentSender(message: message) {
- corners.formUnion(.topLeft)
- corners.formUnion(.bottomLeft)
- if !isPreviousMessageSameSender(at: indexPath) {
- corners.formUnion(.topRight)
- }
- if !isNextMessageSameSender(at: indexPath) {
- corners.formUnion(.bottomRight)
- }
- } else {
- corners.formUnion(.topRight)
- corners.formUnion(.bottomRight)
- if !isPreviousMessageSameSender(at: indexPath) {
- corners.formUnion(.topLeft)
- }
- if !isNextMessageSameSender(at: indexPath) {
- corners.formUnion(.bottomLeft)
- }
- }
- return .custom { view in
- let radius: CGFloat = 16
- let path = UIBezierPath(roundedRect: view.bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
- let mask = CAShapeLayer()
- mask.path = path.cgPath
- view.layer.mask = mask
- }
- }
- func configureAvatarView(_ avatarView: AvatarView, for message: MessageType, at indexPath: IndexPath, in _: MessagesCollectionView) {
- let message = messageList[indexPath.section]
- let contact = message.fromContact
- let avatar = Avatar(image: contact.profileImage, initials: Utils.getInitials(inputName: contact.name))
- avatarView.set(avatar: avatar)
- avatarView.isHidden = isNextMessageSameSender(at: indexPath) || message.isInfo
- }
- func enabledDetectors(for _: MessageType, at _: IndexPath, in _: MessagesCollectionView) -> [DetectorType] {
- return [.url, .date, .phoneNumber, .address]
- }
- }
- // MARK: - MessagesLayoutDelegate
- extension ChatViewController: MessagesLayoutDelegate {
- func cellTopLabelHeight(for _: MessageType, at indexPath: IndexPath, in _: MessagesCollectionView) -> CGFloat {
- if isTimeLabelVisible(at: indexPath) {
- return 18
- }
- return 0
- }
- func messageTopLabelHeight(for message: MessageType, at indexPath: IndexPath, in _: MessagesCollectionView) -> CGFloat {
- if isFromCurrentSender(message: message) {
- return !isPreviousMessageSameSender(at: indexPath) ? 20 : 0
- } else {
- return !isPreviousMessageSameSender(at: indexPath) ? (20 + outgoingAvatarOverlap) : 0
- }
- }
- func messageBottomLabelHeight(for message: MessageType, at indexPath: IndexPath, in _: MessagesCollectionView) -> CGFloat {
- return (!isNextMessageSameSender(at: indexPath) && isFromCurrentSender(message: message)) && !isInfoMessage(at: indexPath) ? 16 : 0
- }
- func heightForLocation(message _: MessageType, at _: IndexPath, with _: CGFloat, in _: MessagesCollectionView) -> CGFloat {
- return 40
- }
- func footerViewSize(for _: MessageType, at _: IndexPath, in messagesCollectionView: MessagesCollectionView) -> CGSize {
- return CGSize(width: messagesCollectionView.bounds.width, height: 10)
- }
- @objc func didPressPhotoButton() {
- if UIImagePickerController.isSourceTypeAvailable(.camera) {
- let cameraViewController = CameraViewController { [weak self] image, _ in
- self?.dismiss(animated: true, completion: nil)
- DispatchQueue.global().async {
- if let pickedImage = image {
- let width = Int32(exactly: pickedImage.size.width)!
- let height = Int32(exactly: pickedImage.size.height)!
- let path = Utils.saveImage(image: pickedImage)
- let msg = dc_msg_new(mailboxPointer, DC_MSG_IMAGE)
- dc_msg_set_file(msg, path, "image/jpeg")
- dc_msg_set_dimension(msg, width, height)
- dc_send_msg(mailboxPointer, UInt32(self!.chatId), msg)
- // cleanup
- dc_msg_unref(msg)
- }
- }
- }
- present(cameraViewController, animated: true, completion: nil)
- } else {
- let alert = UIAlertController(title: "Camera is not available", message: nil, preferredStyle: .alert)
- alert.addAction(UIAlertAction(title: "OK", style: .cancel, handler: { _ in
- self.dismiss(animated: true, completion: nil)
- }))
- present(alert, animated: true, completion: nil)
- }
- }
- }
- // MARK: - MessageCellDelegate
- extension ChatViewController: MessageCellDelegate {
- func didTapMessage(in _: MessageCollectionViewCell) {
- logger.info("Message tapped")
- }
- func didTapAvatar(in _: MessageCollectionViewCell) {
- logger.info("Avatar tapped")
- }
- @objc(didTapCellTopLabelIn:) func didTapCellTopLabel(in _: MessageCollectionViewCell) {
- logger.info("Top label tapped")
- }
- func didTapBottomLabel(in _: MessageCollectionViewCell) {
- print("Bottom label tapped")
- }
- }
- // MARK: - MessageLabelDelegate
- extension ChatViewController: MessageLabelDelegate {
- func didSelectAddress(_ addressComponents: [String: String]) {
- let mapAddress = Utils.formatAddressForQuery(address: addressComponents)
- if let escapedMapAddress = mapAddress.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) {
- // Use query, to handle malformed addresses
- if let url = URL(string: "http://maps.apple.com/?q=\(escapedMapAddress)") {
- UIApplication.shared.open(url as URL)
- }
- }
- }
- func didSelectDate(_ date: Date) {
- let interval = date.timeIntervalSinceReferenceDate
- if let url = NSURL(string: "calshow:\(interval)") {
- UIApplication.shared.open(url as URL)
- }
- }
- func didSelectPhoneNumber(_ phoneNumber: String) {
- logger.info("phone open", phoneNumber)
- if let escapedPhoneNumber = phoneNumber.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) {
- if let url = NSURL(string: "tel:\(escapedPhoneNumber)") {
- UIApplication.shared.open(url as URL)
- }
- }
- }
- func didSelectURL(_ url: URL) {
- UIApplication.shared.open(url)
- }
- }
- // MARK: - LocationMessageDisplayDelegate
- /*
- extension ChatViewController: LocationMessageDisplayDelegate {
- func annotationViewForLocation(message: MessageType, at indexPath: IndexPath, in messageCollectionView: MessagesCollectionView) -> MKAnnotationView? {
- let annotationView = MKAnnotationView(annotation: nil, reuseIdentifier: nil)
- let pinImage = #imageLiteral(resourceName: "ic_block_36pt").withRenderingMode(.alwaysTemplate)
- annotationView.image = pinImage
- annotationView.centerOffset = CGPoint(x: 0, y: -pinImage.size.height / 2)
- return annotationView
- }
- func animationBlockForLocation(message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> ((UIImageView) -> Void)? {
- return { view in
- view.layer.transform = CATransform3DMakeScale(0, 0, 0)
- view.alpha = 0.0
- UIView.animate(withDuration: 0.6, delay: 0, usingSpringWithDamping: 0.9, initialSpringVelocity: 0, options: [], animations: {
- view.layer.transform = CATransform3DIdentity
- view.alpha = 1.0
- }, completion: nil)
- }
- }
- }
- */
- // MARK: - MessageInputBarDelegate
- extension ChatViewController: MessageInputBarDelegate {
- func messageInputBar(_ inputBar: MessageInputBar, didPressSendButtonWith text: String) {
- DispatchQueue.global().async {
- dc_send_text_msg(mailboxPointer, UInt32(self.chatId), text)
- }
- inputBar.inputTextView.text = String()
- }
- }
|