CalendarViewController.swift 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. //
  2. // CalendarViewController.swift
  3. // LMS
  4. //
  5. // Created by Suraj Kumar Mandal on 26/12/22.
  6. //
  7. import UIKit
  8. import FSCalendar
  9. import SideMenu
  10. import Toast_Swift
  11. class CalendarViewController: UIViewController {
  12. @IBOutlet var navigationBar: UINavigationBar!
  13. @IBOutlet var calendar: FSCalendar!
  14. let userData = DBManager.sharedInstance.database.objects(UserDetailsModel.self)
  15. var datesWithEvent = [String]()
  16. // var datesWithMultipleEvents = [String]()
  17. var viewModel = CalendarViewModel()
  18. var calendarData = [CalendarModel]()
  19. fileprivate lazy var dateFormatter2: DateFormatter = {
  20. let formatter = DateFormatter()
  21. formatter.dateFormat = "yyyy-MM-dd"
  22. return formatter
  23. }()
  24. override func viewDidLoad() {
  25. super.viewDidLoad()
  26. // Do any additional setup after loading the view.
  27. viewModel.delegate = self
  28. setupUI()
  29. self.getCalendarData()
  30. calendar.dataSource = self
  31. calendar.delegate = self
  32. }
  33. func setupUI() {
  34. navigationBar.topItem?.title = "Calendar"
  35. calendar.layer.cornerRadius = 10
  36. calendar.dropShadow()
  37. }
  38. func getCurrentMonth() -> String {
  39. let date = Date()
  40. let format = DateFormatter()
  41. format.dateFormat = "MM-yyyy"
  42. let formattedMonth = format.string(from: date)
  43. print(formattedMonth)
  44. return formattedMonth
  45. }
  46. func getCalendarData() {
  47. if Reachability.isConnectedToNetwork() {
  48. viewModel.getCalendarData(id: userData[0].id, month: getCurrentMonth())
  49. } else {
  50. Alert.showInternetFailureAlert(on: self)
  51. }
  52. }
  53. /*
  54. // MARK: - Navigation
  55. // In a storyboard-based application, you will often want to do a little preparation before navigation
  56. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  57. // Get the new view controller using segue.destination.
  58. // Pass the selected object to the new view controller.
  59. }
  60. */
  61. @IBAction func sideMenuAction(_ sender: UIBarButtonItem) {
  62. let menu = storyboard!.instantiateViewController(withIdentifier: "SideMenuNavigationController") as! SideMenuNavigationController
  63. present(menu, animated: true, completion: nil)
  64. }
  65. }
  66. extension CalendarViewController: FSCalendarDataSource, FSCalendarDelegate, FSCalendarDelegateAppearance {
  67. func calendar(_ calendar: FSCalendar, numberOfEventsFor date: Date) -> Int {
  68. let dateString = dateFormatter2.string(from: date)
  69. if self.datesWithEvent.contains(dateString) {
  70. return 1
  71. }
  72. // if self.datesWithMultipleEvents.contains(dateString) {
  73. // return 2
  74. // }
  75. return 0
  76. }
  77. // func calendar(_ calendar: FSCalendar, appearance: FSCalendarAppearance, eventDefaultColorsFor date: Date) -> [UIColor]? {
  78. //
  79. // let key = self.dateFormatter2.string(from: date)
  80. //
  81. // if self.datesWithMultipleEvents.contains(key) {
  82. // return [UIColor.blue]
  83. // }
  84. // return nil
  85. // }
  86. func calendar(_ calendar: FSCalendar, didSelect date: Date, at monthPosition: FSCalendarMonthPosition) {
  87. let vc = self.storyboard?.instantiateViewController(withIdentifier: "MeetingListViewController") as! MeetingListViewController
  88. vc.calendarData = self.calendarData
  89. vc.meetingDate = dateFormatter2.string(from: date)
  90. vc.selectedDate = date
  91. self.present(vc, animated: true, completion: nil)
  92. }
  93. func minimumDate(for calendar: FSCalendar) -> Date {
  94. return Date()
  95. }
  96. }
  97. extension CalendarViewController: CalendarProtocol {
  98. func startLoader() {
  99. ActivityIndicator.start()
  100. }
  101. func stopLoader() {
  102. ActivityIndicator.stop()
  103. }
  104. func showError(error: String) {
  105. self.view.makeToast(error)
  106. }
  107. func loadData(model: [CalendarModel]) {
  108. self.calendarData = model
  109. for data in model {
  110. let date = data.date
  111. datesWithEvent.append(date ?? "")
  112. }
  113. print(datesWithEvent)
  114. self.calendar.reloadData()
  115. }
  116. }