2024-03-18 17:07:33 +01:00
|
|
|
//
|
|
|
|
// FeedViewModel.swift
|
|
|
|
// ReviewsFeed
|
|
|
|
//
|
|
|
|
// Created by Javier Cicchelli on 18/03/2024.
|
|
|
|
// Copyright © 2024 Röck+Cöde. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
2024-03-18 23:22:46 +01:00
|
|
|
import ReviewsiTunesKit
|
2024-03-18 17:07:33 +01:00
|
|
|
|
|
|
|
extension FeedViewController {
|
|
|
|
final class ViewModel: ObservableObject {
|
|
|
|
|
2024-03-18 23:22:46 +01:00
|
|
|
// 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
|
2024-03-18 17:07:33 +01:00
|
|
|
}
|
|
|
|
}
|