123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 |
- //
- // OfflineAssessmentViewController.swift
- // LMS
- //
- // Created by Suraj Kumar Mandal on 11/01/23.
- //
- import UIKit
- import Toast_Swift
- import SideMenu
- import MobileCoreServices
- import UniformTypeIdentifiers
- class OfflineAssessmentViewController: UIViewController {
-
- @IBOutlet var downloadView: UIView!
- @IBOutlet var downloadImageView: UIImageView!
- @IBOutlet var downloadLabel: UILabel!
-
- @IBOutlet var uploadView: UIView!
- @IBOutlet var uploadImageView: UIImageView!
- @IBOutlet var uploadLabel: UILabel!
-
- var viewModel = OfflineAssessmentViewModel()
-
- let userData = DBManager.sharedInstance.database.objects(UserDetailsModel.self)
-
- var assessmentName = String()
- var assessmentId = Int()
- var fileExtension = String()
- var fileId = String()
-
- override func viewDidLoad() {
- super.viewDidLoad()
-
- // Do any additional setup after loading the view.
- viewModel.delegate = self
- setupUI()
- // Add touch gesture to download view
- let downloadGesture = UITapGestureRecognizer(target: self, action: #selector(self.downloadPDF(sender:)))
- self.downloadView.addGestureRecognizer(downloadGesture)
-
- // Add touch gesture to upload view
- let uploadGesture = UITapGestureRecognizer(target: self, action: #selector(self.upload(sender:)))
- self.uploadView.addGestureRecognizer(uploadGesture)
- }
-
- func setupUI() {
- downloadView.layer.cornerRadius = 20
- downloadView.layer.borderWidth = 3
- downloadView.layer.borderColor = #colorLiteral(red: 0.00400000019, green: 0.200000003, blue: 0.3919999897, alpha: 1)
-
- uploadView.layer.cornerRadius = 20
- uploadView.layer.borderWidth = 3
- uploadView.layer.borderColor = #colorLiteral(red: 0.00400000019, green: 0.200000003, blue: 0.3919999897, alpha: 1)
- }
-
- @objc func downloadPDF(sender : UITapGestureRecognizer) {
- if Reachability.isConnectedToNetwork() {
- viewModel.downloadAssessment(fileId: fileId)
- } else {
- Alert.showInternetFailureAlert(on: self)
- }
- }
-
- // This will creates a PDF file
- func createPDF(with base64Info: String) {
- let base64Data = Data(base64Encoded: base64Info, options: .ignoreUnknownCharacters)
- let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
- let docName = "QuestionPaper-\(assessmentName)\(assessmentId).\(fileExtension)"
- let filePath = "\(documentsPath)/\(docName)"
- if FileManager.default.fileExists(atPath: filePath) {
- self.view.makeToast("File already exist!")
- } else {
- let url = URL(fileURLWithPath: filePath)
- do {
- try base64Data?.write(to: url, options: .atomic)
- self.view.makeToast("Download successful.")
- } catch let failedError {
- print("Failed to write the pdf data due to \(failedError.localizedDescription)")
- }
- }
- }
-
- @objc func upload(sender : UITapGestureRecognizer) {
- var documentPicker: UIDocumentPickerViewController!
- if #available(iOS 14, *) {
- // iOS 14 & later
- let supportedTypes: [UTType] = [UTType.pdf]
- documentPicker = UIDocumentPickerViewController(forOpeningContentTypes: supportedTypes)
- } else {
- // iOS 13 or older code
- let supportedTypes: [String] = [kUTTypePDF as String]
- documentPicker = UIDocumentPickerViewController(documentTypes: supportedTypes, in: .import)
- }
- documentPicker.delegate = self
- documentPicker.allowsMultipleSelection = true
- documentPicker.modalPresentationStyle = .formSheet
- self.present(documentPicker, animated: true)
- }
-
-
- /*
- // 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 OfflineAssessmentViewController: OfflineAssessmentProtocol {
- func startLoader() {
- ActivityIndicator.start()
- }
-
- func stopLoader() {
- ActivityIndicator.stop()
- }
-
- func showError(error: String) {
- self.view.makeToast(error)
- }
-
- func getDownloadedFile(base64:String) {
- self.createPDF(with: base64)
- }
-
- func moveToAssessment() {
- let vc = self.storyboard?.instantiateViewController(withIdentifier: "AssessmentBeneficiaryViewController") as! AssessmentBeneficiaryViewController
- self.navigationController?.pushViewController(vc, animated: true)
- }
- }
- extension OfflineAssessmentViewController: UIDocumentPickerDelegate {
- func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
-
- for url in urls {
-
- // Start accessing a security-scoped resource.
- guard url.startAccessingSecurityScopedResource() else {
- // Handle the failure here.
- return
- }
-
- do {
- let data = try Data.init(contentsOf: url)
- print("url: \(url)")
- if Reachability.isConnectedToNetwork() {
- // You will have data of the selected file
- viewModel.uploadAssessment(assessmentId: assessmentId, userId: userData[0].id, fileType: "pdf", docData: data)
- } else {
- Alert.showInternetFailureAlert(on: self)
- }
-
- }
- catch {
- print(error.localizedDescription)
- }
-
- // Make sure you release the security-scoped resource when you finish.
- do { url.stopAccessingSecurityScopedResource() }
- }
- }
-
- func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) {
- controller.dismiss(animated: true, completion: nil)
- }
- }
|