-
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 #8 from Topsort/banner.swift
feat(banners): add banner swift component
- Loading branch information
Showing
8 changed files
with
279 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import Foundation | ||
import SwiftUI | ||
import Topsort_Analytics | ||
|
||
public struct TopsortBanner: View { | ||
@ObservedObject var sharedValues = SharedValues() | ||
|
||
var width: CGFloat | ||
var height: CGFloat | ||
var buttonClickedAction: (AuctionResponse?) -> Void | ||
var analytics: AnalyticsProtocol | ||
|
||
public init( | ||
apiKey: String, | ||
url: String, | ||
width: CGFloat, | ||
height: CGFloat, | ||
slotId: String, | ||
deviceType: String, | ||
buttonClickedAction: @escaping (AuctionResponse?) -> Void, | ||
analytics: AnalyticsProtocol = Analytics.shared | ||
) { | ||
Analytics.shared.configure(apiKey: apiKey, url: url) | ||
self.width = width | ||
self.height = height | ||
self.buttonClickedAction = buttonClickedAction | ||
self.analytics = analytics | ||
|
||
self.run(deviceType: deviceType, slotId: slotId) | ||
} | ||
|
||
private func run(deviceType: String, slotId: String) { | ||
Task { | ||
await self.executeAuctions(deviceType: deviceType, slotId: slotId) | ||
} | ||
} | ||
|
||
internal func executeAuctions(deviceType: String, slotId: String) async { | ||
let auction: Auction = Auction(type: "banners", slots: 1, slotId: slotId, device: deviceType) | ||
let response = await analytics.executeAuctions(auctions: [auction]) | ||
|
||
sharedValues.response = response | ||
sharedValues.setResolvedBidIdAndUrlFromResponse() | ||
sharedValues.loading = false | ||
|
||
let event = Event(resolvedBidId: sharedValues.resolvedBidId!, occurredAt: Date.now) | ||
analytics.track(impression: event) | ||
} | ||
|
||
private func buttonClicked() async { | ||
let event = Event(resolvedBidId: sharedValues.resolvedBidId!, occurredAt: Date.now) | ||
analytics.track(click: event) | ||
self.buttonClickedAction(sharedValues.response) | ||
} | ||
|
||
public var body: some View { | ||
VStack { | ||
if sharedValues.loading { | ||
ProgressView() | ||
} else { | ||
Button(action: { | ||
Task { | ||
await self.buttonClicked() | ||
} | ||
}) { | ||
AsyncImage(url: URL(string: sharedValues.urlString!)) | ||
.frame(width: self.width, height: self.height) | ||
.clipped() | ||
} | ||
.frame(width: self.width, height: self.height) | ||
} | ||
} | ||
} | ||
} |
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,35 @@ | ||
import Foundation | ||
import Topsort_Analytics | ||
|
||
class SharedValues: ObservableObject { | ||
@Published var resolvedBidId: String? | ||
@Published var loading: Bool | ||
@Published var urlString: String? | ||
@Published var response: AuctionResponse? | ||
|
||
init() { | ||
resolvedBidId = nil | ||
loading = true | ||
urlString = nil | ||
response = nil | ||
} | ||
|
||
public func setResolvedBidIdAndUrlFromResponse() { | ||
guard let response = self.response else { | ||
return | ||
} | ||
let results = response.results | ||
|
||
guard let winners = results.first?.winners else { | ||
return | ||
} | ||
guard let winner = winners.first else { | ||
return | ||
} | ||
guard let asset = winner.asset?.first else { | ||
return | ||
} | ||
resolvedBidId = winner.resolvedBidId | ||
urlString = asset.url | ||
} | ||
} |
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,60 @@ | ||
import XCTest | ||
@testable import TopsortBanners | ||
@testable import Topsort_Analytics | ||
|
||
class TopsortBannerTests: XCTestCase { | ||
|
||
func testTopsortBannerInitialization() { | ||
let expectation = self.expectation(description: "Button clicked action") | ||
let banner = TopsortBanner( | ||
apiKey: "test_api_key", | ||
url: "test_url", | ||
width: 300, | ||
height: 250, | ||
slotId: "test_slot_id", | ||
deviceType: "test_device_type" | ||
) { response in | ||
expectation.fulfill() | ||
} | ||
|
||
XCTAssertEqual(banner.width, 300) | ||
XCTAssertEqual(banner.height, 250) | ||
|
||
banner.buttonClickedAction(nil) | ||
|
||
wait(for: [expectation], timeout: 1.0) | ||
} | ||
|
||
func testExecuteAuctions() async { | ||
// Mock the response | ||
let asset = Asset(url: "https://example.com") | ||
let winner = Winner(rank: 1, asset: [asset], type: "type", id: "id", resolvedBidId: "resolved_bid_id") | ||
let auctionResult = AuctionResult(resultType: "result_type", winners: [winner], error: false) | ||
let auctionResponse = AuctionResponse(results: [auctionResult]) | ||
|
||
// Mock Analytics and response | ||
let mockAnalytics = MockAnalytics() | ||
mockAnalytics.executeAuctionsMockResponse = auctionResponse | ||
|
||
let banner = await TopsortBanner( | ||
apiKey: "test_api_key", | ||
url: "test_url", | ||
width: 300, | ||
height: 250, | ||
slotId: "test_slot_id", | ||
deviceType: "test_device_type", | ||
buttonClickedAction: { response in | ||
}, | ||
analytics: mockAnalytics | ||
) | ||
|
||
// Execute the method | ||
await banner.executeAuctions(deviceType: "test_device_type", slotId: "test_slot_id") | ||
|
||
await MainActor.run { | ||
XCTAssertEqual(banner.sharedValues.resolvedBidId, "resolved_bid_id") | ||
XCTAssertEqual(banner.sharedValues.urlString, "https://example.com") | ||
XCTAssertFalse(banner.sharedValues.loading) | ||
} | ||
} | ||
} |
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,32 @@ | ||
import Foundation | ||
import XCTest | ||
@testable import Topsort_Analytics | ||
|
||
public class MockAnalytics: AnalyticsProtocol { | ||
public var opaqueUserId: String = "mocked-opaque-user-id" | ||
public var executeAuctionsMockResponse: AuctionResponse? | ||
|
||
public func set(opaqueUserId: String?) { | ||
// Mock implementation | ||
} | ||
|
||
public func configure(apiKey: String, url: String? = nil) { | ||
// Mock implementation | ||
} | ||
|
||
public func track(impression event: Event) { | ||
// Mock implementation | ||
} | ||
|
||
public func track(click event: Event) { | ||
// Mock implementation | ||
} | ||
|
||
public func track(purchase event: PurchaseEvent) { | ||
// Mock implementation | ||
} | ||
|
||
public func executeAuctions(auctions: [Auction]) async -> AuctionResponse? { | ||
return executeAuctionsMockResponse | ||
} | ||
} |
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,29 @@ | ||
import XCTest | ||
@testable import TopsortBanners | ||
@testable import Topsort_Analytics | ||
|
||
class SharedValuesTests: XCTestCase { | ||
|
||
func testSharedValuesInitialization() { | ||
let sharedValues = SharedValues() | ||
|
||
XCTAssertNil(sharedValues.resolvedBidId) | ||
XCTAssertTrue(sharedValues.loading) | ||
XCTAssertNil(sharedValues.urlString) | ||
XCTAssertNil(sharedValues.response) | ||
} | ||
|
||
func testSetResolvedBidIdAndUrlFromResponse() { | ||
let asset = Asset(url: "https://example.com") | ||
let winner = Winner(rank: 1, asset: [asset], type: "type", id: "id", resolvedBidId: "resolved_bid_id") | ||
let auctionResult = AuctionResult(resultType: "result_type", winners: [winner], error: false) | ||
let auctionResponse = AuctionResponse(results: [auctionResult]) | ||
|
||
let sharedValues = SharedValues() | ||
sharedValues.response = auctionResponse | ||
sharedValues.setResolvedBidIdAndUrlFromResponse() | ||
|
||
XCTAssertEqual(sharedValues.resolvedBidId, "resolved_bid_id") | ||
XCTAssertEqual(sharedValues.urlString, "https://example.com") | ||
} | ||
} |