GifListPresenter.swift 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. //
  2. // GifListPresenter.swift
  3. import Foundation
  4. /*!
  5. GifListPresenters delegate
  6. */
  7. protocol GifListPresenterDelegate: AnyObject {
  8. /// Called before making a call on an empty data
  9. func presenterWillStartLoadingForEmptyData()
  10. /// Called when inital data is loaded
  11. func presenterDidReceiveInitialData()
  12. /// Called when next page is loaded
  13. func presenterDidReceiveAdditionalData(at indexPaths: [IndexPath])
  14. }
  15. /*!
  16. Presenter that loads the trending page,
  17. if a search keyword is present, it uses that keyword to
  18. search the Giphy library
  19. */
  20. class GifListPresenter {
  21. weak var delegate: GifListPresenterDelegate?
  22. /*!
  23. Last searched term
  24. On search term change, reset the pagination
  25. */
  26. private var lastSearchedTerm: String = "" {
  27. willSet {
  28. if newValue != lastSearchedTerm {
  29. pagination = GifListPagination()
  30. }
  31. }
  32. }
  33. /*!
  34. Last pagination object
  35. */
  36. private var pagination: GifListPagination = GifListPagination()
  37. /*!
  38. Array of objects to be shown
  39. */
  40. var gifsList: [GifObject] = []
  41. /*!
  42. Only one pagination request should be active
  43. */
  44. var isLoadingNewPage = false
  45. /*!
  46. If a search query is present, search data is fetched.
  47. If there is no search query, trending data is fetched.
  48. */
  49. func getData(query: String) {
  50. self.lastSearchedTerm = query
  51. if lastSearchedTerm.isEmpty {
  52. self.fetchTrendingData()
  53. } else {
  54. self.fetchSearchData(query: query)
  55. }
  56. }
  57. /// Trending data call
  58. private func fetchTrendingData() {
  59. self.prepareForFetch()
  60. GiphyApiManager.shared.getTrendingGifs(offset: pagination.nextOffset) { [weak self] result in
  61. switch result {
  62. case .success(let listResponse):
  63. self?.didReceiveSuccessfulListResponse(listResponse: listResponse)
  64. case .failure(let error):
  65. print(error)
  66. }
  67. self?.isLoadingNewPage = false
  68. }
  69. }
  70. /// Search data called
  71. private func fetchSearchData(query: String) {
  72. self.prepareForFetch()
  73. GiphyApiManager.shared.getSearchGifs(offset: pagination.nextOffset, query: query) { [weak self] result in
  74. switch result {
  75. case .success(let listResponse):
  76. self?.didReceiveSuccessfulListResponse(listResponse: listResponse)
  77. case .failure(let error):
  78. print(error)
  79. }
  80. self?.isLoadingNewPage = false
  81. }
  82. }
  83. /// Prepares self before making a call
  84. private func prepareForFetch() {
  85. if self.gifsList.count == 0 {
  86. self.delegate?.presenterWillStartLoadingForEmptyData()
  87. }
  88. GiphyApiManager.shared.cancelLastSearchableTask()
  89. if pagination.count + pagination.offset != 0 {
  90. isLoadingNewPage = true
  91. }
  92. }
  93. private func didReceiveSuccessfulListResponse(listResponse: GiphyListResponse) {
  94. let offset = listResponse.pagination.offset
  95. if (offset == 0) {
  96. self.gifsList = listResponse.data
  97. self.delegate?.presenterDidReceiveInitialData()
  98. } else {
  99. let indexPaths = (offset..<(offset + listResponse.data.count)).map({IndexPath(row: $0, section: 0)})
  100. self.gifsList += listResponse.data
  101. self.delegate?.presenterDidReceiveAdditionalData(at: indexPaths)
  102. }
  103. self.pagination = listResponse.pagination
  104. }
  105. }