ChatContactInfoSystemMessageCell.swift 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. // _____ _
  2. // |_ _| |_ _ _ ___ ___ _ __ __ _
  3. // | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. // |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. //
  6. // Threema iOS Client
  7. // Copyright (c) 2020 Threema GmbH
  8. //
  9. // This program is free software: you can redistribute it and/or modify
  10. // it under the terms of the GNU Affero General Public License, version 3,
  11. // as published by the Free Software Foundation.
  12. //
  13. // This program is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU Affero General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Affero General Public License
  19. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. import Foundation
  21. import CocoaLumberjackSwift
  22. @objc open class ChatContactInfoSystemMessageCell: UITableViewCell {
  23. private var _systemMessage: SystemMessage?
  24. private var _msgText = UILabel.init()
  25. private var _msgBackground = UIImageView()
  26. private var _threemaTypeIcon = UIImageView()
  27. private let _iconSize: CGFloat = 26.0
  28. private let _iconY: CGFloat = 12.0
  29. @objc override public init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
  30. super.init(style: style, reuseIdentifier: reuseIdentifier)
  31. self.backgroundColor = .clear
  32. var fontSize = roundf(UserSettings.shared().chatFontSize * 13 / 16.0)
  33. if fontSize < Float(kSystemMessageMinFontSize) {
  34. fontSize = Float(kSystemMessageMinFontSize)
  35. }
  36. else if fontSize > Float(kSystemMessageMaxFontSize) {
  37. fontSize = Float(kSystemMessageMaxFontSize)
  38. }
  39. _msgBackground.clearsContextBeforeDrawing = false
  40. if LicenseStore.requiresLicenseKey() {
  41. _msgBackground.backgroundColor = Colors.green()
  42. } else {
  43. _msgBackground.backgroundColor = Colors.workBlue()
  44. }
  45. _msgBackground.autoresizingMask = .flexibleWidth
  46. _msgBackground.layer.cornerRadius = 5
  47. contentView.addSubview(_msgBackground)
  48. _threemaTypeIcon.image = Utils.threemaTypeIcon()
  49. if #available(iOS 11.0, *) {
  50. _threemaTypeIcon.accessibilityIgnoresInvertColors = true
  51. }
  52. _threemaTypeIcon.frame = CGRect.init(x: 20.0, y: _iconY, width: _iconSize, height: _iconSize)
  53. contentView.addSubview(_threemaTypeIcon)
  54. _msgText.frame = CGRect.init(x: 20.0, y: 21, width: contentView.frame.size.width - 40.0, height: 20.0)
  55. _msgText.font = UIFont.boldSystemFont(ofSize: CGFloat(roundf(fontSize)))
  56. _msgText.textColor = Colors.white()
  57. _msgText.numberOfLines = 0
  58. _msgText.textAlignment = .left
  59. _msgText.autoresizingMask = .flexibleWidth
  60. _msgText.backgroundColor = .clear
  61. contentView.addSubview(_msgText)
  62. }
  63. required public init?(coder: NSCoder) {
  64. fatalError("init(coder:) has not been implemented")
  65. }
  66. }
  67. extension ChatContactInfoSystemMessageCell {
  68. // MARK: Override functions
  69. @objc open class func height(for message: BaseMessage!, forTableWidth tableWidth: CGFloat) -> CGFloat {
  70. let systemMessage = message as! SystemMessage
  71. let maxSize = CGSize.init(width: tableWidth - 26.0 - 8.0, height: CGFloat.greatestFiniteMagnitude)
  72. let text = systemMessage.format()
  73. var dummySystemLabel: UILabel? = nil
  74. if dummySystemLabel == nil {
  75. dummySystemLabel = UILabel.init(frame: CGRect.init(x: 0.0, y: 0.0, width: maxSize.width, height: maxSize.height))
  76. }
  77. var fontSize = roundf(UserSettings.shared().chatFontSize * 13 / 16.0)
  78. if fontSize < Float(kSystemMessageMinFontSize) {
  79. fontSize = Float(kSystemMessageMinFontSize)
  80. }
  81. else if fontSize > Float(kSystemMessageMaxFontSize) {
  82. fontSize = Float(kSystemMessageMaxFontSize)
  83. }
  84. dummySystemLabel!.font = UIFont.boldSystemFont(ofSize: CGFloat(roundf(fontSize)))
  85. dummySystemLabel!.numberOfLines = 3
  86. dummySystemLabel!.text = text
  87. let height = (dummySystemLabel?.sizeThatFits(maxSize).height)!
  88. let threemaTypeIcon = UIImageView()
  89. threemaTypeIcon.image = Utils.threemaTypeIcon()
  90. if (height > threemaTypeIcon.frame.size.height) {
  91. return height
  92. } else {
  93. return threemaTypeIcon.frame.size.height
  94. }
  95. }
  96. @objc override open func layoutSubviews() {
  97. let messageTextWidth = self.layoutMarginsGuide.layoutFrame.size.width - 32.0
  98. let textSize = _msgText.sizeThatFits(CGSize.init(width: messageTextWidth, height: CGFloat.greatestFiniteMagnitude))
  99. super.layoutSubviews()
  100. let bgSideMargin: CGFloat = 8.0
  101. let bgTopOffset: CGFloat = 6.0
  102. let bgHeightMargin: CGFloat = bgTopOffset * 2
  103. let backgroundWidth: CGFloat = bgSideMargin + _iconSize + bgSideMargin + textSize.width + bgSideMargin
  104. let backgroundX: CGFloat = (self.frame.size.width - backgroundWidth) / 2
  105. if (textSize.height > _iconSize) {
  106. _msgBackground.frame = CGRect(x: backgroundX, y: _iconY - bgTopOffset, width: backgroundWidth, height: textSize.height + bgHeightMargin)
  107. _threemaTypeIcon.frame = CGRect.init(x: backgroundX + bgSideMargin, y: _iconY + (textSize.height / 2) - (_iconSize / 2), width: _iconSize, height: _iconSize)
  108. _msgText.frame = CGRect.init(x: backgroundX + bgSideMargin + _iconSize + bgSideMargin, y: _iconY, width: textSize.width, height: textSize.height)
  109. } else {
  110. _msgBackground.frame = CGRect(x: backgroundX, y: _iconY - bgTopOffset, width: backgroundWidth, height: _iconSize + bgHeightMargin)
  111. _threemaTypeIcon.frame = CGRect.init(x: backgroundX + bgSideMargin, y: _iconY, width: _iconSize, height: _iconSize)
  112. _msgText.frame = CGRect.init(x: backgroundX + bgSideMargin + _iconSize + bgSideMargin, y: _iconY + (_iconSize / 2) - (textSize.height / 2), width: textSize.width, height: textSize.height)
  113. }
  114. }
  115. @objc func setMessage(systemMessage: SystemMessage) {
  116. _msgText.text = systemMessage.format()
  117. }
  118. @objc @available(iOS 13.0, *)
  119. open func getContextMenu(_ indexPath: IndexPath!, point: CGPoint) -> UIContextMenuConfiguration! {
  120. return nil
  121. }
  122. }