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

667 lines
29 KiB
Swift
Raw Normal View History

2018-05-01 19:39:48 +02:00
//
// Copyright (c) 2018 Open Whisper Systems. All rights reserved.
//
import Foundation
import SignalServiceKit
import SignalMessaging
import Reachability
2018-05-01 21:53:51 +02:00
import ContactsUI
2018-05-02 16:08:47 +02:00
import MessageUI
2018-05-01 19:39:48 +02:00
class ContactViewController: OWSViewController, ContactShareViewHelperDelegate {
2018-05-01 19:39:48 +02:00
enum ContactViewMode {
case systemContactWithSignal,
systemContactWithoutSignal,
2018-05-01 21:53:51 +02:00
nonSystemContact,
2018-05-01 19:39:48 +02:00
noPhoneNumber,
unknown
}
private var hasLoadedView = false
private var viewMode = ContactViewMode.unknown {
didSet {
SwiftAssertIsOnMainThread(#function)
if oldValue != viewMode && hasLoadedView {
updateContent()
}
}
}
private let contactsManager: OWSContactsManager
2018-05-01 19:39:48 +02:00
private var reachability: Reachability?
2018-05-01 19:39:48 +02:00
2018-05-05 04:32:29 +02:00
private let contactShare: ContactShareViewModel
2018-05-01 19:39:48 +02:00
private var contactShareViewHelper: ContactShareViewHelper
private weak var postDismissNavigationController: UINavigationController?
2018-05-01 19:39:48 +02:00
// MARK: - Initializers
@available(*, unavailable, message: "use init(call:) constructor instead.")
required init?(coder aDecoder: NSCoder) {
fatalError("Unimplemented")
}
required init(contactShare: ContactShareViewModel) {
2018-05-01 19:39:48 +02:00
contactsManager = Environment.current().contactsManager
2018-05-05 04:32:29 +02:00
self.contactShare = contactShare
self.contactShareViewHelper = ContactShareViewHelper(contactsManager: contactsManager)
2018-05-01 19:39:48 +02:00
super.init(nibName: nil, bundle: nil)
contactShareViewHelper.delegate = self
2018-05-03 20:31:11 +02:00
updateMode()
2018-05-01 19:39:48 +02:00
NotificationCenter.default.addObserver(forName: .OWSContactsManagerSignalAccountsDidChange, object: nil, queue: nil) { [weak self] _ in
guard let strongSelf = self else { return }
2018-05-03 20:31:11 +02:00
strongSelf.updateMode()
2018-05-01 19:39:48 +02:00
}
reachability = Reachability.forInternetConnection()
NotificationCenter.default.addObserver(forName: .reachabilityChanged, object: nil, queue: nil) { [weak self] _ in
guard let strongSelf = self else { return }
2018-05-03 20:31:11 +02:00
strongSelf.updateMode()
2018-05-01 19:39:48 +02:00
}
}
// MARK: - View Lifecycle
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
2018-05-01 21:53:51 +02:00
UIUtil.applySignalAppearence()
guard let navigationController = self.navigationController else {
owsFail("\(logTag) in \(#function) navigationController was unexpectedly nil")
return
}
navigationController.isNavigationBarHidden = true
// self.navigationController is nil in viewDidDisappear when transition via message/call buttons
// so we maintain our own reference.
self.postDismissNavigationController = navigationController
2018-05-01 19:39:48 +02:00
contactsManager.requestSystemContactsOnce(completion: { [weak self] _ in
guard let strongSelf = self else { return }
2018-05-03 20:31:11 +02:00
strongSelf.updateMode()
2018-05-01 19:39:48 +02:00
})
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
2018-05-01 21:53:51 +02:00
UIUtil.applySignalAppearence()
2018-05-01 19:39:48 +02:00
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
guard let strongNavigationController = postDismissNavigationController else {
owsFail("\(self.logTag) in \(#function) navigationController was unexpectedly nil")
return
}
strongNavigationController.isNavigationBarHidden = false
}
2018-05-01 21:53:51 +02:00
2018-05-01 19:39:48 +02:00
override func loadView() {
super.loadView()
2018-05-01 21:53:51 +02:00
self.view.preservesSuperviewLayoutMargins = false
2018-05-04 16:30:49 +02:00
self.view.backgroundColor = heroBackgroundColor()
2018-05-01 19:39:48 +02:00
updateContent()
hasLoadedView = true
}
2018-05-03 20:31:11 +02:00
private func updateMode() {
2018-05-01 19:39:48 +02:00
SwiftAssertIsOnMainThread(#function)
guard contactShare.e164PhoneNumbers().count > 0 else {
2018-05-01 19:39:48 +02:00
viewMode = .noPhoneNumber
return
}
2018-05-02 16:08:47 +02:00
if systemContactsWithSignalAccountsForContact().count > 0 {
2018-05-01 19:39:48 +02:00
viewMode = .systemContactWithSignal
return
}
2018-05-02 16:08:47 +02:00
if systemContactsForContact().count > 0 {
2018-05-01 19:39:48 +02:00
viewMode = .systemContactWithoutSignal
return
}
2018-05-01 21:53:51 +02:00
viewMode = .nonSystemContact
2018-05-01 19:39:48 +02:00
}
2018-05-02 16:08:47 +02:00
private func systemContactsWithSignalAccountsForContact() -> [String] {
SwiftAssertIsOnMainThread(#function)
return contactShare.systemContactsWithSignalAccountPhoneNumbers(contactsManager)
2018-05-02 16:08:47 +02:00
}
private func systemContactsForContact() -> [String] {
SwiftAssertIsOnMainThread(#function)
return contactShare.systemContactPhoneNumbers(contactsManager)
}
2018-05-01 19:39:48 +02:00
private func updateContent() {
SwiftAssertIsOnMainThread(#function)
guard let rootView = self.view else {
owsFail("\(logTag) missing root view.")
return
}
2018-05-01 21:53:51 +02:00
for subview in rootView.subviews {
2018-05-01 19:39:48 +02:00
subview.removeFromSuperview()
}
let topView = createTopView()
rootView.addSubview(topView)
topView.autoPin(toTopLayoutGuideOf: self, withInset: 0)
topView.autoPinWidthToSuperview()
// This view provides a background "below the fold".
let bottomView = UIView.container()
bottomView.backgroundColor = UIColor.white
self.view.addSubview(bottomView)
bottomView.layoutMargins = .zero
bottomView.autoPinWidthToSuperview()
bottomView.autoPinEdge(.top, to: .bottom, of: topView)
bottomView.autoPinEdge(toSuperviewEdge: .bottom)
let scrollView = UIScrollView()
scrollView.preservesSuperviewLayoutMargins = false
self.view.addSubview(scrollView)
scrollView.layoutMargins = .zero
scrollView.autoPinWidthToSuperview()
scrollView.autoPinEdge(.top, to: .bottom, of: topView)
scrollView.autoPinEdge(toSuperviewEdge: .bottom)
let fieldsView = createFieldsView()
scrollView.addSubview(fieldsView)
fieldsView.autoPinLeadingToSuperviewMargin()
fieldsView.autoPinTrailingToSuperviewMargin()
fieldsView.autoPinEdge(toSuperviewEdge: .top)
fieldsView.autoPinEdge(toSuperviewEdge: .bottom)
}
2018-05-04 16:30:49 +02:00
private func heroBackgroundColor() -> UIColor {
return UIColor(rgbHex: 0xefeff4)
}
private func createTopView() -> UIView {
SwiftAssertIsOnMainThread(#function)
2018-05-01 19:39:48 +02:00
let topView = UIView.container()
2018-05-04 16:30:49 +02:00
topView.backgroundColor = heroBackgroundColor()
topView.preservesSuperviewLayoutMargins = false
// Back Button
let backButtonSize = CGFloat(50)
let backButton = TappableView(actionBlock: { [weak self] _ in
guard let strongSelf = self else { return }
strongSelf.didPressDismiss()
})
backButton.autoSetDimension(.width, toSize: backButtonSize)
backButton.autoSetDimension(.height, toSize: backButtonSize)
topView.addSubview(backButton)
backButton.autoPin(toTopLayoutGuideOf: self, withInset: 0)
backButton.autoPinLeadingToSuperviewMargin()
let backIconName = (self.view.isRTL() ? "system_disclosure_indicator" : "system_disclosure_indicator_rtl")
2018-05-04 19:34:11 +02:00
guard let backIconImage = UIImage(named: backIconName) else {
owsFail("\(logTag) missing icon.")
return topView
}
let backIconView = UIImageView(image: backIconImage.withRenderingMode(.alwaysTemplate))
backIconView.contentMode = .scaleAspectFit
backIconView.tintColor = UIColor.black.withAlphaComponent(0.6)
backButton.addSubview(backIconView)
backIconView.autoCenterInSuperview()
2018-05-01 19:39:48 +02:00
let avatarSize: CGFloat = 100
let avatarView = AvatarImageView()
avatarView.image = contactShare.getAvatarImage(diameter: avatarSize, contactsManager: contactsManager)
2018-05-01 19:39:48 +02:00
topView.addSubview(avatarView)
2018-05-02 16:08:47 +02:00
avatarView.autoPin(toTopLayoutGuideOf: self, withInset: 20)
2018-05-01 19:39:48 +02:00
avatarView.autoHCenterInSuperview()
avatarView.autoSetDimension(.width, toSize: avatarSize)
avatarView.autoSetDimension(.height, toSize: avatarSize)
let nameLabel = UILabel()
2018-05-05 04:32:29 +02:00
nameLabel.text = contactShare.displayName
2018-05-04 19:06:26 +02:00
nameLabel.font = UIFont.ows_dynamicTypeTitle1
2018-05-01 19:39:48 +02:00
nameLabel.textColor = UIColor.black
nameLabel.lineBreakMode = .byTruncatingTail
nameLabel.textAlignment = .center
topView.addSubview(nameLabel)
nameLabel.autoPinEdge(.top, to: .bottom, of: avatarView, withOffset: 10)
2018-05-02 16:08:47 +02:00
nameLabel.autoPinLeadingToSuperviewMargin(withInset: hMargin)
nameLabel.autoPinTrailingToSuperviewMargin(withInset: hMargin)
2018-05-01 19:39:48 +02:00
var lastView: UIView = nameLabel
2018-05-04 19:06:26 +02:00
for phoneNumber in systemContactsWithSignalAccountsForContact() {
2018-05-01 19:39:48 +02:00
let phoneNumberLabel = UILabel()
2018-05-04 19:06:26 +02:00
phoneNumberLabel.text = PhoneNumber.bestEffortLocalizedPhoneNumber(withE164: phoneNumber)
phoneNumberLabel.font = UIFont.ows_dynamicTypeFootnote
2018-05-01 19:39:48 +02:00
phoneNumberLabel.textColor = UIColor.black
phoneNumberLabel.lineBreakMode = .byTruncatingTail
phoneNumberLabel.textAlignment = .center
topView.addSubview(phoneNumberLabel)
2018-05-01 21:53:51 +02:00
phoneNumberLabel.autoPinEdge(.top, to: .bottom, of: lastView, withOffset: 5)
2018-05-02 16:08:47 +02:00
phoneNumberLabel.autoPinLeadingToSuperviewMargin(withInset: hMargin)
phoneNumberLabel.autoPinTrailingToSuperviewMargin(withInset: hMargin)
2018-05-01 19:39:48 +02:00
lastView = phoneNumberLabel
}
switch viewMode {
case .systemContactWithSignal:
2018-05-01 21:53:51 +02:00
// Show actions buttons for system contacts with a Signal account.
2018-05-02 16:08:47 +02:00
let stackView = UIStackView()
stackView.axis = .horizontal
stackView.distribution = .fillEqually
stackView.addArrangedSubview(createCircleActionButton(text: NSLocalizedString("ACTION_SEND_MESSAGE",
comment: "Label for 'sent message' button in contact view."),
2018-05-08 21:52:19 +02:00
imageName: "contact_view_message",
2018-05-02 16:08:47 +02:00
actionBlock: { [weak self] _ in
guard let strongSelf = self else { return }
strongSelf.didPressSendMessage()
}))
stackView.addArrangedSubview(createCircleActionButton(text: NSLocalizedString("ACTION_AUDIO_CALL",
comment: "Label for 'audio call' button in contact view."),
2018-05-08 21:52:19 +02:00
imageName: "contact_view_audio_call",
2018-05-02 16:08:47 +02:00
actionBlock: { [weak self] _ in
guard let strongSelf = self else { return }
strongSelf.didPressAudioCall()
}))
stackView.addArrangedSubview(createCircleActionButton(text: NSLocalizedString("ACTION_VIDEO_CALL",
comment: "Label for 'video call' button in contact view."),
2018-05-08 21:52:19 +02:00
imageName: "contact_view_video_call",
2018-05-02 16:08:47 +02:00
actionBlock: { [weak self] _ in
guard let strongSelf = self else { return }
strongSelf.didPressVideoCall()
}))
topView.addSubview(stackView)
stackView.autoPinEdge(.top, to: .bottom, of: lastView, withOffset: 20)
stackView.autoPinLeadingToSuperviewMargin(withInset: hMargin)
stackView.autoPinTrailingToSuperviewMargin(withInset: hMargin)
lastView = stackView
2018-05-01 19:39:48 +02:00
case .systemContactWithoutSignal:
2018-05-01 21:53:51 +02:00
// Show invite button for system contacts without a Signal account.
2018-05-02 16:08:47 +02:00
let inviteButton = createLargePillButton(text: NSLocalizedString("ACTION_INVITE",
2018-05-04 19:06:26 +02:00
comment: "Label for 'invite' button in contact view."),
2018-05-02 16:08:47 +02:00
actionBlock: { [weak self] _ in
guard let strongSelf = self else { return }
strongSelf.didPressInvite()
})
topView.addSubview(inviteButton)
inviteButton.autoPinEdge(.top, to: .bottom, of: lastView, withOffset: 20)
inviteButton.autoPinLeadingToSuperviewMargin(withInset: 55)
inviteButton.autoPinTrailingToSuperviewMargin(withInset: 55)
lastView = inviteButton
2018-05-01 21:53:51 +02:00
case .nonSystemContact:
2018-05-04 19:06:26 +02:00
// Show "add to contacts" button for non-system contacts.
let addToContactsButton = createLargePillButton(text: NSLocalizedString("CONVERSATION_VIEW_ADD_TO_CONTACTS_OFFER",
comment: "Message shown in conversation view that offers to add an unknown user to your phone's contacts."),
actionBlock: { [weak self] _ in
guard let strongSelf = self else { return }
strongSelf.didPressAddToContacts()
})
topView.addSubview(addToContactsButton)
addToContactsButton.autoPinEdge(.top, to: .bottom, of: lastView, withOffset: 20)
addToContactsButton.autoPinLeadingToSuperviewMargin(withInset: 55)
addToContactsButton.autoPinTrailingToSuperviewMargin(withInset: 55)
lastView = addToContactsButton
2018-05-01 19:39:48 +02:00
break
case .noPhoneNumber:
2018-05-01 21:53:51 +02:00
// Show no action buttons for contacts without a phone number.
2018-05-01 19:39:48 +02:00
break
case .unknown:
let activityIndicator = UIActivityIndicatorView(activityIndicatorStyle: .whiteLarge)
topView.addSubview(activityIndicator)
activityIndicator.autoPinEdge(.top, to: .bottom, of: lastView, withOffset: 10)
activityIndicator.autoHCenterInSuperview()
lastView = activityIndicator
break
}
2018-05-01 21:53:51 +02:00
lastView.autoPinEdge(toSuperviewEdge: .bottom, withInset: 15)
return topView
}
private func createFieldsView() -> UIView {
SwiftAssertIsOnMainThread(#function)
2018-05-04 19:57:29 +02:00
var rows = [UIView]()
2018-05-01 21:53:51 +02:00
// TODO: Not designed yet.
// if viewMode == .systemContactWithSignal ||
// viewMode == .systemContactWithoutSignal {
// addRow(createActionRow(labelText:NSLocalizedString("ACTION_SHARE_CONTACT",
// comment:"Label for 'share contact' button."),
// action:#selector(didPressShareContact)))
// }
2018-05-05 04:32:29 +02:00
for phoneNumber in contactShare.phoneNumbers {
rows.append(ContactFieldView.contactFieldView(forPhoneNumber: phoneNumber,
layoutMargins: UIEdgeInsets(top: 5, left: hMargin, bottom: 5, right: hMargin),
2018-05-09 20:26:51 +02:00
actionBlock: { [weak self] _ in
guard let strongSelf = self else { return }
strongSelf.didPressPhoneNumber(phoneNumber: phoneNumber)
2018-05-03 20:31:11 +02:00
}))
2018-05-01 21:53:51 +02:00
}
2018-05-05 04:32:29 +02:00
for email in contactShare.emails {
rows.append(ContactFieldView.contactFieldView(forEmail: email,
layoutMargins: UIEdgeInsets(top: 5, left: hMargin, bottom: 5, right: hMargin),
2018-05-09 20:26:51 +02:00
actionBlock: { [weak self] _ in
guard let strongSelf = self else { return }
strongSelf.didPressEmail(email: email)
2018-05-03 20:31:11 +02:00
}))
2018-05-01 21:53:51 +02:00
}
2018-05-04 19:06:26 +02:00
for address in contactShare.addresses {
rows.append(ContactFieldView.contactFieldView(forAddress: address,
layoutMargins: UIEdgeInsets(top: 5, left: hMargin, bottom: 5, right: hMargin),
actionBlock: { [weak self] _ in
guard let strongSelf = self else { return }
strongSelf.didPressAddress(address: address)
2018-05-04 19:06:26 +02:00
}))
}
2018-05-01 21:53:51 +02:00
2018-05-04 19:57:29 +02:00
return ContactFieldView(rows: rows, hMargin: hMargin)
2018-05-01 21:53:51 +02:00
}
2018-05-02 16:08:47 +02:00
private let hMargin = CGFloat(16)
2018-05-01 21:53:51 +02:00
private func createActionRow(labelText: String, action: Selector) -> UIView {
let row = UIView()
2018-05-02 16:08:47 +02:00
row.layoutMargins.left = 0
row.layoutMargins.right = 0
2018-05-01 21:53:51 +02:00
row.isUserInteractionEnabled = true
row.addGestureRecognizer(UITapGestureRecognizer(target: self, action: action))
2018-05-02 16:08:47 +02:00
2018-05-01 21:53:51 +02:00
let label = UILabel()
label.text = labelText
label.font = UIFont.ows_dynamicTypeBody
label.textColor = UIColor.ows_materialBlue
label.lineBreakMode = .byTruncatingTail
row.addSubview(label)
label.autoPinTopToSuperviewMargin()
label.autoPinBottomToSuperviewMargin()
label.autoPinLeadingToSuperviewMargin(withInset: hMargin)
label.autoPinTrailingToSuperviewMargin(withInset: hMargin)
2018-05-02 16:08:47 +02:00
2018-05-01 21:53:51 +02:00
return row
}
2018-05-02 16:08:47 +02:00
// TODO: Use real assets.
2018-05-08 21:52:19 +02:00
private func createCircleActionButton(text: String, imageName: String, actionBlock : @escaping () -> Void) -> UIView {
2018-05-02 16:08:47 +02:00
let buttonSize = CGFloat(50)
2018-05-01 19:39:48 +02:00
2018-05-02 16:08:47 +02:00
let button = TappableView(actionBlock: actionBlock)
button.layoutMargins = .zero
button.autoSetDimension(.width, toSize: buttonSize, relation: .greaterThanOrEqual)
let circleView = UIView()
circleView.backgroundColor = UIColor.white
circleView.autoSetDimension(.width, toSize: buttonSize)
circleView.autoSetDimension(.height, toSize: buttonSize)
circleView.layer.cornerRadius = buttonSize * 0.5
button.addSubview(circleView)
circleView.autoPinEdge(toSuperviewEdge: .top)
circleView.autoHCenterInSuperview()
2018-05-08 21:52:19 +02:00
guard let image = UIImage(named: imageName) else {
owsFail("\(logTag) missing image.")
return button
}
let imageView = UIImageView(image: image)
circleView.addSubview(imageView)
imageView.autoCenterInSuperview()
2018-05-02 16:08:47 +02:00
let label = UILabel()
label.text = text
label.font = UIFont.ows_dynamicTypeCaption2
label.textColor = UIColor.black
label.lineBreakMode = .byTruncatingTail
label.textAlignment = .center
button.addSubview(label)
label.autoPinEdge(.top, to: .bottom, of: circleView, withOffset: 3)
label.autoPinEdge(toSuperviewEdge: .bottom)
label.autoPinLeadingToSuperviewMargin()
label.autoPinTrailingToSuperviewMargin()
return button
}
private func createLargePillButton(text: String, actionBlock : @escaping () -> Void) -> UIView {
let button = TappableView(actionBlock: actionBlock)
button.backgroundColor = UIColor.white
button.layoutMargins = .zero
button.autoSetDimension(.height, toSize: 45)
button.layer.cornerRadius = 5
let label = UILabel()
label.text = text
2018-05-04 19:06:26 +02:00
label.font = UIFont.ows_dynamicTypeBody
2018-05-02 16:08:47 +02:00
label.textColor = UIColor.ows_materialBlue
label.lineBreakMode = .byTruncatingTail
label.textAlignment = .center
button.addSubview(label)
label.autoPinLeadingToSuperviewMargin(withInset: 20)
label.autoPinTrailingToSuperviewMargin(withInset: 20)
label.autoVCenterInSuperview()
label.autoPinEdge(toSuperviewEdge: .top, withInset: 0, relation: .greaterThanOrEqual)
label.autoPinEdge(toSuperviewEdge: .bottom, withInset: 0, relation: .greaterThanOrEqual)
return button
}
2018-05-01 21:53:51 +02:00
func didPressShareContact(sender: UIGestureRecognizer) {
2018-05-03 20:31:11 +02:00
Logger.info("\(logTag) \(#function)")
2018-05-01 21:53:51 +02:00
guard sender.state == .recognized else {
return
}
// TODO:
}
2018-05-02 16:08:47 +02:00
func didPressSendMessage() {
2018-05-03 20:31:11 +02:00
Logger.info("\(logTag) \(#function)")
2018-05-01 21:53:51 +02:00
self.contactShareViewHelper.sendMessage(contactShare: self.contactShare, fromViewController: self)
2018-05-02 16:08:47 +02:00
}
func didPressAudioCall() {
2018-05-03 20:31:11 +02:00
Logger.info("\(logTag) \(#function)")
2018-05-02 16:08:47 +02:00
self.contactShareViewHelper.audioCall(contactShare: self.contactShare, fromViewController: self)
2018-05-02 16:08:47 +02:00
}
func didPressVideoCall() {
2018-05-03 20:31:11 +02:00
Logger.info("\(logTag) \(#function)")
2018-05-02 16:08:47 +02:00
self.contactShareViewHelper.videoCall(contactShare: self.contactShare, fromViewController: self)
2018-05-02 16:08:47 +02:00
}
func didPressInvite() {
2018-05-03 20:31:11 +02:00
Logger.info("\(logTag) \(#function)")
2018-05-02 16:08:47 +02:00
self.contactShareViewHelper.inviteContact(contactShare: self.contactShare, fromViewController: self)
2018-05-02 16:08:47 +02:00
}
2018-05-01 21:53:51 +02:00
2018-05-04 19:06:26 +02:00
func didPressAddToContacts() {
Logger.info("\(logTag) \(#function)")
self.contactShareViewHelper.addToContacts(contactShare: self.contactShare, fromViewController: self)
}
func didPressDismiss() {
Logger.info("\(self.logTag) \(#function)")
guard let navigationController = self.navigationController else {
owsFail("\(logTag) in \(#function) navigationController was unexpectedly nil")
return
}
navigationController.popViewController(animated: true)
}
2018-05-09 20:26:51 +02:00
func didPressPhoneNumber(phoneNumber: OWSContactPhoneNumber) {
Logger.info("\(self.logTag) \(#function)")
let actionSheet = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
if let e164 = phoneNumber.tryToConvertToE164() {
if contactShare.systemContactsWithSignalAccountPhoneNumbers(contactsManager).contains(e164) {
actionSheet.addAction(UIAlertAction(title: NSLocalizedString("ACTION_SEND_MESSAGE",
comment: "Label for 'sent message' button in contact view."),
style: .default) { _ in
SignalApp.shared().presentConversation(forRecipientId: e164, action: .compose)
})
actionSheet.addAction(UIAlertAction(title: NSLocalizedString("ACTION_AUDIO_CALL",
comment: "Label for 'audio call' button in contact view."),
style: .default) { _ in
SignalApp.shared().presentConversation(forRecipientId: e164, action: .audioCall)
})
actionSheet.addAction(UIAlertAction(title: NSLocalizedString("ACTION_VIDEO_CALL",
comment: "Label for 'video call' button in contact view."),
style: .default) { _ in
SignalApp.shared().presentConversation(forRecipientId: e164, action: .videoCall)
})
} else {
// TODO: We could offer callPhoneNumberWithSystemCall.
}
}
actionSheet.addAction(UIAlertAction(title: NSLocalizedString("EDIT_ITEM_COPY_ACTION",
comment: "Short name for edit menu item to copy contents of media message."),
style: .default) { _ in
UIPasteboard.general.string = phoneNumber.phoneNumber
})
actionSheet.addAction(OWSAlerts.cancelAction)
present(actionSheet, animated: true)
}
func callPhoneNumberWithSystemCall(phoneNumber: OWSContactPhoneNumber) {
Logger.info("\(self.logTag) \(#function)")
guard let url = NSURL(string: "tel:\(phoneNumber.phoneNumber)") else {
owsFail("\(ContactViewController.logTag) could not open phone number.")
return
}
UIApplication.shared.openURL(url as URL)
}
func didPressEmail(email: OWSContactEmail) {
Logger.info("\(self.logTag) \(#function)")
let actionSheet = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
2018-05-09 20:36:05 +02:00
actionSheet.addAction(UIAlertAction(title: NSLocalizedString("CONTACT_VIEW_OPEN_EMAIL_IN_EMAIL_APP",
2018-05-09 20:26:51 +02:00
comment: "Label for 'open email in email app' button in contact view."),
style: .default) { [weak self] _ in
self?.openEmailInEmailApp(email: email)
})
actionSheet.addAction(UIAlertAction(title: NSLocalizedString("EDIT_ITEM_COPY_ACTION",
comment: "Short name for edit menu item to copy contents of media message."),
style: .default) { _ in
UIPasteboard.general.string = email.email
})
actionSheet.addAction(OWSAlerts.cancelAction)
present(actionSheet, animated: true)
}
func openEmailInEmailApp(email: OWSContactEmail) {
Logger.info("\(self.logTag) \(#function)")
guard let url = NSURL(string: "mailto:\(email.email)") else {
owsFail("\(ContactViewController.logTag) could not open email.")
return
}
UIApplication.shared.openURL(url as URL)
}
2018-05-04 19:06:26 +02:00
func didPressAddress(address: OWSContactAddress) {
Logger.info("\(self.logTag) \(#function)")
2018-05-09 20:26:51 +02:00
let actionSheet = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
actionSheet.addAction(UIAlertAction(title: NSLocalizedString("CONTACT_VIEW_OPEN_ADDRESS_IN_MAPS_APP",
comment: "Label for 'open address in maps app' button in contact view."),
style: .default) { [weak self] _ in
self?.openAddressInMaps(address: address)
})
actionSheet.addAction(UIAlertAction(title: NSLocalizedString("EDIT_ITEM_COPY_ACTION",
comment: "Short name for edit menu item to copy contents of media message."),
style: .default) { [weak self] _ in
guard let strongSelf = self else { return }
UIPasteboard.general.string = strongSelf.formatAddressForQuery(address: address)
})
actionSheet.addAction(OWSAlerts.cancelAction)
present(actionSheet, animated: true)
}
func openAddressInMaps(address: OWSContactAddress) {
Logger.info("\(self.logTag) \(#function)")
let mapAddress = formatAddressForQuery(address: address)
guard let escapedMapAddress = mapAddress.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) else {
owsFail("\(ContactViewController.logTag) could not open address.")
return
}
// Note that we use "q" (i.e. query) rather than "address" since we can't assume
// this is a well-formed address.
guard let url = URL(string: "http://maps.apple.com/?q=\(escapedMapAddress)") else {
owsFail("\(ContactViewController.logTag) could not open address.")
return
}
UIApplication.shared.openURL(url as URL)
}
func formatAddressForQuery(address: OWSContactAddress) -> String {
Logger.info("\(self.logTag) \(#function)")
2018-05-04 19:06:26 +02:00
// Open address in Apple Maps app.
var addressParts = [String]()
let addAddressPart: ((String?) -> Void) = { (part) in
guard let part = part else {
return
}
guard part.count > 0 else {
return
}
addressParts.append(part)
}
addAddressPart(address.street)
addAddressPart(address.neighborhood)
addAddressPart(address.city)
addAddressPart(address.region)
addAddressPart(address.postcode)
addAddressPart(address.country)
2018-05-09 20:26:51 +02:00
return addressParts.joined(separator: ", ")
2018-05-04 19:06:26 +02:00
}
// MARK: - ContactShareViewHelperDelegate
2018-05-01 21:53:51 +02:00
public func didCreateOrEditContact() {
2018-05-03 20:31:11 +02:00
Logger.info("\(logTag) \(#function)")
2018-05-01 21:53:51 +02:00
navigationController?.popToViewController(self, animated: true)
2018-05-01 21:53:51 +02:00
updateContent()
}
2018-05-01 19:39:48 +02:00
}