Skip to content

Commit be554f9

Browse files
committed
Add the ability to show empty views for different scenarios.
1 parent 5f4e769 commit be554f9

1 file changed

Lines changed: 53 additions & 2 deletions

File tree

WordPress/Classes/ViewRelated/Blog/Blogging Prompts/BloggingPromptsViewController.swift

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import UIKit
22

33

4-
class BloggingPromptsViewController: UIViewController {
4+
class BloggingPromptsViewController: UIViewController, NoResultsViewHost {
55

66
// MARK: - Properties
77

@@ -10,6 +10,11 @@ class BloggingPromptsViewController: UIViewController {
1010

1111
private var blog: Blog?
1212

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
17+
1318
// MARK: - Init
1419

1520
class func controllerWithBlog(_ blog: Blog) -> BloggingPromptsViewController {
@@ -30,6 +35,7 @@ class BloggingPromptsViewController: UIViewController {
3035
title = Strings.viewTitle
3136
configureFilterTabBar()
3237
configureTableView()
38+
showNoResultsViewIfNeeded()
3339
}
3440

3541
}
@@ -47,10 +53,54 @@ private extension BloggingPromptsViewController {
4753
WPStyleGuide.configureColors(view: view, tableView: tableView)
4854
}
4955

56+
func showNoResultsViewIfNeeded() {
57+
hideNoResults()
58+
59+
guard !isLoading else {
60+
showLoadingView()
61+
return
62+
}
63+
64+
// TODO: use fetched prompts count.
65+
guard promptCount == 0 else {
66+
return
67+
}
68+
69+
showNoResultsView()
70+
}
71+
72+
func showNoResultsView() {
73+
configureAndDisplayNoResults(on: tableView,
74+
title: NoResults.emptyTitle,
75+
image: NoResults.imageName)
76+
}
77+
78+
func showLoadingView() {
79+
configureAndDisplayNoResults(on: tableView,
80+
title: NoResults.loadingTitle,
81+
accessoryView: NoResultsViewController.loadingAccessoryView())
82+
}
83+
84+
func showErrorView() {
85+
hideNoResults()
86+
configureAndDisplayNoResults(on: tableView,
87+
title: NoResults.errorTitle,
88+
subtitle: NoResults.errorSubtitle,
89+
image: NoResults.imageName)
90+
}
91+
5092
enum Strings {
5193
static let viewTitle = NSLocalizedString("Prompts", comment: "View title for Blogging Prompts list.")
5294
}
5395

96+
enum NoResults {
97+
static let loadingTitle = NSLocalizedString("Loading prompts...", comment: "Displayed while blogging prompts are being loaded.")
98+
static let errorTitle = NSLocalizedString("Oops", comment: "Title for the view when there's an error loading blogging prompts.")
99+
static let errorSubtitle = NSLocalizedString("There was an error loading prompts.", comment: "Text displayed when there is a failure loading blogging prompts.")
100+
static let emptyTitle = NSLocalizedString("No prompts yet", comment: "Title displayed when there are no blogging prompts to display.")
101+
static let imageName = "wp-illustration-empty-results"
102+
}
103+
54104
}
55105

56106
// MARK: - Table Methods
@@ -59,7 +109,7 @@ extension BloggingPromptsViewController: UITableViewDataSource, UITableViewDeleg
59109

60110
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
61111
// TODO: use fetched prompts count.
62-
return 10
112+
return promptCount
63113
}
64114

65115
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
@@ -102,6 +152,7 @@ private extension BloggingPromptsViewController {
102152
// TODO:
103153
// - track selected filter changed
104154
// - refresh view for selected filter
155+
showNoResultsViewIfNeeded()
105156
}
106157

107158
}

0 commit comments

Comments
 (0)