DashboardViewController.swift 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. //
  2. // DashboardViewController.swift
  3. // LMS
  4. //
  5. // Created by Suraj Kumar Mandal on 18/08/22.
  6. //
  7. import UIKit
  8. import SideMenu
  9. import Toast_Swift
  10. class DashboardViewController: UIViewController {
  11. @IBOutlet var navigationBar: UINavigationBar!
  12. @IBOutlet var usernameLabel: UILabel!
  13. @IBOutlet var academicYearLabel: UILabel!
  14. @IBOutlet var classLabel: UILabel!
  15. @IBOutlet var divisionLabel: UILabel!
  16. @IBOutlet var typeSegmentControl: UISegmentedControl!
  17. @IBOutlet var upcomingSessionView: UIView!
  18. @IBOutlet var sessionListTableView: UITableView!
  19. @IBOutlet var viewAllSessionButton: UIButton!
  20. @IBOutlet var upcomingAssessmentView: UIView!
  21. @IBOutlet var assessmentListTableView: UITableView!
  22. @IBOutlet var viewAllAssessmentButton: UIButton!
  23. let userData = DBManager.sharedInstance.database.objects(UserDetailsModel.self)
  24. let studentData = DBManager.sharedInstance.database.objects(StudentDetailsModel.self)
  25. var viewModel = DashboardViewModel()
  26. var sessionListModel = [SessionListModel]()
  27. var assessmentListModel = [AssessmentListModel]()
  28. var expandedSections = Set<Int>()
  29. override func viewDidLoad() {
  30. super.viewDidLoad()
  31. // Do any additional setup after loading the view.
  32. viewModel.delegate = self
  33. setupData()
  34. upcomingSessionView.isHidden = true
  35. upcomingAssessmentView.isHidden = true
  36. sessionListTableView.delegate = self
  37. sessionListTableView.dataSource = self
  38. assessmentListTableView.delegate = self
  39. assessmentListTableView.dataSource = self
  40. }
  41. override func viewWillAppear(_ animated: Bool) {
  42. viewModel.getSessionList(userId: userData[0].id, interventionId: "", batchId: "", schoolId: "", interventionLevelId: "", month: getCurrentMonth())
  43. viewModel.getAssessmentList(userId: userData[0].id, month: getCurrentMonth())
  44. }
  45. func setupData() {
  46. navigationBar.topItem?.title = "Dashboard"
  47. usernameLabel.text = "Hi \(userData[0].name),"
  48. academicYearLabel.text = studentData[0].academicYear
  49. classLabel.text = studentData[0].standard
  50. divisionLabel.text = studentData[0].division
  51. }
  52. func getCurrentMonth() -> String {
  53. let date = Date()
  54. let format = DateFormatter()
  55. format.dateFormat = "MM-yyyy"
  56. let formattedMonth = format.string(from: date)
  57. return formattedMonth
  58. }
  59. private func executeRepeatedly() {
  60. // put your code here
  61. self.sessionListTableView.reloadData()
  62. self.assessmentListTableView.reloadData()
  63. DispatchQueue.main.asyncAfter(deadline: .now() + 60.0) { [weak self] in
  64. self?.executeRepeatedly()
  65. }
  66. }
  67. func formattedDateFromString(dateString: String, withFormat format: String) -> String? {
  68. let inputFormatter = DateFormatter()
  69. inputFormatter.dateFormat = "yyyy-MM-dd"
  70. if let date = inputFormatter.date(from: dateString) {
  71. let outputFormatter = DateFormatter()
  72. outputFormatter.dateFormat = format
  73. return outputFormatter.string(from: date)
  74. }
  75. return nil
  76. }
  77. func formatDate(dateString: String, withFormat format: String) -> String? {
  78. let inputFormatter = DateFormatter()
  79. inputFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
  80. if let date = inputFormatter.date(from: dateString) {
  81. let outputFormatter = DateFormatter()
  82. outputFormatter.dateFormat = format
  83. return outputFormatter.string(from: date)
  84. }
  85. return nil
  86. }
  87. func formattedTimeFromString(dateString: String, withFormat format: String) -> String? {
  88. let inputFormatter = DateFormatter()
  89. inputFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
  90. if let date = inputFormatter.date(from: dateString) {
  91. let outputFormatter = DateFormatter()
  92. outputFormatter.dateFormat = format
  93. outputFormatter.timeStyle = .short
  94. return outputFormatter.string(from: date)
  95. }
  96. return nil
  97. }
  98. /*
  99. // MARK: - Navigation
  100. // In a storyboard-based application, you will often want to do a little preparation before navigation
  101. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  102. // Get the new view controller using segue.destination.
  103. // Pass the selected object to the new view controller.
  104. }
  105. */
  106. @IBAction func sideMenuAction(_ sender: UIBarButtonItem) {
  107. let menu = storyboard!.instantiateViewController(withIdentifier: "SideMenuNavigationController") as! SideMenuNavigationController
  108. present(menu, animated: true, completion: nil)
  109. }
  110. @IBAction func segmentControllClick(_ sender: Any) {
  111. switch typeSegmentControl.selectedSegmentIndex {
  112. case 0:
  113. upcomingSessionView.isHidden = false
  114. upcomingAssessmentView.isHidden = true
  115. case 1:
  116. upcomingSessionView.isHidden = true
  117. upcomingAssessmentView.isHidden = false
  118. default:
  119. break
  120. }
  121. }
  122. @IBAction func viewAllSessionAction(_ sender: Any) {
  123. let vc = self.storyboard?.instantiateViewController(withIdentifier: "SessionListViewController") as! SessionListViewController
  124. self.navigationController?.pushViewController(vc, animated: true)
  125. }
  126. @IBAction func viewAllAssessmentAction(_ sender: Any) {
  127. let vc = self.storyboard?.instantiateViewController(withIdentifier: "AssessmentBeneficiaryViewController") as! AssessmentBeneficiaryViewController
  128. self.navigationController?.pushViewController(vc, animated: true)
  129. }
  130. }
  131. extension DashboardViewController: DashboardProtocol {
  132. func sessionListModel(model: [SessionListModel]) {
  133. if model.isEmpty == false {
  134. let dateSortedData = model.sorted(by: {$0.date! > $1.date!})
  135. let timeSortedData = dateSortedData.sorted(by: {$0.startTime! > $1.startTime!})
  136. var i = 0
  137. for item in timeSortedData {
  138. if i < 4 {
  139. self.sessionListModel.append(item)
  140. i += 1
  141. }
  142. }
  143. self.sessionListTableView.reloadData()
  144. self.upcomingSessionView.isHidden = false
  145. } else {
  146. self.upcomingSessionView.isHidden = true
  147. }
  148. }
  149. func assessmentListModel(model: [AssessmentListModel]) {
  150. if model.isEmpty == false {
  151. let dateSortedData = model.sorted(by: {$0.startDateTime! > $1.startDateTime!})
  152. var i = 0
  153. for item in dateSortedData {
  154. if i < 4 {
  155. self.assessmentListModel.append(item)
  156. i += 1
  157. }
  158. }
  159. self.assessmentListTableView.reloadData()
  160. self.upcomingAssessmentView.isHidden = false
  161. } else {
  162. self.upcomingAssessmentView.isHidden = true
  163. }
  164. }
  165. func startLoader() {
  166. ActivityIndicator.start()
  167. }
  168. func stopLoader() {
  169. ActivityIndicator.stop()
  170. }
  171. func showError(error: String) {
  172. self.view.makeToast(error)
  173. }
  174. }
  175. extension DashboardViewController: UITableViewDelegate, UITableViewDataSource {
  176. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  177. if tableView == sessionListTableView {
  178. return sessionListModel.count
  179. } else {
  180. return assessmentListModel.count
  181. }
  182. }
  183. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  184. if tableView == sessionListTableView {
  185. guard let cell = tableView.dequeueReusableCell(withIdentifier: "DashboardSessionListTableViewCell", for: indexPath) as? DashboardSessionListTableViewCell else {
  186. return UITableViewCell()
  187. }
  188. cell.selectionStyle = .none
  189. cell.sessionCustomView.layer.cornerRadius = 10
  190. cell.sessionNameLabel.text = sessionListModel[indexPath.row].scheduleTitle
  191. cell.dayDateLabel.text = formattedDateFromString(dateString: sessionListModel[indexPath.row].date!, withFormat: "EEEE, MMM dd, yyyy")
  192. cell.timeLabel.text = "\(formattedTimeFromString(dateString: sessionListModel[indexPath.row].startTime!, withFormat: "HH:mm") ?? "") - \(formattedTimeFromString(dateString: sessionListModel[indexPath.row].endTime!, withFormat: "HH:mm") ?? "")"
  193. cell.joinButton.layer.cornerRadius = 5
  194. cell.joinButton.addTarget(self, action: #selector(joinClass), for: .touchUpInside)
  195. return cell
  196. } else {
  197. guard let cell = tableView.dequeueReusableCell(withIdentifier: "DashboardAssessmentListTableViewCell", for: indexPath) as? DashboardAssessmentListTableViewCell else {
  198. return UITableViewCell()
  199. }
  200. cell.selectionStyle = .none
  201. cell.assessmentCustomView.layer.cornerRadius = 10
  202. cell.assessmentNameLabel.text = assessmentListModel[indexPath.row].assessment?.name
  203. cell.dayDateLabel.text = " " + (formatDate(dateString: assessmentListModel[indexPath.row].startDateTime!, withFormat: "dd MMM") ?? "") + " "
  204. cell.timeLabel.text = (formattedTimeFromString(dateString: assessmentListModel[indexPath.row].startDateTime!, withFormat: "HH:mm") ?? "") + " to " + (formattedTimeFromString(dateString: assessmentListModel[indexPath.row].endDateTime!, withFormat: "HH:mm") ?? "")
  205. cell.activeStatusLabel.text = assessmentListModel[indexPath.row].assessment?.mode
  206. if assessmentListModel[indexPath.row].assessment?.mode == "Online" {
  207. cell.statusIndicatorImageView.tintColor = #colorLiteral(red: 0.3843137255, green: 0.6470588235, blue: 0.262745098, alpha: 1)
  208. cell.startButton.backgroundColor = #colorLiteral(red: 0.00400000019, green: 0.200000003, blue: 0.3919999897, alpha: 1)
  209. cell.startButton.isEnabled = true
  210. } else {
  211. cell.statusIndicatorImageView.tintColor = #colorLiteral(red: 0.7450980544, green: 0.1568627506, blue: 0.07450980693, alpha: 1)
  212. cell.startButton.backgroundColor = #colorLiteral(red: 0.6666666865, green: 0.6666666865, blue: 0.6666666865, alpha: 1)
  213. cell.startButton.isEnabled = false
  214. }
  215. cell.startButton.tag = indexPath.row
  216. cell.startButton.addTarget(self, action: #selector(startQuiz), for: .touchUpInside)
  217. return cell
  218. }
  219. }
  220. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  221. if tableView == sessionListTableView {
  222. UIView.animate(withDuration: 0.3) {
  223. self.sessionListTableView.performBatchUpdates(nil)
  224. }
  225. } else {
  226. UIView.animate(withDuration: 0.3) {
  227. self.assessmentListTableView.performBatchUpdates(nil)
  228. }
  229. }
  230. }
  231. func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
  232. if tableView == sessionListTableView {
  233. if let cell = self.sessionListTableView.cellForRow(at: indexPath) as? DashboardSessionListTableViewCell {
  234. cell.hideDetailView()
  235. }
  236. } else {
  237. if let cell = self.assessmentListTableView.cellForRow(at: indexPath) as? DashboardAssessmentListTableViewCell {
  238. cell.hideDetailView()
  239. }
  240. }
  241. }
  242. @objc func joinClass(sender: UIButton){
  243. print(sender.tag)
  244. guard let url = URL(string: sessionListModel[sender.tag].link ?? "") else { return }
  245. print(sessionListModel[sender.tag].link ?? "")
  246. UIApplication.shared.open(url)
  247. }
  248. @objc func startQuiz(sender: UIButton) {
  249. let date = Date()
  250. let format = DateFormatter()
  251. format.dateFormat = "yyyy-MM-dd HH:mm:ss"
  252. let time = format.string(from: date)
  253. let currentTime = format.date(from: time)
  254. let startTime = format.date(from: assessmentListModel[sender.tag].startDateTime!)
  255. let endTime = format.date(from: assessmentListModel[sender.tag].endDateTime!)
  256. if currentTime! >= startTime! && currentTime! <= endTime! {
  257. if assessmentListModel[sender.tag].assessment?.mode == "Online" {
  258. let vc = self.storyboard?.instantiateViewController(withIdentifier: "InstructionViewController") as! InstructionViewController
  259. vc.assessmentId = assessmentListModel[sender.tag].assessment?.id ?? 0
  260. vc.assessmentName = assessmentListModel[sender.tag].assessment?.name ?? ""
  261. self.navigationController?.pushViewController(vc, animated: true)
  262. } else {
  263. let vc = self.storyboard?.instantiateViewController(withIdentifier: "OfflineAssessmentViewController") as! OfflineAssessmentViewController
  264. let fileType = assessmentListModel[sender.tag].assessment?.fileType
  265. let fileExtension = fileType!.components(separatedBy: "/")[1]
  266. vc.fileExtension = fileExtension
  267. vc.assessmentName = assessmentListModel[sender.tag].assessment?.name ?? ""
  268. vc.assessmentId = assessmentListModel[sender.tag].assessment?.id ?? 0
  269. vc.fileId = assessmentListModel[sender.tag].assessment?.fileId ?? ""
  270. self.navigationController?.pushViewController(vc, animated: true)
  271. }
  272. } else if currentTime! >= startTime! && currentTime! >= endTime! {
  273. Alert.showAlert(on: self, with: "Times Up", message: "Exam is already finished!")
  274. } else {
  275. Alert.showAlert(on: self, with: "Upcoming", message: "Exam will start on \(formattedDateFromString(dateString: assessmentListModel[sender.tag].startDateTime!, withFormat: "dd-MMM-yyyy hh:mm a") ?? "")")
  276. }
  277. }
  278. }