diff --git a/WordPress/Classes/Services/BloggingPromptsService.swift b/WordPress/Classes/Services/BloggingPromptsService.swift index f673d2861a15..ccf81a8aa5a5 100644 --- a/WordPress/Classes/Services/BloggingPromptsService.swift +++ b/WordPress/Classes/Services/BloggingPromptsService.swift @@ -6,6 +6,7 @@ class BloggingPromptsService { private let siteID: NSNumber private let remote: BloggingPromptsServiceRemote private let calendar: Calendar = .autoupdatingCurrent + private let maxListPrompts = 11 /// A UTC date formatter that ignores time information. private static var dateFormatter: DateFormatter = { @@ -23,6 +24,12 @@ class BloggingPromptsService { loadPrompts(from: Date(), number: 1).first } + /// Convenience computed variable that returns prompts for the prompts list from local store. + /// + var localListPrompts: [BloggingPrompt] { + loadPrompts(from: listStartDate, number: maxListPrompts) + } + /// Fetches a number of blogging prompts starting from the specified date. /// When no parameters are specified, this method will attempt to return prompts from ten days ago and two weeks ahead. /// @@ -89,10 +96,29 @@ class BloggingPromptsService { /// - failure: Closure to be called when the fetch process failed. func fetchListPrompts(success: @escaping ([BloggingPrompt]) -> Void, failure: @escaping (Error?) -> Void) { - let fromDate = calendar.date(byAdding: .day, value: -9, to: Date()) ?? Date() - fetchPrompts(from: fromDate, number: 11, success: success, failure: failure) + fetchPrompts(from: listStartDate, number: maxListPrompts, success: success, failure: failure) } + /// Convenience method to obtain the blogging prompts for the prompts list, + /// either from local cache or remote. + /// + /// - Parameters: + /// - success: Closure to be called when the fetch process succeeded. + /// - failure: Closure to be called when the fetch process failed. + func listPrompts(success: @escaping ([BloggingPrompt]) -> Void, + failure: @escaping (Error?) -> Void) { + + // If there aren't maxListPrompts cached, need to fetch more. + guard localListPrompts.count < maxListPrompts else { + success(localListPrompts) + return + } + + fetchListPrompts(success: success, failure: failure) + } + + // MARK: - Init + required init?(contextManager: CoreDataStack = ContextManager.shared, remote: BloggingPromptsServiceRemote? = nil, blog: Blog? = nil) { @@ -115,6 +141,10 @@ private extension BloggingPromptsService { calendar.date(byAdding: .day, value: -10, to: Date()) ?? Date() } + var listStartDate: Date { + calendar.date(byAdding: .day, value: -(maxListPrompts - 2), to: Date()) ?? Date() + } + /// Converts the given date to UTC and ignores the time information. /// Example: Given `2022-05-01 03:00:00 UTC-5`, this should return `2022-05-01 00:00:00 UTC`. /// diff --git a/WordPress/Classes/ViewRelated/Blog/Blogging Prompts/BloggingPromptsViewController.swift b/WordPress/Classes/ViewRelated/Blog/Blogging Prompts/BloggingPromptsViewController.swift index 94fc074836b5..d285c7ed02ee 100644 --- a/WordPress/Classes/ViewRelated/Blog/Blogging Prompts/BloggingPromptsViewController.swift +++ b/WordPress/Classes/ViewRelated/Blog/Blogging Prompts/BloggingPromptsViewController.swift @@ -106,8 +106,6 @@ private extension BloggingPromptsViewController { } func fetchPrompts() { - // TODO: show cached prompts first. - guard let bloggingPromptsService = bloggingPromptsService else { DDLogError("Failed creating BloggingPromptsService instance.") showErrorView() @@ -116,7 +114,7 @@ private extension BloggingPromptsViewController { isLoading = true - bloggingPromptsService.fetchListPrompts(success: { [weak self] (prompts) in + bloggingPromptsService.listPrompts(success: { [weak self] (prompts) in self?.isLoading = false self?.prompts = prompts.sorted(by: { $0.date.compare($1.date) == .orderedDescending }) }, failure: { [weak self] (error) in diff --git a/WordPress/Classes/ViewRelated/System/Floating Create Button/CreateButtonCoordinator.swift b/WordPress/Classes/ViewRelated/System/Floating Create Button/CreateButtonCoordinator.swift index f0e4f9fa2b28..8463bc423b16 100644 --- a/WordPress/Classes/ViewRelated/System/Floating Create Button/CreateButtonCoordinator.swift +++ b/WordPress/Classes/ViewRelated/System/Floating Create Button/CreateButtonCoordinator.swift @@ -309,16 +309,16 @@ private extension CreateButtonCoordinator { // TODO: check for cached prompt first. guard let bloggingPromptsService = bloggingPromptsService else { - DDLogError("FAB > failed creating BloggingPromptsService instance.") + DDLogError("FAB: failed creating BloggingPromptsService instance.") prompt = nil return } - bloggingPromptsService.fetchTodaysPrompt(success: { [weak self] (prompt) in + bloggingPromptsService.todaysPrompt(success: { [weak self] (prompt) in self?.prompt = prompt }, failure: { [weak self] (error) in self?.prompt = nil - DDLogError("FAB > failed fetching blogging prompt: \(String(describing: error))") + DDLogError("FAB: failed fetching blogging prompt: \(String(describing: error))") }) }