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.

67 lines
1.4 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. // MARK: Initialization
  15. init(service: RecipientsServiceType) {
  16. self.service = service
  17. }
  18. }
  19. // MARK: Recipients interactor input interface
  20. extension RecipientsInteractor: RecipientsInteractorInput {
  21. func fetchRecipients(isRefresh: Bool) {
  22. if isRefresh {
  23. fetchRecipients()
  24. return
  25. }
  26. guard
  27. let recipients = self.recipients
  28. else {
  29. fetchRecipients()
  30. return
  31. }
  32. output?.setRecipients(using: recipients)
  33. }
  34. func deleteRecipient(who recipient: Recipient) {
  35. let myUsername = GMEDB.shared.user.string(.userId) ?? ""
  36. service.deleteRecipient(
  37. username: myUsername,
  38. reciepient: recipient,
  39. success: { self.fetchRecipients(isRefresh: true) },
  40. failure: {self.output?.setError(with: $0)}
  41. )
  42. }
  43. private func fetchRecipients() {
  44. service.fetchRecipients(
  45. success: {
  46. self.recipients = $0.recipients
  47. self.output?.setRecipients(using: $0.recipients ?? [])
  48. },
  49. failure: { self.output?.setError(with:$0) }
  50. )
  51. }
  52. }