// // CalendarViewController.swift // LMS // // Created by Suraj Kumar Mandal on 26/12/22. // import UIKit import FSCalendar import SideMenu import Toast_Swift class CalendarViewController: UIViewController { @IBOutlet var navigationBar: UINavigationBar! @IBOutlet var calendar: FSCalendar! let userData = DBManager.sharedInstance.database.objects(UserDetailsModel.self) var datesWithEvent = [String]() // var datesWithMultipleEvents = [String]() var viewModel = CalendarViewModel() var calendarData = [CalendarModel]() fileprivate lazy var dateFormatter2: DateFormatter = { let formatter = DateFormatter() formatter.dateFormat = "yyyy-MM-dd" return formatter }() override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. viewModel.delegate = self setupUI() self.getCalendarData() calendar.dataSource = self calendar.delegate = self } func setupUI() { navigationBar.topItem?.title = "Calendar" calendar.layer.cornerRadius = 10 calendar.dropShadow() } func getCurrentMonth() -> String { let date = Date() let format = DateFormatter() format.dateFormat = "MM-yyyy" let formattedMonth = format.string(from: date) print(formattedMonth) return formattedMonth } func getCalendarData() { if Reachability.isConnectedToNetwork() { viewModel.getCalendarData(id: userData[0].id, month: getCurrentMonth()) } else { Alert.showInternetFailureAlert(on: self) } } /* // MARK: - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation override func prepare(for segue: UIStoryboardSegue, sender: Any?) { // Get the new view controller using segue.destination. // Pass the selected object to the new view controller. } */ @IBAction func sideMenuAction(_ sender: UIBarButtonItem) { let menu = storyboard!.instantiateViewController(withIdentifier: "SideMenuNavigationController") as! SideMenuNavigationController present(menu, animated: true, completion: nil) } } extension CalendarViewController: FSCalendarDataSource, FSCalendarDelegate, FSCalendarDelegateAppearance { func calendar(_ calendar: FSCalendar, numberOfEventsFor date: Date) -> Int { let dateString = dateFormatter2.string(from: date) if self.datesWithEvent.contains(dateString) { return 1 } // if self.datesWithMultipleEvents.contains(dateString) { // return 2 // } return 0 } // func calendar(_ calendar: FSCalendar, appearance: FSCalendarAppearance, eventDefaultColorsFor date: Date) -> [UIColor]? { // // let key = self.dateFormatter2.string(from: date) // // if self.datesWithMultipleEvents.contains(key) { // return [UIColor.blue] // } // return nil // } func calendar(_ calendar: FSCalendar, didSelect date: Date, at monthPosition: FSCalendarMonthPosition) { let vc = self.storyboard?.instantiateViewController(withIdentifier: "MeetingListViewController") as! MeetingListViewController vc.calendarData = self.calendarData vc.meetingDate = dateFormatter2.string(from: date) vc.selectedDate = date self.present(vc, animated: true, completion: nil) } func minimumDate(for calendar: FSCalendar) -> Date { return Date() } } extension CalendarViewController: CalendarProtocol { func startLoader() { ActivityIndicator.start() } func stopLoader() { ActivityIndicator.stop() } func showError(error: String) { self.view.makeToast(error) } func loadData(model: [CalendarModel]) { self.calendarData = model for data in model { let date = data.date datesWithEvent.append(date ?? "") } print(datesWithEvent) self.calendar.reloadData() } }