-
Notifications
You must be signed in to change notification settings - Fork 680
/
MoviesQueryListView.swift
51 lines (44 loc) · 1.5 KB
/
MoviesQueryListView.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
import Foundation
import SwiftUI
@available(iOS 13.0, *)
extension MoviesQueryListItemViewModel: Identifiable { }
@available(iOS 13.0, *)
struct MoviesQueryListView: View {
@ObservedObject var viewModelWrapper: MoviesQueryListViewModelWrapper
var body: some View {
List(viewModelWrapper.items) { item in
Button(action: {
self.viewModelWrapper.viewModel?.didSelect(item: item)
}) {
Text(item.query)
}
}
.onAppear {
self.viewModelWrapper.viewModel?.viewWillAppear()
}
}
}
@available(iOS 13.0, *)
final class MoviesQueryListViewModelWrapper: ObservableObject {
var viewModel: MoviesQueryListViewModel?
@Published var items: [MoviesQueryListItemViewModel] = []
init(viewModel: MoviesQueryListViewModel?) {
self.viewModel = viewModel
viewModel?.items.observe(on: self) { [weak self] values in self?.items = values }
}
}
#if DEBUG
@available(iOS 13.0, *)
struct MoviesQueryListView_Previews: PreviewProvider {
static var previews: some View {
MoviesQueryListView(viewModelWrapper: previewViewModelWrapper)
}
static var previewViewModelWrapper: MoviesQueryListViewModelWrapper = {
var viewModel = MoviesQueryListViewModelWrapper(viewModel: nil)
viewModel.items = [MoviesQueryListItemViewModel(query: "item 1"),
MoviesQueryListItemViewModel(query: "item 2")
]
return viewModel
}()
}
#endif