Skip to content

Commit 83eca76

Browse files
authored
Merge pull request #18572 from wordpress-mobile/feature/18429-compact_card_show_prompt
Blogging Prompt Compact Card: show a real prompt
2 parents 2aee64b + 1d169de commit 83eca76

File tree

3 files changed

+83
-25
lines changed

3 files changed

+83
-25
lines changed

WordPress/Classes/ViewRelated/Blog/Blog Dashboard/Cards/Prompts/DashboardPromptsCardCell.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,7 @@ private extension DashboardPromptsCardCell {
359359
self?.prompt = prompt
360360
self?.didFailLoadingPrompt = false
361361
}, failure: { [weak self] (error) in
362+
self?.prompt = nil
362363
self?.didFailLoadingPrompt = true
363364
DDLogError("Failed fetching blogging prompt: \(String(describing: error))")
364365
})

WordPress/Classes/ViewRelated/System/Action Sheet/BloggingPromptsHeaderView.swift

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,21 @@ class BloggingPromptsHeaderView: UIView, NibLoadable {
1818
configureView()
1919
}
2020

21-
@IBAction private func answerPromptTapped(_ sender: Any) {
22-
answerPromptHandler?()
21+
static func view(for prompt: BloggingPrompt?) -> BloggingPromptsHeaderView {
22+
let promptsHeaderView = BloggingPromptsHeaderView.loadFromNib()
23+
promptsHeaderView.configure(prompt)
24+
return promptsHeaderView
2325
}
2426

25-
@IBAction private func shareTapped(_ sender: Any) {
26-
// TODO
27-
}
2827
}
2928

3029
// MARK: - Private methods
3130

