Skip to content

Commit be5cfe6

Browse files
authored
Merge pull request #18544 from wordpress-mobile/feature/18473-consolidate-prompt-objects
Blogging Prompts: Remove extra `Prompt` object and use `BloggingPrompt`
2 parents 361b4dc + faf8b7a commit be5cfe6

File tree

3 files changed

+19
-71
lines changed

3 files changed

+19
-71
lines changed
Lines changed: 3 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,11 @@
1-
21
extension Post {
32

4-
func prepareForPrompt(_ prompt: Prompt?) {
3+
func prepareForPrompt(_ prompt: BloggingPrompt?) {
54
guard let prompt = prompt else {
65
return
76
}
8-
postTitle = prompt.postTitle
9-
let pullquoteBlock = getPullquoteBlock(title: prompt.promptText,
10-
promptUrl: prompt.promptUrl?.absoluteString,
11-
answerUrl: prompt.answerUrl?.absoluteString,
12-
answerCount: prompt.answerCount)
13-
content = pullquoteBlock + Strings.emptyParagraph
7+
postTitle = prompt.title
8+
content = prompt.content
149
}
1510

1611
}
17-
18-
// MARK: - Private methods
19-
20-
private extension Post {
21-
22-
func getPullquoteBlock(title: String,
23-
promptUrl: String?,
24-
answerUrl: String?,
25-
answerCount: Int) -> String {
26-
let answerFormat = answerCount == 1 ? Strings.answerInfoSingularFormat : Strings.answerInfoPluralFormat
27-
let answerText = String(format: answerFormat, answerCount)
28-
let promptUrlHtml = getUrlHtml(url: promptUrl, urlText: Strings.prompt)
29-
let answerUrlHtml = getUrlHtml(url: answerUrl, urlText: answerText)
30-
let separatorText = promptUrlHtml.isEmpty || answerUrlHtml.isEmpty ? "" : ""
31-
let subtitleHtml = promptUrlHtml.isEmpty && answerUrlHtml.isEmpty ? "" : "<cite>\(promptUrlHtml)\(separatorText)\(answerUrlHtml)</cite>"
32-
return """
33-
<!-- wp:pullquote -->
34-
<figure class="wp-block-pullquote"><blockquote><p>\(title)</p>\(subtitleHtml)</blockquote></figure>
35-
<!-- /wp:pullquote -->
36-
"""
37-
}
38-
39-
func getUrlHtml(url: String?, urlText: String) -> String {
40-
guard let url = url else {
41-
return ""
42-
}
43-
return "<a href=\"\(url)\">\(urlText)</a>"
44-
}
45-
46-
// MARK: - Strings
47-
48-
struct Strings {
49-
static let prompt = NSLocalizedString("Prompt", comment: "Prompt link text in a new blogging prompts post")
50-
static let answerInfoSingularFormat = NSLocalizedString("%1$d answer", comment: "Singular format string for displaying the number of users that answered the blogging prompt.")
51-
static let answerInfoPluralFormat = NSLocalizedString("%1$d answers", comment: "Plural format string for displaying the number of users that answered the blogging prompt.")
52-
static let emptyParagraph = """
53-
<!-- wp:paragraph -->
54-
<p></p>
55-
<!-- /wp:paragraph -->
56-
"""
57-
}
58-
59-
}
60-
61-
// MARK: - Temporary prompt object
62-
63-
// TODO: Remove after prompt object is created and use that
64-
struct Prompt {
65-
let postTitle: String
66-
let promptText: String
67-
let promptUrl: URL?
68-
let answerUrl: URL?
69-
let answerCount: Int
70-
71-
static let examplePrompt = Prompt(postTitle: "Cast the movie of my life",
72-
promptText: "Cast the movie of your life.",
73-
promptUrl: URL(string: "https://wordpress.com"),
74-
answerUrl: URL(string: "https://wordpress.com"),
75-
answerCount: 19)
76-
}

WordPress/Classes/Services/BloggingPromptsService.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,19 @@ struct BloggingPrompt {
7474
let answerCount: Int
7575
let displayAvatarURLs: [URL]
7676

77+
static let examplePrompt = BloggingPrompt(
78+
promptID: 239,
79+
text: "Was there a toy or thing you always wanted as a child, during the holidays or on your birthday, but never received? Tell us about it.",
80+
title: "Prompt number 1",
81+
content: "<!-- wp:pullquote -->\n<figure class=\"wp-block-pullquote\"><blockquote><p>Was there a toy or thing you always wanted as a child, during the holidays or on your birthday, but never received? Tell us about it.</p><cite>(courtesy of plinky.com)</cite></blockquote></figure>\n<!-- /wp:pullquote -->",
82+
date: Date(),
83+
answered: false,
84+
answerCount: 5,
85+
displayAvatarURLs: []
86+
)
87+
}
88+
89+
extension BloggingPrompt {
7790
init(with remotePrompt: RemoteBloggingPrompt) {
7891
promptID = remotePrompt.promptID
7992
text = remotePrompt.text

WordPress/Classes/ViewRelated/Post/EditPostViewController.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class EditPostViewController: UIViewController {
2323
private let loadAutosaveRevision: Bool
2424

2525
@objc fileprivate(set) var post: Post?
26-
private let prompt: Prompt?
26+
private let prompt: BloggingPrompt?
2727
fileprivate var hasShownEditor = false
2828
fileprivate var editingExistingPost = false
2929
fileprivate let blog: Blog
@@ -65,7 +65,7 @@ class EditPostViewController: UIViewController {
6565
///
6666
/// - Parameter blog: blog to create a new post for
6767
/// - Parameter prompt: blogging prompt to configure the new post for
68-
convenience init(blog: Blog, prompt: Prompt) {
68+
convenience init(blog: Blog, prompt: BloggingPrompt) {
6969
self.init(post: nil, blog: blog, prompt: prompt)
7070
}
7171

@@ -75,7 +75,7 @@ class EditPostViewController: UIViewController {
7575
/// - post: the post to edit
7676
/// - blog: the blog to create a post for, if post is nil
7777
/// - Note: it's preferable to use one of the convenience initializers
78-
fileprivate init(post: Post?, blog: Blog, loadAutosaveRevision: Bool = false, prompt: Prompt? = nil) {
78+
fileprivate init(post: Post?, blog: Blog, loadAutosaveRevision: Bool = false, prompt: BloggingPrompt? = nil) {
7979
self.post = post
8080
self.loadAutosaveRevision = loadAutosaveRevision
8181
if let post = post {

0 commit comments

Comments
 (0)