[ios] add UndoToast
class
Signed-off-by: Kiryl Kaveryn <kirylkaveryn@gmail.com>
This commit is contained in:
parent
9acd327f35
commit
6a8b8e3f05
1 changed files with 66 additions and 0 deletions
|
@ -38,6 +38,7 @@ extension Toast {
|
|||
}
|
||||
|
||||
@objc func show() {
|
||||
Self.hideAll()
|
||||
show(in: UIApplication.shared.keyWindow, alignment: .bottom, duration: Toast.duration)
|
||||
}
|
||||
|
||||
|
@ -100,6 +101,11 @@ extension Toast {
|
|||
@objc static func undoToast(withText text: String, undoAction: @escaping () -> Void) -> Toast {
|
||||
UndoToast(text, undoAction: undoAction)
|
||||
}
|
||||
|
||||
@objc static func undoToast(deletedObject: String, undoAction: @escaping () -> Void) -> Toast {
|
||||
// TODO: localize text
|
||||
undoToast(withText: "The \(deletedObject) was deleted", undoAction: undoAction)
|
||||
}
|
||||
}
|
||||
|
||||
private final class DefaultToast: Toast {
|
||||
|
@ -134,3 +140,63 @@ private final class DefaultToast: Toast {
|
|||
])
|
||||
}
|
||||
}
|
||||
|
||||
private final class UndoToast: Toast {
|
||||
|
||||
private let messageLabel = UILabel()
|
||||
private let undoButton = UIButton()
|
||||
private var undoAction: (() -> Void)?
|
||||
|
||||
fileprivate convenience init(_ text: String, undoAction: @escaping () -> Void) {
|
||||
self.init()
|
||||
self.undoAction = undoAction
|
||||
setupMessageLabel(text)
|
||||
setupUndoButton()
|
||||
layoutViews()
|
||||
}
|
||||
|
||||
private func setupMessageLabel(_ text: String) {
|
||||
messageLabel.text = text
|
||||
messageLabel.textAlignment = .center
|
||||
messageLabel.numberOfLines = 0
|
||||
messageLabel.font = .regular14()
|
||||
messageLabel.textColor = .white
|
||||
messageLabel.isUserInteractionEnabled = false
|
||||
}
|
||||
|
||||
private func setupUndoButton() {
|
||||
undoButton.setTitle(L("undo"), for: .normal)
|
||||
undoButton.setTitleColor(.white, for: .normal)
|
||||
undoButton.setTitleColor(.lightGray, for: .highlighted)
|
||||
undoButton.titleLabel?.font = .bold17()
|
||||
undoButton.backgroundColor = .clear
|
||||
undoButton.addTarget(self, action: #selector(undoButtonDidTap), for: .touchUpInside)
|
||||
}
|
||||
|
||||
@objc private func undoButtonDidTap() {
|
||||
undoAction?()
|
||||
self.hide()
|
||||
}
|
||||
|
||||
private func layoutViews() {
|
||||
contentView.addSubview(messageLabel)
|
||||
contentView.addSubview(undoButton)
|
||||
|
||||
messageLabel.translatesAutoresizingMaskIntoConstraints = false
|
||||
undoButton.translatesAutoresizingMaskIntoConstraints = false
|
||||
|
||||
messageLabel.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
|
||||
NSLayoutConstraint.activate([
|
||||
contentView.heightAnchor.constraint(greaterThanOrEqualToConstant: 40),
|
||||
|
||||
messageLabel.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 12),
|
||||
messageLabel.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 8),
|
||||
messageLabel.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -8),
|
||||
|
||||
undoButton.leadingAnchor.constraint(equalTo: messageLabel.trailingAnchor, constant: 20),
|
||||
undoButton.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -25),
|
||||
undoButton.centerYAnchor.constraint(equalTo: messageLabel.centerYAnchor),
|
||||
undoButton.heightAnchor.constraint(equalToConstant: 30),
|
||||
])
|
||||
}
|
||||
}
|
||||
|
|
Reference in a new issue