// // NewAssessmentViewController.swift // LMS // // Created by Suraj Kumar Mandal on 19/10/23. // import UIKit import SideMenu class NewAssessmentViewController: UIViewController { @IBOutlet var navigationBar: UINavigationBar! @IBOutlet var assessmentListCollectionView: UICollectionView! var viewModel = NewAssessmentViewModel() var newAssessmentListModel = [NewAssessmentModel]() let userData = DBManager.sharedInstance.database.objects(UserDetailsModel.self) var upcomingAssessment = [AssessmentListModel]() var pastAssessment = [AssessmentListModel]() override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. viewModel.delegate = self assessmentListCollectionView.delegate = self assessmentListCollectionView.dataSource = self setupUI() } func setupUI() { navigationBar.topItem?.title = "Assessment" } override func viewWillAppear(_ animated: Bool) { if Reachability.isConnectedToNetwork() { viewModel.getNewAssessmentList(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 NewAssessmentViewController: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout { func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return newAssessmentListModel.count } func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "NewAssessmentCollectionViewCell", for: indexPath as IndexPath) as! NewAssessmentCollectionViewCell 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.serialNoLabel.text = "\(indexPath.row + 1)" cell.testNameLabel.text = newAssessmentListModel[indexPath.row].name cell.scoreLabel.text = "Highest Score:" + "\(newAssessmentListModel[indexPath.row].achievedMarks ?? 0)" + "/" + "\(newAssessmentListModel[indexPath.row].totalMarks ?? 0)" cell.attemptsLabel.text = "No. Of Attempts: \(newAssessmentListModel[indexPath.row].numberofAtmt ?? 0)" cell.takeExamButton.layer.cornerRadius = 5 cell.takeExamButton.tag = indexPath.row cell.takeExamButton.addTarget(self, action: #selector(startQuiz), for: .touchUpInside) return cell } func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { return CGSize(width: (collectionView.frame.size.width-10)/2, height: 280) } // func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { // let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "NewAssessmentCollectionViewCell", for: indexPath as IndexPath) as! NewAssessmentCollectionViewCell // // cell.testNameLabel.text = newAssessmentListModel[indexPath.row].name // // // Calculate the cell size based on the label's content and constraints // let labelSize = cell.testNameLabel.sizeThatFits(CGSize(width: cell.testNameLabel.frame.width, height: .greatestFiniteMagnitude)) // let cellHeight = labelSize.height // // return CGSize(width: (collectionView.frame.size.width-10)/2, height: cellHeight + 220) // } @objc func startQuiz(sender: UIButton) { let vc = self.storyboard?.instantiateViewController(withIdentifier: "InstructionViewController") as! InstructionViewController vc.assessmentId = newAssessmentListModel[sender.tag].id ?? 0 vc.assessmentName = newAssessmentListModel[sender.tag].name ?? "" vc.quesCount = newAssessmentListModel[sender.tag].totalQuestions ?? 0 vc.assessmentType = "Assessment" self.navigationController?.pushViewController(vc, animated: true) } } extension NewAssessmentViewController: NewAssessmentProtocol { func startLoader() { ActivityIndicator.start() } func stopLoader() { ActivityIndicator.stop() } func showError(error: String) { self.view.makeToast(error) } func assessmentListModel(model: [NewAssessmentModel]) { self.newAssessmentListModel = model self.assessmentListCollectionView.reloadData() } }