summaryrefslogtreecommitdiffhomepage
path: root/ios/MullvadVPN/Notifications/UI/NotificationBannerView.swift
blob: ff7f5f016baa9c3bf2e6cefc684a523e2f339954 (plain)
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
//
//  NotificationBannerView.swift
//  MullvadVPN
//
//  Created by pronebird on 01/06/2021.
//  Copyright © 2025 Mullvad VPN AB. All rights reserved.
//

import UIKit

final class NotificationBannerView: UIView {
    private let backgroundView = UIVisualEffectView(effect: UIBlurEffect(style: .dark))

    private let titleLabel: UILabel = {
        let textLabel = UILabel()
        textLabel.font = .mullvadTinySemiBold
        textLabel.adjustsFontForContentSizeCategory = true
        textLabel.textColor = UIColor.InAppNotificationBanner.titleColor
        textLabel.numberOfLines = 0
        textLabel.lineBreakMode = .byWordWrapping
        textLabel.lineBreakStrategy = []
        return textLabel
    }()

    private let bodyLabel: UILabel = {
        let textLabel = UILabel()
        textLabel.font = .mullvadTiny
        textLabel.adjustsFontForContentSizeCategory = true
        textLabel.textColor = UIColor.InAppNotificationBanner.bodyColor
        textLabel.numberOfLines = 0
        textLabel.lineBreakMode = .byWordWrapping
        textLabel.lineBreakStrategy = []
        return textLabel
    }()

    private let indicatorView: UIView = {
        let view = UIView()
        view.backgroundColor = .dangerColor
        view.layer.cornerRadius = UIMetrics.InAppBannerNotification.indicatorSize.width * 0.5
        view.layer.cornerCurve = .circular
        return view
    }()

    private let wrapperView: UIView = {
        let view = UIView()
        view.directionalLayoutMargins = UIMetrics.InAppBannerNotification.layoutMargins
        return view
    }()

    private lazy var bodyStackView: UIStackView = {
        let stackView = UIStackView(arrangedSubviews: [titleLabel, bodyLabel])
        stackView.alignment = .top
        stackView.distribution = .fill
        stackView.axis = .vertical
        stackView.spacing = UIStackView.spacingUseSystem
        return stackView
    }()

    private lazy var contentStackView: UIStackView = {
        let stackView = UIStackView(arrangedSubviews: [bodyStackView, actionButton])
        stackView.spacing = UIStackView.spacingUseSystem
        stackView.alignment = .center
        return stackView
    }()

    private let actionButton: IncreasedHitButton = {
        let button = IncreasedHitButton(type: .system)
        button.tintColor = UIColor.InAppNotificationBanner.actionButtonColor
        return button
    }()

    var title: String? {
        didSet {
            titleLabel.text = title
        }
    }

    var body: NSAttributedString? {
        didSet {
            bodyLabel.attributedText = body
        }
    }

    var style: NotificationBannerStyle = .error {
        didSet {
            indicatorView.backgroundColor = style.color
        }
    }

    var action: InAppNotificationAction? {
        didSet {
            let image = action?.image
            let showsAction = image != nil

            actionButton.setBackgroundImage(image, for: .normal)
            actionButton.widthAnchor.constraint(equalToConstant: 24).isActive = true
            actionButton.heightAnchor.constraint(equalTo: actionButton.widthAnchor).isActive = true
            actionButton.isHidden = !showsAction
        }
    }

    var tapAction: InAppNotificationAction?

    override init(frame: CGRect) {
        super.init(frame: frame)
        addSubviews()
        addTapHandler()
        addActionHandlers()
        addConstraints()
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    private func addTapHandler() {
        let tapGesture = UITapGestureRecognizer(target: self, action: #selector(handleTap))
        addGestureRecognizer(tapGesture)
    }

    private func addActionHandlers() {
        actionButton.addTarget(self, action: #selector(handleActionTap), for: .touchUpInside)
    }

    @objc
    private func handleTap() {
        tapAction?.handler?()
    }

    private func addSubviews() {
        wrapperView.addConstrainedSubviews([indicatorView, contentStackView])
        backgroundView.contentView.addConstrainedSubviews([wrapperView]) {
            wrapperView.pinEdgesToSuperview()
        }
        addConstrainedSubviews([backgroundView]) {
            backgroundView.pinEdgesToSuperview()
        }
    }

    private func addConstraints() {
        NSLayoutConstraint.activate([
            indicatorView.bottomAnchor.constraint(equalTo: titleLabel.firstBaselineAnchor),
            indicatorView.leadingAnchor.constraint(equalTo: wrapperView.layoutMarginsGuide.leadingAnchor),
            indicatorView.widthAnchor
                .constraint(equalToConstant: UIMetrics.InAppBannerNotification.indicatorSize.width),
            indicatorView.heightAnchor
                .constraint(equalToConstant: UIMetrics.InAppBannerNotification.indicatorSize.height),

            contentStackView.topAnchor.constraint(equalTo: wrapperView.layoutMarginsGuide.topAnchor),
            contentStackView.leadingAnchor.constraint(
                equalToSystemSpacingAfter: indicatorView.trailingAnchor,
                multiplier: 1
            ),
            contentStackView.trailingAnchor.constraint(equalTo: wrapperView.layoutMarginsGuide.trailingAnchor),
            contentStackView.bottomAnchor.constraint(equalTo: wrapperView.layoutMarginsGuide.bottomAnchor),
        ])
    }

    @objc private func handleActionTap() {
        action?.handler?()
    }
}

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
        }
    }
}