Javier Cicchelli c9f4b9a677 [Framework] Feed item view in the Feed framework (#10)
This PR contains the work done to implement the `FeedItemViewController` view controller, that shows in details a selected review from the `FeedListViewController` view controller.

Reviewed-on: #10
Co-authored-by: Javier Cicchelli <javier@rock-n-code.com>
Co-committed-by: Javier Cicchelli <javier@rock-n-code.com>
2024-03-20 01:42:21 +00:00

70 lines
2.0 KiB
Swift

//
// 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 FeedListViewController {
final class ViewModel: ObservableObject {
// MARK: Type aliases
typealias Configuration = FeedListViewController.Configuration
// MARK: Constants
private let configuration: Configuration
// MARK: Properties
@Published var loading: Bool = false
var items: [Review] = []
// MARK: Initialisers
init(configuration: Configuration = .init()) {
self.configuration = configuration
}
// MARK: Computed
lazy private var iTunesService: iTunesService = {
.init(configuration: .init(session: configuration.session))
}()
// MARK: Functions
func fetch() {
Task {
loading = true
do {
let output = try await iTunesService.getReviews(.init(
appID: configuration.appID,
countryCode: configuration.countryCode
))
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
}
}
}
}