NonEMIViewModel.swift 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. //
  2. // NonEMIViewModel.swift
  3. // Product Calculator
  4. //
  5. // Created by Suraj Kumar Mandal on 07/12/21.
  6. //
  7. import Foundation
  8. import Alamofire
  9. class NonEMIViewModel {
  10. var delegate: NonEMIViewProtocol?
  11. var nonEmiModel = [EMILoanOriginalModel]()
  12. //POST Non-EMI Base Loan API
  13. func getNonEMILoan(loanAmount:String, interestRate:String, loanTenure:String, interestPaymentFrequency:String, loanStartDate:String) {
  14. if let delegate = delegate {
  15. delegate.startLoader()
  16. let url = ApiUrl.BASE_URL + ApiUrl.GET_NON_EMI_LOAN
  17. var interest = String()
  18. switch interestPaymentFrequency {
  19. case "Monthly":
  20. interest = "12"
  21. case "Bi-Monthly":
  22. interest = "6"
  23. case "Quarterly":
  24. interest = "4"
  25. case "Triannually":
  26. interest = "3"
  27. case "Half Yearly":
  28. interest = "2"
  29. case "Annually":
  30. interest = "1"
  31. default:
  32. interest = ""
  33. }
  34. let parameters: [String: String] = [
  35. "loanAmount": loanAmount,
  36. "interestRate": interestRate,
  37. "loanTenure": loanTenure,
  38. "interestPaymentFrequency": interest,
  39. "loanStartDate": loanStartDate
  40. ]
  41. print(parameters)
  42. AF.request(url, method: .post, parameters: parameters, encoding: JSONEncoding.default).responseJSON { response in
  43. switch response.result {
  44. case .success(let value):
  45. delegate.stopLoader()
  46. print(value)
  47. let data = value as! NSDictionary
  48. print(data)
  49. DispatchQueue.main.async {
  50. let loanCategory = data["loanCategory"] as? Int
  51. let loanCategoryLabel = data["loanCategoryLabel"] as? String
  52. let loanDiscription = data["loanDiscription"] as? String
  53. let loanProvider = data["loanProvider"] as? String
  54. let loanType = data["loanType"] as? Int
  55. let loanTypeLabel = data["loanTypeLabel"] as? String
  56. let loanAmount = data["loanAmount"] as? Double
  57. let interestRate = data["interestRate"] as! Double
  58. let loanTenure = data["loanTenure"] as! Int
  59. let loanStartDate = data["loanStartDate"] as? String
  60. let numberOfEMI = data["numberOfEMI"] as! Int
  61. let emiAmount = data["emiAmount"] as! Double
  62. let loanEndDate = data["loanEndDate"] as! String
  63. let interestPaymentFrequency = data["interestPaymentFrequency"] as! Int
  64. let totalInterestPaid = data["totalInterestPaid"] as! Double
  65. let loan_original_flag = data["loan_original_flag"] as? String
  66. let displayDate = data["displayDate"] as? String
  67. let nonEmiData = EMILoanOriginalModel(loanCategoryLabel: loanCategoryLabel ?? "", loanDiscription: loanDiscription ?? "", loanProvider: loanProvider ?? "", loanTypeLabel: loanTypeLabel ?? "", loanStartDate: loanStartDate ?? "", loanEndDate: loanEndDate, loan_original_flag: loan_original_flag ?? "", displayDate: displayDate ?? "", loanCategory: loanCategory ?? 0, loanType: loanType ?? 0, loanTenure: loanTenure, numberOfEMI: numberOfEMI, interestPaymentFrequency: interestPaymentFrequency, loanAmount: loanAmount ?? 0.0 , interestRate: interestRate, emiAmount: emiAmount, totalInterestPaid: totalInterestPaid)
  68. self.nonEmiModel.append(nonEmiData)
  69. delegate.navigate(nonEmiData)
  70. }
  71. case .failure(let error):
  72. delegate.stopLoader()
  73. print(error)
  74. //delegate.showError(error: error)
  75. }
  76. }
  77. }
  78. }
  79. }
  80. protocol NonEMIViewProtocol {
  81. func startLoader()
  82. func stopLoader()
  83. func showError(error:String)
  84. func navigate(_ data:EMILoanOriginalModel)
  85. }