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.

135 lines
4.1 KiB

6 years ago
6 years ago
6 years ago
  1. //
  2. // AddReciepientPresenter.swift
  3. // GMERemittance
  4. //
  5. // Created by gme_2 on 26/08/2018.
  6. //Copyright © 2018 Gobal Money Express Co. Ltd. All rights reserved.
  7. //
  8. import Foundation
  9. class AddReciepientPresenter {
  10. // MARK: Properties
  11. var transferReasons: [TransferReason] = []
  12. var relations: [Relation] = []
  13. var countries: [CountryModel] = []
  14. weak var view: AddReciepientViewInterface?
  15. var interactor: AddReciepientInteractorInput?
  16. var wireframe: AddReciepientWireframeInput?
  17. // MARK: Converting entities
  18. func convert(models: [CountryModel]) {
  19. let viewmodels: [SendMoneyCountryViewModel] = models.map({
  20. let viewmodel = SendMoneyCountryViewModel()
  21. viewmodel.name = $0.name
  22. viewmodel.code = $0.code
  23. viewmodel.id = $0.id
  24. viewmodel.proviencesRequired = $0.provienceRequired
  25. viewmodel.proviences = self.convert(models: $0.proviences ?? [])
  26. return viewmodel
  27. })
  28. self.view?.show(countries: viewmodels)
  29. }
  30. func convert(models: [Provience]) -> [SendMoneyProvienceViewModel] {
  31. return models.map({
  32. var viewmodel = SendMoneyProvienceViewModel()
  33. viewmodel.name = $0.name
  34. viewmodel.id = $0.id
  35. viewmodel.districts = self.convert(models: $0.districts ?? [])
  36. return viewmodel
  37. })
  38. }
  39. func convert(models: [District]) -> [SendMoneyDistrictViewModel] {
  40. let viewmodels: [SendMoneyDistrictViewModel] = models.map({
  41. let viewmodel = SendMoneyDistrictViewModel()
  42. viewmodel.name = $0.name
  43. viewmodel.id = $0.id
  44. return viewmodel
  45. })
  46. return viewmodels
  47. }
  48. func convert(models: [TransferReason]) {
  49. let viewmodels: [SendMoneyTransferReasonViewModel] = models.map({
  50. var viewmodel = SendMoneyTransferReasonViewModel()
  51. viewmodel.title = $0.title
  52. viewmodel.id = $0.id
  53. return viewmodel
  54. })
  55. self.view?.show(transferReasons: viewmodels)
  56. }
  57. func convert(models: [Relation]) {
  58. let viewmodels: [SendMoneyRelationViewModel] = models.map({
  59. var viemodel = SendMoneyRelationViewModel()
  60. viemodel.title = $0.title
  61. viemodel.id = $0.id
  62. return viemodel
  63. })
  64. self.view?.show(relations: viewmodels)
  65. }
  66. func convert(model: ReceipientViewModel) {
  67. var reciepient = Recipient()
  68. reciepient.firstName = model.firstName
  69. reciepient.middleName = model.middleName
  70. reciepient.lastName = model.lastName
  71. reciepient.countryId = model.countryId
  72. reciepient.stateId = model.stateId
  73. reciepient.districtId = model.districtId
  74. reciepient.address = model.address
  75. reciepient.relationId = model.relationId
  76. reciepient.reasonId = model.reasonId
  77. reciepient.mobileNumber = model.mobileNumber
  78. reciepient.email = model.email
  79. self.interactor?.save(reciepient: reciepient)
  80. }
  81. }
  82. // MARK: AddReciepient module interface
  83. extension AddReciepientPresenter: AddReciepientModuleInterface {
  84. func viewIsReady() {
  85. self.view?.showLoading()
  86. self.interactor?.viewIsReady()
  87. }
  88. func save(model: ReceipientViewModel) {
  89. self.convert(model: model)
  90. }
  91. }
  92. // MARK: AddReciepient interactor output interface
  93. extension AddReciepientPresenter: AddReciepientInteractorOutput {
  94. func show(model: SendMoneyModel?) {
  95. self.transferReasons = model?.transferReasons ?? []
  96. self.relations = model?.relations ?? []
  97. self.countries = model?.countries ?? []
  98. self.view?.hideLoading()
  99. self.convert(models: self.countries)
  100. self.convert(models: self.transferReasons)
  101. self.convert(models: self.relations)
  102. }
  103. func show(error: Error) {
  104. self.view?.hideLoading()
  105. self.view?.show(error: error.localizedDescription)
  106. }
  107. func success() {
  108. print("successfully added")
  109. self.wireframe?.dismiss()
  110. }
  111. }