@@ -9,11 +9,24 @@ class BloggingPromptsViewController: UIViewController, NoResultsViewHost {
9
9
@IBOutlet private weak var filterTabBar : FilterTabBar !
10
10
11
11
private var blog : Blog ?
12
+ private var prompts : [ BloggingPrompt ] = [ ] {
13
+ didSet {
14
+ tableView. reloadData ( )
15
+ showNoResultsViewIfNeeded ( )
16
+ }
17
+ }
12
18
13
- // TODO: remove when prompts are fetched, use fetched prompts count.
14
- private var promptCount : Int = 10
15
- // TODO: set when prompt fetching is added.
16
- private var isLoading : Bool = false
19
+ private lazy var bloggingPromptsService : BloggingPromptsService ? = {
20
+ return BloggingPromptsService ( blog: blog)
21
+ } ( )
22
+
23
+ private var isLoading : Bool = false {
24
+ didSet {
25
+ if isLoading != oldValue {
26
+ showNoResultsViewIfNeeded ( )
27
+ }
28
+ }
29
+ }
17
30
18
31
// MARK: - Init
19
32
@@ -35,9 +48,12 @@ class BloggingPromptsViewController: UIViewController, NoResultsViewHost {
35
48
title = Strings . viewTitle
36
49
configureFilterTabBar ( )
37
50
configureTableView ( )
38
- showNoResultsViewIfNeeded ( )
39
51
}
40
52
53
+ override func viewWillAppear( _ animated: Bool ) {
54
+ super. viewWillAppear ( animated)
55
+ fetchPrompts ( )
56
+ }
41
57
}
42
58
43
59
// MARK: - Private Methods
@@ -54,28 +70,28 @@ private extension BloggingPromptsViewController {
54
70
}
55
71
56
72
func showNoResultsViewIfNeeded( ) {
57
- hideNoResults ( )
58
-
59
73
guard !isLoading else {
60
74
showLoadingView ( )
61
75
return
62
76
}
63
77
64
- // TODO: use fetched prompts count.
65
- guard promptCount == 0 else {
78
+ guard prompts. isEmpty else {
79
+ hideNoResults ( )
66
80
return
67
81
}
68
82
69
83
showNoResultsView ( )
70
84
}
71
85
72
86
func showNoResultsView( ) {
87
+ hideNoResults ( )
73
88
configureAndDisplayNoResults ( on: tableView,
74
89
title: NoResults . emptyTitle,
75
90
image: NoResults . imageName)
76
91
}
77
92
78
93
func showLoadingView( ) {
94
+ hideNoResults ( )
79
95
configureAndDisplayNoResults ( on: tableView,
80
96
title: NoResults . loadingTitle,
81
97
accessoryView: NoResultsViewController . loadingAccessoryView ( ) )
@@ -89,6 +105,27 @@ private extension BloggingPromptsViewController {
89
105
image: NoResults . imageName)
90
106
}
91
107
108
+ func fetchPrompts( ) {
109
+ // TODO: show cached prompts first.
110
+
111
+ guard let bloggingPromptsService = bloggingPromptsService else {
112
+ DDLogError ( " Failed creating BloggingPromptsService instance. " )
113
+ showErrorView ( )
114
+ return
115
+ }
116
+
117
+ isLoading = true
118
+
119
+ bloggingPromptsService. fetchPrompts ( success: { [ weak self] ( prompts) in
120
+ self ? . isLoading = false
121
+ self ? . prompts = prompts. sorted ( by: { $0. date. compare ( $1. date) == . orderedDescending } )
122
+ } , failure: { [ weak self] ( error) in
123
+ DDLogError ( " Failed fetching blogging prompts: \( String ( describing: error) ) " )
124
+ self ? . isLoading = false
125
+ self ? . showErrorView ( )
126
+ } )
127
+ }
128
+
92
129
enum Strings {
93
130
static let viewTitle = NSLocalizedString ( " Prompts " , comment: " View title for Blogging Prompts list. " )
94
131
}
@@ -108,17 +145,16 @@ private extension BloggingPromptsViewController {
108
145
extension BloggingPromptsViewController : UITableViewDataSource , UITableViewDelegate {
109
146
110
147
func tableView( _ tableView: UITableView , numberOfRowsInSection section: Int ) -> Int {
111
- // TODO: use fetched prompts count.
112
- return promptCount
148
+ return prompts. count
113
149
}
114
150
115
151
func tableView( _ tableView: UITableView , cellForRowAt indexPath: IndexPath ) -> UITableViewCell {
116
- guard let cell = tableView. dequeueReusableCell ( withIdentifier: BloggingPromptTableViewCell . defaultReuseID) as? BloggingPromptTableViewCell else {
152
+ guard let cell = tableView. dequeueReusableCell ( withIdentifier: BloggingPromptTableViewCell . defaultReuseID) as? BloggingPromptTableViewCell ,
153
+ let prompt = prompts [ safe: indexPath. row] else {
117
154
return UITableViewCell ( )
118
155
}
119
156
120
- // TODO: replace answered with BloggingPrompt.
121
- cell. configure ( answered: indexPath. row > 4 )
157
+ cell. configure ( prompt)
122
158
return cell
123
159
}
124
160
@@ -152,7 +188,6 @@ private extension BloggingPromptsViewController {
152
188
// TODO:
153
189
// - track selected filter changed
154
190
// - refresh view for selected filter
155
- showNoResultsViewIfNeeded ( )
156
191
}
157
192
158
193
}
0 commit comments