70 lines
1.9 KiB
Swift
Raw Normal View History

//
// FeedViewModel.swift
// ReviewsFeed
//
// Created by Javier Cicchelli on 18/03/2024.
// Copyright © 2024 Röck+Cöde. All rights reserved.
//
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
}
}