MeetingListViewController.swift 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. //
  2. // MeetingListViewController.swift
  3. // LMS
  4. //
  5. // Created by Suraj Kumar Mandal on 18/08/22.
  6. //
  7. import UIKit
  8. class MeetingListViewController: UIViewController {
  9. @IBOutlet var meetingListTableView: UITableView!
  10. var calendarData = [CalendarModel]()
  11. var meetingDate = String()
  12. var meetings = [CalendarModel]()
  13. var selectedDate = Date()
  14. override func viewDidLoad() {
  15. super.viewDidLoad()
  16. // Do any additional setup after loading the view.
  17. executeRepeatedly()
  18. for data in calendarData {
  19. if data.date == meetingDate {
  20. meetings.append(data)
  21. }
  22. }
  23. // if let i = calendarData.firstIndex(where: { $0.date == meetingDate }) {
  24. // meetings.append(calendarData[i])
  25. // }
  26. meetingListTableView.delegate = self
  27. meetingListTableView.dataSource = self
  28. meetingListTableView.reloadData()
  29. }
  30. func formattedDateFromString(dateString: String, withFormat format: String) -> String? {
  31. let inputFormatter = DateFormatter()
  32. inputFormatter.dateFormat = "yyyy-MM-dd"
  33. if let date = inputFormatter.date(from: dateString) {
  34. let outputFormatter = DateFormatter()
  35. outputFormatter.dateFormat = format
  36. return outputFormatter.string(from: date)
  37. }
  38. return nil
  39. }
  40. func formattedTimeFromString(dateString: String, withFormat format: String) -> String? {
  41. let inputFormatter = DateFormatter()
  42. inputFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
  43. if let date = inputFormatter.date(from: dateString) {
  44. let outputFormatter = DateFormatter()
  45. outputFormatter.dateFormat = format
  46. outputFormatter.timeStyle = .short
  47. return outputFormatter.string(from: date)
  48. }
  49. return nil
  50. }
  51. private func executeRepeatedly() {
  52. // put your code here
  53. meetingListTableView.reloadData()
  54. DispatchQueue.main.asyncAfter(deadline: .now() + 60.0) { [weak self] in
  55. self?.executeRepeatedly()
  56. }
  57. }
  58. /*
  59. // MARK: - Navigation
  60. // In a storyboard-based application, you will often want to do a little preparation before navigation
  61. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  62. // Get the new view controller using segue.destination.
  63. // Pass the selected object to the new view controller.
  64. }
  65. */
  66. }
  67. extension MeetingListViewController: UITableViewDelegate, UITableViewDataSource {
  68. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  69. return meetings.count
  70. }
  71. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  72. if meetings[indexPath.row].offline == true {
  73. guard let cell = tableView.dequeueReusableCell(withIdentifier: "MeetingListTableViewCell", for: indexPath) as? MeetingListTableViewCell else {
  74. return UITableViewCell()
  75. }
  76. cell.customView.layer.cornerRadius = 10
  77. cell.customView.dropShadow()
  78. cell.meetingNameLabel.text = meetings[indexPath.row].scheduleTitle
  79. cell.dayDateLabel.text = formattedDateFromString(dateString: meetings[indexPath.row].date!, withFormat: "EEEE, MMM dd, yyyy")
  80. cell.timeLabel.text = "\(formattedTimeFromString(dateString: meetings[indexPath.row].startTime!, withFormat: "HH:mm") ?? "") - \(formattedTimeFromString(dateString: meetings[indexPath.row].endTime!, withFormat: "HH:mm") ?? "")"
  81. cell.joinClassButton.layer.cornerRadius = 5
  82. let date = Date()
  83. let format = DateFormatter()
  84. format.dateFormat = "yyyy-MM-dd HH:mm:ss"
  85. let time = format.string(from: date)
  86. let currentTime = format.date(from: time)
  87. let startTime = format.date(from: meetings[indexPath.row].startTime!)?.addingTimeInterval(TimeInterval(10.0 * 60.0))
  88. let endTime = format.date(from: meetings[indexPath.row].endTime!)?.addingTimeInterval(TimeInterval(20.0 * 60.0))
  89. if currentTime! > startTime! && currentTime! < endTime! {
  90. cell.joinClassButton.backgroundColor = #colorLiteral(red: 0.00400000019, green: 0.200000003, blue: 0.3919999897, alpha: 1)
  91. cell.joinClassButton.tag = indexPath.row
  92. cell.joinClassButton.addTarget(self, action: #selector(joinClass), for: .touchUpInside)
  93. } else {
  94. cell.joinClassButton.backgroundColor = UIColor.lightGray
  95. cell.joinClassButton.isUserInteractionEnabled = false
  96. }
  97. return cell
  98. } else {
  99. guard let cell = tableView.dequeueReusableCell(withIdentifier: "OfflineMeetingTableViewCell", for: indexPath) as? OfflineMeetingTableViewCell else {
  100. return UITableViewCell()
  101. }
  102. cell.customView.layer.cornerRadius = 10
  103. cell.customView.dropShadow()
  104. cell.meetingNameLabel.text = meetings[indexPath.row].scheduleTitle
  105. cell.dateLabel.text = formattedDateFromString(dateString: meetings[indexPath.row].date!, withFormat: "EEEE, MMM dd, yyyy")
  106. cell.timeLabel.text = "\(formattedTimeFromString(dateString: meetings[indexPath.row].startTime!, withFormat: "HH:mm") ?? "") - \(formattedTimeFromString(dateString: meetings[indexPath.row].endTime!, withFormat: "HH:mm") ?? "")"
  107. return cell
  108. }
  109. }
  110. @objc func joinClass(sender: UIButton){
  111. print(sender.tag)
  112. guard let url = URL(string: meetings[sender.tag].link ?? "") else { return }
  113. print(meetings[sender.tag].link ?? "")
  114. UIApplication.shared.open(url)
  115. }
  116. }