|
| 1 | +// |
| 2 | +// ALMonthAndYearPicker.swift |
| 3 | +// ALFormInput_Example |
| 4 | +// |
| 5 | +// Created by AppLogist on 14.04.2020. |
| 6 | +// Copyright © 2020 CocoaPods. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +import ActionSheetPicker_3_0 |
| 10 | + |
| 11 | +public enum ALMonthAndYearPickerFormat { |
| 12 | + /// 01/2020 |
| 13 | + case withNumeric |
| 14 | + /// January 2020 |
| 15 | + case withMonthSymbol |
| 16 | +} |
| 17 | + |
| 18 | +public class ALMonthAndYearPicker: ALDatePicker { |
| 19 | + |
| 20 | + // Formats: 01/2020, January 2020 |
| 21 | + |
| 22 | + private var monthSymbols = DateFormatter().monthSymbols |
| 23 | + private var format: ALMonthAndYearPickerFormat? = .withNumeric |
| 24 | + |
| 25 | + private var minYear: Int? = Calendar.current.dateComponents([.year], from: Date()).year ?? 0 |
| 26 | + private var maxYear: Int? = (Calendar.current.dateComponents([.year], from: Date()).year ?? 0) + 20 |
| 27 | + private var selectedIndexes: [Int]? |
| 28 | + |
| 29 | + |
| 30 | + /// You can get a certain day with setting this variable. Default value is **1** |
| 31 | + public var day: Int = 1 |
| 32 | + |
| 33 | + private func convertToDate(month: Int, year: Int) -> Date? { |
| 34 | + let components = DateComponents(calendar: .current, |
| 35 | + timeZone: .current, |
| 36 | + year: year, |
| 37 | + month: month, |
| 38 | + day: day) |
| 39 | + return Calendar.current.date(from: components) |
| 40 | + } |
| 41 | + |
| 42 | + |
| 43 | + /// You can setup datepicker minimum year and maximum year |
| 44 | + /// - Parameters: |
| 45 | + /// - minYear: Minimum Year |
| 46 | + /// - maxYear: Maximum Year |
| 47 | + /// - format: Format for display (default `.withNumeric`) |
| 48 | + public func setup(minYear: Int? = Calendar.current.dateComponents([.year], from: Date()).year ?? 0, |
| 49 | + maxYear: Int? = (Calendar.current.dateComponents([.year], from: Date()).year ?? 0) + 20, |
| 50 | + format: ALMonthAndYearPickerFormat = .withNumeric) { |
| 51 | + self.minYear = minYear |
| 52 | + self.maxYear = maxYear |
| 53 | + self.format = format |
| 54 | + } |
| 55 | + |
| 56 | + |
| 57 | + |
| 58 | + private func showActionSheetDatePicker() { |
| 59 | + guard let minYear = minYear, let maxYear = maxYear else { return } |
| 60 | + let years = (minYear...maxYear).map { "\($0)"} |
| 61 | + let months = format == .withNumeric ? monthSymbols?.compactMap{ String((monthSymbols?.index(of: $0) ?? 0) + 1)} : (monthSymbols ?? []) |
| 62 | + let picker = ActionSheetMultipleStringPicker(title: title, |
| 63 | + rows: [months ?? [], years ], |
| 64 | + initialSelection: selectedIndexes ?? [0,0] , |
| 65 | + doneBlock: { [weak self] (_, indexes, _) in |
| 66 | + guard let self = self else { return } |
| 67 | + guard let indexes = indexes as? [Int] else { return } |
| 68 | + self.selectedIndexes = indexes |
| 69 | + let month = months?[indexes[0]] ?? "" |
| 70 | + |
| 71 | + let year = years[indexes[1]] |
| 72 | + |
| 73 | + if let year = Int(year), |
| 74 | + let monthIndex = indexes.first, |
| 75 | + let selectedDate = self.convertToDate(month: monthIndex + 1, |
| 76 | + year: year) { |
| 77 | + self.datePickerDelegate?.didSelectDate(self, selectedDate: selectedDate) |
| 78 | + self.selectedDate = selectedDate |
| 79 | + } |
| 80 | + |
| 81 | + if self.format == .withNumeric { |
| 82 | + let month = String(format:"%02d",(indexes.first ?? 0) + 1) |
| 83 | + self.text = "\(month)/\(year)" |
| 84 | + return |
| 85 | + } |
| 86 | + self.text = "\(month) \(year)" |
| 87 | + |
| 88 | + }, |
| 89 | + cancel: nil, |
| 90 | + origin: self) |
| 91 | + |
| 92 | + |
| 93 | + picker?.addButtons() |
| 94 | + picker?.show() |
| 95 | + } |
| 96 | + |
| 97 | + public override func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool { |
| 98 | + showActionSheetDatePicker() |
| 99 | + return false |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | + |
0 commit comments