Untitled
// // CalendarVC.swift // LeaveManagementSystem // // Created by Chou visalroth on 1/11/23. // import UIKit import FSCalendar class CalendarVC: UIViewController, FSCalendarDataSource, FSCalendarDelegate { @IBOutlet weak var monthLabel: UILabel! @IBOutlet weak var fsCalendarView: FSCalendar! private let calendar = DateHelper.shared.calendar var selectCalendarVM = SelectCalendarVM() var gloabalYear = 2023 override func viewDidLoad() { super.viewDidLoad() selectCalendarVM.initData() setupCalendar() } @IBAction func nextMonthButtonDidTap(_ sender: Any) { fsCalendarView.setCurrentPage(getNextMonth(date: fsCalendarView.currentPage), animated: true) } @IBAction func previousMonthButtonDidTap(_ sender: Any) { fsCalendarView.setCurrentPage(getPreviousMonth(date: fsCalendarView.currentPage), animated: true) } func getNextMonth(date:Date)->Date { return Calendar.current.date(byAdding: .month, value: 1, to:date)! } func getPreviousMonth(date:Date)->Date { return Calendar.current.date(byAdding: .month, value: -1, to:date)! } // Functions private func setupCalendar() { fsCalendarView.dataSource = self fsCalendarView.delegate = self fsCalendarView.register(CalendarCell.self, forCellReuseIdentifier: "CalendarCell") // Appearance fsCalendarView.appearance.caseOptions = .weekdayUsesSingleUpperCase fsCalendarView.appearance.titleOffset = CGPoint(x: 0.0, y: 2.5) fsCalendarView.isUserInteractionEnabled = true fsCalendarView.allowsSelection = false // heremen / important fsCalendarView.backgroundColor = .clear fsCalendarView.appearance.weekdayTextColor = .white // S, M, T, W, T, F, S fsCalendarView.appearance.weekdayFont = UIFont.boldSystemFont(ofSize: 17) // fsCalendarView.appearance.titleTodayColor = .white// 31 : Green fsCalendarView.appearance.titleFont = UIFont.boldSystemFont(ofSize: 16) // Customizations fsCalendarView.placeholderType = .fillHeadTail fsCalendarView.calendarHeaderView.isHidden = true } } extension CalendarVC { func calendar(_ calendar: FSCalendar, cellFor date: Date, at position: FSCalendarMonthPosition) -> FSCalendarCell { let cell = calendar.dequeueReusableCell(withIdentifier: "CalendarCell", for: date, at: position) return cell } } extension CalendarVC { func calendar(_ calendar: FSCalendar, willDisplay cell: FSCalendarCell, for date: Date, at monthPosition: FSCalendarMonthPosition) { self.configureCell(cell, for: date, at: monthPosition) } // func calendar(_ calendar: FSCalendar, didSelect date: Date, at monthPosition: FSCalendarMonthPosition) { // self.configureVisibleCells() // } // // func calendar(_ calendar: FSCalendar, didDeselect date: Date, at monthPosition: FSCalendarMonthPosition) { // self.configureVisibleCells() // } } extension CalendarVC { private func configureCell(_ cell: FSCalendarCell?, for date: Date?, at position: FSCalendarMonthPosition) { guard let cell = cell as? CalendarCell else { return } var selectionType = SelectionType.none if let date = date, let cellCalendar = cell.calendar, cellCalendar.selectedDates.contains(where: { $0.isEqual(date: date, toGranularity: .day) }), let previousDate = self.calendar.date(byAdding: .day, value: -1, to: date), let nextDate = self.calendar.date(byAdding: .day, value: 1, to: date) { if cellCalendar.selectedDates.contains(previousDate) && cellCalendar.selectedDates.contains(nextDate) { selectionType = .middle } else if cellCalendar.selectedDates.contains(previousDate) && cellCalendar.selectedDates.contains(date) { selectionType = .rightBorder } else if cellCalendar.selectedDates.contains(nextDate) { selectionType = .leftBorder } else { selectionType = .single } } else if let date = date, date.isEqual() { selectionType = .today } else { let dateFormatter = DateFormatter() dateFormatter.dateFormat = "YYYY" dateFormatter.locale = Locale(identifier: "en_US_POSIX") let testing = dateFormatter.string(from: date!) let components = date!.get(.day, .month, .year) var day = 0 var month = 0 var year = 0 if let day1 = components.day, let month1 = components.month, let year1 = components.year { // print("day: \(day1), month: \(month1), year: \(year1)") day = day1 month = month1 year = year1 } gloabalYear = Int(testing) ?? 2023 let selectedYear = selectCalendarVM.data for year_obj in selectedYear { if year_obj.year == gloabalYear { for month_obj in year_obj.monththly_leave { if month_obj.month == month { for day1 in month_obj.permission_rec { if day1.day == day { print("=============================") print(" Day on Calendar: ", day) print(" Day in loop : ", day1.day) switch day1.permission_type { case .StartRange : selectionType = .leftBorder case .MiddleRange: selectionType = .middle case .EndRange : selectionType = .rightBorder case .SingleDay : selectionType = .single case .SingleDayHalf: selectionType = .singleHalf case .StartRangeHalf: selectionType = .leftBorderHalf case .EndRangeHalf: selectionType = .rightBorderHalf } print(" Selection Type : ", selectionType) } } } else { } } } // if obj.month == month { // // for day1 in obj.permission_rec { // if day1.day == day { // // print("=============================") // print(" Day on Calendar: ", day) // print(" Day in loop : ", day1.day) // // switch day1.permission_type { // case .StartRange : // selectionType = .leftBorder // case .MiddleRange: // selectionType = .middle // case .EndRange : // selectionType = .rightBorder // case .SingleDay : // selectionType = .singleHalf // case .SingleDayHalf: // selectionType = .singleHalf // case .StartRangeHalf: // selectionType = .leftBorderHalf // case .EndRangeHalf: // selectionType = .rightBorderHalf // } // // print(" Selection Type : ", selectionType) // } // } // } } } if let date1 = date { let components = date1.get(.day, .month, .year) if let day1 = components.day, let month1 = components.month, let year1 = components.year { // print("day: \(day1), month: \(month1), year: \(year1)") self.monthLabel.text = "\(date1.month) \(gloabalYear)" } } cell.selectionType = selectionType } private func configureVisibleCells() { fsCalendarView.visibleCells().forEach { (cell) in let date = fsCalendarView.date(for: cell) let position = fsCalendarView.monthPosition(for: cell) configureCell(cell, for: date, at: position) } } } extension Date { var month: String { let dateFormatter = DateFormatter() dateFormatter.dateFormat = "MMMM" dateFormatter.locale = Locale(identifier: "en_US_POSIX") return dateFormatter.string(from: self) } } extension Date { func get(_ components: Calendar.Component..., calendar: Calendar = Calendar.current) -> DateComponents { return calendar.dateComponents(Set(components), from: self) } func get(_ component: Calendar.Component, calendar: Calendar = Calendar.current) -> Int { return calendar.component(component, from: self) } }
Leave a Comment