// // StudyMaterialsViewController.swift // LMS // // Created by Suraj Kumar Mandal on 24/08/22. // import UIKit import SideMenu import Toast_Swift class StudyMaterialsViewController: UIViewController { @IBOutlet var navigationBar: UINavigationBar! @IBOutlet var pageNameLabel: UILabel! @IBOutlet var unitsCollectionView: UICollectionView! let userData = DBManager.sharedInstance.database.objects(UserDetailsModel.self) var viewModel = StudyMaterialsViewModel() var unitModel = [UnitsListModel]() override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. viewModel.delegate = self unitsCollectionView.delegate = self unitsCollectionView.dataSource = self setupUI() getUnitList() } func setupUI() { navigationBar.topItem?.title = "Study Materials" } func getUnitList() { if Reachability.isConnectedToNetwork() { viewModel.getUnitsList(userId: userData[0].id) } 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 StudyMaterialsViewController: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout { func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return unitModel.count } func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "UnitsCollectionViewCell", for: indexPath as IndexPath) as! UnitsCollectionViewCell cell.customView.layer.borderWidth = 1 cell.customView.layer.borderColor = #colorLiteral(red: 0.8039215803, green: 0.8039215803, blue: 0.8039215803, alpha: 1) cell.customView.layer.cornerRadius = 5 cell.serialNumberLabel.text = "\(indexPath.row + 1)" cell.subjectNameLabel.text = unitModel[indexPath.row].name return cell } func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { let vc = self.storyboard?.instantiateViewController(withIdentifier: "LessonViewController") as! LessonViewController vc.unitId = unitModel[indexPath.row].id ?? 0 vc.interventionId = unitModel[indexPath.row].intervention?.id ?? 0 vc.interventionLevelId = unitModel[indexPath.row].level?.id ?? 0 vc.lessonName = unitModel[indexPath.row].name ?? "" self.navigationController?.pushViewController(vc, animated: true) } func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { return CGSize(width: (collectionView.frame.size.width-10)/2, height: 240) } } extension StudyMaterialsViewController: StudyMaterialsProtocol { func startLoader() { ActivityIndicator.start() } func stopLoader() { ActivityIndicator.stop() } func showError(error: String) { self.view.makeToast(error) } func unitsModel(model: [UnitsListModel]) { self.unitModel = model self.unitsCollectionView.reloadData() } }