PORecurringDepositViewController.swift 10 KB

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