-
Notifications
You must be signed in to change notification settings - Fork 5
/
MediaCard.swift
205 lines (168 loc) · 6.49 KB
/
MediaCard.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
//
// MediaCard.swift
//
// Made with ❤️ by Novum
//
// Copyright © Telefonica. All rights reserved.
//
import Foundation
import UIKit
// MARK: MediaCardConfiguration
public struct MediaCardConfiguration {
let richMedia: UIView
let headline: String?
let title: String?
let pretitle: String?
let descriptionTitle: String
let button: CardButton?
let link: CardLinkButton?
public init(
richMedia: UIView,
headline: String? = nil,
title: String? = nil,
pretitle: String? = nil,
descriptionTitle: String,
button: CardButton? = nil,
link: CardLinkButton? = nil
) {
self.richMedia = richMedia
self.headline = headline
self.title = title
self.pretitle = pretitle
self.descriptionTitle = descriptionTitle
self.button = button
self.link = link
}
}
// MARK: MediaCard
public class MediaCard: UIView {
private enum Constants {
static let cardLayoutMargins = NSDirectionalEdgeInsets(top: 0, leading: 16, bottom: 24, trailing: 16)
static let spacingAfterRichMediaView = CGFloat(8)
}
private lazy var cardAccessibilityElement = UIAccessibilityElement(accessibilityContainer: self)
private var richMediaContainerView = UIView()
private let baseCardView = CardBase()
public var fragmentView: UIView? {
didSet {
baseCardView.fragmentView = fragmentView
}
}
override public var accessibilityElements: [Any]? {
get {
cardAccessibilityElement.accessibilityFrameInContainerSpace = bounds
return [
cardAccessibilityElement,
fragmentView as Any,
baseCardView.buttonsView
].compactMap { $0 }
}
set {}
}
public var contentConfiguration: MediaCardConfiguration? {
didSet {
if let contentConfiguration = contentConfiguration {
configure(with: contentConfiguration)
} else {
configure(with: .emptyConfiguration)
}
}
}
override public init(frame: CGRect) {
super.init(frame: frame)
commomInit()
}
public required init?(coder: NSCoder) {
super.init(coder: coder)
commomInit()
}
override public func layoutSubviews() {
super.layoutSubviews()
setMisticaRadius(.container)
clipsToBounds = true
}
}
// MARK: Public
public extension MediaCard {
var primaryButton: Button {
baseCardView.buttonsView.primaryButton
}
var linkButton: Button {
baseCardView.buttonsView.linkButton
}
override var accessibilityTraits: UIAccessibilityTraits {
get {
cardAccessibilityElement.accessibilityTraits
}
set {
cardAccessibilityElement.accessibilityTraits = newValue
}
}
override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
super.traitCollectionDidChange(previousTraitCollection)
guard traitCollection.userInterfaceStyle != previousTraitCollection?.userInterfaceStyle else { return }
addBorder(color: .border)
}
}
// MARK: Private
private extension MediaCard {
func commomInit() {
layoutViews()
styleViews()
}
func layoutViews() {
directionalLayoutMargins = Constants.cardLayoutMargins
insetsLayoutMarginsFromSafeArea = false
addSubview(richMediaContainerView, constraints: [
richMediaContainerView.topAnchor.constraint(equalTo: topAnchor),
richMediaContainerView.trailingAnchor.constraint(equalTo: trailingAnchor),
richMediaContainerView.leadingAnchor.constraint(equalTo: leadingAnchor)
])
addSubview(baseCardView, constraints: [
baseCardView.topAnchor.constraint(equalTo: richMediaContainerView.bottomAnchor, constant: Constants.spacingAfterRichMediaView),
baseCardView.trailingAnchor.constraint(equalTo: layoutMarginsGuide.trailingAnchor),
baseCardView.bottomAnchor.constraint(equalTo: layoutMarginsGuide.bottomAnchor),
baseCardView.leadingAnchor.constraint(equalTo: layoutMarginsGuide.leadingAnchor)
])
}
func styleViews() {
backgroundColor = .backgroundContainer
baseCardView.contentView.backgroundColor = .backgroundContainer
baseCardView.contentView.headlineTopSpacing = 8
baseCardView.contentView.titleLabel.font = .textPreset1(weight: .cardTitle)
baseCardView.contentView.titleLabel.textColor = .textPrimary
baseCardView.contentView.titleLabel.minHeight = 16
baseCardView.contentView.titleLabel.numberOfLines = 0
baseCardView.contentView.titleTopSpacing = 8
baseCardView.contentView.subtitleLabel.font = .textPreset4(weight: .regular)
baseCardView.contentView.subtitleLabel.textColor = .textPrimary
baseCardView.contentView.subtitleLabel.minHeight = 24
baseCardView.contentView.subtitleLabel.numberOfLines = 0
baseCardView.contentView.subtitleTopSpacing = 4
baseCardView.contentView.descriptionLabel.font = .textPreset2(weight: .regular)
baseCardView.contentView.descriptionLabel.textColor = .textSecondary
baseCardView.contentView.descriptionLabel.minHeight = 20
baseCardView.contentView.descriptionLabel.numberOfLines = 0
baseCardView.contentView.descriptionTitleTopSpacing = 8
richMediaContainerView.clipsToBounds = true
addBorder(color: .border)
}
func configure(with configuration: MediaCardConfiguration) {
richMediaContainerView.subviews.first?.removeFromSuperview()
richMediaContainerView.addSubview(withDefaultConstraints: configuration.richMedia)
baseCardView.headline = configuration.headline
baseCardView.title = configuration.pretitle?.uppercased()
baseCardView.subtitle = configuration.title
baseCardView.descriptionTitle = configuration.descriptionTitle
baseCardView.configureButtons(primaryButton: configuration.button, linkButton: configuration.link)
cardAccessibilityElement.accessibilityLabel = [
baseCardView.headline,
baseCardView.title,
baseCardView.subtitle,
baseCardView.descriptionTitle
].compactMap { $0 }.joined(separator: " ")
}
}
private extension MediaCardConfiguration {
static let emptyConfiguration = MediaCardConfiguration(richMedia: UIView(), descriptionTitle: "")
}