/
PokemonListView+Model.swift
47 lines (44 loc) · 1.24 KB
/
PokemonListView+Model.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import Foundation
import PokemonData
import SwiftUI
extension PokemonListView {
/*
This is the source of truth for the view.
Whenever anything changes in this model, the UI will react accordingly.
*/
struct Model {
var pokemons: [Pokemon] = []
var isLoading: Bool = false
var error: Error? = nil
var isDisplayingDeletionErrorToast: Bool = false
}
}
extension PokemonListView.Model {
/*
Whenever we receive a new update in the view, we will call this method to modify the state.
This is the most important part of the UI layer to test.
*/
mutating func updateStates(with newState: LoadingState<[Pokemon]>) {
switch newState {
case .idle:
pokemons = []
isLoading = false
error = nil
case .loading:
isLoading = true
case .success(let t):
pokemons = t
isLoading = false
error = nil
case .failure(let e):
error = e
if e as? PokemonRepository.Errors == .deletionError {
isDisplayingDeletionErrorToast = true
}
isLoading = false
}
}
var hasPokemons: Bool {
!pokemons.isEmpty
}
}