EPFViewController.swift 12 KB

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