session-ios/Session/Conversations/Message Cells/InfoMessageCell.swift

119 lines
4.3 KiB
Swift
Raw Normal View History

// Copyright © 2022 Rangeproof Pty Ltd. All rights reserved.
2021-01-29 01:46:32 +01:00
import UIKit
import SessionUIKit
import SessionMessagingKit
final class InfoMessageCell: MessageCell {
private static let iconSize: CGFloat = 16
public static let inset = Values.mediumSpacing
private var isHandlingLongPress: Bool = false
override var contextSnapshotView: UIView? { return label }
// MARK: - UI
2021-01-29 01:46:32 +01:00
private lazy var iconImageViewWidthConstraint = iconImageView.set(.width, to: InfoMessageCell.iconSize)
private lazy var iconImageViewHeightConstraint = iconImageView.set(.height, to: InfoMessageCell.iconSize)
private lazy var iconImageView: UIImageView = UIImageView()
2021-01-29 01:46:32 +01:00
private lazy var label: UILabel = {
let result: UILabel = UILabel()
2021-01-29 01:46:32 +01:00
result.font = .boldSystemFont(ofSize: Values.verySmallFontSize)
result.themeTextColor = .textPrimary
2021-01-29 01:46:32 +01:00
result.textAlignment = .center
result.lineBreakMode = .byWordWrapping
result.numberOfLines = 0
2021-01-29 01:46:32 +01:00
return result
}()
2021-01-29 01:46:32 +01:00
private lazy var stackView: UIStackView = {
let result: UIStackView = UIStackView(arrangedSubviews: [ iconImageView, label ])
2021-01-29 01:46:32 +01:00
result.axis = .vertical
result.alignment = .center
result.spacing = Values.smallSpacing
2021-01-29 01:46:32 +01:00
return result
}()
// MARK: - Lifecycle
2021-01-29 01:46:32 +01:00
override func setUpViewHierarchy() {
super.setUpViewHierarchy()
2021-01-29 01:46:32 +01:00
iconImageViewWidthConstraint.isActive = true
iconImageViewHeightConstraint.isActive = true
addSubview(stackView)
2021-01-29 01:46:32 +01:00
stackView.pin(.left, to: .left, of: self, withInset: InfoMessageCell.inset)
stackView.pin(.top, to: .top, of: self, withInset: InfoMessageCell.inset)
stackView.pin(.right, to: .right, of: self, withInset: -InfoMessageCell.inset)
stackView.pin(.bottom, to: .bottom, of: self, withInset: -InfoMessageCell.inset)
}
override func setUpGestureRecognizers() {
let longPressRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(handleLongPress))
addGestureRecognizer(longPressRecognizer)
}
// MARK: - Updating
2021-01-29 01:46:32 +01:00
Merge branch 'feature/database-refactor' into emoji-reacts # Conflicts: # Session.xcodeproj/project.pbxproj # Session/Conversations/Context Menu/ContextMenuVC+Action.swift # Session/Conversations/Context Menu/ContextMenuVC+ActionView.swift # Session/Conversations/Context Menu/ContextMenuVC.swift # Session/Conversations/ConversationVC+Interaction.swift # Session/Conversations/ConversationVC.swift # Session/Conversations/ConversationViewItem.h # Session/Conversations/ConversationViewItem.m # Session/Conversations/Message Cells/Content Views/LinkPreviewView.swift # Session/Conversations/Message Cells/MessageCell.swift # Session/Conversations/Message Cells/VisibleMessageCell.swift # Session/Conversations/Views & Modals/BodyTextView.swift # Session/Meta/Translations/en.lproj/Localizable.strings # Session/Shared/UserCell.swift # SessionMessagingKit/Jobs/MessageSendJob.swift # SessionMessagingKit/Messages/Signal/TSMessage.h # SessionMessagingKit/Messages/Signal/TSMessage.m # SessionMessagingKit/Messages/Visible Messages/VisibleMessage.swift # SessionMessagingKit/Open Groups/OpenGroupAPIV2.swift # SessionMessagingKit/Sending & Receiving/MessageReceiver+Handling.swift # SessionMessagingKit/Sending & Receiving/Notifications/NotificationsProtocol.h # SessionMessagingKit/Sending & Receiving/Pollers/OpenGroupPollerV2.swift # SessionMessagingKit/Utilities/General.swift # SessionNotificationServiceExtension/NSENotificationPresenter.swift # SignalUtilitiesKit/Utilities/DisplayableText.swift # SignalUtilitiesKit/Utilities/NoopNotificationsManager.swift # SignalUtilitiesKit/Utilities/Notification+Loki.swift
2022-07-25 07:39:56 +02:00
override func update(
with cellViewModel: MessageViewModel,
mediaCache: NSCache<NSString, AnyObject>,
playbackInfo: ConversationViewModel.PlaybackInfo?,
showExpandedReactions: Bool,
lastSearchText: String?
) {
guard cellViewModel.variant.isInfoMessage else { return }
self.accessibilityIdentifier = "Configuration message"
self.isAccessibilityElement = true
self.viewModel = cellViewModel
let icon: UIImage? = {
switch cellViewModel.variant {
case .infoDisappearingMessagesUpdate:
return (cellViewModel.threadHasDisappearingMessagesEnabled ?
UIImage(named: "ic_timer") :
UIImage(named: "ic_timer_disabled")
)
case .infoMediaSavedNotification: return UIImage(named: "ic_download")
default: return nil
2021-01-29 01:46:32 +01:00
}
}()
2021-01-29 01:46:32 +01:00
if let icon = icon {
iconImageView.image = icon.withRenderingMode(.alwaysTemplate)
iconImageView.themeTintColor = .textPrimary
2021-01-29 01:46:32 +01:00
}
2021-01-29 01:46:32 +01:00
iconImageViewWidthConstraint.constant = (icon != nil) ? InfoMessageCell.iconSize : 0
iconImageViewHeightConstraint.constant = (icon != nil) ? InfoMessageCell.iconSize : 0
self.label.text = cellViewModel.body
2023-03-08 07:10:21 +01:00
self.label.themeTextColor = (cellViewModel.variant == .infoClosedGroupCurrentUserErrorLeaving) ? .danger : .textPrimary
}
override func dynamicUpdate(with cellViewModel: MessageViewModel, playbackInfo: ConversationViewModel.PlaybackInfo?) {
2021-01-29 01:46:32 +01:00
}
// MARK: - Interaction
@objc func handleLongPress(_ gestureRecognizer: UITapGestureRecognizer) {
if [ .ended, .cancelled, .failed ].contains(gestureRecognizer.state) {
isHandlingLongPress = false
return
}
guard !isHandlingLongPress, let cellViewModel: MessageViewModel = self.viewModel else { return }
delegate?.handleItemLongPressed(cellViewModel)
isHandlingLongPress = true
}
2021-01-29 01:46:32 +01:00
}