1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- //
- // LessonListViewModel.swift
- // LMS
- //
- // Created by Suraj Kumar Mandal on 06/09/22.
- //
- import Foundation
- import Alamofire
- class LessonListViewModel {
-
- var delegate: LessonListProtocol?
-
- func getLessonsList(id:Int) {
- if let delegate = delegate {
- delegate.startLoader()
- let accessToken = UserDefaults.standard.value(forKey: "accessToken") as! String
- let url = ApiURL.GetLessonsList
- print(url)
-
- let headers: HTTPHeaders = [
- "Authorization": "Bearer \(accessToken)",
- "Accept": "application/json",
- "Content-Type": "application/json"
- ]
- print(headers)
-
- let parameters: [String: Int] = [
- "lessonId" : id
- ]
- print(parameters)
-
- AF.request(url,
- method: .post,
- parameters: parameters,
- encoding: JSONEncoding.default,
- 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 lessonListData = try decoder.decode([LessonListModel].self, from: jsonData)
- delegate.lessonListModel(model: lessonListData)
- print(lessonListData.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
- }
- } else {
- delegate.stopLoader()
- print("Server error")
- }
- }
- }
- }
- }
- protocol LessonListProtocol {
- func startLoader()
- func stopLoader()
- func showError(error:String)
- func lessonListModel(model: [LessonListModel])
- }
|