Implemented the FeedViewModel view model in the Feed framework.

This commit is contained in:
Javier Cicchelli 2024-03-18 23:22:46 +01:00
parent 908ca1d4c9
commit 6d5b416471
2 changed files with 55 additions and 1 deletions

View File

@ -7,9 +7,63 @@
//
import Foundation
import ReviewsiTunesKit
extension FeedViewController {
final class ViewModel: ObservableObject {
// MARK: Constants
private let iTunesService: iTunesService
// MARK: Properties
@Published var loading: Bool = false
var items: [Review] = []
// MARK: Initialisers
init(_ configuration: Configuration = .init()) {
self.iTunesService = .init(configuration: .init(
session: configuration.session
))
}
// MARK: Functions
func fetch() {
Task {
loading = true
do {
let output = try await iTunesService.getReviews(.init(
appID: "474495017",
countryCode: "nl"
))
items = output.reviews
.map { review -> Review in
.init(
author: review.author,
comment: review.content,
id: review.id,
rating: .init(
stars: review.rating,
appVersion: review.version
),
title: review.title
)
}
} catch {
// TODO: handle this error gracefully.
}
loading = false
}
}
}
}
// MARK: - Structs
extension FeedViewController.ViewModel {
struct Configuration {
let session: URLSessionConfiguration = .ephemeral
}
}

View File

@ -13,7 +13,7 @@ import ReviewsFeedKit
extension ServiceConfiguration {
// MARK: Initialisers
init(session: URLSessionConfiguration = .ephemeral) {
public init(session: URLSessionConfiguration = .ephemeral) {
self.init(
host: .iTunes,
session: session,