Skip to content
Snippets Groups Projects
ScanView.swift 3.82 KiB
Newer Older
Bruno Muniz's avatar
Bruno Muniz committed
import UIKit
import Shared

final class ScanView: UIView {
Ahmed Shehata's avatar
Ahmed Shehata committed
    private let statusLabel = UILabel()
    private let imageView = UIImageView()
    private let stackView = UIStackView()
    private let animationView = DotAnimation()
    private let overlayView = ScanOverlayView()
    private(set) var actionButton = CapsuleButton()
Bruno Muniz's avatar
Bruno Muniz committed

    init() {
        super.init(frame: .zero)
Ahmed Shehata's avatar
Ahmed Shehata committed
        imageView.contentMode = .center
Bruno Muniz's avatar
Bruno Muniz committed
        actionButton.setStyle(.brandColored)

        statusLabel.numberOfLines = 0
        statusLabel.textAlignment = .center
        statusLabel.textColor = Asset.neutralWhite.color
        statusLabel.font = Fonts.Mulish.regular.font(size: 14.0)

        stackView.spacing = 15
        stackView.axis = .vertical
        stackView.addArrangedSubview(animationView)
Ahmed Shehata's avatar
Ahmed Shehata committed
        stackView.addArrangedSubview(imageView)
Bruno Muniz's avatar
Bruno Muniz committed
        stackView.addArrangedSubview(statusLabel)
        stackView.addArrangedSubview(actionButton)

Ahmed Shehata's avatar
Ahmed Shehata committed
        imageView.isHidden = true
Bruno Muniz's avatar
Bruno Muniz committed
        actionButton.isHidden = true
Ahmed Shehata's avatar
Ahmed Shehata committed
        animationView.isHidden = false
Bruno Muniz's avatar
Bruno Muniz committed

Ahmed Shehata's avatar
Ahmed Shehata committed
        addSubview(overlayView)
Bruno Muniz's avatar
Bruno Muniz committed
        addSubview(stackView)

Ahmed Shehata's avatar
Ahmed Shehata committed
        overlayView.snp.makeConstraints {
            $0.top.equalToSuperview()
            $0.left.equalToSuperview()
            $0.right.equalToSuperview()
            $0.bottom.equalToSuperview()
Bruno Muniz's avatar
Bruno Muniz committed
        }

Ahmed Shehata's avatar
Ahmed Shehata committed
        stackView.snp.makeConstraints {
            $0.left.equalToSuperview().offset(57)
            $0.right.equalToSuperview().offset(-57)
            $0.bottom.equalTo(safeAreaLayoutGuide).offset(-100)
Bruno Muniz's avatar
Bruno Muniz committed
        }
    }

    required init?(coder: NSCoder) { nil }

    func update(with state: ScanStatus) {
        var text: String

        switch state {
        case .reading, .processing:
Ahmed Shehata's avatar
Ahmed Shehata committed
            imageView.isHidden = true
Bruno Muniz's avatar
Bruno Muniz committed
            actionButton.isHidden = true
            text = Localized.Scan.Status.reading
Ahmed Shehata's avatar
Ahmed Shehata committed
            overlayView.updateCornerColor(Asset.brandPrimary.color)
Bruno Muniz's avatar
Bruno Muniz committed

        case .success:
            animationView.isHidden = true
            actionButton.isHidden = true
Ahmed Shehata's avatar
Ahmed Shehata committed
            imageView.isHidden = false
            imageView.image = Asset.sharedSuccess.image
Bruno Muniz's avatar
Bruno Muniz committed
            text = Localized.Scan.Status.success
Ahmed Shehata's avatar
Ahmed Shehata committed
            overlayView.updateCornerColor(Asset.accentSuccess.color)
Bruno Muniz's avatar
Bruno Muniz committed

        case .failed(let error):
            animationView.isHidden = true
Ahmed Shehata's avatar
Ahmed Shehata committed
            imageView.image = Asset.scanError.image
            imageView.isHidden = false
            overlayView.updateCornerColor(Asset.accentDanger.color)
Bruno Muniz's avatar
Bruno Muniz committed

            switch error {
            case .requestOpened:
                text = Localized.Scan.Error.requested
                actionButton.setTitle(Localized.Scan.requests, for: .normal)
                actionButton.isHidden = false

            case .alreadyFriends(let name):
Bruno Muniz's avatar
Bruno Muniz committed
                text = Localized.Scan.Error.alreadyFriends(name)
Bruno Muniz's avatar
Bruno Muniz committed
                actionButton.setTitle(Localized.Scan.contact, for: .normal)
                actionButton.isHidden = false

            case .cameraPermission:
Bruno Muniz's avatar
Bruno Muniz committed
                text = Localized.Scan.Error.cameraPermissionNeeded
Bruno Muniz's avatar
Bruno Muniz committed
                actionButton.setTitle(Localized.Scan.settings, for: .normal)
                actionButton.isHidden = false

            case .unknown(let content):
                text = content
            }
        }

        let attString = NSMutableAttributedString(string: text)
        let paragraph = NSMutableParagraphStyle()
        paragraph.alignment = .center
        paragraph.lineHeightMultiple = 1.35

        attString.addAttribute(.paragraphStyle, value: paragraph)
        attString.addAttribute(.foregroundColor, value: Asset.neutralWhite.color)
Ahmed Shehata's avatar
Ahmed Shehata committed
        attString.addAttribute(.font, value: Fonts.Mulish.regular.font(size: 14.0) as Any)
Bruno Muniz's avatar
Bruno Muniz committed

        if text.contains("#") {
            attString.addAttribute(name: .foregroundColor, value: Asset.brandPrimary.color, betweenCharacters: "#")
        }

        statusLabel.attributedText = attString
    }
}