SukanyaSamriddhiViewController.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. //
  2. // SukanyaSamriddhiViewController.swift
  3. // Product Calculator
  4. //
  5. // Created by Suraj Kumar Mandal on 05/12/21.
  6. //
  7. import UIKit
  8. import SideMenu
  9. import Toast_Swift
  10. class SukanyaSamriddhiViewController: UIViewController {
  11. @IBOutlet var depositDateTF: UITextField!
  12. @IBOutlet var girlChildAgeTF: UITextField!
  13. @IBOutlet var depositAmountTF: UITextField!
  14. @IBOutlet var amountDepositFrequencyTF: UITextField!
  15. @IBOutlet var annualInterestRateTF: UITextField!
  16. @IBOutlet var paymentTermTF: UITextField!
  17. @IBOutlet var maturityTermTF: UITextField!
  18. @IBOutlet var compoundFrequencyTF: UITextField!
  19. fileprivate let customPicker = ToolbarPickerView()
  20. var activeTextField = 0
  21. var menu = Int()
  22. var viewModel = SukanyaSamriddhiViewModel()
  23. override func viewDidLoad() {
  24. super.viewDidLoad()
  25. // Do any additional setup after loading the view.
  26. viewModel.delegate = self
  27. depositDateTF.delegate = self
  28. girlChildAgeTF.delegate = self
  29. depositAmountTF.delegate = self
  30. amountDepositFrequencyTF.delegate = self
  31. annualInterestRateTF.delegate = self
  32. paymentTermTF.delegate = self
  33. maturityTermTF.delegate = self
  34. compoundFrequencyTF.delegate = self
  35. }
  36. override func viewWillAppear(_ animated: Bool) {
  37. super.viewWillAppear(animated)
  38. navigationController?.navigationBar.barStyle = .black
  39. setupUI()
  40. }
  41. override var preferredStatusBarStyle: UIStatusBarStyle {
  42. return .lightContent
  43. }
  44. func setupUI() {
  45. self.navigationItem.title = AppConstant.MENU_TITLE[menu]
  46. self.navigationItem.hidesBackButton = true
  47. let menuBarButtonItem = UIBarButtonItem(image: UIImage(systemName: "line.3.horizontal"), style: .plain, target: self, action: #selector(openMenu))
  48. self.navigationItem.leftBarButtonItem = menuBarButtonItem
  49. if #available(iOS 13, *) {
  50. let appearance = UINavigationBarAppearance()
  51. appearance.backgroundColor = .systemBlue
  52. appearance.titleTextAttributes = [.foregroundColor: UIColor.white]
  53. appearance.largeTitleTextAttributes = [.foregroundColor: UIColor.white]
  54. navigationController?.navigationBar.tintColor = .white
  55. navigationController?.navigationBar.standardAppearance = appearance
  56. navigationController?.navigationBar.compactAppearance = appearance
  57. navigationController?.navigationBar.scrollEdgeAppearance = appearance
  58. }
  59. //Date Picker
  60. depositDateTF.datePicker(target: self,
  61. doneAction: #selector(doneAction),
  62. cancelAction: #selector(cancelAction),
  63. datePickerMode: .date)
  64. depositDateTF.delegate = self
  65. //Picker View
  66. createPickerView()
  67. }
  68. @objc func openMenu(_ sender: Any) {
  69. let menu = storyboard!.instantiateViewController(withIdentifier: "SideMenuNavigationController") as! SideMenuNavigationController
  70. present(menu, animated: true, completion: nil)
  71. }
  72. func createPickerView() {
  73. self.amountDepositFrequencyTF.inputView = self.customPicker
  74. self.amountDepositFrequencyTF.inputAccessoryView = self.customPicker.toolbar
  75. self.compoundFrequencyTF.inputView = self.customPicker
  76. self.compoundFrequencyTF.inputAccessoryView = self.customPicker.toolbar
  77. self.customPicker.delegate = self
  78. self.customPicker.dataSource = self
  79. self.customPicker.toolbarDelegate = self
  80. }
  81. @objc
  82. func cancelAction() {
  83. self.depositDateTF.resignFirstResponder()
  84. }
  85. @objc
  86. func doneAction() {
  87. if let datePickerView = self.depositDateTF.inputView as? UIDatePicker {
  88. let dateFormatter = DateFormatter()
  89. dateFormatter.dateFormat = AppConstant.dateFormat2
  90. let dateString = dateFormatter.string(from: datePickerView.date)
  91. self.depositDateTF.text = dateString
  92. print(datePickerView.date)
  93. print(dateString)
  94. self.depositDateTF.resignFirstResponder()
  95. if Reachability.isConnectedToNetwork() {
  96. viewModel.getInterestRate(depositDate: dateString)
  97. } else {
  98. Alert.showInternetFailureAlert(on: self)
  99. }
  100. }
  101. }
  102. func dataValidation() {
  103. if depositDateTF.text?.isEmpty == true {
  104. self.view.makeToast("Select deposit date!")
  105. } else if girlChildAgeTF.text?.isEmpty == true {
  106. self.view.makeToast("Select girl child age!")
  107. } else if depositAmountTF.text?.isEmpty == true {
  108. self.view.makeToast("Fill deposit amount!")
  109. } else if amountDepositFrequencyTF.text?.isEmpty == true {
  110. self.view.makeToast("Select amount deposit frequency!")
  111. } else if annualInterestRateTF.text?.isEmpty == true {
  112. self.view.makeToast("Fill annual interest rate!")
  113. } else if paymentTermTF.text?.isEmpty == true {
  114. self.view.makeToast("Select payment term in years!")
  115. } else if maturityTermTF.text?.isEmpty == true {
  116. self.view.makeToast("Select maturity term in years!")
  117. } else if compoundFrequencyTF.text?.isEmpty == true {
  118. self.view.makeToast("Select compound frequency!")
  119. } else {
  120. if Reachability.isConnectedToNetwork() {
  121. viewModel.getSukanyaSamriddhiOutput(depositAmount: depositAmountTF.text!, tenureType: "Y", paymenttenure: paymentTermTF.text!, rdDepositFreq: amountDepositFrequencyTF.text!, compundingFreq: compoundFrequencyTF.text!, depositDate: depositDateTF.text!, maturitytenure: maturityTermTF.text!)
  122. } else {
  123. Alert.showInternetFailureAlert(on: self)
  124. }
  125. }
  126. }
  127. /*
  128. // MARK: - Navigation
  129. // In a storyboard-based application, you will often want to do a little preparation before navigation
  130. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  131. // Get the new view controller using segue.destination.
  132. // Pass the selected object to the new view controller.
  133. }
  134. */
  135. @IBAction func saveAction(_ sender: Any) {
  136. self.dataValidation()
  137. }
  138. @IBAction func undoAction(_ sender: Any) {
  139. depositDateTF.text = nil
  140. girlChildAgeTF.text = nil
  141. depositAmountTF.text = nil
  142. amountDepositFrequencyTF.text = nil
  143. annualInterestRateTF.text = nil
  144. paymentTermTF.text = nil
  145. maturityTermTF.text = nil
  146. compoundFrequencyTF.text = nil
  147. }
  148. }
  149. extension SukanyaSamriddhiViewController: UIPickerViewDelegate, UIPickerViewDataSource {
  150. // MARK: UIPickerView Delegation
  151. func numberOfComponents(in pickerView: UIPickerView) -> Int {
  152. return 1
  153. }
  154. func pickerView( _ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
  155. switch activeTextField
  156. {
  157. case 1:
  158. return AppConstant.INTEREST_PAYOUT_FREQUENCY.count
  159. case 2:
  160. return AppConstant.INTEREST_PAYOUT_FREQUENCY.count
  161. default:
  162. return 0
  163. }
  164. }
  165. func pickerView( _ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
  166. switch activeTextField{
  167. case 1:
  168. return AppConstant.INTEREST_PAYOUT_FREQUENCY[row]
  169. case 2:
  170. return AppConstant.INTEREST_PAYOUT_FREQUENCY[row]
  171. default:
  172. return ""
  173. }
  174. }
  175. func pickerView( _ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
  176. switch activeTextField{
  177. case 1:
  178. amountDepositFrequencyTF.text = AppConstant.INTEREST_PAYOUT_FREQUENCY[row]
  179. break
  180. case 2:
  181. compoundFrequencyTF.text = AppConstant.INTEREST_PAYOUT_FREQUENCY[row]
  182. break
  183. default:
  184. break
  185. }
  186. }
  187. }
  188. extension SukanyaSamriddhiViewController: UITextFieldDelegate {
  189. func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
  190. switch textField {
  191. case amountDepositFrequencyTF:
  192. return false
  193. case compoundFrequencyTF:
  194. return false
  195. default:
  196. return true
  197. }
  198. }
  199. // when user select a textfield, this method will be called
  200. func textFieldDidBeginEditing(_ textField: UITextField) {
  201. switch textField {
  202. case amountDepositFrequencyTF:
  203. activeTextField = 1
  204. customPicker.reloadAllComponents()
  205. case compoundFrequencyTF:
  206. activeTextField = 2
  207. customPicker.reloadAllComponents()
  208. default:
  209. activeTextField = 0
  210. }
  211. }
  212. }
  213. extension SukanyaSamriddhiViewController: ToolbarPickerViewDelegate {
  214. func didTapDone() {
  215. switch activeTextField {
  216. case 1:
  217. let row = self.customPicker.selectedRow(inComponent: 0)
  218. self.customPicker.selectRow(row, inComponent: 0, animated: false)
  219. self.amountDepositFrequencyTF.text = AppConstant.DEPOSIT_TYPE[row]
  220. self.amountDepositFrequencyTF.resignFirstResponder()
  221. case 2:
  222. let row = self.customPicker.selectedRow(inComponent: 0)
  223. self.customPicker.selectRow(row, inComponent: 0, animated: false)
  224. self.compoundFrequencyTF.text = AppConstant.FIXED_DEPOSIT_TERM[row]
  225. self.compoundFrequencyTF.resignFirstResponder()
  226. default:
  227. return
  228. }
  229. }
  230. func didTapCancel() {
  231. switch activeTextField {
  232. case 1:
  233. self.amountDepositFrequencyTF.text = nil
  234. self.amountDepositFrequencyTF.resignFirstResponder()
  235. case 2:
  236. self.compoundFrequencyTF.text = nil
  237. self.compoundFrequencyTF.resignFirstResponder()
  238. default:
  239. return
  240. }
  241. }
  242. }
  243. extension SukanyaSamriddhiViewController: SukanyaSamriddhiViewProtocol {
  244. func navigate(_ data: SukanyaSamriddhiModel) {
  245. DispatchQueue.main.async {
  246. let sukanyaSummaryVC = self.storyboard?.instantiateViewController(withIdentifier: "SukanyaSamriddhiSummaryViewController") as! SukanyaSamriddhiSummaryViewController
  247. sukanyaSummaryVC.sukanyaModel = data
  248. self.navigationController?.pushViewController(sukanyaSummaryVC, animated: true)
  249. }
  250. }
  251. func setInterestRate(rate: String) {
  252. annualInterestRateTF.text = rate
  253. paymentTermTF.text = "15"
  254. maturityTermTF.text = "21"
  255. compoundFrequencyTF.text = "Annually"
  256. }
  257. func startLoader() {
  258. ActivityIndicator.start()
  259. }
  260. func stopLoader() {
  261. ActivityIndicator.stop()
  262. }
  263. func showError(error: String) {
  264. self.view.makeToast(error)
  265. }
  266. }