-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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 #20944 from wordpress-mobile/feature/blaze-campaig…
…ns-integrate-api Blaze Manage Campaigns: Integrate the API
- Loading branch information
Showing
13 changed files
with
352 additions
and
52 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
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
38 changes: 30 additions & 8 deletions
38
WordPress/Classes/ViewRelated/Blog/Blog Dashboard/Cards/Blaze/DashboardBlazeCardCell.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
74 changes: 74 additions & 0 deletions
74
...Classes/ViewRelated/Blog/Blog Dashboard/Cards/Blaze/DashboardBlazeCardCellViewModel.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,74 @@ | ||
import Foundation | ||
import WordPressKit | ||
|
||
final class DashboardBlazeCardCellViewModel { | ||
private(set) var state: State = .promo | ||
|
||
private let blog: Blog | ||
private let service: BlazeServiceProtocol? | ||
private let store: DashboardBlazeStoreProtocol | ||
private var isRefreshing = false | ||
private let isBlazeCampaignsFlagEnabled: () -> Bool | ||
|
||
enum State { | ||
/// Showing "Promote you content with Blaze" promo card. | ||
case promo | ||
/// Showing the latest Blaze campaign. | ||
case campaign(BlazeCampaignViewModel) | ||
} | ||
|
||
var onRefresh: ((DashboardBlazeCardCellViewModel) -> Void)? | ||
|
||
init(blog: Blog, | ||
service: BlazeServiceProtocol? = BlazeService(), | ||
store: DashboardBlazeStoreProtocol = BlogDashboardPersistence(), | ||
isBlazeCampaignsFlagEnabled: @escaping () -> Bool = { RemoteFeatureFlag.blazeManageCampaigns.enabled() }) { | ||
self.blog = blog | ||
self.service = service | ||
self.store = store | ||
self.isBlazeCampaignsFlagEnabled = isBlazeCampaignsFlagEnabled | ||
|
||
if isBlazeCampaignsFlagEnabled(), | ||
let blogID = blog.dotComID?.intValue, | ||
let campaign = store.getBlazeCampaign(forBlogID: blogID) { | ||
self.state = .campaign(BlazeCampaignViewModel(campaign: campaign)) | ||
} | ||
|
||
NotificationCenter.default.addObserver(self, selector: #selector(refresh), name: .blazeCampaignCreated, object: nil) | ||
} | ||
|
||
@objc func refresh() { | ||
guard isBlazeCampaignsFlagEnabled() else { | ||
return // Continue showing the default `Promo` card | ||
} | ||
|
||
guard !isRefreshing, let service else { return } | ||
isRefreshing = true | ||
|
||
service.getRecentCampaigns(for: blog) { [weak self] in | ||
self?.didRefresh(with: $0) | ||
} | ||
} | ||
|
||
private func didRefresh(with result: Result<BlazeCampaignsSearchResponse, Error>) { | ||
if case .success(let response) = result { | ||
let campaign = response.campaigns?.first | ||
if let blogID = blog.dotComID?.intValue { | ||
store.setBlazeCampaign(campaign, forBlogID: blogID) | ||
} | ||
if let campaign { | ||
state = .campaign(BlazeCampaignViewModel(campaign: campaign)) | ||
} else { | ||
state = .promo | ||
} | ||
} | ||
|
||
isRefreshing = false | ||
onRefresh?(self) | ||
} | ||
} | ||
|
||
protocol DashboardBlazeStoreProtocol { | ||
func getBlazeCampaign(forBlogID blogID: Int) -> BlazeCampaign? | ||
func setBlazeCampaign(_ campaign: BlazeCampaign?, forBlogID blogID: Int) | ||
} |
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.