// // LogoutDialogueView.swift // MullvadVPN // // Created by Mojgan on 2023-08-29. // Copyright © 2025 Mullvad VPN AB. All rights reserved. // import UIKit class LogoutDialogueView: UIView { private let containerView: UIView = { let view = UIView() view.backgroundColor = .secondaryColor view.layer.cornerRadius = 11 view.directionalLayoutMargins = UIMetrics.CustomAlert.containerMargins view.clipsToBounds = true return view }() private let messageLabel: UILabel = { let label = UILabel() let message = NSMutableAttributedString( string: NSLocalizedString( "It looks like you’ve entered an account number instead of a voucher code. " + "If you would like to change the active account, please log out first.", comment: "" ) ) message.apply(paragraphStyle: .alert) label.attributedText = message label.font = .preferredFont(forTextStyle: .callout, weight: .semibold) label.numberOfLines = .zero label.textColor = .white return label }() private let logoutButton: AppButton = { let button = AppButton(style: .danger) button.setTitle(NSLocalizedString("Log out", comment: ""), for: .normal) return button }() private var showConstraint: NSLayoutConstraint? private var hideConstraint: NSLayoutConstraint? private var didRequestToLogOut: (LogoutDialogueView) -> Void var isLoading = true { didSet { logoutButton.isEnabled = !isLoading } } override var isHidden: Bool { willSet { if newValue == true { fadeOut() } else { fadeIn() } } } init(didRequestToLogOut: @escaping (LogoutDialogueView) -> Void) { self.didRequestToLogOut = didRequestToLogOut super.init(frame: .zero) setupAppearance() configureUI() addActions() } required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } private func setupAppearance() { containerView.layer.cornerRadius = 11 containerView.backgroundColor = .primaryColor } private func configureUI() { addConstrainedSubviews([containerView]) { containerView.pinEdgesToSuperview(.all().excluding(.bottom)) } containerView.addConstrainedSubviews([messageLabel, logoutButton]) { messageLabel.pinEdgesToSuperviewMargins(.all().excluding(.bottom)) logoutButton.pinEdgesToSuperviewMargins(.all().excluding(.top)) logoutButton.topAnchor.constraint( equalTo: messageLabel.bottomAnchor, constant: UIMetrics.padding16 ).withPriority(.defaultHigh) } showConstraint = containerView.bottomAnchor.constraint(equalTo: bottomAnchor) hideConstraint = containerView.bottomAnchor.constraint(equalTo: topAnchor) hideConstraint?.isActive = true } private func addActions() { logoutButton.addTarget(self, action: #selector(logout), for: .touchUpInside) } @objc private func logout() { didRequestToLogOut(self) } private func fadeIn() { guard hideConstraint?.isActive == true else { return } showConstraint?.isActive = true hideConstraint?.isActive = false animateWith( animations: { self.containerView.alpha = 1.0 }, duration: 0.3, delay: 0.2) } private func fadeOut() { guard showConstraint?.isActive == true else { return } showConstraint?.isActive = false hideConstraint?.isActive = true animateWith( animations: { self.containerView.alpha = 0.0 }, duration: 0.0, delay: 0.0) } private func animateWith( animations: @escaping () -> Void, duration: TimeInterval, delay: TimeInterval ) { UIView.animate( withDuration: duration, delay: delay, options: .curveEaseInOut, animations: { animations() self.layoutIfNeeded() }, completion: nil ) } }