NotificationTableViewCell.swift 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. //
  2. // NotificationTableViewCell.swift
  3. // Mastodon
  4. //
  5. // Created by MainasuK on 2022-1-21.
  6. //
  7. import os.log
  8. import UIKit
  9. import Combine
  10. import MastodonCore
  11. import MastodonUI
  12. final class NotificationTableViewCell: UITableViewCell {
  13. let logger = Logger(subsystem: "NotificationTableViewCell", category: "View")
  14. weak var delegate: NotificationTableViewCellDelegate?
  15. var disposeBag = Set<AnyCancellable>()
  16. private var _disposeBag = Set<AnyCancellable>()
  17. let notificationView = NotificationView()
  18. let separatorLine = UIView.separatorLine
  19. var containerViewLeadingLayoutConstraint: NSLayoutConstraint!
  20. var containerViewTrailingLayoutConstraint: NSLayoutConstraint!
  21. override func prepareForReuse() {
  22. super.prepareForReuse()
  23. disposeBag.removeAll()
  24. notificationView.prepareForReuse()
  25. }
  26. override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
  27. super.init(style: style, reuseIdentifier: reuseIdentifier)
  28. _init()
  29. }
  30. required init?(coder: NSCoder) {
  31. super.init(coder: coder)
  32. _init()
  33. }
  34. }
  35. extension NotificationTableViewCell {
  36. private func _init() {
  37. notificationView.translatesAutoresizingMaskIntoConstraints = false
  38. contentView.addSubview(notificationView)
  39. setupContainerViewMarginConstraints()
  40. NSLayoutConstraint.activate([
  41. notificationView.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 16),
  42. containerViewLeadingLayoutConstraint,
  43. containerViewTrailingLayoutConstraint,
  44. contentView.bottomAnchor.constraint(equalTo: notificationView.bottomAnchor),
  45. ])
  46. updateContainerViewMarginConstraints()
  47. separatorLine.translatesAutoresizingMaskIntoConstraints = false
  48. contentView.addSubview(separatorLine)
  49. NSLayoutConstraint.activate([
  50. separatorLine.leadingAnchor.constraint(equalTo: contentView.leadingAnchor),
  51. separatorLine.trailingAnchor.constraint(equalTo: contentView.trailingAnchor),
  52. separatorLine.bottomAnchor.constraint(equalTo: contentView.bottomAnchor),
  53. separatorLine.heightAnchor.constraint(equalToConstant: UIView.separatorLineHeight(of: contentView)).priority(.required - 1),
  54. ])
  55. notificationView.quoteBackgroundView.backgroundColor = ThemeService.shared.currentTheme.value.secondarySystemBackgroundColor
  56. ThemeService.shared.currentTheme
  57. .sink { [weak self] theme in
  58. guard let self = self else { return }
  59. self.notificationView.quoteBackgroundView.backgroundColor = theme.secondarySystemBackgroundColor
  60. }
  61. .store(in: &_disposeBag)
  62. notificationView.delegate = self
  63. }
  64. override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
  65. super.traitCollectionDidChange(previousTraitCollection)
  66. updateContainerViewMarginConstraints()
  67. }
  68. }
  69. // MARK: - AdaptiveContainerMarginTableViewCell
  70. extension NotificationTableViewCell: AdaptiveContainerMarginTableViewCell {
  71. var containerView: NotificationView {
  72. notificationView
  73. }
  74. }
  75. // MARK: - NotificationViewContainerTableViewCell
  76. extension NotificationTableViewCell: NotificationViewContainerTableViewCell { }
  77. // MARK: - NotificationTableViewCellDelegate
  78. extension NotificationTableViewCell: NotificationViewDelegate { }