diff --git a/iphone/Maps/Classes/CustomAlert/Toast/Toast.swift b/iphone/Maps/Classes/CustomAlert/Toast/Toast.swift index c610aa3ecd..7e5e514be3 100644 --- a/iphone/Maps/Classes/CustomAlert/Toast/Toast.swift +++ b/iphone/Maps/Classes/CustomAlert/Toast/Toast.swift @@ -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), + ]) + } +}