[Framework] Feed List improvements #19

Merged
javier merged 8 commits from framework/feed/view-model-tests into main 2024-03-22 14:44:52 +00:00
2 changed files with 40 additions and 42 deletions
Showing only changes of commit 8eec6f5666 - Show all commits

View File

@ -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) {

View File

@ -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
)