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
|
//
// NotificationBannerView.swift
// MullvadVPN
//
// Created by pronebird on 01/06/2021.
// Copyright © 2021 Mullvad VPN AB. All rights reserved.
//
import UIKit
final class NotificationBannerView: UIView {
private static let indicatorViewSize = CGSize(width: 12, height: 12)
private let backgroundView: UIVisualEffectView = {
let effect = UIBlurEffect(style: .dark)
let visualEffectView = UIVisualEffectView(effect: effect)
visualEffectView.translatesAutoresizingMaskIntoConstraints = false
return visualEffectView
}()
private let titleLabel: UILabel = {
let textLabel = UILabel()
textLabel.translatesAutoresizingMaskIntoConstraints = false
textLabel.font = UIFont.systemFont(ofSize: 17, weight: .bold)
textLabel.textColor = UIColor.InAppNotificationBanner.titleColor
textLabel.numberOfLines = 0
textLabel.lineBreakMode = .byWordWrapping
if #available(iOS 14.0, *) {
// See: https://stackoverflow.com/q/46200027/351305
textLabel.lineBreakStrategy = []
}
return textLabel
}()
private let bodyLabel: UILabel = {
let textLabel = UILabel()
textLabel.translatesAutoresizingMaskIntoConstraints = false
textLabel.font = UIFont.systemFont(ofSize: 17)
textLabel.textColor = UIColor.InAppNotificationBanner.bodyColor
textLabel.numberOfLines = 0
textLabel.lineBreakMode = .byWordWrapping
if #available(iOS 14.0, *) {
// See: https://stackoverflow.com/q/46200027/351305
textLabel.lineBreakStrategy = []
}
return textLabel
}()
private let indicatorView: UIView = {
let view = UIView()
view.translatesAutoresizingMaskIntoConstraints = false
view.backgroundColor = .dangerColor
view.layer.cornerRadius = NotificationBannerView.indicatorViewSize.width * 0.5
view.layer.cornerCurve = .circular
return view
}()
private let wrapperView: UIView = {
let view = UIView()
view.translatesAutoresizingMaskIntoConstraints = false
view.layoutMargins = UIMetrics.inAppBannerNotificationLayoutMargins
return view
}()
var title: String? {
didSet {
titleLabel.text = title
}
}
var body: String? {
didSet {
bodyLabel.text = body
}
}
var style: NotificationBannerStyle = .error {
didSet {
indicatorView.backgroundColor = style.color
}
}
override init(frame: CGRect) {
super.init(frame: frame)
for subview in [titleLabel, bodyLabel, indicatorView] {
wrapperView.addSubview(subview)
}
backgroundView.contentView.addSubview(wrapperView)
addSubview(backgroundView)
NSLayoutConstraint.activate([
backgroundView.topAnchor.constraint(equalTo: topAnchor),
backgroundView.leadingAnchor.constraint(equalTo: leadingAnchor),
backgroundView.trailingAnchor.constraint(equalTo: trailingAnchor),
backgroundView.bottomAnchor.constraint(equalTo: bottomAnchor),
wrapperView.topAnchor.constraint(equalTo: backgroundView.contentView.topAnchor),
wrapperView.leadingAnchor.constraint(equalTo: backgroundView.contentView.leadingAnchor),
wrapperView.trailingAnchor
.constraint(equalTo: backgroundView.contentView.trailingAnchor),
wrapperView.bottomAnchor.constraint(equalTo: backgroundView.contentView.bottomAnchor),
indicatorView.bottomAnchor.constraint(equalTo: titleLabel.firstBaselineAnchor),
indicatorView.leadingAnchor
.constraint(equalTo: wrapperView.layoutMarginsGuide.leadingAnchor),
indicatorView.widthAnchor.constraint(equalToConstant: Self.indicatorViewSize.width),
indicatorView.heightAnchor.constraint(equalToConstant: Self.indicatorViewSize.height),
titleLabel.topAnchor.constraint(equalTo: wrapperView.layoutMarginsGuide.topAnchor),
titleLabel.leadingAnchor.constraint(
equalToSystemSpacingAfter: indicatorView.trailingAnchor,
multiplier: 1
),
titleLabel.trailingAnchor
.constraint(equalTo: wrapperView.layoutMarginsGuide.trailingAnchor),
bodyLabel.topAnchor.constraint(
equalToSystemSpacingBelow: titleLabel.bottomAnchor,
multiplier: 1
),
bodyLabel.leadingAnchor.constraint(equalTo: titleLabel.leadingAnchor),
bodyLabel.trailingAnchor.constraint(equalTo: titleLabel.trailingAnchor),
bodyLabel.bottomAnchor.constraint(equalTo: wrapperView.layoutMarginsGuide.bottomAnchor),
])
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
private extension NotificationBannerStyle {
var color: UIColor {
switch self {
case .success:
return UIColor.InAppNotificationBanner.successIndicatorColor
case .warning:
return UIColor.InAppNotificationBanner.warningIndicatorColor
case .error:
return UIColor.InAppNotificationBanner.errorIndicatorColor
}
}
}
|