<commit_before>
//
//  PersianPickerView.swift
//  PersianPickerView
//
//  Created by Farhad on 5/7/17.
//  Copyright © 2017 Farhad. All rights reserved.
//

import UIKit

@IBDesignable
public class PersianDatePickerView: UIView {
	//MARK: - Inspectables
	@IBInspectable var fontColor: UIColor = .black
	//MARK: - Properties
	public var year = 1300
	public var month = 1
	public var day = 1

	fileprivate let pickerView = UIPickerView()
	fileprivate let persianDateDataSource = PersianDateDataSource()
	
	public var onChange: (Listener)?
	public var font: UIFont?

    public var pickerStyle: PersianDatePickerStyle = .long {
		didSet {
            pickerView.reloadAllComponents()
        }
    }
	
	
	//MARK: - Initialization
    override public init(frame: CGRect) {
        super.init(frame: frame)
        setup()
    }
    
    required public init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setup()
    }
	
	convenience public init(date: Date, timeZone: TimeZone? = nil) {
		self.init()
		setup()
		
		var cal = Calendar(identifier: .persian)
		timeZone.map { cal.timeZone = $0 }
		
		let comps = cal.dateComponents([.day, .month, .year], from: date)
		(year, month, day) = (comps.year!, comps.month!, comps.day!)
	}
    
    private func setup() {
		setupPicker()
		gotoCurrentDate()
    }
	
	
	private func setupPicker() {
		addSubview(pickerView)
		pickerView.dataSource = self
		pickerView.delegate = self
	}
	
    
    public func gotoCurrentDate() {	
        let currentDate = persianDateDataSource.getDateToday()
		year = currentDate.year
		month = currentDate.month
		
		let yearIndex = persianDateDataSource.years.index(of: year)!
		let monthIndex = month - 1
		
		pickerView.selectRow(yearIndex, inComponent: 0, animated: true)
		pickerView.selectRow(monthIndex, inComponent: 1, animated: true)

		if pickerStyle == .long {
			day = currentDate.day
			pickerView.selectRow(day - 1, inComponent: 2, animated: true)
		}
    }
	
	
    override public func layoutSubviews() {
        super.layoutSubviews()
        pickerView.translatesAutoresizingMaskIntoConstraints = false
        pickerView.topAnchor.constraint(equalTo: topAnchor, constant: 0).isActive = true
        pickerView.rightAnchor.constraint(equalTo: rightAnchor, constant: 0).isActive = true
        pickerView.bottomAnchor.constraint(equalTo: bottomAnchor, constant: 0).isActive = true
        pickerView.leftAnchor.constraint(equalTo: leftAnchor, constant: 0).isActive = true
    }
}


//MARK: - PickerView DataSource
extension PersianDatePickerView: UIPickerViewDataSource {
    public func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return pickerStyle.numberOfComponents
    }
	
	
    public func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        switch component {
        case 0:
            return persianDateDataSource.years.count
        case 1:
            return persianDateDataSource.persianMonths.count
        default:
			return persianDateDataSource.daysRange(ofYear: year, month: month).count
        }
    }
}


//MARK: - PickerView Delegate
extension PersianDatePickerView: UIPickerViewDelegate {
	public func pickerView(_ pickerView: UIPickerView,
						   viewForRow row: Int,
						   forComponent component: Int,
						   reusing view: UIView?) -> UIView {
		let label = UILabel()
		label.textAlignment = .center
		label.textColor = fontColor
		let format = NumberFormatter()
		format.locale = Locale(identifier: "fa_IR")
		
		if let font = self.font {
			label.font = font
		}
		
		switch component {
		case 0:
			let year = String(persianDateDataSource.years[row])
			let persianNumber = format.number(from: year)
			label.text = format.string(from: persianNumber!)
			
		case 1:
			label.text = persianDateDataSource.persianMonths[row]
			
		case 2:
			let day = String(persianDateDataSource.daysRange(ofYear: year, month: month)[row])
			label.text = format.string(from: format.number(from: day)!)
			
		default:
			break
		}
		
		return label
	}
	
	
	public func pickerView(_ pickerView: UIPickerView,
						   didSelectRow row: Int,
						   inComponent component: Int) {
		
		switch component {
		case 0:
			year = persianDateDataSource.years[row]
		case 1:
			month = row + 1
		case 2:
			day = persianDateDataSource.daysRange(ofYear: year, month: month)[row]
		default:
			break
		}
		
		if pickerStyle == .long {
			pickerView.reloadComponent(2)
		}
		
		self.onChange?(year, month, day)
	}
}

