BankRecurringViewController.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. //
  2. // BankRecurringViewController.swift
  3. // Product Calculator
  4. //
  5. // Created by Suraj Kumar Mandal on 29/11/21.
  6. //
  7. import UIKit
  8. import SideMenu
  9. import Toast_Swift
  10. class BankRecurringViewController: UIViewController {
  11. @IBOutlet var depositAmountTF: UITextField!
  12. @IBOutlet var annualInterestRateTF: UITextField!
  13. @IBOutlet var amountDepositFrequencyTF: UITextField!
  14. @IBOutlet var termYearTF: UITextField!
  15. @IBOutlet var termMonthTF: UITextField!
  16. @IBOutlet var compoundFrequencyTF: UITextField!
  17. @IBOutlet var depositDateTF: UITextField!
  18. fileprivate let customPicker = ToolbarPickerView()
  19. var activeTextField = 0
  20. var menu = Int()
  21. var viewModel = BankRecurringViewModel()
  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. annualInterestRateTF.delegate = self
  28. amountDepositFrequencyTF.delegate = self
  29. termYearTF.delegate = self
  30. termMonthTF.delegate = self
  31. compoundFrequencyTF.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.amountDepositFrequencyTF.inputView = self.customPicker
  72. self.amountDepositFrequencyTF.inputAccessoryView = self.customPicker.toolbar
  73. self.termYearTF.inputView = self.customPicker
  74. self.termYearTF.inputAccessoryView = self.customPicker.toolbar
  75. self.termMonthTF.inputView = self.customPicker
  76. self.termMonthTF.inputAccessoryView = self.customPicker.toolbar
  77. self.compoundFrequencyTF.inputView = self.customPicker
  78. self.compoundFrequencyTF.inputAccessoryView = self.customPicker.toolbar
  79. self.customPicker.delegate = self
  80. self.customPicker.dataSource = self
  81. self.customPicker.toolbarDelegate = self
  82. }
  83. @objc
  84. func cancelAction() {
  85. self.depositDateTF.resignFirstResponder()
  86. }
  87. @objc
  88. func doneAction() {
  89. if let datePickerView = self.depositDateTF.inputView as? UIDatePicker {
  90. let dateFormatter = DateFormatter()
  91. dateFormatter.dateFormat = AppConstant.dateFormat2
  92. let dateString = dateFormatter.string(from: datePickerView.date)
  93. self.depositDateTF.text = dateString
  94. //self.dateOfBirth = dateString
  95. print(datePickerView.date)
  96. print(dateString)
  97. self.depositDateTF.resignFirstResponder()
  98. }
  99. }
  100. func dataValidation() {
  101. if depositAmountTF.text?.isEmpty == true {
  102. self.view.makeToast("Fill deposit amount!")
  103. } else if annualInterestRateTF.text?.isEmpty == true {
  104. self.view.makeToast("Fill annual interest rate!")
  105. } else if amountDepositFrequencyTF.text?.isEmpty == true {
  106. self.view.makeToast("Select amount deposit frequency!")
  107. } else if termYearTF.text?.isEmpty == true {
  108. self.view.makeToast("Select term year!")
  109. } else if termMonthTF.text?.isEmpty == true {
  110. self.view.makeToast("Select term month!")
  111. } else if compoundFrequencyTF.text?.isEmpty == true {
  112. self.view.makeToast("Select compound frequency!")
  113. } else if depositDateTF.text?.isEmpty == true {
  114. self.view.makeToast("Select deposit date!")
  115. } else {
  116. if Reachability.isConnectedToNetwork() {
  117. let interest = annualInterestRateTF.text!
  118. let interestRate = Double(interest)!/100.0
  119. viewModel.getRecurringDeposit(depositAmount: depositAmountTF.text!, annualInterestRate: String(interestRate), year: termYearTF.text!, month: termMonthTF.text!, amountDepositFrequency: amountDepositFrequencyTF.text!, compoundFrequency: compoundFrequencyTF.text!, depositDate: depositDateTF.text!)
  120. } else {
  121. Alert.showInternetFailureAlert(on: self)
  122. }
  123. }
  124. }
  125. /*
  126. // MARK: - Navigation
  127. // In a storyboard-based application, you will often want to do a little preparation before navigation
  128. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  129. // Get the new view controller using segue.destination.
  130. // Pass the selected object to the new view controller.
  131. }
  132. */
  133. @IBAction func saveAction(_ sender: Any) {
  134. self.dataValidation()
  135. }
  136. @IBAction func undoAction(_ sender: Any) {
  137. depositAmountTF.text = nil
  138. annualInterestRateTF.text = nil
  139. amountDepositFrequencyTF.text = nil
  140. termYearTF.text = nil
  141. termMonthTF.text = nil
  142. compoundFrequencyTF.text = nil
  143. depositDateTF.text = nil
  144. }
  145. }
  146. extension BankRecurringViewController: UIPickerViewDelegate, UIPickerViewDataSource {
  147. // MARK: UIPickerView Delegation
  148. func numberOfComponents(in pickerView: UIPickerView) -> Int {
  149. return 1
  150. }
  151. func pickerView( _ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
  152. switch activeTextField
  153. {
  154. case 1:
  155. return AppConstant.AMOUNT_DEPOSIT_FREQUENCY.count
  156. case 2:
  157. return AppConstant.TERM_YEARS.count
  158. case 3:
  159. return AppConstant.TERM_MONTHS.count
  160. case 4:
  161. return AppConstant.COMPOUND_FREQUENCY.count
  162. default:
  163. return 0
  164. }
  165. }
  166. func pickerView( _ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
  167. switch activeTextField{
  168. case 1:
  169. return AppConstant.AMOUNT_DEPOSIT_FREQUENCY[row]
  170. case 2:
  171. return AppConstant.TERM_YEARS[row]
  172. case 3:
  173. return AppConstant.TERM_MONTHS[row]
  174. case 4:
  175. return AppConstant.COMPOUND_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. amountDepositFrequencyTF.text = AppConstant.AMOUNT_DEPOSIT_FREQUENCY[row]
  184. break
  185. case 2:
  186. termYearTF.text = AppConstant.TERM_YEARS[row]
  187. break
  188. case 3:
  189. termMonthTF.text = AppConstant.TERM_MONTHS[row]
  190. case 4:
  191. compoundFrequencyTF.text = AppConstant.COMPOUND_FREQUENCY[row]
  192. break
  193. default:
  194. break
  195. }
  196. }
  197. }
  198. extension BankRecurringViewController: UITextFieldDelegate {
  199. func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
  200. switch textField {
  201. case amountDepositFrequencyTF:
  202. return false
  203. case termYearTF:
  204. return false
  205. case termMonthTF:
  206. return false
  207. case compoundFrequencyTF:
  208. return false
  209. case depositDateTF:
  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 amountDepositFrequencyTF:
  219. activeTextField = 1
  220. customPicker.reloadAllComponents()
  221. case termYearTF:
  222. activeTextField = 2
  223. customPicker.reloadAllComponents()
  224. case termMonthTF:
  225. activeTextField = 3
  226. customPicker.reloadAllComponents()
  227. case compoundFrequencyTF:
  228. activeTextField = 4
  229. customPicker.reloadAllComponents()
  230. default:
  231. activeTextField = 0
  232. }
  233. }
  234. }
  235. extension BankRecurringViewController: ToolbarPickerViewDelegate {
  236. func didTapDone() {
  237. switch activeTextField {
  238. case 1:
  239. let row = self.customPicker.selectedRow(inComponent: 0)
  240. self.customPicker.selectRow(row, inComponent: 0, animated: false)
  241. self.amountDepositFrequencyTF.text = AppConstant.AMOUNT_DEPOSIT_FREQUENCY[row]
  242. self.amountDepositFrequencyTF.resignFirstResponder()
  243. case 2:
  244. let row = self.customPicker.selectedRow(inComponent: 0)
  245. self.customPicker.selectRow(row, inComponent: 0, animated: false)
  246. self.termYearTF.text = AppConstant.TERM_YEARS[row]
  247. self.termYearTF.resignFirstResponder()
  248. case 3:
  249. let row = self.customPicker.selectedRow(inComponent: 0)
  250. self.customPicker.selectRow(row, inComponent: 0, animated: false)
  251. self.termMonthTF.text = AppConstant.TERM_MONTHS[row]
  252. self.termMonthTF.resignFirstResponder()
  253. case 4:
  254. let row = self.customPicker.selectedRow(inComponent: 0)
  255. self.customPicker.selectRow(row, inComponent: 0, animated: false)
  256. self.compoundFrequencyTF.text = AppConstant.COMPOUND_FREQUENCY[row]
  257. self.compoundFrequencyTF.resignFirstResponder()
  258. default:
  259. return
  260. }
  261. }
  262. func didTapCancel() {
  263. switch activeTextField {
  264. case 1:
  265. self.amountDepositFrequencyTF.text = nil
  266. self.amountDepositFrequencyTF.resignFirstResponder()
  267. case 2:
  268. self.termYearTF.text = nil
  269. self.termYearTF.resignFirstResponder()
  270. case 3:
  271. self.termMonthTF.text = nil
  272. self.termMonthTF.resignFirstResponder()
  273. case 4:
  274. self.compoundFrequencyTF.text = nil
  275. self.compoundFrequencyTF.resignFirstResponder()
  276. default:
  277. return
  278. }
  279. }
  280. }
  281. extension BankRecurringViewController: BankRecurringViewProtocol {
  282. func navigate(_ data:BankRecurringModel) {
  283. DispatchQueue.main.async {
  284. let recurringSummaryVC = self.storyboard?.instantiateViewController(withIdentifier: "BankRecurringSummaryViewController") as! BankRecurringSummaryViewController
  285. recurringSummaryVC.recurringModel = data
  286. self.navigationController?.pushViewController(recurringSummaryVC, animated: true)
  287. }
  288. }
  289. func startLoader() {
  290. ActivityIndicator.start()
  291. }
  292. func stopLoader() {
  293. ActivityIndicator.stop()
  294. }
  295. func showError(error: String) {
  296. self.view.makeToast(error)
  297. }
  298. }