Skip to content

Commit f6f051d

Browse files
authored
Merge pull request #18481 from wordpress-mobile/feature/18473-prompts-answer-prompt
Blogging Prompts: Add creating a new post with a blogging prompt
2 parents 58a97da + b3ace0c commit f6f051d

3 files changed

Lines changed: 94 additions & 1 deletion

File tree

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
2+
extension Post {
3+
4+
func prepareForPrompt(_ prompt: Prompt?) {
5+
guard let prompt = prompt else {
6+
return
7+
}
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
14+
}
15+
16+
}
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/ViewRelated/Post/EditPostViewController.swift

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

2525
@objc fileprivate(set) var post: Post?
26+
private let prompt: Prompt?
2627
fileprivate var hasShownEditor = false
2728
fileprivate var editingExistingPost = false
2829
fileprivate let blog: Blog
@@ -60,13 +61,21 @@ class EditPostViewController: UIViewController {
6061
self.init(post: nil, blog: blog)
6162
}
6263

64+
/// Initialize as an editor to create a new post for the provided blog and prompt
65+
///
66+
/// - Parameter blog: blog to create a new post for
67+
/// - Parameter prompt: blogging prompt to configure the new post for
68+
convenience init(blog: Blog, prompt: Prompt) {
69+
self.init(post: nil, blog: blog, prompt: prompt)
70+
}
71+
6372
/// Initialize as an editor with a specified post to edit and blog to post too.
6473
///
6574
/// - Parameters:
6675
/// - post: the post to edit
6776
/// - blog: the blog to create a post for, if post is nil
6877
/// - Note: it's preferable to use one of the convenience initializers
69-
fileprivate init(post: Post?, blog: Blog, loadAutosaveRevision: Bool = false) {
78+
fileprivate init(post: Post?, blog: Blog, loadAutosaveRevision: Bool = false, prompt: Prompt? = nil) {
7079
self.post = post
7180
self.loadAutosaveRevision = loadAutosaveRevision
7281
if let post = post {
@@ -77,6 +86,7 @@ class EditPostViewController: UIViewController {
7786
post.fixLocalMediaURLs()
7887
}
7988
self.blog = blog
89+
self.prompt = prompt
8090
super.init(nibName: nil, bundle: nil)
8191
modalPresentationStyle = .fullScreen
8292
modalTransitionStyle = .coverVertical
@@ -126,6 +136,7 @@ class EditPostViewController: UIViewController {
126136
let context = ContextManager.sharedInstance().mainContext
127137
let postService = PostService(managedObjectContext: context)
128138
let newPost = postService.createDraftPost(for: blog)
139+
newPost.prepareForPrompt(prompt)
129140
post = newPost
130141
return newPost
131142
}

WordPress/WordPress.xcodeproj/project.pbxproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1407,6 +1407,8 @@
14071407
8370D10A11FA499A009D650F /* WPTableViewActivityCell.m in Sources */ = {isa = PBXBuildFile; fileRef = 8370D10911FA499A009D650F /* WPTableViewActivityCell.m */; };
14081408
839B150B2795DEE0009F5E77 /* UIView+Margins.swift in Sources */ = {isa = PBXBuildFile; fileRef = 839B150A2795DEE0009F5E77 /* UIView+Margins.swift */; };
14091409
839B150C2795DEE0009F5E77 /* UIView+Margins.swift in Sources */ = {isa = PBXBuildFile; fileRef = 839B150A2795DEE0009F5E77 /* UIView+Margins.swift */; };
1410+
83C972E0281C45AB0049E1FE /* Post+BloggingPrompts.swift in Sources */ = {isa = PBXBuildFile; fileRef = 83C972DF281C45AB0049E1FE /* Post+BloggingPrompts.swift */; };
1411+
83C972E1281C45AB0049E1FE /* Post+BloggingPrompts.swift in Sources */ = {isa = PBXBuildFile; fileRef = 83C972DF281C45AB0049E1FE /* Post+BloggingPrompts.swift */; };
14101412
83F3E26011275E07004CD686 /* MapKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 83F3E25F11275E07004CD686 /* MapKit.framework */; };
14111413
83F3E2D311276371004CD686 /* CoreLocation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 83F3E2D211276371004CD686 /* CoreLocation.framework */; };
14121414
83FEFC7611FF6C5A0078B462 /* SiteSettingsViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 83FEFC7411FF6C5A0078B462 /* SiteSettingsViewController.m */; };
@@ -6178,6 +6180,7 @@
61786180
8370D10811FA499A009D650F /* WPTableViewActivityCell.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WPTableViewActivityCell.h; sourceTree = "<group>"; };
61796181
8370D10911FA499A009D650F /* WPTableViewActivityCell.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = WPTableViewActivityCell.m; sourceTree = "<group>"; };
61806182
839B150A2795DEE0009F5E77 /* UIView+Margins.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIView+Margins.swift"; sourceTree = "<group>"; };
6183+
83C972DF281C45AB0049E1FE /* Post+BloggingPrompts.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Post+BloggingPrompts.swift"; sourceTree = "<group>"; };
61816184
83F3E25F11275E07004CD686 /* MapKit.framework */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = wrapper.framework; name = MapKit.framework; path = System/Library/Frameworks/MapKit.framework; sourceTree = SDKROOT; };
61826185
83F3E2D211276371004CD686 /* CoreLocation.framework */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = wrapper.framework; name = CoreLocation.framework; path = System/Library/Frameworks/CoreLocation.framework; sourceTree = SDKROOT; };
61836186
83FB4D3E122C38F700DB9506 /* MediaPlayer.framework */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = wrapper.framework; name = MediaPlayer.framework; path = System/Library/Frameworks/MediaPlayer.framework; sourceTree = SDKROOT; };
@@ -13125,6 +13128,7 @@
1312513128
B5BE31C31CB825A100BDF770 /* NSURLCache+Helpers.swift */,
1312613129
E1CFC1561E0AC8FF001DF9E9 /* Pattern.swift */,
1312713130
FF70A3201FD5840500BC270D /* PHAsset+Metadata.swift */,
13131+
83C972DF281C45AB0049E1FE /* Post+BloggingPrompts.swift */,
1312813132
FFD12D5D1FE1998D00F20A00 /* Progress+Helpers.swift */,
1312913133
AE2F3124270B6DA000B2A9C2 /* Scanner+QuotedText.swift */,
1313013134
E177807F1C97FA9500FA7E14 /* StoreKit+Debug.swift */,
@@ -17954,6 +17958,7 @@
1795417958
E66969DC1B9E55C300EC9C00 /* ReaderTopicToReaderListTopic37to38.swift in Sources */,
1795517959
9A2D0B23225CB92B009E585F /* BlogService+JetpackConvenience.swift in Sources */,
1795617960
C856748F243EF177001A995E /* GutenbergTenorMediaPicker.swift in Sources */,
17961+
83C972E0281C45AB0049E1FE /* Post+BloggingPrompts.swift in Sources */,
1795717962
B50C0C5F1EF42A4A00372C65 /* AztecPostViewController.swift in Sources */,
1795817963
086E1FE01BBB35D2002D86CA /* MenusViewController.m in Sources */,
1795917964
FAFC064B27D22E4C002F0483 /* QuickStartTourStateView.swift in Sources */,
@@ -20300,6 +20305,7 @@
2030020305
FABB21872602FC2C00C8785C /* NavBarTitleDropdownButton.m in Sources */,
2030120306
FABB21882602FC2C00C8785C /* ReaderTopicsCardCell.swift in Sources */,
2030220307
FA25FA342609AAAA0005E08F /* AppConfiguration.swift in Sources */,
20308+
83C972E1281C45AB0049E1FE /* Post+BloggingPrompts.swift in Sources */,
2030320309
3FE3D1FC26A6F2AC00F3CD10 /* ListTableViewCell.swift in Sources */,
2030420310
FABB21892602FC2C00C8785C /* PostService+RefreshStatus.swift in Sources */,
2030520311
F1585405267D3B5000A2E966 /* BloggingRemindersFlowIntroViewController.swift in Sources */,

0 commit comments

Comments
 (0)