diff --git a/handMadeCalendarAdvance/NewCalendarViewController.swift b/handMadeCalendarAdvance/NewCalendarViewController.swift index 11b8ab3..11845cc 100644 --- a/handMadeCalendarAdvance/NewCalendarViewController.swift +++ b/handMadeCalendarAdvance/NewCalendarViewController.swift @@ -14,6 +14,21 @@ class NewCalendarViewController: UIViewController { // 日本の祝祭日判定用のインスタンス private let holidayObject: CalculateCalendarLogic = CalculateCalendarLogic() + private var stringDateFormatter: DateFormatter = { + let dateFormatter = DateFormatter() + dateFormatter.locale = Locale(identifier: "ja_JP") + dateFormatter.timeZone = TimeZone.current + dateFormatter.dateFormat = "yyyy/MM/dd" + return dateFormatter + }() + + private var currentCalendar: Calendar = { + var calendar = Calendar.current + calendar.locale = Locale(identifier: "ja_JP") + calendar.timeZone = TimeZone.current + return calendar + }() + override func viewDidLoad() { super.viewDidLoad() setupCalendarView() @@ -25,9 +40,13 @@ class NewCalendarViewController: UIViewController { if #available(iOS 16.0, *) { let calendarView = UICalendarView() calendarView.calendar = Calendar(identifier: .gregorian) - calendarView.locale = Locale(identifier: "ja") + calendarView.locale = Locale(identifier: "ja_JP") calendarView.delegate = self - + // 例. 任意の日付を1つ選択可能にする + // 補足: 複数日付を選択する場合はこちらをする + // https://developer.apple.com/documentation/uikit/uicalendarselectionmultidate + let dateSelection = UICalendarSelectionSingleDate(delegate: self) + calendarView.selectionBehavior = dateSelection view.addSubview(calendarView) calendarView.translatesAutoresizingMaskIntoConstraints = false calendarView.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.topAnchor).isActive = true @@ -64,14 +83,59 @@ extension NewCalendarViewController: UICalendarViewDelegate { @available(iOS 16.0, *) func calendarView(_ calendarView: UICalendarView, decorationFor dateComponents: DateComponents) -> UICalendarView.Decoration? { guard let targetYear = dateComponents.year, - let targetMonth = dateComponents.month, - let targetDay = dateComponents.day else { + let targetMonth = dateComponents.month, + let targetDay = dateComponents.day else { return nil } - if holidayObject.judgeJapaneseHoliday(year: targetYear, month: targetMonth, day: targetDay) { + + // 注意: weekdayプロパティはUICalendarViewDelegateまで常にnilになる... + // print("dateComponents.weekday", dateComponents.weekday) + + let weekday = getWeekDay(year: targetYear, month: targetMonth, day: targetDay) + let isHoliday = holidayObject.judgeJapaneseHoliday(year: targetYear, month: targetMonth, day: targetDay) + + if isHoliday { return .image(UIImage(systemName: "flag.2.crossed.fill"), color: .orange, size: .medium) + } else if weekday == 1 { + return .default(color: .red, size: .small) + } else if weekday == 7 { + return .default(color: .blue, size: .small) } else { return nil } } + + // 年・月・日から曜日(日曜日:1 ~ 土曜日:7)を取得する + private func getWeekDay(year: Int, month: Int, day: Int) -> Int { + let formatString = "%04d/%02d/%02d" + guard let date: Date = stringDateFormatter.date(from: String(format: formatString, year, month, day)) else { + return 0 + } + return currentCalendar.component(.weekday, from: date) + } +} + +// MARK: - UICalendarSelectionSingleDateDelegate + +extension NewCalendarViewController: UICalendarSelectionSingleDateDelegate { + + // 日付を選択したタイミングで選択した日付情報を返す + @available(iOS 16.0, *) + func dateSelection(_ selection: UICalendarSelectionSingleDate, didSelectDate dateComponents: DateComponents?) { + guard let targetDateComponents = dateComponents else { + return + } + guard let targetYear = targetDateComponents.year, + let targetMonth = targetDateComponents.month, + let targetDay = targetDateComponents.day else { + return + } + print("あなたが選択した日付:", "\(targetYear)年\(targetMonth)月\(targetDay)日") + } + + // 日付を選択可能な状態とするかを設定する + @available(iOS 16.0, *) + func dateSelection(_ selection: UICalendarSelectionSingleDate, canSelectDate dateComponents: DateComponents?) -> Bool { + return true + } }