Skip to content

Commit 112239b

Browse files
committed
Fetch blogging prompts, show in the table.
1 parent e651f2d commit 112239b

File tree

2 files changed

+56
-25
lines changed

2 files changed

+56
-25
lines changed

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

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ class BloggingPromptTableViewCell: UITableViewCell, NibReusable {
2727
configureView()
2828
}
2929

30-
// TODO: remove answered param. Add BloggingPrompt, configure with its properties.
31-
func configure(answered: Bool) {
32-
titleLabel.text = "Cast the movie of your life."
33-
dateLabel.text = dateFormatter.string(from: Date())
30+
func configure(_ prompt: BloggingPrompt) {
31+
self.prompt = prompt
32+
titleLabel.text = prompt.text.stringByDecodingXMLCharacters().trim()
33+
dateLabel.text = dateFormatter.string(from: prompt.date)
3434
answerCountLabel.text = answerInfoText
35-
answeredStateView.isHidden = !answered
35+
answeredStateView.isHidden = !prompt.answered
3636
}
3737
}
3838

@@ -52,11 +52,7 @@ private extension BloggingPromptTableViewCell {
5252
}
5353

5454
var answerInfoText: String {
55-
// TODO: remove when we have a prompt.
56-
guard let answerCount = prompt?.answerCount else {
57-
return "99 answers"
58-
}
59-
55+
let answerCount = prompt?.answerCount ?? 0
6056
let stringFormat = (answerCount == 1 ? Strings.answerInfoSingularFormat : Strings.answerInfoPluralFormat)
6157
return String(format: stringFormat, answerCount)
6258
}

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

Lines changed: 50 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -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 {
108145
extension 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

Comments
 (0)