NonEMIViewController.swift 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. //
  2. // NonEMIViewController.swift
  3. // Product Calculator
  4. //
  5. // Created by Suraj Kumar Mandal on 07/12/21.
  6. //
  7. import UIKit
  8. import SideMenu
  9. class NonEMIViewController: UIViewController {
  10. @IBOutlet var loanAmountTF: UITextField!
  11. @IBOutlet var interestRateTF: UITextField!
  12. @IBOutlet var loanTermTF: UITextField!
  13. @IBOutlet var interestPayoutFrequencyTF: UITextField!
  14. @IBOutlet var loanStartDateTF: UITextField!
  15. fileprivate let customPicker = ToolbarPickerView()
  16. var menu = Int()
  17. var viewModel = NonEMIViewModel()
  18. override func viewDidLoad() {
  19. super.viewDidLoad()
  20. // Do any additional setup after loading the view.
  21. viewModel.delegate = self
  22. loanAmountTF.delegate = self
  23. interestRateTF.delegate = self
  24. loanTermTF.delegate = self
  25. interestPayoutFrequencyTF.delegate = self
  26. interestPayoutFrequencyTF.delegate = self
  27. }
  28. override func viewWillAppear(_ animated: Bool) {
  29. super.viewWillAppear(animated)
  30. navigationController?.navigationBar.barStyle = .black
  31. setupUI()
  32. }
  33. override var preferredStatusBarStyle: UIStatusBarStyle {
  34. return .lightContent
  35. }
  36. func setupUI() {
  37. self.navigationItem.title = AppConstant.MENU_TITLE[menu]
  38. self.navigationItem.hidesBackButton = true
  39. let menuBarButtonItem = UIBarButtonItem(image: UIImage(systemName: "line.3.horizontal"), style: .plain, target: self, action: #selector(openMenu))
  40. self.navigationItem.leftBarButtonItem = menuBarButtonItem
  41. if #available(iOS 13, *) {
  42. let appearance = UINavigationBarAppearance()
  43. appearance.backgroundColor = .systemBlue
  44. appearance.titleTextAttributes = [.foregroundColor: UIColor.white]
  45. appearance.largeTitleTextAttributes = [.foregroundColor: UIColor.white]
  46. navigationController?.navigationBar.tintColor = .white
  47. navigationController?.navigationBar.standardAppearance = appearance
  48. navigationController?.navigationBar.compactAppearance = appearance
  49. navigationController?.navigationBar.scrollEdgeAppearance = appearance
  50. }
  51. //Date Picker
  52. loanStartDateTF.datePicker(target: self,
  53. doneAction: #selector(doneAction),
  54. cancelAction: #selector(cancelAction),
  55. datePickerMode: .date)
  56. loanStartDateTF.delegate = self
  57. //Picker View
  58. createPickerView()
  59. }
  60. @objc func openMenu(_ sender: Any) {
  61. let menu = storyboard!.instantiateViewController(withIdentifier: "SideMenuNavigationController") as! SideMenuNavigationController
  62. present(menu, animated: true, completion: nil)
  63. }
  64. func createPickerView() {
  65. self.interestPayoutFrequencyTF.inputView = self.customPicker
  66. self.interestPayoutFrequencyTF.inputAccessoryView = self.customPicker.toolbar
  67. self.customPicker.delegate = self
  68. self.customPicker.dataSource = self
  69. self.customPicker.toolbarDelegate = self
  70. }
  71. @objc
  72. func cancelAction() {
  73. self.loanStartDateTF.resignFirstResponder()
  74. }
  75. @objc
  76. func doneAction() {
  77. if let datePickerView = self.loanStartDateTF.inputView as? UIDatePicker {
  78. let dateFormatter = DateFormatter()
  79. dateFormatter.dateFormat = AppConstant.dateFormat1
  80. let dateString = dateFormatter.string(from: datePickerView.date)
  81. self.loanStartDateTF.text = dateString
  82. print(datePickerView.date)
  83. print(dateString)
  84. self.loanStartDateTF.resignFirstResponder()
  85. }
  86. }
  87. func dataValidation() {
  88. if loanAmountTF.text?.isEmpty == true {
  89. self.view.makeToast("Enter loan amount!")
  90. } else if interestRateTF.text?.isEmpty == true {
  91. self.view.makeToast("Enter interest rate!")
  92. } else if loanTermTF.text?.isEmpty == true {
  93. self.view.makeToast("Enter loan term!")
  94. } else if interestPayoutFrequencyTF.text?.isEmpty == true {
  95. self.view.makeToast("Select interest payout frequency!")
  96. } else if loanStartDateTF.text?.isEmpty == true {
  97. self.view.makeToast("Select loan start date!")
  98. } else {
  99. if Reachability.isConnectedToNetwork() {
  100. let interest = interestRateTF.text!
  101. let interestRate = Double(interest)!/100.0
  102. viewModel.getNonEMILoan(loanAmount: loanAmountTF.text!, interestRate: String(interestRate), loanTenure: loanTermTF.text!, interestPaymentFrequency: interestPayoutFrequencyTF.text!, loanStartDate: loanStartDateTF.text!)
  103. } else {
  104. Alert.showInternetFailureAlert(on: self)
  105. }
  106. }
  107. }
  108. /*
  109. // MARK: - Navigation
  110. // In a storyboard-based application, you will often want to do a little preparation before navigation
  111. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  112. // Get the new view controller using segue.destination.
  113. // Pass the selected object to the new view controller.
  114. }
  115. */
  116. @IBAction func saveAction(_ sender: Any) {
  117. self.dataValidation()
  118. }
  119. @IBAction func undoAction(_ sender: Any) {
  120. loanAmountTF.text = nil
  121. interestRateTF.text = nil
  122. loanTermTF.text = nil
  123. interestPayoutFrequencyTF.text = nil
  124. interestPayoutFrequencyTF.text = nil
  125. }
  126. }
  127. extension NonEMIViewController: UIPickerViewDelegate, UIPickerViewDataSource {
  128. // MARK: UIPickerView Delegation
  129. func numberOfComponents(in pickerView: UIPickerView) -> Int {
  130. return 1
  131. }
  132. func pickerView( _ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
  133. return AppConstant.INTEREST_PAYOUT_FREQUENCY.count
  134. }
  135. func pickerView( _ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
  136. return AppConstant.INTEREST_PAYOUT_FREQUENCY[row]
  137. }
  138. func pickerView( _ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
  139. interestPayoutFrequencyTF.text = AppConstant.INTEREST_PAYOUT_FREQUENCY[row]
  140. }
  141. }
  142. extension NonEMIViewController: UITextFieldDelegate {
  143. func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
  144. switch textField {
  145. case interestPayoutFrequencyTF:
  146. return false
  147. default:
  148. return true
  149. }
  150. }
  151. // when user select a textfield, this method will be called
  152. func textFieldDidBeginEditing(_ textField: UITextField) {
  153. switch textField {
  154. case interestPayoutFrequencyTF:
  155. customPicker.reloadAllComponents()
  156. default:
  157. break
  158. }
  159. }
  160. }
  161. extension NonEMIViewController: ToolbarPickerViewDelegate {
  162. func didTapDone() {
  163. let row = self.customPicker.selectedRow(inComponent: 0)
  164. self.customPicker.selectRow(row, inComponent: 0, animated: false)
  165. self.interestPayoutFrequencyTF.text = AppConstant.INTEREST_PAYOUT_FREQUENCY[row]
  166. self.interestPayoutFrequencyTF.resignFirstResponder()
  167. }
  168. func didTapCancel() {
  169. self.interestPayoutFrequencyTF.text = nil
  170. self.interestPayoutFrequencyTF.resignFirstResponder()
  171. }
  172. }
  173. extension NonEMIViewController: NonEMIViewProtocol {
  174. func navigate(_ data: EMILoanOriginalModel) {
  175. DispatchQueue.main.async {
  176. let nonEMISummaryVC = self.storyboard?.instantiateViewController(withIdentifier: "NonEMISummaryViewController") as! NonEMISummaryViewController
  177. nonEMISummaryVC.nonEmiModel = data
  178. self.navigationController?.pushViewController(nonEMISummaryVC, animated: true)
  179. }
  180. }
  181. func startLoader() {
  182. ActivityIndicator.start()
  183. }
  184. func stopLoader() {
  185. ActivityIndicator.stop()
  186. }
  187. func showError(error: String) {
  188. self.view.makeToast(error)
  189. }
  190. }