-
Notifications
You must be signed in to change notification settings - Fork 417
/
ActionMessageView.swift
189 lines (155 loc) · 7.35 KB
/
ActionMessageView.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
//
// ActionMessageView.swift
// DuckDuckGo
//
// Copyright © 2021 DuckDuckGo. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import UIKit
extension ActionMessageView: NibLoading {}
class ActionMessageView: UIView {
enum PresentationLocation {
case withBottomBar(andAddressBarBottom: Bool)
case withoutBottomBar
}
private static var presentedMessages = [ActionMessageView]()
private enum Constants {
static var maxWidth: CGFloat = 346
static var minimumHorizontalPadding: CGFloat = 20
static var cornerRadius: CGFloat = 10
static var animationDuration: TimeInterval = 0.2
static var duration: TimeInterval = 3.0
static var windowBottomPaddingWithBottomBar: CGFloat {
if UIDevice.current.userInterfaceIdiom == .phone && !isPortrait {
return 40
}
return 70
}
static var windowBottomPaddingWithAddressBar: CGFloat {
return windowBottomPaddingWithBottomBar + 52
}
static var windowBottomPaddingWithoutBottomBar: CGFloat {
return 0
}
}
private static func bottomPadding(for location: PresentationLocation) -> CGFloat {
switch location {
case .withBottomBar(let isAddressBarBottom):
return isAddressBarBottom ? Constants.windowBottomPaddingWithAddressBar : Constants.windowBottomPaddingWithBottomBar
case .withoutBottomBar:
return Constants.windowBottomPaddingWithoutBottomBar
}
}
@IBOutlet weak var message: UILabel!
@IBOutlet weak var actionButton: UIButton!
@IBOutlet var labelToButton: NSLayoutConstraint!
@IBOutlet var labelToTrailing: NSLayoutConstraint!
private var action: () -> Void = {}
private var onDidDismiss: () -> Void = {}
private var dismissWorkItem: DispatchWorkItem?
static func loadFromXib() -> ActionMessageView {
return ActionMessageView.load(nibName: "ActionMessageView")
}
override func awakeFromNib() {
super.awakeFromNib()
layer.cornerRadius = Constants.cornerRadius
}
static func present(message: NSAttributedString,
numberOfLines: Int = 0,
actionTitle: String? = nil,
presentationLocation: PresentationLocation = .withBottomBar(andAddressBarBottom: false),
onAction: @escaping () -> Void = {},
onDidDismiss: @escaping () -> Void = {}) {
let messageView = loadFromXib()
messageView.message.attributedText = message
messageView.message.numberOfLines = numberOfLines
ActionMessageView.present(messageView: messageView,
message: message.string,
actionTitle: actionTitle,
presentationLocation: presentationLocation,
onAction: onAction,
onDidDismiss: onDidDismiss)
}
static func present(message: String,
actionTitle: String? = nil,
presentationLocation: PresentationLocation = .withBottomBar(andAddressBarBottom: false),
onAction: @escaping () -> Void = {},
onDidDismiss: @escaping () -> Void = {}) {
let messageView = loadFromXib()
messageView.message.setAttributedTextString(message)
ActionMessageView.present(messageView: messageView,
message: message,
actionTitle: actionTitle,
presentationLocation: presentationLocation,
onAction: onAction,
onDidDismiss: onDidDismiss)
}
private static func present(messageView: ActionMessageView,
message: String,
actionTitle: String? = nil,
presentationLocation: PresentationLocation = .withBottomBar(andAddressBarBottom: false),
onAction: @escaping () -> Void = {},
onDidDismiss: @escaping () -> Void = {}) {
guard let window = UIApplication.shared.windows.filter({ $0.isKeyWindow }).first else { return }
dismissAllMessages()
if let actionTitle = actionTitle, let title = messageView.actionButton.attributedTitle(for: .normal) {
messageView.actionButton.setAttributedTitle(title.withText(actionTitle), for: .normal)
messageView.action = onAction
} else {
messageView.labelToButton.isActive = false
messageView.labelToTrailing.isActive = true
messageView.actionButton.isHidden = true
}
messageView.onDidDismiss = onDidDismiss
window.addSubview(messageView)
window.safeAreaLayoutGuide.bottomAnchor.constraint(equalTo: messageView.bottomAnchor,
constant: bottomPadding(for: presentationLocation)).isActive = true
let messageViewWidth = window.frame.width <= Constants.maxWidth ? window.frame.width - Constants.minimumHorizontalPadding : Constants.maxWidth
messageView.widthAnchor.constraint(equalToConstant: messageViewWidth).isActive = true
messageView.centerXAnchor.constraint(equalTo: window.centerXAnchor).isActive = true
window.layoutIfNeeded()
messageView.alpha = 0
UIView.animate(withDuration: Constants.animationDuration) {
messageView.alpha = 1
} completion: { _ in
UIAccessibility.post(notification: .announcement, argument: message)
}
let workItem = DispatchWorkItem { [weak messageView] in
messageView?.dismissAndFadeOut()
}
messageView.dismissWorkItem = workItem
DispatchQueue.main.asyncAfter(deadline: .now() + Constants.duration, execute: workItem)
presentedMessages.append(messageView)
}
static func dismissAllMessages() {
presentedMessages.forEach { $0.dismissAndFadeOut() }
}
func dismissAndFadeOut() {
dismissWorkItem?.cancel()
dismissWorkItem = nil
UIView.animate(withDuration: Constants.animationDuration, animations: {
self.alpha = 0
}, completion: { _ in
self.removeFromSuperview()
if let position = Self.presentedMessages.firstIndex(of: self) {
Self.presentedMessages.remove(at: position)
}
self.onDidDismiss()
})
}
@IBAction func onButtonTap() {
action()
dismissAndFadeOut()
}
}