You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

224 lines
7.5 KiB

  1. //
  2. // AutoDebitViewController.swift
  3. // GME Remit
  4. //
  5. // Created by Mac on 12/19/18.
  6. //Copyright © 2018 Gobal Money Express Co. Ltd. All rights reserved.
  7. //
  8. import UIKit
  9. class AutoDebitViewController: UIViewController {
  10. // MARK:- IBOutlets
  11. @IBOutlet weak var tableView: UITableView!
  12. @IBOutlet weak var viewAddAccount: UIView! // this is the view that contains add reciepient. should be header
  13. @IBOutlet weak var labelSwipeInfo: UILabel!
  14. var presenter: AutoDebitPresenter?
  15. // MARK:- properties
  16. var kftcDetail: KFTCModel? {
  17. didSet {
  18. self.accounts = kftcDetail?.model ?? []
  19. self.languages = kftcDetail?.languages
  20. self.header = kftcDetail?.header
  21. self.url = kftcDetail?.url
  22. self.tableView.reloadData()
  23. }
  24. }
  25. var accounts: [Account]? {
  26. didSet {
  27. if (accounts ?? []).isEmpty {
  28. }else {
  29. self.tableView.isHidden = false
  30. self.labelSwipeInfo.isHidden = false
  31. }
  32. }
  33. }
  34. var languages: [KftcLanguage]?
  35. var header: [KftcHeader]?
  36. var url: String?
  37. var selectedLanguage: KftcLanguage? {
  38. didSet {
  39. if var url = self.url {
  40. let string = "&lang=\(selectedLanguage?.key ?? "")"
  41. url.append(string)
  42. openWkWebViewWith(url: url)
  43. }
  44. }
  45. }
  46. // MARK:- Life Cycle
  47. override func viewWillAppear(_ animated: Bool) {
  48. super.viewWillAppear(animated)
  49. self.setupNormalNavigation()
  50. self.navigationItem.title = "My Accounts"
  51. fetchReceipients()
  52. }
  53. override func viewDidLoad() {
  54. super.viewDidLoad()
  55. self.setupDelegates()
  56. viewAddAccount.layer.cornerRadius = 10
  57. }
  58. override func viewWillDisappear(_ animated: Bool) {
  59. super.viewWillDisappear(animated)
  60. // self.title = ""
  61. self.navigationItem.title = ""
  62. }
  63. private func openWkWebViewWith(url: String) {
  64. let viewcontroller = UIStoryboard.init(name: "WKWebView", bundle: nil).instantiateViewController(withIdentifier: "WkWebViewController") as! WkWebViewController
  65. viewcontroller.url = url
  66. viewcontroller.headers = self.header
  67. let nvc = UINavigationController.init(rootViewController: viewcontroller)
  68. self.present(nvc, animated: true, completion: nil)
  69. }
  70. // MARK:- IBAction
  71. @IBAction func loadMoreAction(_ sender: Any) {
  72. print("load more")
  73. }
  74. @IBAction func addNewAccount(_ sender: UITapGestureRecognizer) {
  75. print("add new account")
  76. if (self.languages ?? []).isEmpty {
  77. self.alert(message: "No Lanugages Available")
  78. }
  79. showLanguageSelection()
  80. }
  81. // MARK:- other functions
  82. private func setupDelegates() {
  83. self.tableView.delegate = self
  84. self.tableView.dataSource = self
  85. }
  86. private func showLanguageSelection() {
  87. let picker = UIAlertController.init(title: "Select Language", message: nil, preferredStyle: UIAlertControllerStyle.actionSheet)
  88. self.languages?.forEach({language in
  89. let action = UIAlertAction.init(title: language.value, style: UIAlertActionStyle.default, handler: { (action) in
  90. self.selectedLanguage = language
  91. })
  92. picker.addAction(action)
  93. })
  94. let cancelAction = UIAlertAction.init(title: "Cancel", style: UIAlertActionStyle.cancel, handler: nil)
  95. picker.addAction(cancelAction)
  96. self.present(picker, animated: true, completion: nil)
  97. }
  98. private func createDummyLangugage() -> [KftcLanguage] {
  99. let leng = KftcLanguage()
  100. leng.key = "eng"
  101. leng.value = "English"
  102. let leng1 = KftcLanguage()
  103. leng1.key = "Nepali"
  104. leng1.value = "Nepali"
  105. return [leng, leng1]
  106. }
  107. }
  108. extension AutoDebitViewController: UITableViewDelegate,UITableViewDataSource {
  109. func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
  110. return 120.0
  111. }
  112. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  113. return self.accounts?.count ?? 0
  114. }
  115. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  116. let cell = tableView.dequeueReusableCell(withIdentifier: "recipientList", for: indexPath) as! AutoDebitTableViewCell
  117. cell.model = self.accounts?.elementAt(index: indexPath.row)
  118. cell.setup()
  119. return cell
  120. }
  121. // AutoDebitTableViewCell
  122. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  123. // startSendMoneyProcess(index: indexPath.row)
  124. }
  125. private func startSendMoneyProcess(index: Int) {
  126. if let navigation = self.navigationController {
  127. if let reciepient = self.accounts?.elementAt(index: index) {
  128. let wireframe = SendMoneyParentWireframe()
  129. // wireframe.open(for: reciepient, in: navigation)
  130. }
  131. }
  132. }
  133. func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
  134. let delete = UITableViewRowAction(style: .destructive, title: "Delete") { (action, indexPath) in
  135. self.alertWithOkCancel(message: "Do you want to delete this Account?", title: "Alert!", OkStyle: UIAlertActionStyle.destructive, okAction: {
  136. let defaults = UserDefaults.standard
  137. let myUsername = defaults.string(forKey: "com.gmeremit.username") ?? ""
  138. self.showProgressHud()
  139. if let account = self.accounts?.elementAt(index: indexPath.row) {
  140. self.deleteAccount(username: myUsername, account: account, success: {
  141. self.fetchReceipients()
  142. }, failure: { (error) in
  143. self.hideProgressHud()
  144. self.alert(message: error.localizedDescription)
  145. })
  146. self.showProgressHud()
  147. }
  148. })
  149. }
  150. let edit = UITableViewRowAction(style: .normal, title: "Edit") { (action, indexPath) in
  151. guard let navigation = self.navigationController else {return}
  152. if let reciepient = self.accounts?.elementAt(index: indexPath.row) {
  153. // do some editing
  154. }
  155. }
  156. edit.backgroundColor = UIColor.init(hex: "#F39826")
  157. delete.backgroundColor = UIColor.init(hex: "DE333C")
  158. return [delete, edit]
  159. }
  160. func fetchReceipients() {
  161. self.showProgressHud()
  162. let defaults = UserDefaults.standard
  163. let myUsername = defaults.string(forKey: UserKeys.email) ?? ""
  164. self.fetchAccountList(username: myUsername, success: { (kftc) in
  165. self.hideProgressHud()
  166. self.kftcDetail = kftc
  167. }) { (error) in
  168. self.hideProgressHud()
  169. self.alert(message: error.localizedDescription)
  170. }
  171. }
  172. // private func
  173. private func showAddNewReciepientViewController() {
  174. let viewcontroller = AddReciepientWireframe().getMainView()
  175. self.navigationController?.pushViewController(viewcontroller, animated: true)
  176. }
  177. }
  178. extension AutoDebitViewController: FetchAccountList, DeleteAccountService {
  179. }
  180. extension AutoDebitViewController: AutoDebitViewInterface {
  181. }
  182. //AutoDebitViewController