POTimeDepositViewController.swift 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. //
  2. // POTimeDepositViewController.swift
  3. // Product Calculator
  4. //
  5. // Created by Suraj Kumar Mandal on 04/12/21.
  6. //
  7. import UIKit
  8. import SideMenu
  9. import Toast_Swift
  10. class POTimeDepositViewController: UIViewController {
  11. @IBOutlet var depositAmountTF: UITextField!
  12. @IBOutlet var annualInterestRateTF: UITextField!
  13. @IBOutlet var termYearsTF: UITextField!
  14. @IBOutlet var interestPayoutFrequencyTF: UITextField!
  15. @IBOutlet var compoundFrequencyTF: UITextField!
  16. @IBOutlet var depositDateTF: UITextField!
  17. fileprivate let customPicker = ToolbarPickerView()
  18. var activeTextField = 0
  19. var menu = Int()
  20. var viewModel = POTimeDepositViewModel()
  21. override func viewDidLoad() {
  22. super.viewDidLoad()
  23. // Do any additional setup after loading the view.
  24. viewModel.delegate = self
  25. depositAmountTF.delegate = self
  26. annualInterestRateTF.delegate = self
  27. termYearsTF.delegate = self
  28. interestPayoutFrequencyTF.delegate = self
  29. compoundFrequencyTF.delegate = self
  30. depositDateTF.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.interestPayoutFrequencyTF.inputView = self.customPicker
  70. self.interestPayoutFrequencyTF.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.dateFormat1
  86. let dateString = dateFormatter.string(from: datePickerView.date)
  87. self.depositDateTF.text = dateString
  88. //self.dateOfBirth = dateString
  89. print(datePickerView.date)
  90. print(dateString)
  91. self.depositDateTF.resignFirstResponder()
  92. }
  93. }
  94. func dataValidation() {
  95. if depositAmountTF.text?.isEmpty == true {
  96. self.view.makeToast("Fill deposit amount!")
  97. } else if annualInterestRateTF.text?.isEmpty == true {
  98. self.view.makeToast("Fill annual interest rate!")
  99. } else if termYearsTF.text?.isEmpty == true {
  100. self.view.makeToast("Select term in years!")
  101. } else if interestPayoutFrequencyTF.text?.isEmpty == true {
  102. self.view.makeToast("Select interest payout frequency!")
  103. } else if compoundFrequencyTF.text?.isEmpty == true {
  104. self.view.makeToast("Select compound frequency!")
  105. } else if depositDateTF.text?.isEmpty == true {
  106. self.view.makeToast("Select deposit date!")
  107. } else {
  108. if Reachability.isConnectedToNetwork() {
  109. let interest = annualInterestRateTF.text!
  110. let interestRate = Double(interest)!/100.0
  111. viewModel.getPoTimeOutput(deposit: depositDateTF.text!, yearsDays: termYearsTF.text!, compundingFreq: compoundFrequencyTF.text!, rdDepositFreq: interestPayoutFrequencyTF.text!, depositDate: depositDateTF.text!, interestRate: String(interestRate))
  112. } else {
  113. Alert.showInternetFailureAlert(on: self)
  114. }
  115. }
  116. }
  117. /*
  118. // MARK: - Navigation
  119. // In a storyboard-based application, you will often want to do a little preparation before navigation
  120. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  121. // Get the new view controller using segue.destination.
  122. // Pass the selected object to the new view controller.
  123. }
  124. */
  125. @IBAction func saveAction(_ sender: Any) {
  126. self.dataValidation()
  127. }
  128. @IBAction func undoAction(_ sender: Any) {
  129. depositAmountTF.text = nil
  130. annualInterestRateTF.text = nil
  131. termYearsTF.text = nil
  132. interestPayoutFrequencyTF.text = nil
  133. compoundFrequencyTF.text = nil
  134. depositDateTF.text = nil
  135. }
  136. }
  137. extension POTimeDepositViewController: UIPickerViewDelegate, UIPickerViewDataSource {
  138. // MARK: UIPickerView Delegation
  139. func numberOfComponents(in pickerView: UIPickerView) -> Int {
  140. return 1
  141. }
  142. func pickerView( _ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
  143. switch activeTextField
  144. {
  145. case 1:
  146. return AppConstant.INTEREST_PAYOUT_FREQUENCY.count
  147. case 2:
  148. return AppConstant.INTEREST_PAYOUT_FREQUENCY.count
  149. default:
  150. return 0
  151. }
  152. }
  153. func pickerView( _ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
  154. switch activeTextField{
  155. case 1:
  156. return AppConstant.INTEREST_PAYOUT_FREQUENCY[row]
  157. case 2:
  158. return AppConstant.INTEREST_PAYOUT_FREQUENCY[row]
  159. default:
  160. return ""
  161. }
  162. }
  163. func pickerView( _ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
  164. switch activeTextField{
  165. case 1:
  166. interestPayoutFrequencyTF.text = AppConstant.INTEREST_PAYOUT_FREQUENCY[row]
  167. break
  168. case 2:
  169. compoundFrequencyTF.text = AppConstant.INTEREST_PAYOUT_FREQUENCY[row]
  170. break
  171. default:
  172. break
  173. }
  174. }
  175. }
  176. extension POTimeDepositViewController: UITextFieldDelegate {
  177. func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
  178. switch textField {
  179. case interestPayoutFrequencyTF:
  180. return false
  181. case compoundFrequencyTF:
  182. return false
  183. default:
  184. return true
  185. }
  186. }
  187. // when user select a textfield, this method will be called
  188. func textFieldDidBeginEditing(_ textField: UITextField) {
  189. switch textField {
  190. case interestPayoutFrequencyTF:
  191. activeTextField = 1
  192. customPicker.reloadAllComponents()
  193. case compoundFrequencyTF:
  194. activeTextField = 2
  195. customPicker.reloadAllComponents()
  196. default:
  197. activeTextField = 0
  198. }
  199. }
  200. }
  201. extension POTimeDepositViewController: ToolbarPickerViewDelegate {
  202. func didTapDone() {
  203. switch activeTextField {
  204. case 1:
  205. let row = self.customPicker.selectedRow(inComponent: 0)
  206. self.customPicker.selectRow(row, inComponent: 0, animated: false)
  207. self.interestPayoutFrequencyTF.text = AppConstant.DEPOSIT_TYPE[row]
  208. self.interestPayoutFrequencyTF.resignFirstResponder()
  209. case 2:
  210. let row = self.customPicker.selectedRow(inComponent: 0)
  211. self.customPicker.selectRow(row, inComponent: 0, animated: false)
  212. self.compoundFrequencyTF.text = AppConstant.FIXED_DEPOSIT_TERM[row]
  213. self.compoundFrequencyTF.resignFirstResponder()
  214. default:
  215. return
  216. }
  217. }
  218. func didTapCancel() {
  219. switch activeTextField {
  220. case 1:
  221. self.interestPayoutFrequencyTF.text = nil
  222. self.interestPayoutFrequencyTF.resignFirstResponder()
  223. case 2:
  224. self.compoundFrequencyTF.text = nil
  225. self.compoundFrequencyTF.resignFirstResponder()
  226. default:
  227. return
  228. }
  229. }
  230. }
  231. extension POTimeDepositViewController: POTimeDepositViewProtocol {
  232. func navigate(_ data:POTimeDepositModel) {
  233. DispatchQueue.main.async {
  234. let poTimeSummaryVC = self.storyboard?.instantiateViewController(withIdentifier: "POTimeSummaryViewController") as! POTimeSummaryViewController
  235. poTimeSummaryVC.poTimeModel = data
  236. self.navigationController?.pushViewController(poTimeSummaryVC, animated: true)
  237. }
  238. }
  239. func startLoader() {
  240. ActivityIndicator.start()
  241. }
  242. func stopLoader() {
  243. ActivityIndicator.stop()
  244. }
  245. func showError(error: String) {
  246. self.view.makeToast(error)
  247. }
  248. }