diff --git a/Frameworks/Feed/Bundle/Sources/Logic/View Models/FeedListViewModel.swift b/Frameworks/Feed/Bundle/Sources/Logic/View Models/FeedListViewModel.swift index 992e180..5ee2de6 100644 --- a/Frameworks/Feed/Bundle/Sources/Logic/View Models/FeedListViewModel.swift +++ b/Frameworks/Feed/Bundle/Sources/Logic/View Models/FeedListViewModel.swift @@ -61,49 +61,47 @@ extension FeedListViewController { } // MARK: Functions - func fetch() { - Task { - isFilterEnabled = false - isLoading = items.isEmpty + func fetch() async { + isFilterEnabled = false + isLoading = items.isEmpty - do { - let output = try await iTunesService.getReviews(.init( - appID: configuration.appID, - countryCode: configuration.countryCode - )) - - reviewsAll = output.reviews.map(Review.init) - reviewsFiltered = FilterOption.allCases - .reduce(into: FilteredReviews()) { partialResult, option in - partialResult[option] = reviewsAll.filter { $0.rating.stars == option.rawValue } - } - reviewsTopWords = reviewsFiltered - .mapValues { reviews in - reviews.map(\.comment) - .compactMap { try? filterWords($0) } - } - .mapValues { - topWords($0).map(TopWord.init) - } + do { + let output = try await iTunesService.getReviews(.init( + appID: configuration.appID, + countryCode: configuration.countryCode + )) + + reviewsAll = output.reviews.map(Review.init) + reviewsFiltered = FilterOption.allCases + .reduce(into: FilteredReviews()) { partialResult, option in + partialResult[option] = reviewsAll.filter { $0.rating.stars == option.rawValue } + } + reviewsTopWords = reviewsFiltered + .mapValues { reviews in + reviews.map(\.comment) + .compactMap { try? filterWords($0) } + } + .mapValues { + topWords($0).map(TopWord.init) + } - items = filter == .all - ? reviewsAll - : reviewsFiltered[filter] ?? [] - words = filter == .all - ? [] - : reviewsTopWords[filter] ?? [] + items = filter == .all + ? reviewsAll + : reviewsFiltered[filter] ?? [] + words = filter == .all + ? [] + : reviewsTopWords[filter] ?? [] - isFilterEnabled = !items.isEmpty - state = items.isEmpty - ? .empty - : .populated - } catch { - items = [] - state = .error - } - - isLoading = false + isFilterEnabled = !items.isEmpty + state = items.isEmpty + ? .empty + : .populated + } catch { + items = [] + state = .error } + + isLoading = false } func filter(by option: FilterOption) { diff --git a/Frameworks/Feed/Bundle/Sources/UI/View Controllers/FeedListViewController.swift b/Frameworks/Feed/Bundle/Sources/UI/View Controllers/FeedListViewController.swift index 991ae02..152a5ed 100644 --- a/Frameworks/Feed/Bundle/Sources/UI/View Controllers/FeedListViewController.swift +++ b/Frameworks/Feed/Bundle/Sources/UI/View Controllers/FeedListViewController.swift @@ -118,7 +118,7 @@ final class FeedListViewController: UIViewController { registerTableCells() bindViewModel() - viewModel.fetch() + Task { await viewModel.fetch() } } } @@ -170,7 +170,7 @@ private extension FeedListViewController { // MARK: Actions @objc func refresh(_ sender: AnyObject) { - self.viewModel.fetch() + Task { await self.viewModel.fetch() } } // MARK: Functions @@ -264,7 +264,7 @@ private extension FeedListViewController { ) : nil, action: isErrorState - ? { self.viewModel.fetch() } + ? { Task { await self.viewModel.fetch() } } : nil )