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.

72 lines
1.6 KiB

  1. //
  2. // RecipientsInteractor.swift
  3. // GME Remit
  4. //
  5. // Created by InKwon James Kim on 08/08/2019.
  6. //Copyright © 2019 Gobal Money Express Co. Ltd. All rights reserved.
  7. //
  8. import Foundation
  9. class RecipientsInteractor {
  10. // MARK: Properties
  11. weak var output: RecipientsInteractorOutput?
  12. private let service: RecipientsServiceType
  13. private var recipients: [Recipient]?
  14. private var accounts: [Account]?
  15. // MARK: Initialization
  16. init(service: RecipientsServiceType) {
  17. self.service = service
  18. }
  19. }
  20. // MARK: Recipients interactor input interface
  21. extension RecipientsInteractor: RecipientsInteractorInput {
  22. func fetchRecipients(isRefresh: Bool) {
  23. if isRefresh {
  24. fetchRecipients()
  25. return
  26. }
  27. guard
  28. let recipients = self.recipients,
  29. let accounts = self.accounts
  30. else {
  31. fetchRecipients()
  32. return
  33. }
  34. output?.setRecipients(using: recipients)
  35. output?.setAccounts(using: accounts)
  36. }
  37. func deleteRecipient(who recipient: Recipient) {
  38. let myUsername = GMEDB.shared.user.string(.userId) ?? ""
  39. service.deleteRecipient(
  40. username: myUsername,
  41. reciepient: recipient,
  42. success: { self.fetchRecipients(isRefresh: true) },
  43. failure: {self.output?.setError(with: $0)}
  44. )
  45. }
  46. private func fetchRecipients() {
  47. service.fetchRecipients(
  48. success: {
  49. self.recipients = $0.recipients
  50. self.accounts = $0.accounts
  51. self.output?.setRecipients(using: $0.recipients ?? [])
  52. self.output?.setAccounts(using: $0.accounts ?? [])
  53. },
  54. failure: { self.output?.setError(with:$0) }
  55. )
  56. }
  57. }