123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- //
- // ScromViewController.swift
- // LMS
- //
- // Created by Suraj Kumar Mandal on 18/01/23.
- //
- import UIKit
- import Toast_Swift
- import WebKit
- import Zip
- class ScromViewController: UIViewController {
-
- @IBOutlet var navigationBar: UINavigationBar!
- @IBOutlet var htmlWebView: WKWebView!
-
- var viewModel = ScromViewModel()
- var id = String()
- var docType = String()
- var name = String()
-
- override func viewDidLoad() {
- super.viewDidLoad()
-
- // Do any additional setup after loading the view.
- navigationBar.topItem?.title = name
- viewModel.delegate = self
- getScromData()
-
- let value = UIInterfaceOrientation.landscapeLeft.rawValue
- UIDevice.current.setValue(value, forKey: "orientation")
- }
-
- override func viewWillDisappear(_ animated: Bool) {
- super.viewWillDisappear(animated)
- let value = UIInterfaceOrientation.portrait.rawValue
- UIDevice.current.setValue(value, forKey: "orientation")
- }
-
- func getScromData() {
- if Reachability.isConnectedToNetwork() {
- let stringData = id.description.components(separatedBy: "scrom/unzip/")
- viewModel.getScromData(fileId: stringData[1])
- } else {
- Alert.showInternetFailureAlert(on: self)
- }
- }
-
- func decodeToZip(base64String: String) {
- let data = Data(base64Encoded: base64String, options: .ignoreUnknownCharacters)
- if let data = data {
- let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
- let docName = "\(name).zip"
- let filePath = "\(documentsPath)/\(docName)"
- let url = URL(fileURLWithPath: filePath)
- try? data.write(to: url)
-
- unzipFile()
- }
- }
-
- func unzipFile() {
- do {
- let filePath = URL(string: "\(NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0])/\(name).zip")!
- let fileURL = try Zip.quickUnzipFile(filePath)
- let htmlFileURL = fileURL.appendingPathComponent("index.html")
- loadHTMLFile(htmlFileURL)
- } catch {
- print("Error unzipping file: \(error)")
- }
- }
-
- func loadHTMLFile(_ fileURL: URL) {
- let request = URLRequest(url: fileURL)
- htmlWebView.load(request)
- }
-
-
-
- /*
- // 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 backAction(_ sender: UIBarButtonItem) {
- self.navigationController?.popViewController(animated: true)
- }
-
- }
- extension ScromViewController: ScromProtocol {
- func startLoader() {
- ActivityIndicator.start()
- }
-
- func stopLoader() {
- ActivityIndicator.stop()
- }
-
- func showError(error: String) {
- self.view.makeToast(error)
- }
-
- func scromBase64(base64: String) {
- self.decodeToZip(base64String: base64)
- }
- }
|