3231
private extension BloggingPromptsHeaderView {
3332

33+
// MARK: - Configure View
34+
3435
func configureView() {
35-
// TODO: Hide correct UI based on if prompt is answered
36-
answeredStackView.isHidden = true
3736
configureSpacing()
3837
configureStrings()
3938
configureStyles()
@@ -49,8 +48,6 @@ private extension BloggingPromptsHeaderView {
4948

5049
func configureStrings() {
5150
titleLabel.text = Strings.title
52-
// TODO: Use prompt from backend
53-
promptLabel.text = Strings.examplePrompt
5451
answerPromptButton.titleLabel?.text = Strings.answerButtonTitle
5552
answeredLabel.text = Strings.answeredLabelTitle
5653
shareButton.titleLabel?.text = Strings.shareButtonTitle
@@ -91,6 +88,24 @@ private extension BloggingPromptsHeaderView {
9188
}
9289
}
9390

91+
func configure(_ prompt: BloggingPrompt?) {
92+
promptLabel.text = prompt?.text
93+
94+
let answered = prompt?.answered ?? false
95+
answerPromptButton.isHidden = answered
96+
answeredStackView.isHidden = !answered
97+
}
98+
99+
// MARK: - Button Actions
100+
101+
@IBAction func answerPromptTapped(_ sender: Any) {
102+
answerPromptHandler?()
103+
}
104+
105+
@IBAction func shareTapped(_ sender: Any) {
106+
// TODO
107+
}
108+
94109
// MARK: - Constants
95110

96111
struct Constants {
@@ -102,7 +117,6 @@ private extension BloggingPromptsHeaderView {
102117
}
103118

104119
struct Strings {
105-
static let examplePrompt = NSLocalizedString("Cast the movie of your life.", comment: "Example prompt for blogging prompts in the create new bottom action sheet.")
106120
static let title = NSLocalizedString("Prompts", comment: "Title label for blogging prompts in the create new bottom action sheet.")
107121
static let answerButtonTitle = NSLocalizedString("Answer Prompt", comment: "Title for a call-to-action button in the create new bottom action sheet.")
108122
static let answeredLabelTitle = NSLocalizedString("✓ Answered", comment: "Title label that indicates the prompt has been answered.")

WordPress/Classes/ViewRelated/System/Floating Create Button/CreateButtonCoordinator.swift

Lines changed: 58 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -56,20 +56,11 @@ import WordPressFlux
5656
}
5757
}
5858

59-
private lazy var promptsHeaderView: BloggingPromptsHeaderView? = {
60-
let headerView = FeatureFlag.bloggingPrompts.enabled ? BloggingPromptsHeaderView.loadFromNib() : nil
61-
headerView?.answerPromptHandler = { [weak self] in
62-
self?.viewController?.dismiss(animated: true) {
63-
guard let blog = self?.blog else {
64-
return
65-
}
66-
let editor = EditPostViewController(blog: blog, prompt: .examplePrompt)
67-
editor.modalPresentationStyle = .fullScreen
68-
editor.entryPoint = .bloggingPromptsActionSheetHeader
69-
self?.viewController?.present(editor, animated: true)
70-
}
71-
}
72-
return headerView
59+
// TODO: when prompt is used, get prompt from cache so it's using the latest.
60+
private var prompt: BloggingPrompt?
61+
62+
private lazy var bloggingPromptsService: BloggingPromptsService? = {
63+
return BloggingPromptsService(blog: blog)
7364
}()
7465

7566
private weak var noticeContainerView: NoticeContainerView?
@@ -92,6 +83,11 @@ import WordPressFlux
9283
super.init()
9384

9485
listenForQuickStart()
86+
87+
// Only fetch the prompt if it is actually needed, i.e. on the FAB that has multiple actions.
88+
if actions.count > 1 {
89+
fetchBloggingPrompt()
90+
}
9591
}
9692

9793
deinit {
@@ -161,7 +157,7 @@ import WordPressFlux
161157
}
162158

163159
private func actionSheetController(with traitCollection: UITraitCollection) -> UIViewController {
164-
let actionSheetVC = CreateButtonActionSheet(headerView: promptsHeaderView, actions: actions)
160+
let actionSheetVC = CreateButtonActionSheet(headerView: createPromptHeaderView(), actions: actions)
165161
setupPresentation(on: actionSheetVC, for: traitCollection)
166162
return actionSheetVC
167163
}
@@ -302,3 +298,50 @@ extension UserDefaults {
302298
}
303299
}
304300
}
301+
302+
303+
// MARK: - Blogging Prompts Methods
304+
305+
private extension CreateButtonCoordinator {
306+
307+
private func fetchBloggingPrompt() {
308+
309+
// TODO: check for cached prompt first.
310+
311+
guard let bloggingPromptsService = bloggingPromptsService else {
312+
DDLogError("FAB > failed creating BloggingPromptsService instance.")
313+
prompt = nil
314+
return
315+
}
316+
317+
bloggingPromptsService.fetchTodaysPrompt(success: { [weak self] (prompt) in
318+
self?.prompt = prompt
319+
}, failure: { [weak self] (error) in
320+
self?.prompt = nil
321+
DDLogError("FAB > failed fetching blogging prompt: \(String(describing: error))")
322+
})
323+
}
324+
325+
private func createPromptHeaderView() -> BloggingPromptsHeaderView? {
326+
guard FeatureFlag.bloggingPrompts.enabled,
327+
let blog = blog,
328+
let prompt = prompt else {
329+
return nil
330+
}
331+
332+
let promptsHeaderView = BloggingPromptsHeaderView.view(for: prompt)
333+
334+
promptsHeaderView.answerPromptHandler = { [weak self] in
335+
self?.viewController?.dismiss(animated: true) {
336+
// TODO: pass prompt to post editor
337+
let editor = EditPostViewController(blog: blog, prompt: .examplePrompt)
338+
editor.modalPresentationStyle = .fullScreen
339+
editor.entryPoint = .bloggingPromptsActionSheetHeader
340+
self?.viewController?.present(editor, animated: true)
341+
}
342+
}
343+
344+
return promptsHeaderView
345+
}
346+
347+
}

0 commit comments

Comments
 (0)