implement Open URL modal

pull/1023/head
Ryan ZHAO 1 year ago
parent 6d27ef0dfa
commit 5881b36384

@ -1183,28 +1183,31 @@ extension ConversationVC:
func openUrl(_ urlString: String) { func openUrl(_ urlString: String) {
guard let url: URL = URL(string: urlString) else { return } guard let url: URL = URL(string: urlString) else { return }
// URLs can be unsafe, so always ask the user whether they want to open one let modal: ConfirmationModal = ConfirmationModal(
let actionSheet: UIAlertController = UIAlertController( targetView: self.view,
title: "urlOpen".localized(), info: ConfirmationModal.Info(
message: "urlOpenDescription" title: "urlOpen".localized(),
.put(key: "url", value: url.absoluteString) body: .text(
.localized(), "urlOpenDescription"
preferredStyle: .actionSheet .put(key: "url", value: url.absoluteString)
.localized()
),
confirmTitle: "open".localized(),
confirmStyle: .danger,
cancelTitle: "urlCopy".localized(),
cancelStyle: .alert_text,
onConfirm: { [weak self] _ in
UIApplication.shared.open(url, options: [:], completionHandler: nil)
self?.showInputAccessoryView()
},
onCancel: { [weak self] _ in
UIPasteboard.general.string = url.absoluteString
self?.showInputAccessoryView()
}
)
) )
actionSheet.addAction(UIAlertAction(title: "open".localized(), style: .default) { [weak self] _ in
UIApplication.shared.open(url, options: [:], completionHandler: nil)
self?.showInputAccessoryView()
})
actionSheet.addAction(UIAlertAction(title: "urlCopy".localized(), style: .default) { [weak self] _ in
UIPasteboard.general.string = url.absoluteString
self?.showInputAccessoryView()
})
actionSheet.addAction(UIAlertAction(title: "cancel".localized(), style: .cancel) { [weak self] _ in
self?.showInputAccessoryView()
})
Modal.setupForIPadIfNeeded(actionSheet, targetView: self.view) self.present(modal, animated: true)
self.present(actionSheet, animated: true)
} }
func handleReplyButtonTapped(for cellViewModel: MessageViewModel, using dependencies: Dependencies) { func handleReplyButtonTapped(for cellViewModel: MessageViewModel, using dependencies: Dependencies) {

@ -47,6 +47,15 @@ public class ConfirmationModal: Modal, UITextFieldDelegate {
return result return result
}() }()
private lazy var explanationLabelContainer: UIScrollView = {
let result: UIScrollView = UIScrollView()
result.isHidden = true
return result
}()
private lazy var explanationLabelContainerHeightConstraint = explanationLabelContainer.set(.height, to: 0)
private lazy var explanationLabel: UILabel = { private lazy var explanationLabel: UILabel = {
let result: UILabel = UILabel() let result: UILabel = UILabel()
result.font = .systemFont(ofSize: Values.smallFontSize) result.font = .systemFont(ofSize: Values.smallFontSize)
@ -54,7 +63,6 @@ public class ConfirmationModal: Modal, UITextFieldDelegate {
result.textAlignment = .center result.textAlignment = .center
result.lineBreakMode = .byWordWrapping result.lineBreakMode = .byWordWrapping
result.numberOfLines = 0 result.numberOfLines = 0
result.isHidden = true
return result return result
}() }()
@ -107,7 +115,7 @@ public class ConfirmationModal: Modal, UITextFieldDelegate {
}() }()
private lazy var contentStackView: UIStackView = { private lazy var contentStackView: UIStackView = {
let result = UIStackView(arrangedSubviews: [ titleLabel, explanationLabel, textFieldContainer, imageViewContainer ]) let result = UIStackView(arrangedSubviews: [ titleLabel, explanationLabelContainer, textFieldContainer, imageViewContainer ])
result.axis = .vertical result.axis = .vertical
result.spacing = Values.smallSpacing result.spacing = Values.smallSpacing
result.isLayoutMarginsRelativeArrangement = true result.isLayoutMarginsRelativeArrangement = true
@ -178,6 +186,10 @@ public class ConfirmationModal: Modal, UITextFieldDelegate {
contentView.addSubview(mainStackView) contentView.addSubview(mainStackView)
contentView.addSubview(closeButton) contentView.addSubview(closeButton)
explanationLabelContainer.addSubview(explanationLabel)
explanationLabel.pin(to: explanationLabelContainer)
explanationLabel.set(.width, to: .width, of: explanationLabelContainer)
textFieldContainer.addSubview(textField) textFieldContainer.addSubview(textField)
textField.pin(to: textFieldContainer, withInset: 12) textField.pin(to: textFieldContainer, withInset: 12)
@ -191,6 +203,14 @@ public class ConfirmationModal: Modal, UITextFieldDelegate {
closeButton.pin(.right, to: .right, of: contentView, withInset: -8) closeButton.pin(.right, to: .right, of: contentView, withInset: -8)
} }
private func layoutExplanationLabel() {
let labelWidth = view.frame.width - 2 * Values.veryLargeSpacing
let maxLabelSize = CGSize(width: labelWidth, height: CGFloat.greatestFiniteMagnitude)
let expectedLabelSize = explanationLabel.sizeThatFits(maxLabelSize)
let lineHeight = explanationLabel.font.lineHeight
explanationLabelContainerHeightConstraint.constant = min(expectedLabelSize.height, lineHeight * 5)
}
// MARK: - Content // MARK: - Content
public func updateContent(with info: Info) { public func updateContent(with info: Info) {
@ -221,16 +241,18 @@ public class ConfirmationModal: Modal, UITextFieldDelegate {
case .text(let text): case .text(let text):
mainStackView.spacing = Values.smallSpacing mainStackView.spacing = Values.smallSpacing
explanationLabel.text = text explanationLabel.text = text
explanationLabel.isHidden = false explanationLabelContainer.isHidden = false
self.layoutExplanationLabel()
case .attributedText(let attributedText): case .attributedText(let attributedText):
mainStackView.spacing = Values.smallSpacing mainStackView.spacing = Values.smallSpacing
explanationLabel.attributedText = attributedText explanationLabel.attributedText = attributedText
explanationLabel.isHidden = false explanationLabelContainer.isHidden = false
self.layoutExplanationLabel()
case .input(let explanation, let placeholder, let value, let clearButton, let onTextChanged): case .input(let explanation, let placeholder, let value, let clearButton, let onTextChanged):
explanationLabel.attributedText = explanation explanationLabel.attributedText = explanation
explanationLabel.isHidden = (explanation == nil) explanationLabelContainer.isHidden = (explanation == nil)
textField.placeholder = placeholder textField.placeholder = placeholder
textField.text = (value ?? "") textField.text = (value ?? "")
textField.clearButtonMode = (clearButton ? .always : .never) textField.clearButtonMode = (clearButton ? .always : .never)

Loading…
Cancel
Save