DiscoveryIntroBannerView.swift 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. //
  2. // DiscoveryIntroBannerView.swift
  3. // Mastodon
  4. //
  5. // Created by MainasuK on 2022-4-19.
  6. //
  7. import os.log
  8. import UIKit
  9. import Combine
  10. import MastodonAsset
  11. import MastodonCore
  12. import MastodonUI
  13. import MastodonLocalization
  14. public protocol DiscoveryIntroBannerViewDelegate: AnyObject {
  15. func discoveryIntroBannerView(_ bannerView: DiscoveryIntroBannerView, closeButtonDidPressed button: UIButton)
  16. }
  17. public final class DiscoveryIntroBannerView: UIView {
  18. let logger = Logger(subsystem: "DiscoveryIntroBannerView", category: "View")
  19. var _disposeBag = Set<AnyCancellable>()
  20. public weak var delegate: DiscoveryIntroBannerViewDelegate?
  21. let label: UILabel = {
  22. let label = UILabel()
  23. label.font = UIFontMetrics(forTextStyle: .headline).scaledFont(for: .systemFont(ofSize: 16, weight: .regular))
  24. label.textColor = Asset.Colors.Label.primary.color
  25. label.text = L10n.Scene.Discovery.intro
  26. label.numberOfLines = 0
  27. return label
  28. }()
  29. let closeButton: HitTestExpandedButton = {
  30. let button = HitTestExpandedButton(type: .system)
  31. button.setImage(UIImage(systemName: "xmark.circle.fill"), for: .normal)
  32. button.tintColor = Asset.Colors.Label.secondary.color
  33. return button
  34. }()
  35. public override init(frame: CGRect) {
  36. super.init(frame: frame)
  37. _init()
  38. }
  39. public required init?(coder: NSCoder) {
  40. super.init(coder: coder)
  41. _init()
  42. }
  43. }
  44. extension DiscoveryIntroBannerView {
  45. private func _init() {
  46. preservesSuperviewLayoutMargins = true
  47. setupAppearance(theme: ThemeService.shared.currentTheme.value)
  48. ThemeService.shared.currentTheme
  49. .receive(on: DispatchQueue.main)
  50. .sink { [weak self] theme in
  51. guard let self = self else { return }
  52. self.setupAppearance(theme: theme)
  53. }
  54. .store(in: &_disposeBag)
  55. closeButton.translatesAutoresizingMaskIntoConstraints = false
  56. addSubview(closeButton)
  57. NSLayoutConstraint.activate([
  58. closeButton.topAnchor.constraint(equalTo: topAnchor, constant: 16).priority(.required - 1),
  59. layoutMarginsGuide.trailingAnchor.constraint(equalTo: closeButton.trailingAnchor),
  60. closeButton.heightAnchor.constraint(equalToConstant: 20).priority(.required - 1),
  61. closeButton.widthAnchor.constraint(equalToConstant: 20).priority(.required - 1),
  62. ])
  63. label.translatesAutoresizingMaskIntoConstraints = false
  64. addSubview(label)
  65. NSLayoutConstraint.activate([
  66. label.topAnchor.constraint(equalTo: topAnchor, constant: 16).priority(.required - 1),
  67. label.leadingAnchor.constraint(equalTo: layoutMarginsGuide.leadingAnchor),
  68. closeButton.leadingAnchor.constraint(equalTo: label.trailingAnchor, constant: 10),
  69. bottomAnchor.constraint(equalTo: label.bottomAnchor, constant: 16).priority(.required - 1),
  70. ])
  71. closeButton.addTarget(self, action: #selector(DiscoveryIntroBannerView.closeButtonDidPressed(_:)), for: .touchUpInside)
  72. }
  73. }
  74. extension DiscoveryIntroBannerView {
  75. @objc private func closeButtonDidPressed(_ sender: UIButton) {
  76. logger.log(level: .debug, "\((#file as NSString).lastPathComponent, privacy: .public)[\(#line, privacy: .public)], \(#function, privacy: .public)")
  77. delegate?.discoveryIntroBannerView(self, closeButtonDidPressed: sender)
  78. }
  79. }
  80. extension DiscoveryIntroBannerView {
  81. private func setupAppearance(theme: Theme) {
  82. backgroundColor = theme.systemBackgroundColor
  83. }
  84. }