PhoneNumberView.swift 3.47 KB
//
//  PhoneNumberView.swift
//  InterQR-Internship
//
//  Created by Дмитрий Тимофеев on 31.05.2022.
//

import UIKit
import CoreTelephony

class PhoneNumberView: UIView {
    
    var containerPickerView: UIView = {
        var view = UIView()
        view.backgroundColor = .systemRed
        return view
    }()
    
    var titleButton: UIButton = {
        var obj = UIButton()
        obj.setTitle("Select your country", for: .normal)
        obj.setTitleColor(UIColor(red: 50/255, green: 55/255, blue: 85/255, alpha: 1), for: .normal)
        obj.titleLabel?.font = UIFont(name: SkModernistFontType.bold.rawValue, size: 18)
        obj.titleLabel?.textAlignment = .center
        return obj
    }()
    
    var dividingLineView: UIView = {
        var view = UIView()
        view.backgroundColor = UIColor(red: 246/255, green: 246/255, blue: 246/255, alpha: 1)
        return view
    }()
    
    var phoneNumberPicker: UIPickerView = {
        var picker = UIPickerView()
        return picker
    }()
    
    var selectButton: UIButton = {
        var button = UIButton()
        
        button.backgroundColor = UIColor(red: 2/255, green: 115/255, blue: 185/255, alpha: 1)
        button.clipsToBounds = true
        
        return button
    }()
    
    var selectLabel: UILabel = {
        var label = UILabel()
        label.text = "Select"
        label.font = UIFont(name: SkModernistFontType.bold.rawValue, size: 16)
        label.textColor = .white
        return label
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        layout()
        setup()
        backgroundColor = .white
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func layout() {
        self.backgroundColor  = .white
        addSubview(titleButton)
        addSubview(dividingLineView)
        addSubview(phoneNumberPicker)
        addSubview(selectButton)
        
        selectButton.addSubview(selectLabel)
        
        titleButton.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(20)
            make.leading.trailing.equalToSuperview()
        }
        
        dividingLineView.snp.makeConstraints {
            $0.left.right.equalToSuperview()
            $0.height.equalTo(1)
            $0.top.equalTo(titleButton.snp.bottom).offset(22)
        }
        phoneNumberPicker.snp.makeConstraints {
            $0.left.right.equalToSuperview().inset(5)
            $0.top.equalTo(dividingLineView.snp.bottom).offset(20)
        }
        selectButton.snp.makeConstraints {
            $0.right.equalToSuperview()
            $0.left.equalToSuperview()
            $0.bottom.equalToSuperview()
            $0.height.equalTo(70)
            $0.top.equalTo(phoneNumberPicker.snp.bottom).offset(20)
        }
        selectLabel.snp.makeConstraints {
            $0.centerX.equalTo(selectButton.snp.centerX)
            $0.top.equalTo(selectButton.snp.top).offset(20)
        }
    }
}

extension PhoneNumberView {
    
    func setup() {
        
        let countryCode: String
        if let carrier = CTTelephonyNetworkInfo().serviceSubscriberCellularProviders?.first?.value,
           let code = carrier.isoCountryCode?.uppercased() {
            countryCode = code
        } else {
            countryCode = "USA"
        }
        
        phoneNumberPicker.selectRow(0, inComponent: 0, animated: true)
        
        phoneNumberPicker.subviews[1].backgroundColor = .clear
        
    }
}