EMILoanViewController.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. //
  2. // EMILoanViewController.swift
  3. // Product Calculator
  4. //
  5. // Created by Suraj Kumar Mandal on 01/12/21.
  6. //
  7. import UIKit
  8. import SideMenu
  9. import Toast_Swift
  10. class EMILoanViewController: UIViewController {
  11. @IBOutlet var loanCategoryTF: UITextField!
  12. @IBOutlet var loanDescriptionTF: UITextFieldWithDoneButton!
  13. @IBOutlet var loanProviderTF: UITextFieldWithDoneButton!
  14. @IBOutlet var loanAmountTypeTF: UITextField!
  15. @IBOutlet var loanStartDateTF: UITextField!
  16. @IBOutlet var originalLoanTenureTF: UITextFieldWithDoneButton!
  17. @IBOutlet var originalLoanAmountTF: UITextFieldWithDoneButton!
  18. @IBOutlet var originalInterestRateTF: UITextFieldWithDoneButton!
  19. @IBOutlet var loanStartDateLabel: UILabel!
  20. @IBOutlet var originalLoanTenureLabel: UILabel!
  21. @IBOutlet var originalLoanAmountLabel: UILabel!
  22. @IBOutlet var originalInterestRateLabel: UILabel!
  23. fileprivate let customPicker = ToolbarPickerView()
  24. var activeTextField = 0
  25. var menu = Int()
  26. var viewModel = EMILoanViewModel()
  27. override func viewDidLoad() {
  28. super.viewDidLoad()
  29. // Do any additional setup after loading the view.
  30. viewModel.delegate = self
  31. loanCategoryTF.delegate = self
  32. loanDescriptionTF.delegate = self
  33. loanProviderTF.delegate = self
  34. loanAmountTypeTF.delegate = self
  35. loanStartDateTF.delegate = self
  36. originalLoanTenureTF.delegate = self
  37. originalLoanAmountTF.delegate = self
  38. originalInterestRateTF.delegate = self
  39. }
  40. override func viewWillAppear(_ animated: Bool) {
  41. super.viewWillAppear(animated)
  42. navigationController?.navigationBar.barStyle = .black
  43. setupUI()
  44. }
  45. override var preferredStatusBarStyle: UIStatusBarStyle {
  46. return .lightContent
  47. }
  48. func setupUI() {
  49. self.navigationItem.title = AppConstant.MENU_TITLE[menu]
  50. self.navigationItem.hidesBackButton = true
  51. let menuBarButtonItem = UIBarButtonItem(image: UIImage(systemName: "line.3.horizontal"), style: .plain, target: self, action: #selector(openMenu))
  52. self.navigationItem.leftBarButtonItem = menuBarButtonItem
  53. if #available(iOS 13, *) {
  54. let appearance = UINavigationBarAppearance()
  55. appearance.backgroundColor = .systemBlue
  56. appearance.titleTextAttributes = [.foregroundColor: UIColor.white]
  57. appearance.largeTitleTextAttributes = [.foregroundColor: UIColor.white]
  58. navigationController?.navigationBar.tintColor = .white
  59. navigationController?.navigationBar.standardAppearance = appearance
  60. navigationController?.navigationBar.compactAppearance = appearance
  61. navigationController?.navigationBar.scrollEdgeAppearance = appearance
  62. }
  63. //Date Picker
  64. loanStartDateTF.datePicker(target: self,
  65. doneAction: #selector(doneAction),
  66. cancelAction: #selector(cancelAction),
  67. datePickerMode: .date)
  68. loanStartDateTF.delegate = self
  69. //Picker View
  70. createPickerView()
  71. }
  72. @objc func openMenu(_ sender: Any) {
  73. let menu = storyboard!.instantiateViewController(withIdentifier: "SideMenuNavigationController") as! SideMenuNavigationController
  74. present(menu, animated: true, completion: nil)
  75. }
  76. func createPickerView() {
  77. self.loanCategoryTF.inputView = self.customPicker
  78. self.loanCategoryTF.inputAccessoryView = self.customPicker.toolbar
  79. self.loanAmountTypeTF.inputView = self.customPicker
  80. self.loanAmountTypeTF.inputAccessoryView = self.customPicker.toolbar
  81. self.customPicker.delegate = self
  82. self.customPicker.dataSource = self
  83. self.customPicker.toolbarDelegate = self
  84. }
  85. @objc
  86. func cancelAction() {
  87. self.loanStartDateTF.resignFirstResponder()
  88. }
  89. @objc
  90. func doneAction() {
  91. if let datePickerView = self.loanStartDateTF.inputView as? UIDatePicker {
  92. let dateFormatter = DateFormatter()
  93. dateFormatter.dateFormat = AppConstant.dateFormat1
  94. let dateString = dateFormatter.string(from: datePickerView.date)
  95. self.loanStartDateTF.text = dateString
  96. //self.dateOfBirth = dateString
  97. print(datePickerView.date)
  98. print(dateString)
  99. self.loanStartDateTF.resignFirstResponder()
  100. }
  101. }
  102. func dataValidation() {
  103. if loanCategoryTF.text?.isEmpty == true {
  104. self.view.makeToast("Select loan category!")
  105. } else if loanDescriptionTF.text?.isEmpty == true {
  106. self.view.makeToast("Fill loan description!")
  107. } else if loanProviderTF.text?.isEmpty == true {
  108. self.view.makeToast("Fill loan provider!")
  109. } else if loanAmountTypeTF.text?.isEmpty == true {
  110. self.view.makeToast("Select loan amount type!")
  111. } else if loanStartDateTF.text?.isEmpty == true {
  112. self.view.makeToast("Select loan start date!")
  113. } else if originalLoanTenureTF.text?.isEmpty == true {
  114. self.view.makeToast("Fill original loan tenure!")
  115. } else if originalLoanAmountTF.text?.isEmpty == true {
  116. self.view.makeToast("Fill original loan amount!")
  117. } else if originalInterestRateTF.text?.isEmpty == true {
  118. self.view.makeToast("Fill original interest rate!")
  119. } else {
  120. if Reachability.isConnectedToNetwork() {
  121. if loanAmountTypeTF.text == "Original Loan Amount(Rs)" {
  122. let interest = originalInterestRateTF.text!
  123. let interestPercent = Double(interest)!/100.0
  124. viewModel.getEMIOriginalLoan(loanFlag: "Y", originalInterestRate: String(interestPercent), originalLoanAmount: originalLoanAmountTF.text!, loanStartDate: loanStartDateTF.text!, loanTenure: originalLoanTenureTF.text!)
  125. } else {
  126. let interest = originalLoanAmountTF.text!
  127. let interestPercent = Double(interest)!/100.0
  128. viewModel.getEMIOutstandingLoan(loanFlag: "N", originalInterestRate: String(interestPercent), originalLoanAmount: originalLoanTenureTF.text!, loanDayout: loanStartDateTF.text!, loanEMIAmt: originalInterestRateTF.text!)
  129. }
  130. } else {
  131. Alert.showInternetFailureAlert(on: self)
  132. }
  133. }
  134. }
  135. /*
  136. // MARK: - Navigation
  137. // In a storyboard-based application, you will often want to do a little preparation before navigation
  138. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  139. // Get the new view controller using segue.destination.
  140. // Pass the selected object to the new view controller.
  141. }
  142. */
  143. @IBAction func saveAction(_ sender: Any) {
  144. self.dataValidation()
  145. }
  146. @IBAction func undoAction(_ sender: Any) {
  147. loanCategoryTF.text = nil
  148. loanDescriptionTF.text = nil
  149. loanProviderTF.text = nil
  150. loanAmountTypeTF.text = nil
  151. loanStartDateTF.text = nil
  152. originalLoanTenureTF.text = nil
  153. originalLoanAmountTF.text = nil
  154. originalInterestRateTF.text = nil
  155. }
  156. }
  157. extension EMILoanViewController: UIPickerViewDelegate, UIPickerViewDataSource {
  158. // MARK: UIPickerView Delegation
  159. func numberOfComponents(in pickerView: UIPickerView) -> Int {
  160. return 1
  161. }
  162. func pickerView( _ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
  163. switch activeTextField
  164. {
  165. case 1:
  166. return AppConstant.LOAN_CATEGORY.count
  167. case 2:
  168. return AppConstant.LOAN_AMOUNT_TYPE.count
  169. default:
  170. return 0
  171. }
  172. }
  173. func pickerView( _ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
  174. switch activeTextField
  175. {
  176. case 1:
  177. return AppConstant.LOAN_CATEGORY[row]
  178. case 2:
  179. return AppConstant.LOAN_AMOUNT_TYPE[row]
  180. default:
  181. return ""
  182. }
  183. }
  184. func pickerView( _ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
  185. switch activeTextField
  186. {
  187. case 1:
  188. loanCategoryTF.text = AppConstant.LOAN_CATEGORY[row]
  189. case 2:
  190. loanAmountTypeTF.text = AppConstant.LOAN_AMOUNT_TYPE[row]
  191. if loanAmountTypeTF.text == "Original Loan Amount(Rs)" {
  192. loanStartDateLabel.text = "Loan Start Date :"
  193. originalLoanTenureLabel.text = "Original Loan Tenure(Years) :"
  194. originalLoanAmountLabel.text = "Original Loan Amount(Rs) :"
  195. originalInterestRateLabel.text = "Original Interest Rate(%) :"
  196. } else {
  197. loanStartDateLabel.text = "Reference Date :"
  198. originalLoanTenureLabel.text = "Outstanding Principal Today(Rs) :"
  199. originalLoanAmountLabel.text = "Current Interest Rate(%) :"
  200. originalInterestRateLabel.text = "Current EMI Amount(Rs) :"
  201. }
  202. default:
  203. break
  204. }
  205. }
  206. }
  207. extension EMILoanViewController: UITextFieldDelegate {
  208. func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
  209. switch textField {
  210. case loanCategoryTF:
  211. return false
  212. case loanAmountTypeTF:
  213. return false
  214. default:
  215. return true
  216. }
  217. }
  218. // when user select a textfield, this method will be called
  219. func textFieldDidBeginEditing(_ textField: UITextField) {
  220. switch textField {
  221. case loanCategoryTF:
  222. activeTextField = 1
  223. customPicker.reloadAllComponents()
  224. case loanAmountTypeTF:
  225. activeTextField = 2
  226. customPicker.reloadAllComponents()
  227. default:
  228. activeTextField = 0
  229. }
  230. }
  231. }
  232. extension EMILoanViewController: ToolbarPickerViewDelegate {
  233. func didTapDone() {
  234. switch activeTextField {
  235. case 1:
  236. let row = self.customPicker.selectedRow(inComponent: 0)
  237. self.customPicker.selectRow(row, inComponent: 0, animated: false)
  238. self.loanCategoryTF.text = AppConstant.LOAN_CATEGORY[row]
  239. self.loanCategoryTF.resignFirstResponder()
  240. case 2:
  241. let row = self.customPicker.selectedRow(inComponent: 0)
  242. self.customPicker.selectRow(row, inComponent: 0, animated: false)
  243. self.loanAmountTypeTF.text = AppConstant.LOAN_AMOUNT_TYPE[row]
  244. self.loanAmountTypeTF.resignFirstResponder()
  245. default:
  246. return
  247. }
  248. }
  249. func didTapCancel() {
  250. switch activeTextField {
  251. case 1:
  252. self.loanCategoryTF.text = nil
  253. self.loanCategoryTF.resignFirstResponder()
  254. case 2:
  255. self.loanAmountTypeTF.text = nil
  256. self.loanAmountTypeTF.resignFirstResponder()
  257. default:
  258. return
  259. }
  260. }
  261. }
  262. extension EMILoanViewController: EMILoanViewProtocol {
  263. func navigateOriginal(_ data: EMILoanOriginalModel) {
  264. DispatchQueue.main.async {
  265. let emiOriginalSummaryVC = self.storyboard?.instantiateViewController(withIdentifier: "EMILoanSummaryViewController") as! EMILoanSummaryViewController
  266. emiOriginalSummaryVC.emiOriginalModel = data
  267. self.navigationController?.pushViewController(emiOriginalSummaryVC, animated: true)
  268. }
  269. }
  270. func navigateOutstanding(_ data: EMILoanOutstandingModel) {
  271. DispatchQueue.main.async {
  272. let emiOutstandingSummaryVC = self.storyboard?.instantiateViewController(withIdentifier: "EMILoanSummaryViewController") as! EMILoanSummaryViewController
  273. emiOutstandingSummaryVC.emiOutstandingModel = data
  274. self.navigationController?.pushViewController(emiOutstandingSummaryVC, animated: true)
  275. }
  276. }
  277. func startLoader() {
  278. ActivityIndicator.start()
  279. }
  280. func stopLoader() {
  281. ActivityIndicator.stop()
  282. }
  283. func showError(error: String) {
  284. self.view.makeToast(error)
  285. }
  286. }