-
Notifications
You must be signed in to change notification settings - Fork 682
/
Copy pathMoviesListViewModel.swift
executable file
·165 lines (136 loc) · 5.12 KB
/
MoviesListViewModel.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
//
// MoviesListViewModel.swift
// ExampleMVVM
//
// Created by Oleh Kudinov on 01.10.18.
//
import Foundation
struct MoviesListViewModelClosures {
let showMovieDetails: (Movie) -> Void
let showMovieQueriesSuggestions: (@escaping (_ didSelect: MovieQuery) -> Void) -> Void
let closeMovieQueriesSuggestions: () -> Void
}
enum MoviesListViewModelLoading {
case fullScreen
case nextPage
}
protocol MoviesListViewModelInput {
func viewDidLoad()
func didLoadNextPage()
func didSearch(query: String)
func didCancelSearch()
func showQueriesSuggestions()
func closeQueriesSuggestions()
func didSelectItem(at index: Int)
}
protocol MoviesListViewModelOutput {
var items: Observable<[MoviesListItemViewModel]> { get }
var loadingType: Observable<MoviesListViewModelLoading?> { get }
var query: Observable<String> { get }
var error: Observable<String> { get }
var isEmpty: Bool { get }
var screenTitle: String { get }
var emptyDataTitle: String { get }
var errorTitle: String { get }
var searchBarPlaceholder: String { get }
}
protocol MoviesListViewModel: MoviesListViewModelInput, MoviesListViewModelOutput {}
final class DefaultMoviesListViewModel: MoviesListViewModel {
private let searchMoviesUseCase: SearchMoviesUseCase
private let closures: MoviesListViewModelClosures?
var currentPage: Int = 0
var totalPageCount: Int = 1
var hasMorePages: Bool { currentPage < totalPageCount }
var nextPage: Int { hasMorePages ? currentPage + 1 : currentPage }
fileprivate struct Page {
let moviesPage: MoviesPage
let items: [MoviesListItemViewModel]
}
private var pages: [Page] = []
private var moviesLoadTask: Cancellable? { willSet { moviesLoadTask?.cancel() } }
// MARK: - OUTPUT
let items: Observable<[MoviesListItemViewModel]> = Observable([])
let loadingType: Observable<MoviesListViewModelLoading?> = Observable(.none)
let query: Observable<String> = Observable("")
let error: Observable<String> = Observable("")
var isEmpty: Bool { return items.value.isEmpty }
let screenTitle = NSLocalizedString("Movies", comment: "")
let emptyDataTitle = NSLocalizedString("Search results", comment: "")
let errorTitle = NSLocalizedString("Error", comment: "")
let searchBarPlaceholder = NSLocalizedString("Search Movies", comment: "")
init(searchMoviesUseCase: SearchMoviesUseCase,
closures: MoviesListViewModelClosures? = nil) {
self.searchMoviesUseCase = searchMoviesUseCase
self.closures = closures
}
private func appendPage(_ moviesPage: MoviesPage) {
currentPage = moviesPage.page
totalPageCount = moviesPage.totalPages
pages = pages
.filter { $0.moviesPage.page != moviesPage.page }
+ [Page(moviesPage: moviesPage,
items: moviesPage.movies.map(MoviesListItemViewModel.init))]
items.value = pages.items
}
private func resetPages() {
currentPage = 0
totalPageCount = 1
pages.removeAll()
items.value.removeAll()
}
private func load(movieQuery: MovieQuery, loadingType: MoviesListViewModelLoading) {
self.loadingType.value = loadingType
query.value = movieQuery.query
moviesLoadTask = searchMoviesUseCase.execute(
requestValue: .init(query: movieQuery, page: nextPage),
cached: appendPage,
completion: { result in
switch result {
case .success(let page):
self.appendPage(page)
case .failure(let error):
self.handle(error: error)
}
self.loadingType.value = .none
})
}
private func handle(error: Error) {
self.error.value = error.isInternetConnectionError ?
NSLocalizedString("No internet connection", comment: "") :
NSLocalizedString("Failed loading movies", comment: "")
}
private func update(movieQuery: MovieQuery) {
resetPages()
load(movieQuery: movieQuery, loadingType: .fullScreen)
}
}
// MARK: - INPUT. View event methods
extension DefaultMoviesListViewModel {
func viewDidLoad() { }
func didLoadNextPage() {
guard hasMorePages, loadingType.value == .none else { return }
load(movieQuery: MovieQuery(query: query.value),
loadingType: .nextPage)
}
func didSearch(query: String) {
guard !query.isEmpty else { return }
update(movieQuery: MovieQuery(query: query))
}
func didCancelSearch() {
moviesLoadTask?.cancel()
}
func showQueriesSuggestions() {
closures?.showMovieQueriesSuggestions(update(movieQuery:))
}
func closeQueriesSuggestions() {
closures?.closeMovieQueriesSuggestions()
}
func didSelectItem(at index: Int) {
closures?.showMovieDetails(pages.movies[index])
}
}
// MARK: - Private
private extension Array where Element == DefaultMoviesListViewModel.Page {
var movies: [Movie] { flatMap { $0.moviesPage.movies } }
var items: [MoviesListItemViewModel] { flatMap { $0.items } }
}