Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 32 additions & 2 deletions WordPress/Classes/Services/BloggingPromptsService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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.
///
Expand Down Expand Up @@ -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) {
Expand All @@ -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`.
///
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))")
})
}

Expand Down