StudyMaterialsViewController.swift 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. //
  2. // StudyMaterialsViewController.swift
  3. // LMS
  4. //
  5. // Created by Suraj Kumar Mandal on 24/08/22.
  6. //
  7. import UIKit
  8. import SideMenu
  9. import Toast_Swift
  10. class StudyMaterialsViewController: UIViewController {
  11. @IBOutlet var navigationBar: UINavigationBar!
  12. @IBOutlet var pageNameLabel: UILabel!
  13. @IBOutlet var unitsCollectionView: UICollectionView!
  14. let userData = DBManager.sharedInstance.database.objects(UserDetailsModel.self)
  15. var viewModel = StudyMaterialsViewModel()
  16. var unitModel = [UnitsListModel]()
  17. override func viewDidLoad() {
  18. super.viewDidLoad()
  19. // Do any additional setup after loading the view.
  20. viewModel.delegate = self
  21. unitsCollectionView.delegate = self
  22. unitsCollectionView.dataSource = self
  23. setupUI()
  24. getUnitList()
  25. }
  26. func setupUI() {
  27. navigationBar.topItem?.title = "Study Materials"
  28. }
  29. func getUnitList() {
  30. if Reachability.isConnectedToNetwork() {
  31. viewModel.getUnitsList(userId: userData[0].id)
  32. } else {
  33. Alert.showInternetFailureAlert(on: self)
  34. }
  35. }
  36. /*
  37. // MARK: - Navigation
  38. // In a storyboard-based application, you will often want to do a little preparation before navigation
  39. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  40. // Get the new view controller using segue.destination.
  41. // Pass the selected object to the new view controller.
  42. }
  43. */
  44. @IBAction func sideMenuAction(_ sender: UIBarButtonItem) {
  45. let menu = storyboard!.instantiateViewController(withIdentifier: "SideMenuNavigationController") as! SideMenuNavigationController
  46. present(menu, animated: true, completion: nil)
  47. }
  48. }
  49. extension StudyMaterialsViewController: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
  50. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  51. return unitModel.count
  52. }
  53. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  54. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "UnitsCollectionViewCell", for: indexPath as IndexPath) as! UnitsCollectionViewCell
  55. cell.customView.layer.borderWidth = 1
  56. cell.customView.layer.borderColor = #colorLiteral(red: 0.8039215803, green: 0.8039215803, blue: 0.8039215803, alpha: 1)
  57. cell.customView.layer.cornerRadius = 5
  58. cell.serialNumberLabel.text = "\(indexPath.row + 1)"
  59. cell.subjectNameLabel.text = unitModel[indexPath.row].name
  60. return cell
  61. }
  62. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  63. let vc = self.storyboard?.instantiateViewController(withIdentifier: "LessonViewController") as! LessonViewController
  64. vc.unitId = unitModel[indexPath.row].id ?? 0
  65. vc.interventionId = unitModel[indexPath.row].intervention?.id ?? 0
  66. vc.interventionLevelId = unitModel[indexPath.row].level?.id ?? 0
  67. vc.lessonName = unitModel[indexPath.row].name ?? ""
  68. self.navigationController?.pushViewController(vc, animated: true)
  69. }
  70. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
  71. return CGSize(width: (collectionView.frame.size.width-10)/2, height: 240)
  72. }
  73. }
  74. extension StudyMaterialsViewController: StudyMaterialsProtocol {
  75. func startLoader() {
  76. ActivityIndicator.start()
  77. }
  78. func stopLoader() {
  79. ActivityIndicator.stop()
  80. }
  81. func showError(error: String) {
  82. self.view.makeToast(error)
  83. }
  84. func unitsModel(model: [UnitsListModel]) {
  85. self.unitModel = model
  86. self.unitsCollectionView.reloadData()
  87. }
  88. }