FixedDepositViewController.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. //
  2. // FixedDepositViewController.swift
  3. // Product Calculator
  4. //
  5. // Created by Suraj Kumar Mandal on 15/11/21.
  6. //
  7. import UIKit
  8. import SideMenu
  9. import Toast_Swift
  10. class FixedDepositViewController: UIViewController {
  11. @IBOutlet var depositAmountTF: UITextField!
  12. @IBOutlet var interestRateTF: UITextField!
  13. @IBOutlet var depositTypeTF: UITextField!
  14. @IBOutlet var termTF: UITextField!
  15. @IBOutlet var daysTF: UITextField!
  16. @IBOutlet var interestPayoutTF: UITextField!
  17. @IBOutlet var depositDateTF: UITextField!
  18. fileprivate let customPicker = ToolbarPickerView()
  19. var activeTextField = 0
  20. var menu = Int()
  21. var viewModel = FixedDepositViewModel()
  22. override func viewDidLoad() {
  23. super.viewDidLoad()
  24. // Do any additional setup after loading the view.
  25. viewModel.delegate = self
  26. depositAmountTF.delegate = self
  27. interestRateTF.delegate = self
  28. depositTypeTF.delegate = self
  29. termTF.delegate = self
  30. daysTF.delegate = self
  31. interestPayoutTF.delegate = self
  32. depositDateTF.delegate = self
  33. }
  34. override func viewWillAppear(_ animated: Bool) {
  35. super.viewWillAppear(animated)
  36. navigationController?.navigationBar.barStyle = .black
  37. setupUI()
  38. }
  39. override var preferredStatusBarStyle: UIStatusBarStyle {
  40. return .lightContent
  41. }
  42. func setupUI() {
  43. self.navigationItem.title = AppConstant.MENU_TITLE[menu]
  44. self.navigationItem.hidesBackButton = true
  45. let menuBarButtonItem = UIBarButtonItem(image: UIImage(systemName: "line.3.horizontal"), style: .plain, target: self, action: #selector(openMenu))
  46. self.navigationItem.leftBarButtonItem = menuBarButtonItem
  47. if #available(iOS 13, *) {
  48. let appearance = UINavigationBarAppearance()
  49. appearance.backgroundColor = .systemBlue
  50. appearance.titleTextAttributes = [.foregroundColor: UIColor.white]
  51. appearance.largeTitleTextAttributes = [.foregroundColor: UIColor.white]
  52. navigationController?.navigationBar.tintColor = .white
  53. navigationController?.navigationBar.standardAppearance = appearance
  54. navigationController?.navigationBar.compactAppearance = appearance
  55. navigationController?.navigationBar.scrollEdgeAppearance = appearance
  56. }
  57. //Date Picker
  58. depositDateTF.datePicker(target: self,
  59. doneAction: #selector(doneAction),
  60. cancelAction: #selector(cancelAction),
  61. datePickerMode: .date)
  62. depositDateTF.delegate = self
  63. //Picker View
  64. createPickerView()
  65. }
  66. @objc func openMenu(_ sender: Any) {
  67. let menu = storyboard!.instantiateViewController(withIdentifier: "SideMenuNavigationController") as! SideMenuNavigationController
  68. present(menu, animated: true, completion: nil)
  69. }
  70. func createPickerView() {
  71. self.depositTypeTF.inputView = self.customPicker
  72. self.depositTypeTF.inputAccessoryView = self.customPicker.toolbar
  73. self.termTF.inputView = self.customPicker
  74. self.termTF.inputAccessoryView = self.customPicker.toolbar
  75. self.interestPayoutTF.inputView = self.customPicker
  76. self.interestPayoutTF.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.dateFormat1
  90. let dateString = dateFormatter.string(from: datePickerView.date)
  91. self.depositDateTF.text = dateString
  92. //self.dateOfBirth = dateString
  93. print(datePickerView.date)
  94. print(dateString)
  95. self.depositDateTF.resignFirstResponder()
  96. }
  97. }
  98. func dataValidation() {
  99. if depositAmountTF.text?.isEmpty == true {
  100. self.view.makeToast("Fill deposit amount!")
  101. } else if interestRateTF.text?.isEmpty == true {
  102. self.view.makeToast("Fill annual interest rate!")
  103. } else if depositTypeTF.text?.isEmpty == true {
  104. self.view.makeToast("Select deposit type!")
  105. } else if termTF.text?.isEmpty == true {
  106. self.view.makeToast("Select term!")
  107. } else if daysTF.text?.isEmpty == true {
  108. self.view.makeToast("Fill no. of days/year!")
  109. } else if interestPayoutTF.text?.isEmpty == true {
  110. self.view.makeToast("Select interest payout frequency!")
  111. } else if depositDateTF.text?.isEmpty == true {
  112. self.view.makeToast("Select deposit date!")
  113. } else {
  114. if Reachability.isConnectedToNetwork() {
  115. if depositTypeTF.text == "Bank FD TDR" {
  116. let interest = interestRateTF.text!
  117. let interestRate = Double(interest)!/100.0
  118. viewModel.getBankFDTDRDetails(depositAmount: depositAmountTF.text ?? "", annualInterest: String(format: "%.2f", interestRate), termYearsDays: termTF.text ?? "", tenure: daysTF.text ?? "", payoutFrq: interestPayoutTF.text ?? "", depositDate: depositDateTF.text ?? "")
  119. } else {
  120. let interest = interestRateTF.text!
  121. let interestRate = Double(interest)!/100.0
  122. viewModel.getBankFDSTDRCDCPOutputList(depositAmount: depositAmountTF.text ?? "", annualInterest: String(format: "%.2f", interestRate), termYearsDays: termTF.text ?? "", tenure: daysTF.text ?? "", compoundFrequency: interestPayoutTF.text ?? "", depositDate: depositDateTF.text ?? "")
  123. }
  124. } else {
  125. Alert.showInternetFailureAlert(on: self)
  126. }
  127. }
  128. }
  129. /*
  130. // MARK: - Navigation
  131. // In a storyboard-based application, you will often want to do a little preparation before navigation
  132. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  133. // Get the new view controller using segue.destination.
  134. // Pass the selected object to the new view controller.
  135. }
  136. */
  137. @IBAction func saveAction(_ sender: Any) {
  138. self.dataValidation()
  139. }
  140. @IBAction func undoAction(_ sender: Any) {
  141. depositAmountTF.text = nil
  142. interestRateTF.text = nil
  143. depositTypeTF.text = nil
  144. termTF.text = nil
  145. daysTF.text = nil
  146. interestPayoutTF.text = nil
  147. depositDateTF.text = nil
  148. }
  149. }
  150. extension FixedDepositViewController: UIPickerViewDelegate, UIPickerViewDataSource {
  151. // MARK: UIPickerView Delegation
  152. func numberOfComponents(in pickerView: UIPickerView) -> Int {
  153. return 1
  154. }
  155. func pickerView( _ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
  156. switch activeTextField
  157. {
  158. case 1:
  159. return AppConstant.DEPOSIT_TYPE.count
  160. case 2:
  161. return AppConstant.FIXED_DEPOSIT_TERM.count
  162. case 3:
  163. return AppConstant.INTEREST_PAYOUT_FREQUENCY.count
  164. default:
  165. return 0
  166. }
  167. }
  168. func pickerView( _ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
  169. switch activeTextField{
  170. case 1:
  171. return AppConstant.DEPOSIT_TYPE[row]
  172. case 2:
  173. return AppConstant.FIXED_DEPOSIT_TERM[row]
  174. case 3:
  175. return AppConstant.INTEREST_PAYOUT_FREQUENCY[row]
  176. default:
  177. return ""
  178. }
  179. }
  180. func pickerView( _ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
  181. switch activeTextField{
  182. case 1:
  183. depositTypeTF.text = AppConstant.DEPOSIT_TYPE[row]
  184. break
  185. case 2:
  186. termTF.text = AppConstant.FIXED_DEPOSIT_TERM[row]
  187. break
  188. case 3:
  189. interestPayoutTF.text = AppConstant.INTEREST_PAYOUT_FREQUENCY[row]
  190. break
  191. default:
  192. break
  193. }
  194. }
  195. }
  196. extension FixedDepositViewController: UITextFieldDelegate {
  197. func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
  198. switch textField {
  199. case depositTypeTF:
  200. return false
  201. case termTF:
  202. return false
  203. case interestPayoutTF:
  204. return false
  205. case depositDateTF:
  206. return false
  207. default:
  208. return true
  209. }
  210. }
  211. // when user select a textfield, this method will be called
  212. func textFieldDidBeginEditing(_ textField: UITextField) {
  213. switch textField {
  214. case depositTypeTF:
  215. activeTextField = 1
  216. customPicker.reloadAllComponents()
  217. case termTF:
  218. activeTextField = 2
  219. customPicker.reloadAllComponents()
  220. case interestPayoutTF:
  221. activeTextField = 3
  222. customPicker.reloadAllComponents()
  223. default:
  224. activeTextField = 0
  225. }
  226. }
  227. }
  228. extension FixedDepositViewController: ToolbarPickerViewDelegate {
  229. func didTapDone() {
  230. switch activeTextField {
  231. case 1:
  232. let row = self.customPicker.selectedRow(inComponent: 0)
  233. self.customPicker.selectRow(row, inComponent: 0, animated: false)
  234. self.depositTypeTF.text = AppConstant.DEPOSIT_TYPE[row]
  235. self.depositTypeTF.resignFirstResponder()
  236. case 2:
  237. let row = self.customPicker.selectedRow(inComponent: 0)
  238. self.customPicker.selectRow(row, inComponent: 0, animated: false)
  239. self.termTF.text = AppConstant.FIXED_DEPOSIT_TERM[row]
  240. self.termTF.resignFirstResponder()
  241. case 3:
  242. let row = self.customPicker.selectedRow(inComponent: 0)
  243. self.customPicker.selectRow(row, inComponent: 0, animated: false)
  244. self.interestPayoutTF.text = AppConstant.INTEREST_PAYOUT_FREQUENCY[row]
  245. self.interestPayoutTF.resignFirstResponder()
  246. default:
  247. return
  248. }
  249. }
  250. func didTapCancel() {
  251. switch activeTextField {
  252. case 1:
  253. self.depositTypeTF.text = nil
  254. self.depositTypeTF.resignFirstResponder()
  255. case 2:
  256. self.termTF.text = nil
  257. self.termTF.resignFirstResponder()
  258. case 3:
  259. self.interestPayoutTF.text = nil
  260. self.interestPayoutTF.resignFirstResponder()
  261. default:
  262. return
  263. }
  264. }
  265. }
  266. extension FixedDepositViewController: FixedDepositViewProtocol {
  267. func navigateWithFDTDR(_ data:FDTDRDetailsModel) {
  268. DispatchQueue.main.async {
  269. let fdTDRSummaryVC = self.storyboard?.instantiateViewController(withIdentifier: "FixedDepositSummaryViewController") as! FixedDepositSummaryViewController
  270. fdTDRSummaryVC.fdtdrDetails = data
  271. fdTDRSummaryVC.fdType = "FDTDR"
  272. self.navigationController?.pushViewController(fdTDRSummaryVC, animated: true)
  273. }
  274. }
  275. func navigateWithFDSTDR(_ data:FDSTDRDetailsModel) {
  276. DispatchQueue.main.async {
  277. let fdSTDRSummaryVC = self.storyboard?.instantiateViewController(withIdentifier: "FixedDepositSummaryViewController") as! FixedDepositSummaryViewController
  278. fdSTDRSummaryVC.fdStdrDetails = data
  279. fdSTDRSummaryVC.fdType = "FDSTDR"
  280. self.navigationController?.pushViewController(fdSTDRSummaryVC, animated: true)
  281. }
  282. }
  283. func startLoader() {
  284. ActivityIndicator.start()
  285. }
  286. func stopLoader() {
  287. ActivityIndicator.stop()
  288. }
  289. func showError(error: String) {
  290. self.view.makeToast(error)
  291. }
  292. }