PublicProvidentViewController.swift 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. //
  2. // PublicProvidentViewController.swift
  3. // Product Calculator
  4. //
  5. // Created by Suraj Kumar Mandal on 06/12/21.
  6. //
  7. import UIKit
  8. import SideMenu
  9. import Toast_Swift
  10. class PublicProvidentViewController: UIViewController {
  11. @IBOutlet var planningDateTF: UITextField!
  12. @IBOutlet var ppfDepositAmountTF: UITextField!
  13. @IBOutlet var amountDepositFrequencyTF: UITextField!
  14. @IBOutlet var depositStartDateTF: UITextField!
  15. @IBOutlet var annualInterestRateTF: UITextField!
  16. @IBOutlet var termYearsTF: UITextField!
  17. @IBOutlet var compoundFrequencyTF: UITextField!
  18. @IBOutlet var extensionTF: UITextField!
  19. @IBOutlet var extensionTypeLabel: UILabel!
  20. @IBOutlet var extensionTypeTF: UITextField!
  21. @IBOutlet var extensionDepositAmountLabel: UILabel!
  22. @IBOutlet var extensionDepositAmountTF: UITextField!
  23. @IBOutlet var extensionAmountDepositFrequencyLabel: UILabel!
  24. @IBOutlet var extensionAmountDepositFrequencyTF: UITextField!
  25. @IBOutlet var extensionAnnualInterestRateLabel: UILabel!
  26. @IBOutlet var extensionAnnualInterestRateTF: UITextField!
  27. @IBOutlet var extensionTermYearsLabel: UILabel!
  28. @IBOutlet var extensionTermYearsTF: UITextField!
  29. @IBOutlet var extensionCompoundFrequencyLabel: UILabel!
  30. @IBOutlet var extensionCompoundFrequencyTF: UITextField!
  31. fileprivate let customPicker = ToolbarPickerView()
  32. var activeTextField = 0
  33. var menu = Int()
  34. var isExtension = Bool()
  35. var viewModel = PublicProvidentViewModel()
  36. override func viewDidLoad() {
  37. super.viewDidLoad()
  38. // Do any additional setup after loading the view.
  39. viewModel.delegate = self
  40. planningDateTF.delegate = self
  41. ppfDepositAmountTF.delegate = self
  42. amountDepositFrequencyTF.delegate = self
  43. depositStartDateTF.delegate = self
  44. annualInterestRateTF.delegate = self
  45. termYearsTF.delegate = self
  46. compoundFrequencyTF.delegate = self
  47. extensionTF.delegate = self
  48. extensionTypeTF.delegate = self
  49. extensionDepositAmountTF.delegate = self
  50. extensionAmountDepositFrequencyTF.delegate = self
  51. extensionAnnualInterestRateTF.delegate = self
  52. extensionTermYearsTF.delegate = self
  53. extensionCompoundFrequencyTF.delegate = self
  54. isExtension = false
  55. extensionStatus()
  56. }
  57. override func viewWillAppear(_ animated: Bool) {
  58. super.viewWillAppear(animated)
  59. navigationController?.navigationBar.barStyle = .black
  60. setupUI()
  61. }
  62. override var preferredStatusBarStyle: UIStatusBarStyle {
  63. return .lightContent
  64. }
  65. func setupUI() {
  66. self.navigationItem.title = AppConstant.MENU_TITLE[menu]
  67. self.navigationItem.hidesBackButton = true
  68. let menuBarButtonItem = UIBarButtonItem(image: UIImage(systemName: "line.3.horizontal"), style: .plain, target: self, action: #selector(openMenu))
  69. self.navigationItem.leftBarButtonItem = menuBarButtonItem
  70. if #available(iOS 13, *) {
  71. let appearance = UINavigationBarAppearance()
  72. appearance.backgroundColor = .systemBlue
  73. appearance.titleTextAttributes = [.foregroundColor: UIColor.white]
  74. appearance.largeTitleTextAttributes = [.foregroundColor: UIColor.white]
  75. navigationController?.navigationBar.tintColor = .white
  76. navigationController?.navigationBar.standardAppearance = appearance
  77. navigationController?.navigationBar.compactAppearance = appearance
  78. navigationController?.navigationBar.scrollEdgeAppearance = appearance
  79. }
  80. //Planning date set
  81. let date = Date()
  82. let dateFormatter = DateFormatter()
  83. dateFormatter.dateFormat = "dd-MM-yyyy"
  84. planningDateTF.text = dateFormatter.string(from: date)
  85. //Date Picker
  86. depositStartDateTF.datePicker(target: self,
  87. doneAction: #selector(doneAction),
  88. cancelAction: #selector(cancelAction),
  89. datePickerMode: .date)
  90. depositStartDateTF.delegate = self
  91. //Picker View
  92. createPickerView()
  93. //Term in years
  94. termYearsTF.text = "15"
  95. compoundFrequencyTF.text = "Annually"
  96. }
  97. @objc func openMenu(_ sender: Any) {
  98. let menu = storyboard!.instantiateViewController(withIdentifier: "SideMenuNavigationController") as! SideMenuNavigationController
  99. present(menu, animated: true, completion: nil)
  100. }
  101. func createPickerView() {
  102. self.amountDepositFrequencyTF.inputView = self.customPicker
  103. self.amountDepositFrequencyTF.inputAccessoryView = self.customPicker.toolbar
  104. self.compoundFrequencyTF.inputView = self.customPicker
  105. self.compoundFrequencyTF.inputAccessoryView = self.customPicker.toolbar
  106. self.extensionTF.inputView = self.customPicker
  107. self.extensionTF.inputAccessoryView = self.customPicker.toolbar
  108. self.customPicker.delegate = self
  109. self.customPicker.dataSource = self
  110. self.customPicker.toolbarDelegate = self
  111. }
  112. @objc
  113. func cancelAction() {
  114. self.depositStartDateTF.resignFirstResponder()
  115. }
  116. @objc
  117. func doneAction() {
  118. if let datePickerView = self.depositStartDateTF.inputView as? UIDatePicker {
  119. let dateFormatter = DateFormatter()
  120. dateFormatter.dateFormat = AppConstant.dateFormat2
  121. let dateString = dateFormatter.string(from: datePickerView.date)
  122. self.depositStartDateTF.text = dateString
  123. print(datePickerView.date)
  124. print(dateString)
  125. self.depositStartDateTF.resignFirstResponder()
  126. if Reachability.isConnectedToNetwork() {
  127. viewModel.getInterestRate(depositDate: dateString)
  128. } else {
  129. Alert.showInternetFailureAlert(on: self)
  130. }
  131. }
  132. }
  133. func dataValidationWithExtension() {
  134. if planningDateTF.text?.isEmpty == true {
  135. self.view.makeToast("Select deposit date!")
  136. } else if ppfDepositAmountTF.text?.isEmpty == true {
  137. self.view.makeToast("Select girl child age!")
  138. } else if amountDepositFrequencyTF.text?.isEmpty == true {
  139. self.view.makeToast("Fill deposit amount!")
  140. } else if depositStartDateTF.text?.isEmpty == true {
  141. self.view.makeToast("Select amount deposit frequency!")
  142. } else if annualInterestRateTF.text?.isEmpty == true {
  143. self.view.makeToast("Fill annual interest rate!")
  144. } else if termYearsTF.text?.isEmpty == true {
  145. self.view.makeToast("Select payment term in years!")
  146. } else if compoundFrequencyTF.text?.isEmpty == true {
  147. self.view.makeToast("Select maturity term in years!")
  148. } else if extensionTF.text?.isEmpty == true {
  149. self.view.makeToast("Select compound frequency!")
  150. } else {
  151. if Reachability.isConnectedToNetwork() {
  152. viewModel.getPPFExtOutput(depositAmount: ppfDepositAmountTF.text!, tenureType: "Y", tenure: termYearsTF.text!, rdDepositFreq: annualInterestRateTF.text!, compundingFreq: compoundFrequencyTF.text!, depositDate: depositStartDateTF.text!, totalAmountDiposited: ppfDepositAmountTF.text!)
  153. } else {
  154. Alert.showInternetFailureAlert(on: self)
  155. }
  156. }
  157. }
  158. func dataValidationWithoutExtension() {
  159. if planningDateTF.text?.isEmpty == true {
  160. self.view.makeToast("Select deposit date!")
  161. } else if ppfDepositAmountTF.text?.isEmpty == true {
  162. self.view.makeToast("Select girl child age!")
  163. } else if amountDepositFrequencyTF.text?.isEmpty == true {
  164. self.view.makeToast("Fill deposit amount!")
  165. } else if depositStartDateTF.text?.isEmpty == true {
  166. self.view.makeToast("Select amount deposit frequency!")
  167. } else if annualInterestRateTF.text?.isEmpty == true {
  168. self.view.makeToast("Fill annual interest rate!")
  169. } else if termYearsTF.text?.isEmpty == true {
  170. self.view.makeToast("Select payment term in years!")
  171. } else if compoundFrequencyTF.text?.isEmpty == true {
  172. self.view.makeToast("Select maturity term in years!")
  173. } else if extensionTF.text?.isEmpty == true {
  174. self.view.makeToast("Fill extension!")
  175. } else {
  176. if Reachability.isConnectedToNetwork() {
  177. viewModel.getPPFOutput(depositAmount: ppfDepositAmountTF.text!, tenureType: "Y", tenure: termYearsTF.text!, rdDepositFreq: amountDepositFrequencyTF.text!, compundingFreq: compoundFrequencyTF.text!, depositDate: depositStartDateTF.text!)
  178. } else {
  179. Alert.showInternetFailureAlert(on: self)
  180. }
  181. }
  182. }
  183. func extensionStatus() {
  184. //Extension type UI manage
  185. if isExtension == false {
  186. extensionTypeLabel.isHidden = true
  187. extensionTypeTF.isHidden = true
  188. extensionDepositAmountLabel.isHidden = true
  189. extensionDepositAmountTF.isHidden = true
  190. extensionAmountDepositFrequencyLabel.isHidden = true
  191. extensionAmountDepositFrequencyTF.isHidden = true
  192. extensionAnnualInterestRateLabel.isHidden = true
  193. extensionAnnualInterestRateTF.isHidden = true
  194. extensionTermYearsLabel.isHidden = true
  195. extensionTermYearsTF.isHidden = true
  196. extensionCompoundFrequencyLabel.isHidden = true
  197. extensionCompoundFrequencyTF.isHidden = true
  198. } else {
  199. extensionTypeLabel.isHidden = false
  200. extensionTypeTF.isHidden = false
  201. extensionDepositAmountLabel.isHidden = false
  202. extensionDepositAmountTF.isHidden = false
  203. extensionAmountDepositFrequencyLabel.isHidden = false
  204. extensionAmountDepositFrequencyTF.isHidden = false
  205. extensionAnnualInterestRateLabel.isHidden = false
  206. extensionAnnualInterestRateTF.isHidden = false
  207. extensionTermYearsLabel.isHidden = false
  208. extensionTermYearsTF.isHidden = false
  209. extensionCompoundFrequencyLabel.isHidden = false
  210. extensionCompoundFrequencyTF.isHidden = false
  211. }
  212. }
  213. /*
  214. // MARK: - Navigation
  215. // In a storyboard-based application, you will often want to do a little preparation before navigation
  216. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  217. // Get the new view controller using segue.destination.
  218. // Pass the selected object to the new view controller.
  219. }
  220. */
  221. @IBAction func saveAction(_ sender: Any) {
  222. if isExtension == false {
  223. self.dataValidationWithoutExtension()
  224. } else {
  225. self.dataValidationWithExtension()
  226. }
  227. }
  228. @IBAction func undoAction(_ sender: Any) {
  229. ppfDepositAmountTF.text = nil
  230. amountDepositFrequencyTF.text = nil
  231. depositStartDateTF.text = nil
  232. //extensionTF.text = nil
  233. }
  234. }
  235. extension PublicProvidentViewController: UIPickerViewDelegate, UIPickerViewDataSource {
  236. // MARK: UIPickerView Delegation
  237. func numberOfComponents(in pickerView: UIPickerView) -> Int {
  238. return 1
  239. }
  240. func pickerView( _ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
  241. switch activeTextField
  242. {
  243. case 1:
  244. return AppConstant.INTEREST_PAYOUT_FREQUENCY.count
  245. case 2:
  246. return AppConstant.INTEREST_PAYOUT_FREQUENCY.count
  247. case 3:
  248. return AppConstant.EXTENSION.count
  249. default:
  250. return 0
  251. }
  252. }
  253. func pickerView( _ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
  254. switch activeTextField{
  255. case 1:
  256. return AppConstant.INTEREST_PAYOUT_FREQUENCY[row]
  257. case 2:
  258. return AppConstant.INTEREST_PAYOUT_FREQUENCY[row]
  259. case 3:
  260. return AppConstant.EXTENSION[row]
  261. default:
  262. return ""
  263. }
  264. }
  265. func pickerView( _ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
  266. switch activeTextField{
  267. case 1:
  268. amountDepositFrequencyTF.text = AppConstant.INTEREST_PAYOUT_FREQUENCY[row]
  269. break
  270. case 2:
  271. compoundFrequencyTF.text = AppConstant.INTEREST_PAYOUT_FREQUENCY[row]
  272. break
  273. case 3:
  274. extensionTF.text = AppConstant.EXTENSION[row]
  275. break
  276. default:
  277. break
  278. }
  279. }
  280. }
  281. extension PublicProvidentViewController: UITextFieldDelegate {
  282. func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
  283. switch textField {
  284. case amountDepositFrequencyTF:
  285. return false
  286. case compoundFrequencyTF:
  287. return false
  288. case extensionTF:
  289. return false
  290. default:
  291. return true
  292. }
  293. }
  294. // when user select a textfield, this method will be called
  295. func textFieldDidBeginEditing(_ textField: UITextField) {
  296. switch textField {
  297. case amountDepositFrequencyTF:
  298. activeTextField = 1
  299. customPicker.reloadAllComponents()
  300. case compoundFrequencyTF:
  301. activeTextField = 2
  302. customPicker.reloadAllComponents()
  303. case extensionTF:
  304. activeTextField = 3
  305. customPicker.reloadAllComponents()
  306. default:
  307. activeTextField = 0
  308. }
  309. }
  310. }
  311. extension PublicProvidentViewController: ToolbarPickerViewDelegate {
  312. func didTapDone() {
  313. switch activeTextField {
  314. case 1:
  315. let row = self.customPicker.selectedRow(inComponent: 0)
  316. self.customPicker.selectRow(row, inComponent: 0, animated: false)
  317. self.amountDepositFrequencyTF.text = AppConstant.INTEREST_PAYOUT_FREQUENCY[row]
  318. self.amountDepositFrequencyTF.resignFirstResponder()
  319. case 2:
  320. let row = self.customPicker.selectedRow(inComponent: 0)
  321. self.customPicker.selectRow(row, inComponent: 0, animated: false)
  322. self.compoundFrequencyTF.text = AppConstant.INTEREST_PAYOUT_FREQUENCY[row]
  323. self.compoundFrequencyTF.resignFirstResponder()
  324. case 3:
  325. let row = self.customPicker.selectedRow(inComponent: 0)
  326. self.customPicker.selectRow(row, inComponent: 0, animated: false)
  327. self.extensionTF.text = AppConstant.EXTENSION[row]
  328. if self.extensionTF.text == "Yes" {
  329. isExtension = true
  330. extensionStatus()
  331. } else {
  332. isExtension = false
  333. extensionStatus()
  334. }
  335. self.extensionTF.resignFirstResponder()
  336. default:
  337. return
  338. }
  339. }
  340. func didTapCancel() {
  341. switch activeTextField {
  342. case 1:
  343. self.amountDepositFrequencyTF.text = nil
  344. self.amountDepositFrequencyTF.resignFirstResponder()
  345. case 2:
  346. self.compoundFrequencyTF.text = nil
  347. self.compoundFrequencyTF.resignFirstResponder()
  348. case 3:
  349. self.extensionTF.text = nil
  350. self.extensionTF.resignFirstResponder()
  351. default:
  352. return
  353. }
  354. }
  355. }
  356. extension PublicProvidentViewController: PublicProvidentViewProtocol {
  357. func navigate(_ data: PublicProvidentModel) {
  358. DispatchQueue.main.async {
  359. let ppfSummaryVC = self.storyboard?.instantiateViewController(withIdentifier: "PPFSummaryViewController") as! PPFSummaryViewController
  360. ppfSummaryVC.ppfModel = data
  361. self.navigationController?.pushViewController(ppfSummaryVC, animated: true)
  362. }
  363. }
  364. func setInterestRate(rate: String) {
  365. annualInterestRateTF.text = rate
  366. }
  367. func startLoader() {
  368. ActivityIndicator.start()
  369. }
  370. func stopLoader() {
  371. ActivityIndicator.stop()
  372. }
  373. func showError(error: String) {
  374. self.view.makeToast(error)
  375. }
  376. }