public typealias Listener = (_ year: Int, _ month: Int, _ day: Int) -> Void
public enum PersianDatePickerStyle {
	case short
	case long<commit_msg>shorten pickerStyle to style to be more concise<commit_after>
//
//  PersianPickerView.swift
//  PersianPickerView
//
//  Created by Farhad on 5/7/17.
//  Copyright © 2017 Farhad. All rights reserved.
//

import UIKit

@IBDesignable
public class PersianDatePickerView: UIView {
	//MARK: - Inspectables
	@IBInspectable var fontColor: UIColor = .black
	//MARK: - Properties
	public var year = 1300
	public var month = 1
	public var day = 1

	fileprivate let pickerView = UIPickerView()
	fileprivate let persianDateDataSource = PersianDateDataSource()
	
	public var onChange: (Listener)?
	public var font: UIFont?

    public var style: PersianDatePickerStyle = .long {
		didSet {
            pickerView.reloadAllComponents()
        }
    }
	
	
	//MARK: - Initialization
    override public init(frame: CGRect) {
        super.init(frame: frame)
        setup()
    }
    
    required public init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setup()
    }
	
	convenience public init(date: Date, timeZone: TimeZone? = nil) {
		self.init()
		setup()
		
		var cal = Calendar(identifier: .persian)
		timeZone.map { cal.timeZone = $0 }
		
		let comps = cal.dateComponents([.day, .month, .year], from: date)
		(year, month, day) = (comps.year!, comps.month!, comps.day!)
	}
    
    private func setup() {
		setupPicker()
		gotoCurrentDate()
    }
	
	
	private func setupPicker() {
		addSubview(pickerView)
		pickerView.dataSource = self
		pickerView.delegate = self
	}
	
    
    public func gotoCurrentDate() {	
        let currentDate = persianDateDataSource.getDateToday()
		year = currentDate.year
		month = currentDate.month
		
		let yearIndex = persianDateDataSource.years.index(of: year)!
		let monthIndex = month - 1
		
		pickerView.selectRow(yearIndex, inComponent: 0, animated: true)
		pickerView.selectRow(monthIndex, inComponent: 1, animated: true)

		if style == .long {
			day = currentDate.day
			pickerView.selectRow(day - 1, inComponent: 2, animated: true)
		}
    }
	
	
    override public func layoutSubviews() {
        super.layoutSubviews()
        pickerView.translatesAutoresizingMaskIntoConstraints = false
        pickerView.topAnchor.constraint(equalTo: topAnchor, constant: 0).isActive = true
        pickerView.rightAnchor.constraint(equalTo: rightAnchor, constant: 0).isActive = true
        pickerView.bottomAnchor.constraint(equalTo: bottomAnchor, constant: 0).isActive = true
        pickerView.leftAnchor.constraint(equalTo: leftAnchor, constant: 0).isActive = true
    }
}


//MARK: - PickerView DataSource
extension PersianDatePickerView: UIPickerViewDataSource {
    public func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return style.numberOfComponents
    }
	
	
    public func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        switch component {
        case 0:
            return persianDateDataSource.years.count
        case 1:
            return persianDateDataSource.persianMonths.count
        default:
			return persianDateDataSource.daysRange(ofYear: year, month: month).count
        }
    }
}


//MARK: - PickerView Delegate
extension PersianDatePickerView: UIPickerViewDelegate {
	public func pickerView(_ pickerView: UIPickerView,
						   viewForRow row: Int,
						   forComponent component: Int,
						   reusing view: UIView?) -> UIView {
		let label = UILabel()
		label.textAlignment = .center
		label.textColor = fontColor
		let format = NumberFormatter()
		format.locale = Locale(identifier: "fa_IR")
		
		if let font = self.font {
			label.font = font
		}
		
		switch component {
		case 0:
			let year = String(persianDateDataSource.years[row])
			let persianNumber = format.number(from: year)
			label.text = format.string(from: persianNumber!)
			
		case 1:
			label.text = persianDateDataSource.persianMonths[row]
			
		case 2:
			let day = String(persianDateDataSource.daysRange(ofYear: year, month: month)[row])
			label.text = format.string(from: format.number(from: day)!)
			
		default:
			break
		}
		
		return label
	}
	
	
	public func pickerView(_ pickerView: UIPickerView,
						   didSelectRow row: Int,
						   inComponent component: Int) {
		
		switch component {
		case 0:
			year = persianDateDataSource.years[row]
		case 1:
			month = row + 1
		case 2:
			day = persianDateDataSource.daysRange(ofYear: year, month: month)[row]
		default:
			break
		}
		
		if style == .long {
			pickerView.reloadComponent(2)
		}
		
		self.onChange?(year, month, day)
	}
}

public typealias Listener = (_ year: Int, _ month: Int, _ day: Int) -> Void
public enum PersianDatePickerStyle {
	case short
	case long
{'fraction_non_alphanumeric': 0.09645542427497315, 'fraction_numerical': 0.007303974221267454, 'mean_word_length': 3.1272163120567376, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '968308', 'n_tokens_mistral': 3256, 'n_tokens_neox': 2812, 'n_words': 724}