@@ -9,11 +9,24 @@ class BloggingPromptsViewController: UIViewController, NoResultsViewHost {
99 @IBOutlet private weak var filterTabBar : FilterTabBar !
1010
1111 private var blog : Blog ?
12+ private var prompts : [ BloggingPrompt ] = [ ] {
13+ didSet {
14+ tableView. reloadData ( )
15+ showNoResultsViewIfNeeded ( )
16+ }
17+ }
1218
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+ }
1730
1831 // MARK: - Init
1932
@@ -35,9 +48,12 @@ class BloggingPromptsViewController: UIViewController, NoResultsViewHost {
3548 title = Strings . viewTitle
3649 configureFilterTabBar ( )
3750 configureTableView ( )
38- showNoResultsViewIfNeeded ( )
3951 }
4052
53+ override func viewWillAppear( _ animated: Bool ) {
54+ super. viewWillAppear ( animated)
55+ fetchPrompts ( )
56+ }
4157}
4258
4359// MARK: - Private Methods
@@ -54,28 +70,28 @@ private extension BloggingPromptsViewController {
5470 }
5571
5672 func showNoResultsViewIfNeeded( ) {
57- hideNoResults ( )
58-
5973 guard !isLoading else {
6074 showLoadingView ( )
6175 return
6276 }
6377
64- // TODO: use fetched prompts count.
65- guard promptCount == 0 else {
78+ guard prompts. isEmpty else {
79+ hideNoResults ( )
6680 return
6781 }
6882
6983 showNoResultsView ( )
7084 }
7185
7286 func showNoResultsView( ) {
87+ hideNoResults ( )
7388 configureAndDisplayNoResults ( on: tableView,
7489 title: NoResults . emptyTitle,
7590 image: NoResults . imageName)
7691 }
7792
7893 func showLoadingView( ) {
94+ hideNoResults ( )
7995 configureAndDisplayNoResults ( on: tableView,
8096 title: NoResults . loadingTitle,
8197 accessoryView: NoResultsViewController . loadingAccessoryView ( ) )
@@ -89,6 +105,27 @@ private extension BloggingPromptsViewController {
89105 image: NoResults . imageName)
90106 }
91107
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+
92129 enum Strings {
93130 static let viewTitle = NSLocalizedString ( " Prompts " , comment: " View title for Blogging Prompts list. " )
94131 }
@@ -108,17 +145,16 @@ private extension BloggingPromptsViewController {
108145extension BloggingPromptsViewController : UITableViewDataSource , UITableViewDelegate {
109146
110147 func tableView( _ tableView: UITableView , numberOfRowsInSection section: Int ) -> Int {
111- // TODO: use fetched prompts count.
112- return promptCount
148+ return prompts. count
113149 }
114150
115151 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 {
117154 return UITableViewCell ( )
118155 }
119156
120- // TODO: replace answered with BloggingPrompt.
121- cell. configure ( answered: indexPath. row > 4 )
157+ cell. configure ( prompt)
122158 return cell
123159 }
124160
@@ -152,7 +188,6 @@ private extension BloggingPromptsViewController {
152188 // TODO:
153189 // - track selected filter changed
154190 // - refresh view for selected filter
155- showNoResultsViewIfNeeded ( )
156191 }
157192
158193}
0 commit comments