1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- //
- // PdfReaderViewModel.swift
- // LMS
- //
- // Created by Suraj Kumar Mandal on 07/09/22.
- //
- import Foundation
- import Alamofire
- class PdfReaderViewModel {
-
- var delegate: PdfReaderProtocol?
-
- func getPdfData(fileId:String) {
- if let delegate = delegate {
- delegate.startLoader()
- let accessToken = UserDefaults.standard.value(forKey: "accessToken") as! String
- let url = ApiURL.DownloadBase64 + "/\(fileId)"
- print(url)
-
- let headers: HTTPHeaders = [
- "Authorization": "Bearer \(accessToken)",
- "Accept": "application/json",
- "Content-Type": "application/json"
- ]
- print(headers)
-
- AF.request(url,
- method: .get,
- encoding: URLEncoding.httpBody,
- headers: headers)
- .responseString { response in
- if response.response?.statusCode == 200 {
- switch response.result {
- case .success(_):
- delegate.stopLoader()
- let stringData = response.description.components(separatedBy: ",")
- delegate.pdfBase64(base64: stringData[1])
- break
- case .failure(let error):
- delegate.stopLoader()
- print(error.localizedDescription)
- delegate.showError(error: error.localizedDescription)
- break
- }
- } else {
- print("Server error")
- }
- }
- }
- }
- }
- protocol PdfReaderProtocol {
- func startLoader()
- func stopLoader()
- func showError(error:String)
- func pdfBase64(base64:String)
- }
|