1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- //
- // StudyMaterialsViewModel.swift
- // LMS
- //
- // Created by Suraj Kumar Mandal on 06/09/22.
- //
- import Foundation
- import Alamofire
- class StudyMaterialsViewModel {
-
- var delegate: StudyMaterialsProtocol?
-
- func getUnitsList(userId:Int) {
- if let delegate = delegate {
- delegate.startLoader()
- let accessToken = UserDefaults.standard.value(forKey: "accessToken") as! String
- let url = ApiURL.GetUnitsList + "/\(userId)/false"
- 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)
- .responseJSON { response in
- if response.response?.statusCode == 200 {
- switch response.result {
- case .success(_):
- delegate.stopLoader()
- guard let jsonData = response.data else {
- return
- }
- let decoder = JSONDecoder()
- do {
- let unitsData = try decoder.decode([UnitsListModel].self, from: jsonData)
- delegate.unitsModel(model: unitsData)
- print(unitsData.count)
- } catch {
- print("getDataList Unexpected error: \(error).")
- delegate.showError(error: "getDataList Unexpected error: \(error).")
- }
-
- break
- case .failure(let error):
- delegate.stopLoader()
- print(error.localizedDescription)
- delegate.showError(error: error.localizedDescription)
- break
- }
- }
- }
- }
- }
- }
- protocol StudyMaterialsProtocol {
- func startLoader()
- func stopLoader()
- func showError(error:String)
- func unitsModel(model: [UnitsListModel])
- }
|