2022-04-27 14:27:49 +00:00
|
|
|
//
|
|
|
|
// Copyright 2022 New Vector Ltd
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
//
|
|
|
|
|
|
|
|
import UIKit
|
|
|
|
|
|
|
|
/// Provider for mention Pills attachment view.
|
2022-04-28 09:25:47 +00:00
|
|
|
@available(iOS 15.0, *)
|
2022-04-27 14:27:49 +00:00
|
|
|
@objc class PillAttachmentViewProvider: NSTextAttachmentViewProvider {
|
2022-05-13 11:52:46 +00:00
|
|
|
// MARK: - Properties
|
2022-05-10 15:19:52 +00:00
|
|
|
private static let pillAttachmentViewSizes = PillAttachmentView.Sizes(verticalMargin: 2.0,
|
|
|
|
horizontalMargin: 4.0,
|
|
|
|
avatarSideLength: 16.0)
|
2022-05-13 12:00:06 +00:00
|
|
|
private weak var messageTextView: MXKMessageTextView?
|
2022-05-13 11:52:46 +00:00
|
|
|
|
|
|
|
// MARK: - Override
|
|
|
|
override init(textAttachment: NSTextAttachment, parentView: UIView?, textLayoutManager: NSTextLayoutManager?, location: NSTextLocation) {
|
|
|
|
super.init(textAttachment: textAttachment, parentView: parentView, textLayoutManager: textLayoutManager, location: location)
|
|
|
|
|
|
|
|
self.messageTextView = parentView?.superview as? MXKMessageTextView
|
|
|
|
}
|
2022-05-09 14:53:38 +00:00
|
|
|
|
2022-04-27 14:27:49 +00:00
|
|
|
override func loadView() {
|
|
|
|
super.loadView()
|
|
|
|
|
|
|
|
guard let textAttachment = self.textAttachment as? PillTextAttachment else {
|
2022-04-28 09:25:47 +00:00
|
|
|
MXLog.debug("[PillAttachmentViewProvider]: attachment is missing or not of expected class")
|
2022-04-27 14:27:49 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-05-09 13:25:47 +00:00
|
|
|
guard let pillData = textAttachment.data else {
|
2022-05-09 15:14:32 +00:00
|
|
|
MXLog.debug("[PillAttachmentViewProvider]: attachment misses pill data")
|
2022-04-27 14:27:49 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-05-09 15:49:05 +00:00
|
|
|
let mainSession = AppDelegate.theDelegate().mxSessions.first as? MXSession
|
|
|
|
|
2022-05-13 11:52:46 +00:00
|
|
|
let pillView = PillAttachmentView(frame: CGRect(origin: .zero, size: Self.size(forDisplayText: pillData.displayText,
|
|
|
|
andFont: pillData.font)),
|
|
|
|
sizes: Self.pillAttachmentViewSizes,
|
|
|
|
theme: ThemeService.shared().theme,
|
|
|
|
mediaManager: mainSession?.mediaManager,
|
|
|
|
andPillData: pillData)
|
|
|
|
view = pillView
|
|
|
|
messageTextView?.registerPillView(pillView)
|
2022-04-27 14:27:49 +00:00
|
|
|
}
|
|
|
|
}
|
2022-05-09 14:53:38 +00:00
|
|
|
|
|
|
|
@available(iOS 15.0, *)
|
|
|
|
extension PillAttachmentViewProvider {
|
|
|
|
/// Computes size required to display a pill for given display text.
|
|
|
|
///
|
2022-05-10 15:19:52 +00:00
|
|
|
/// - Parameters:
|
|
|
|
/// - displayText: display text for the pill
|
|
|
|
/// - font: the text font
|
2022-05-09 14:53:38 +00:00
|
|
|
/// - Returns: required size for pill
|
2022-05-10 15:19:52 +00:00
|
|
|
static func size(forDisplayText displayText: String, andFont font: UIFont) -> CGSize {
|
2022-05-09 14:53:38 +00:00
|
|
|
let label = UILabel(frame: .zero)
|
|
|
|
label.text = displayText
|
2022-05-10 15:19:52 +00:00
|
|
|
label.font = font
|
2022-05-09 14:53:38 +00:00
|
|
|
let labelSize = label.sizeThatFits(CGSize(width: CGFloat.greatestFiniteMagnitude,
|
2022-05-10 15:19:52 +00:00
|
|
|
height: pillAttachmentViewSizes.pillBackgroundHeight))
|
2022-05-09 14:53:38 +00:00
|
|
|
|
2022-05-10 15:19:52 +00:00
|
|
|
return CGSize(width: labelSize.width + pillAttachmentViewSizes.totalWidthWithoutLabel,
|
|
|
|
height: pillAttachmentViewSizes.pillHeight)
|
2022-05-09 14:53:38 +00:00
|
|
|
}
|
|
|
|
}
|