2019-05-13 07:21:16 +02:00
|
|
|
|
2019-06-17 06:39:35 +02:00
|
|
|
@objc(LKFriendRequestView)
|
|
|
|
final class FriendRequestView : UIView {
|
2019-05-21 02:30:06 +02:00
|
|
|
private let message: TSMessage
|
2019-05-13 07:21:16 +02:00
|
|
|
@objc weak var delegate: FriendRequestViewDelegate?
|
2019-05-21 02:30:06 +02:00
|
|
|
|
|
|
|
private var kind: Kind {
|
|
|
|
let isIncoming = message.interactionType() == .incomingMessage
|
|
|
|
return isIncoming ? .incoming : .outgoing
|
|
|
|
}
|
2019-05-15 08:48:16 +02:00
|
|
|
|
2019-05-14 03:27:34 +02:00
|
|
|
// MARK: Types
|
|
|
|
enum Kind : String { case incoming, outgoing }
|
2019-05-13 07:21:16 +02:00
|
|
|
|
|
|
|
// MARK: Components
|
2019-05-24 08:23:27 +02:00
|
|
|
private lazy var topSpacer: UIView = {
|
|
|
|
let result = UIView()
|
|
|
|
result.autoSetDimension(.height, toSize: 12)
|
|
|
|
return result
|
|
|
|
}()
|
|
|
|
|
2019-05-13 07:21:16 +02:00
|
|
|
private lazy var label: UILabel = {
|
|
|
|
let result = UILabel()
|
|
|
|
result.textColor = Theme.secondaryColor
|
|
|
|
result.font = UIFont.ows_dynamicTypeSubheadlineClamped
|
|
|
|
result.numberOfLines = 0
|
|
|
|
result.textAlignment = .center
|
|
|
|
result.lineBreakMode = .byWordWrapping
|
|
|
|
return result
|
|
|
|
}()
|
2019-05-15 08:48:16 +02:00
|
|
|
|
|
|
|
private lazy var buttonStackView: UIStackView = {
|
|
|
|
let result = UIStackView()
|
|
|
|
result.axis = .horizontal
|
|
|
|
result.distribution = .fillEqually
|
|
|
|
return result
|
|
|
|
}()
|
|
|
|
|
|
|
|
private lazy var buttonFont = UIFont.ows_dynamicTypeBodyClamped.ows_mediumWeight()
|
|
|
|
private lazy var buttonHeight = buttonFont.pointSize * 48 / 17
|
2019-05-13 07:21:16 +02:00
|
|
|
|
|
|
|
// MARK: Initialization
|
2019-05-21 02:30:06 +02:00
|
|
|
@objc init(message: TSMessage) {
|
|
|
|
self.message = message
|
2019-05-14 03:27:34 +02:00
|
|
|
super.init(frame: CGRect.zero)
|
2019-05-14 04:54:13 +02:00
|
|
|
initialize()
|
2019-05-14 03:27:34 +02:00
|
|
|
}
|
|
|
|
|
2019-05-21 02:30:06 +02:00
|
|
|
required init?(coder: NSCoder) { fatalError("Using FriendRequestView.init(coder:) isn't allowed. Use FriendRequestView.init(message:) instead.") }
|
|
|
|
override init(frame: CGRect) { fatalError("Using FriendRequestView.init(frame:) isn't allowed. Use FriendRequestView.init(message:) instead.") }
|
2019-05-13 07:21:16 +02:00
|
|
|
|
|
|
|
private func initialize() {
|
2019-05-21 02:30:06 +02:00
|
|
|
// Set up UI
|
2019-05-13 07:21:16 +02:00
|
|
|
let mainStackView = UIStackView()
|
|
|
|
mainStackView.axis = .vertical
|
|
|
|
mainStackView.distribution = .fill
|
2019-05-24 08:23:27 +02:00
|
|
|
mainStackView.addArrangedSubview(topSpacer)
|
2019-05-13 07:21:16 +02:00
|
|
|
mainStackView.addArrangedSubview(label)
|
2019-05-14 03:27:34 +02:00
|
|
|
switch kind {
|
|
|
|
case .incoming:
|
|
|
|
mainStackView.addArrangedSubview(buttonStackView)
|
|
|
|
let acceptButton = OWSFlatButton.button(title: NSLocalizedString("Accept", comment: ""), font: buttonFont, titleColor: .ows_materialBlue, backgroundColor: .white, target: self, selector: #selector(accept))
|
2019-06-14 08:36:40 +02:00
|
|
|
acceptButton.setBackgroundColors(upColor: .clear, downColor: .clear)
|
2019-05-14 03:27:34 +02:00
|
|
|
acceptButton.autoSetDimension(.height, toSize: buttonHeight)
|
|
|
|
buttonStackView.addArrangedSubview(acceptButton)
|
|
|
|
let declineButton = OWSFlatButton.button(title: NSLocalizedString("Decline", comment: ""), font: buttonFont, titleColor: .ows_destructiveRed, backgroundColor: .white, target: self, selector: #selector(decline))
|
2019-06-14 08:36:40 +02:00
|
|
|
declineButton.setBackgroundColors(upColor: .clear, downColor: .clear)
|
2019-05-14 03:27:34 +02:00
|
|
|
declineButton.autoSetDimension(.height, toSize: buttonHeight)
|
|
|
|
buttonStackView.addArrangedSubview(declineButton)
|
|
|
|
case .outgoing: break
|
|
|
|
}
|
2019-05-13 07:21:16 +02:00
|
|
|
addSubview(mainStackView)
|
|
|
|
mainStackView.autoPin(toEdgesOf: self)
|
2019-05-21 02:30:06 +02:00
|
|
|
updateUI()
|
|
|
|
// Observe friend request status changes
|
2019-05-21 01:09:48 +02:00
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(handleFriendRequestStatusChangedNotification), name: .messageFriendRequestStatusChanged, object: nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
deinit {
|
|
|
|
NotificationCenter.default.removeObserver(self)
|
2019-05-13 07:21:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: Updating
|
2019-05-21 01:09:48 +02:00
|
|
|
@objc private func handleFriendRequestStatusChangedNotification(_ notification: Notification) {
|
2019-05-21 02:30:06 +02:00
|
|
|
let messageID = notification.object as! String
|
|
|
|
guard messageID == message.uniqueId else { return }
|
2019-05-21 01:09:48 +02:00
|
|
|
message.reload()
|
2019-05-21 02:30:06 +02:00
|
|
|
updateUI()
|
2019-05-21 01:09:48 +02:00
|
|
|
}
|
|
|
|
|
2019-05-21 02:30:06 +02:00
|
|
|
private func updateUI() {
|
2019-05-14 03:27:34 +02:00
|
|
|
switch kind {
|
|
|
|
case .incoming:
|
|
|
|
guard let message = message as? TSIncomingMessage else { preconditionFailure() }
|
2019-05-21 02:30:06 +02:00
|
|
|
buttonStackView.isHidden = message.friendRequestStatus != .pending
|
2019-05-20 04:54:57 +02:00
|
|
|
let format: String = {
|
2019-05-20 08:36:22 +02:00
|
|
|
switch (message.friendRequestStatus) {
|
2019-05-24 08:23:27 +02:00
|
|
|
case .none, .sendingOrFailed: preconditionFailure()
|
|
|
|
case .pending: return NSLocalizedString("%@ sent you a friend request", comment: "")
|
2019-05-20 08:36:22 +02:00
|
|
|
case .accepted: return NSLocalizedString("You've accepted %@'s friend request", comment: "")
|
|
|
|
case .declined: return NSLocalizedString("You've declined %@'s friend request", comment: "")
|
|
|
|
case .expired: return NSLocalizedString("%@'s friend request has expired", comment: "")
|
2019-05-24 08:23:27 +02:00
|
|
|
default: preconditionFailure()
|
2019-05-20 08:36:22 +02:00
|
|
|
}
|
2019-05-15 08:48:16 +02:00
|
|
|
}()
|
2019-06-14 08:36:40 +02:00
|
|
|
let contactID = message.authorId
|
|
|
|
let displayName = Environment.shared.contactsManager.profileName(forRecipientId: contactID) ?? contactID
|
|
|
|
label.text = String(format: format, displayName)
|
2019-05-14 03:27:34 +02:00
|
|
|
case .outgoing:
|
|
|
|
guard let message = message as? TSOutgoingMessage else { preconditionFailure() }
|
2019-05-24 08:23:27 +02:00
|
|
|
let format: String? = {
|
2019-05-20 08:36:22 +02:00
|
|
|
switch (message.friendRequestStatus) {
|
2019-05-24 08:23:27 +02:00
|
|
|
case .none: preconditionFailure()
|
|
|
|
case .sendingOrFailed: return nil
|
|
|
|
case .pending: return NSLocalizedString("You've sent %@ a friend request", comment: "")
|
2019-05-20 08:36:22 +02:00
|
|
|
case .accepted: return NSLocalizedString("%@ accepted your friend request", comment: "")
|
2019-05-24 08:23:27 +02:00
|
|
|
case .declined: preconditionFailure()
|
2019-05-20 08:36:22 +02:00
|
|
|
case .expired: return NSLocalizedString("Your friend request to %@ has expired", comment: "")
|
2019-05-24 08:23:27 +02:00
|
|
|
default: preconditionFailure()
|
2019-05-20 08:36:22 +02:00
|
|
|
}
|
2019-05-17 03:58:46 +02:00
|
|
|
}()
|
2019-05-24 08:23:27 +02:00
|
|
|
if let format = format {
|
2019-06-14 08:36:40 +02:00
|
|
|
let contactID = message.thread.contactIdentifier()!
|
|
|
|
let displayName = Environment.shared.contactsManager.profileName(forRecipientId: contactID) ?? contactID
|
|
|
|
label.text = String(format: format, displayName)
|
2019-05-24 08:23:27 +02:00
|
|
|
}
|
|
|
|
label.isHidden = (format == nil)
|
|
|
|
topSpacer.isHidden = (label.isHidden)
|
2019-05-14 03:27:34 +02:00
|
|
|
}
|
2019-05-13 07:21:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: Interaction
|
|
|
|
@objc private func accept() {
|
2019-05-14 03:27:34 +02:00
|
|
|
guard let message = message as? TSIncomingMessage else { preconditionFailure() }
|
2019-05-20 08:36:22 +02:00
|
|
|
message.saveFriendRequestStatus(.accepted, with: nil)
|
2019-05-13 07:21:16 +02:00
|
|
|
delegate?.acceptFriendRequest(message)
|
|
|
|
}
|
|
|
|
|
|
|
|
@objc private func decline() {
|
2019-05-14 03:27:34 +02:00
|
|
|
guard let message = message as? TSIncomingMessage else { preconditionFailure() }
|
2019-05-20 08:36:22 +02:00
|
|
|
message.saveFriendRequestStatus(.declined, with: nil)
|
2019-05-13 07:21:16 +02:00
|
|
|
delegate?.declineFriendRequest(message)
|
|
|
|
}
|
2019-05-14 02:28:02 +02:00
|
|
|
|
|
|
|
// MARK: Measuring
|
2019-05-14 03:27:34 +02:00
|
|
|
@objc static func calculateHeight(message: TSMessage, conversationStyle: ConversationStyle) -> CGFloat {
|
2019-05-14 02:28:02 +02:00
|
|
|
let width = conversationStyle.contentWidth
|
2019-05-21 02:30:06 +02:00
|
|
|
let dummyFriendRequestView = FriendRequestView(message: message)
|
2019-05-24 08:23:27 +02:00
|
|
|
let hasTopSpacer = !dummyFriendRequestView.topSpacer.isHidden
|
|
|
|
let topSpacing: CGFloat = hasTopSpacer ? 12 : 0
|
|
|
|
let hasLabel = !dummyFriendRequestView.label.isHidden
|
|
|
|
let labelHeight = hasLabel ? dummyFriendRequestView.label.sizeThatFits(CGSize(width: width, height: CGFloat.greatestFiniteMagnitude)).height : 0
|
2019-05-21 02:30:06 +02:00
|
|
|
let hasButtonStackView = dummyFriendRequestView.buttonStackView.superview != nil && !dummyFriendRequestView.buttonStackView.isHidden
|
|
|
|
let buttonHeight = hasButtonStackView ? dummyFriendRequestView.buttonHeight : 0
|
|
|
|
let totalHeight = topSpacing + labelHeight + buttonHeight
|
2019-05-14 02:28:02 +02:00
|
|
|
return totalHeight.rounded(.up)
|
|
|
|
}
|
2019-05-13 07:21:16 +02:00
|
|
|
}
|