Skip to content

Commit

Permalink
make functions suspendable and add docs
Browse files Browse the repository at this point in the history
  • Loading branch information
fcs-ts committed Sep 13, 2024
1 parent d1ba92b commit 096ced6
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,31 @@ import com.topsort.analytics.model.Placement
import com.topsort.analytics.model.auctions.EntityType


/**
* View for displaying banners powered by auctions.
*
* @constructor The constructor is meant to be called automatically from XML inflation.
* You can add this view to your layout by using a `com.topsort.analytics.banners.BannerView` element.
*
* @param context
* @param attrs AttributeSet for the view. Since this view inherits from `ImageView`
* you can set attributes as you would with a regular `ImageView`.
*/
class BannerView(
context: Context,
attrs: AttributeSet
) : ImageView(context, attrs) {

fun setup(
/**
* Setup the banner in the view by running an auction in the background.
*
* @param config a BannerConfig object that specifies the parameters for the auction
* @param path identifier for the activity where the banner is displayed. It's recommended to be the deeplink for the view.
* @param location optional name for the location within the view where the banner is displayed.
* @param onClick callback for when the banner is clicked. Usually this should navigate to an activity related to the banner (e.g. the product page for the product shown in the banner).
* @receiver
*/
suspend fun setup(
config: BannerConfig,
path: String,
location: String?,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,28 @@ package com.topsort.analytics.banners

import com.topsort.analytics.model.auctions.Auction
import com.topsort.analytics.model.auctions.AuctionRequest
import com.topsort.analytics.model.auctions.AuctionResponse
import com.topsort.analytics.service.TopsortAuctionsHttpService
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

/**
* Run a banner auction with a single slot
*
* @param config the banner configuration that specifies which kind of banner auction to run
* @return A BannerResponse if the auction successfully returned a winner or null if not.
*/
fun runBannerAuction(config: BannerConfig): BannerResponse? {
suspend fun runBannerAuction(config: BannerConfig): BannerResponse? {
val auction = buildBannerAuction(config)
val request = AuctionRequest(listOf(auction))
val response = TopsortAuctionsHttpService.runAuctions(request)
var response: AuctionResponse? = null;
val auctionJob = CoroutineScope(Dispatchers.IO).launch {
response = TopsortAuctionsHttpService.runAuctions(request)
}
if ((response?.results?.isNotEmpty() == true)) {
if (response.results[0].winners.isNotEmpty()) {
val winner = response.results[0].winners[0]
if (response!!.results[0].winners.isNotEmpty()) {
val winner = response!!.results[0].winners[0]
return BannerResponse(
id = winner.id,
url = winner.asset!!.url,
Expand Down
12 changes: 6 additions & 6 deletions app/src/main/java/com/topsort/example/SampleActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ class SampleActivity : AppCompatActivity() {
{ id, entityType -> onBannerClick(id, entityType) })
}

//reportPurchaseWithResolvedBidId()
//reportClickWithResolvedBidId()
//reportImpressionWithResolvedBidId()
reportPurchaseWithResolvedBidId()
reportClickWithResolvedBidId()
reportImpressionWithResolvedBidId()

//reportPurchase()
//reportClick()
//reportImpression()
reportPurchase()
reportClick()
reportImpression()
}


Expand Down

0 comments on commit 096ced6

Please sign in to comment.