2020-11-10 07:27:08 +01:00
|
|
|
// Copyright © 2020 Metabolist. All rights reserved.
|
|
|
|
|
2021-02-02 23:33:54 +01:00
|
|
|
import Combine
|
2020-11-10 07:27:08 +01:00
|
|
|
import Mastodon
|
|
|
|
import UIKit
|
2021-02-22 08:10:34 +01:00
|
|
|
import ViewModels
|
2020-11-10 07:27:08 +01:00
|
|
|
|
|
|
|
final class AccountFieldView: UIView {
|
2021-02-22 08:10:34 +01:00
|
|
|
let nameLabel = AnimatedAttachmentLabel()
|
2020-11-10 07:27:08 +01:00
|
|
|
let valueTextView = TouchFallthroughTextView()
|
2021-02-06 19:56:13 +01:00
|
|
|
let checkButton = UIButton()
|
|
|
|
private var valueTextViewTrailingConstraint: NSLayoutConstraint?
|
2021-02-02 23:33:54 +01:00
|
|
|
private var cancellables = Set<AnyCancellable>()
|
2020-11-10 07:27:08 +01:00
|
|
|
|
|
|
|
// swiftlint:disable:next function_body_length
|
2021-02-22 08:10:34 +01:00
|
|
|
init(name: String,
|
|
|
|
value: NSAttributedString,
|
|
|
|
verifiedAt: Date?,
|
|
|
|
emojis: [Emoji],
|
|
|
|
identityContext: IdentityContext) {
|
2020-11-10 07:27:08 +01:00
|
|
|
super.init(frame: .zero)
|
|
|
|
|
2021-02-02 23:33:54 +01:00
|
|
|
NotificationCenter.default.publisher(for: UIAccessibility.voiceOverStatusDidChangeNotification)
|
|
|
|
.sink { [weak self] _ in self?.configureUserInteractionEnabledForAccessibility() }
|
|
|
|
.store(in: &cancellables)
|
|
|
|
|
2020-11-10 07:27:08 +01:00
|
|
|
backgroundColor = .systemBackground
|
|
|
|
|
|
|
|
let nameBackgroundView = UIView()
|
|
|
|
|
|
|
|
addSubview(nameBackgroundView)
|
|
|
|
nameBackgroundView.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
nameBackgroundView.backgroundColor = .secondarySystemBackground
|
|
|
|
|
|
|
|
let valueBackgroundView = UIView()
|
|
|
|
|
|
|
|
addSubview(valueBackgroundView)
|
|
|
|
valueBackgroundView.translatesAutoresizingMaskIntoConstraints = false
|
2020-11-11 08:31:56 +01:00
|
|
|
valueBackgroundView.backgroundColor = verifiedAt == nil
|
|
|
|
? .systemBackground
|
|
|
|
: UIColor.systemGreen.withAlphaComponent(0.25)
|
2020-11-10 07:27:08 +01:00
|
|
|
|
|
|
|
addSubview(nameLabel)
|
|
|
|
nameLabel.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
nameLabel.numberOfLines = 0
|
|
|
|
nameLabel.font = .preferredFont(forTextStyle: .headline)
|
|
|
|
nameLabel.textAlignment = .center
|
|
|
|
nameLabel.textColor = .secondaryLabel
|
|
|
|
|
2020-11-11 08:31:56 +01:00
|
|
|
let mutableName = NSMutableAttributedString(string: name)
|
2020-11-10 07:27:08 +01:00
|
|
|
|
2021-02-22 08:10:34 +01:00
|
|
|
mutableName.insert(emojis: emojis, view: nameLabel, identityContext: identityContext)
|
2020-11-10 07:27:08 +01:00
|
|
|
mutableName.resizeAttachments(toLineHeight: nameLabel.font.lineHeight)
|
|
|
|
nameLabel.attributedText = mutableName
|
|
|
|
|
|
|
|
let dividerView = UIView()
|
|
|
|
|
|
|
|
addSubview(dividerView)
|
|
|
|
dividerView.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
dividerView.backgroundColor = .separator
|
|
|
|
|
|
|
|
addSubview(valueTextView)
|
|
|
|
valueTextView.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
valueTextView.backgroundColor = .clear
|
|
|
|
|
2020-11-11 08:31:56 +01:00
|
|
|
if verifiedAt != nil {
|
2020-11-10 07:27:08 +01:00
|
|
|
valueTextView.linkTextAttributes = [
|
|
|
|
.foregroundColor: UIColor.systemGreen as Any,
|
|
|
|
.underlineColor: UIColor.clear]
|
|
|
|
}
|
|
|
|
|
2020-11-11 08:31:56 +01:00
|
|
|
let valueFont = UIFont.preferredFont(forTextStyle: verifiedAt == nil ? .body : .headline)
|
|
|
|
let mutableValue = NSMutableAttributedString(attributedString: value)
|
2020-11-10 07:27:08 +01:00
|
|
|
let valueRange = NSRange(location: 0, length: mutableValue.length)
|
|
|
|
|
|
|
|
mutableValue.removeAttribute(.font, range: valueRange)
|
|
|
|
mutableValue.addAttributes(
|
|
|
|
[.font: valueFont as Any,
|
|
|
|
.foregroundColor: UIColor.label],
|
|
|
|
range: valueRange)
|
2021-02-22 08:10:34 +01:00
|
|
|
mutableValue.insert(emojis: emojis, view: valueTextView, identityContext: identityContext)
|
2020-11-10 07:27:08 +01:00
|
|
|
mutableValue.resizeAttachments(toLineHeight: valueFont.lineHeight)
|
|
|
|
|
|
|
|
valueTextView.attributedText = mutableValue
|
|
|
|
valueTextView.textAlignment = .center
|
|
|
|
|
|
|
|
checkButton.setImage(
|
|
|
|
UIImage(
|
|
|
|
systemName: "checkmark",
|
|
|
|
withConfiguration: UIImage.SymbolConfiguration(scale: .small)),
|
|
|
|
for: .normal)
|
|
|
|
|
|
|
|
addSubview(checkButton)
|
|
|
|
checkButton.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
checkButton.tintColor = .systemGreen
|
2020-11-11 08:31:56 +01:00
|
|
|
checkButton.isHidden = verifiedAt == nil
|
2020-11-10 07:27:08 +01:00
|
|
|
checkButton.showsMenuAsPrimaryAction = true
|
2021-01-28 07:36:46 +01:00
|
|
|
checkButton.setContentCompressionResistancePriority(.required, for: .horizontal)
|
|
|
|
checkButton.setContentHuggingPriority(.required, for: .horizontal)
|
2020-11-10 07:27:08 +01:00
|
|
|
|
2020-11-11 08:31:56 +01:00
|
|
|
if let verifiedAt = verifiedAt {
|
2020-11-10 07:27:08 +01:00
|
|
|
checkButton.menu = UIMenu(
|
|
|
|
title: String.localizedStringWithFormat(
|
|
|
|
NSLocalizedString("account.field.verified", comment: ""),
|
|
|
|
Self.dateFormatter.string(from: verifiedAt)),
|
|
|
|
options: .displayInline,
|
|
|
|
children: [UIAction(title: NSLocalizedString("ok", comment: "")) { _ in }])
|
|
|
|
}
|
|
|
|
|
|
|
|
let nameLabelBottomConstraint = nameLabel.bottomAnchor.constraint(
|
|
|
|
equalTo: bottomAnchor,
|
|
|
|
constant: -.defaultSpacing)
|
|
|
|
let valueTextViewBottomConstraint = valueTextView.bottomAnchor.constraint(
|
|
|
|
lessThanOrEqualTo: bottomAnchor,
|
|
|
|
constant: -.defaultSpacing)
|
2021-02-06 19:56:13 +01:00
|
|
|
valueTextViewTrailingConstraint = valueTextView.trailingAnchor.constraint(
|
|
|
|
equalTo: trailingAnchor,
|
|
|
|
constant: -.defaultSpacing)
|
2020-11-10 07:27:08 +01:00
|
|
|
|
|
|
|
for constraint in [nameLabelBottomConstraint, valueTextViewBottomConstraint] {
|
|
|
|
constraint.priority = .justBelowMax
|
|
|
|
}
|
|
|
|
|
|
|
|
NSLayoutConstraint.activate([
|
|
|
|
nameLabel.leadingAnchor.constraint(equalTo: leadingAnchor, constant: .defaultSpacing),
|
|
|
|
nameLabel.topAnchor.constraint(equalTo: topAnchor, constant: .defaultSpacing),
|
|
|
|
nameLabelBottomConstraint,
|
|
|
|
dividerView.leadingAnchor.constraint(equalTo: nameLabel.trailingAnchor, constant: .defaultSpacing),
|
|
|
|
dividerView.topAnchor.constraint(equalTo: topAnchor),
|
|
|
|
dividerView.bottomAnchor.constraint(equalTo: bottomAnchor),
|
|
|
|
dividerView.widthAnchor.constraint(equalToConstant: .hairline),
|
|
|
|
checkButton.leadingAnchor.constraint(equalTo: dividerView.trailingAnchor, constant: .defaultSpacing),
|
|
|
|
valueTextView.leadingAnchor.constraint(
|
2020-11-11 08:31:56 +01:00
|
|
|
equalTo: verifiedAt == nil ? dividerView.trailingAnchor : checkButton.trailingAnchor,
|
2020-11-10 07:27:08 +01:00
|
|
|
constant: .defaultSpacing),
|
|
|
|
valueTextView.topAnchor.constraint(greaterThanOrEqualTo: topAnchor, constant: .defaultSpacing),
|
2021-02-06 19:56:13 +01:00
|
|
|
valueTextViewTrailingConstraint!,
|
2020-11-10 07:27:08 +01:00
|
|
|
valueTextViewBottomConstraint,
|
|
|
|
nameLabel.widthAnchor.constraint(equalTo: widthAnchor, multiplier: 1 / 3),
|
|
|
|
checkButton.centerYAnchor.constraint(equalTo: valueTextView.centerYAnchor),
|
|
|
|
valueTextView.centerYAnchor.constraint(equalTo: nameLabel.centerYAnchor),
|
|
|
|
nameBackgroundView.leadingAnchor.constraint(equalTo: leadingAnchor),
|
|
|
|
nameBackgroundView.topAnchor.constraint(equalTo: topAnchor),
|
|
|
|
nameBackgroundView.trailingAnchor.constraint(equalTo: dividerView.leadingAnchor),
|
|
|
|
nameBackgroundView.bottomAnchor.constraint(equalTo: bottomAnchor),
|
|
|
|
valueBackgroundView.leadingAnchor.constraint(equalTo: dividerView.trailingAnchor),
|
|
|
|
valueBackgroundView.topAnchor.constraint(equalTo: topAnchor),
|
|
|
|
valueBackgroundView.trailingAnchor.constraint(equalTo: trailingAnchor),
|
|
|
|
valueBackgroundView.bottomAnchor.constraint(equalTo: bottomAnchor)
|
|
|
|
])
|
2021-02-02 23:33:54 +01:00
|
|
|
|
|
|
|
let accessibilityAttributedLabel = NSMutableAttributedString(attributedString: mutableName)
|
|
|
|
|
|
|
|
accessibilityAttributedLabel.appendWithSeparator(mutableValue)
|
|
|
|
|
|
|
|
if let verifiedAt = verifiedAt {
|
|
|
|
accessibilityAttributedLabel.appendWithSeparator(
|
|
|
|
String.localizedStringWithFormat(
|
|
|
|
NSLocalizedString("account.field.verified", comment: ""),
|
|
|
|
Self.dateFormatter.string(from: verifiedAt)))
|
|
|
|
}
|
|
|
|
|
|
|
|
isAccessibilityElement = true
|
|
|
|
|
|
|
|
var accessibilityCustomActions = [UIAccessibilityCustomAction]()
|
|
|
|
|
|
|
|
mutableValue.enumerateAttribute(
|
|
|
|
.link,
|
|
|
|
in: NSRange(location: 0, length: mutableValue.length),
|
|
|
|
options: []) { attribute, range, _ in
|
|
|
|
guard let url = attribute as? URL else { return }
|
|
|
|
|
|
|
|
accessibilityCustomActions.append(
|
|
|
|
UIAccessibilityCustomAction(
|
|
|
|
name: String.localizedStringWithFormat(
|
2021-02-03 08:24:45 +01:00
|
|
|
NSLocalizedString("accessibility.activate-link-%@", comment: ""),
|
2021-02-02 23:33:54 +01:00
|
|
|
mutableValue.attributedSubstring(from: range).string)) { [weak self] _ in
|
|
|
|
guard let valueTextView = self?.valueTextView else { return false }
|
|
|
|
|
|
|
|
_ = valueTextView.delegate?.textView?(
|
|
|
|
valueTextView,
|
|
|
|
shouldInteractWith: url,
|
|
|
|
in: range,
|
|
|
|
interaction: .invokeDefaultAction)
|
|
|
|
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
self.accessibilityAttributedLabel = accessibilityAttributedLabel
|
|
|
|
self.accessibilityCustomActions = accessibilityCustomActions
|
|
|
|
|
|
|
|
configureUserInteractionEnabledForAccessibility()
|
2020-11-10 07:27:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@available(*, unavailable)
|
|
|
|
required init?(coder: NSCoder) {
|
|
|
|
fatalError("init(coder:) has not been implemented")
|
|
|
|
}
|
2021-02-06 19:56:13 +01:00
|
|
|
|
|
|
|
override func layoutSubviews() {
|
|
|
|
super.layoutSubviews()
|
|
|
|
|
|
|
|
if !checkButton.isHidden {
|
|
|
|
valueTextViewTrailingConstraint?.constant = -.defaultSpacing * 2 - checkButton.frame.width
|
|
|
|
}
|
|
|
|
}
|
2020-11-10 07:27:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private extension AccountFieldView {
|
|
|
|
static let dateFormatter: DateFormatter = {
|
|
|
|
let formatter = DateFormatter()
|
|
|
|
|
|
|
|
formatter.dateStyle = .full
|
|
|
|
|
|
|
|
return formatter
|
|
|
|
}()
|
2021-02-02 23:33:54 +01:00
|
|
|
|
|
|
|
func configureUserInteractionEnabledForAccessibility() {
|
|
|
|
valueTextView.isUserInteractionEnabled = !UIAccessibility.isVoiceOverRunning
|
|
|
|
}
|
2020-11-10 07:27:08 +01:00
|
|
|
}
|