forked from wordpress-mobile/WordPress-iOS
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request wordpress-mobile#18750 from wordpress-mobile/featu…
…re/18739-push-notification-handling Blogging Prompts: Add handler for prompt notifications
- Loading branch information
Showing
10 changed files
with
275 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
WordPress/Classes/ViewRelated/Blog/Blogging Prompts/BloggingPromptCoordinator.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
import UIKit | ||
|
||
/// Helps manage the flow related to Blogging Prompts. | ||
/// | ||
@objc class BloggingPromptCoordinator: NSObject { | ||
|
||
private let promptsServiceFactory: BloggingPromptsServiceFactory | ||
|
||
enum Errors: Error { | ||
case invalidSite | ||
case promptNotFound | ||
case unknown | ||
} | ||
|
||
/// Defines the interaction sources for Blogging Prompts. | ||
enum Source { | ||
case dashboard | ||
case featureIntroduction | ||
case actionSheetHeader | ||
case promptNotification | ||
case promptStaticNotification | ||
case unknown | ||
|
||
var editorEntryPoint: PostEditorEntryPoint { | ||
switch self { | ||
case .dashboard: | ||
return .dashboard | ||
case .featureIntroduction: | ||
return .bloggingPromptsFeatureIntroduction | ||
case .actionSheetHeader: | ||
return .bloggingPromptsActionSheetHeader | ||
case .promptNotification, .promptStaticNotification: | ||
return .bloggingPromptsNotification | ||
default: | ||
return .unknown | ||
} | ||
} | ||
} | ||
|
||
// MARK: Public Method | ||
|
||
init(bloggingPromptsServiceFactory: BloggingPromptsServiceFactory = .init()) { | ||
self.promptsServiceFactory = bloggingPromptsServiceFactory | ||
} | ||
|
||
/// Present the post creation flow to answer the prompt with `promptID`. | ||
/// | ||
/// - Note: When the `promptID` is nil, the coordinator will attempt to fetch and use today's prompt from remote. | ||
/// | ||
/// - Parameters: | ||
/// - viewController: The view controller that will present the post creation flow. | ||
/// - promptID: The ID of the blogging prompt. When nil, the method will use today's prompt. | ||
/// - blog: The blog associated with the blogging prompt. | ||
/// - completion: Closure invoked after the post creation flow is presented. | ||
func showPromptAnsweringFlow(from viewController: UIViewController, | ||
promptID: Int? = nil, | ||
blog: Blog, | ||
source: Source, | ||
completion: (() -> Void)? = nil) { | ||
fetchPrompt(with: promptID, blog: blog) { result in | ||
guard case .success(let prompt) = result else { | ||
completion?() | ||
return | ||
} | ||
|
||
// Present the post creation flow. | ||
let editor = EditPostViewController(blog: blog, prompt: prompt) | ||
editor.modalPresentationStyle = .fullScreen | ||
editor.entryPoint = source.editorEntryPoint | ||
viewController.present(editor, animated: true) | ||
completion?() | ||
} | ||
} | ||
} | ||
|
||
// MARK: Private Helpers | ||
|
||
private extension BloggingPromptCoordinator { | ||
|
||
func fetchPrompt(with localPromptID: Int? = nil, blog: Blog, completion: @escaping (Result<BloggingPrompt, Error>) -> Void) { | ||
guard let service = promptsServiceFactory.makeService(for: blog) else { | ||
completion(.failure(Errors.invalidSite)) | ||
return | ||
} | ||
|
||
// When the promptID is specified, there may be a cached prompt available. | ||
if let promptID = localPromptID, | ||
let prompt = service.loadPrompt(with: promptID, in: blog) { | ||
completion(.success(prompt)) | ||
return | ||
} | ||
|
||
// Otherwise, try to fetch today's prompt from remote. | ||
service.fetchTodaysPrompt { prompt in | ||
guard let prompt = prompt else { | ||
completion(.failure(Errors.promptNotFound)) | ||
return | ||
} | ||
completion(.success(prompt)) | ||
|
||
} failure: { error in | ||
completion(.failure(error ?? Errors.unknown)) | ||
} | ||
} | ||
|
||
} |
28 changes: 28 additions & 0 deletions
28
WordPress/Classes/ViewRelated/Blog/Blogging Prompts/WPTabBarController+BloggingPrompt.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
|
||
/// Encapsulates logic related to Blogging Prompts in WPTabBarController. | ||
/// | ||
extension WPTabBarController { | ||
|
||
@objc func makeBloggingPromptCoordinator() -> BloggingPromptCoordinator { | ||
return BloggingPromptCoordinator() | ||
} | ||
|
||
func showPromptAnsweringFlow(siteID: Int, promptID: Int?, source: BloggingPromptCoordinator.Source) { | ||
guard Feature.enabled(.bloggingPrompts), | ||
let blog = accountSites?.first(where: { $0.dotComID == NSNumber(value: siteID) }), | ||
let viewController = viewControllers?[selectedIndex] else { | ||
return | ||
} | ||
|
||
bloggingPromptCoordinator.showPromptAnsweringFlow(from: viewController, promptID: promptID, blog: blog, source: source) | ||
} | ||
|
||
} | ||
|
||
private extension WPTabBarController { | ||
|
||
var accountSites: [Blog]? { | ||
AccountService(managedObjectContext: ContextManager.shared.mainContext).defaultWordPressComAccount()?.visibleBlogs | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.