Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ class AppDataRepository {
fun getRecommendations(): List<AppData> {
return emptyList()
}

fun getContinuationData(): List<AppData> {
return emptyList()
}
}

@SuppressLint("UseKtx")
Expand Down Expand Up @@ -62,11 +66,28 @@ class ClusterRequestFactory(context: Context) {

val items = appDataRepository.getRecommendations()
val recommendationCluster = com.google.android.engage.common.datamodel.RecommendationCluster.Builder()
.setTitle("Recommended Content") // Required field

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The title string "Recommended Content" is hardcoded. To support internationalization (i18n) and localization, it is recommended to retrieve this string from the application resources using context.getString().

Suggested change
.setTitle("Recommended Content") // Required field
.setTitle(context.getString(R.string.recommended_content)) // Required field

.setRecommendationClusterType(com.google.android.engage.common.datamodel.RecommendationClusterType.TYPE_TOP_PICKS_FOR_YOU) // Required field
for (item in items) {
recommendationCluster.addEntity(ItemToEntityConverter.convert(item))
}
return com.google.android.engage.service.PublishRecommendationClustersRequest.Builder()
.addRecommendationCluster(recommendationCluster.build())
.setAccountProfile(accountProfile) // Set the account profile on the request for personalization/sync
.build()
}

fun constructContinuationClusterRequest(): com.google.android.engage.service.PublishContinuationClusterRequest {
val items = appDataRepository.getContinuationData()

val continuationCluster = com.google.android.engage.common.datamodel.ContinuationCluster.Builder()
.setAccountProfile(accountProfile) // Set the account profile on the request for personalization/sync

for (item in items) {
continuationCluster.addEntity(ItemToEntityConverter.convert(item))
}
return com.google.android.engage.service.PublishContinuationClusterRequest.Builder()
.setContinuationCluster(continuationCluster.build())
.build()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ object Constants {
const val PUBLISH_TYPE_KEY = "PUBLISH_TYPE"
const val PUBLISH_TYPE_RECOMMENDATIONS = "RECOMMENDATIONS"
const val PUBLISH_TYPE_FEATURED = "FEATURED"
// const val PUBLISH_TYPE_CONTINUATION = "CONTINUATION"
const val PUBLISH_TYPE_CONTINUATION = "CONTINUATION"
// ...
const val PUBLISH_TYPE_USER_ACCOUNT_MANAGEMENT = "USER_ACCOUNT_MANAGEMENT"
// const val PUBLISH_TYPE_FOOD_SHOPPING_CARD = "FOOD_SHOPPING_CARD"
Expand Down
24 changes: 23 additions & 1 deletion misc/src/main/java/com/example/snippets/engage/EngageWorker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class EngageWorker(context: Context, workerParams: WorkerParameters) : Coroutine
return when (publishType) {
Constants.PUBLISH_TYPE_RECOMMENDATIONS -> publishRecommendations()
// Constants.PUBLISH_TYPE_FEATURED -> publishFeatured()
// Constants.PUBLISH_TYPE_CONTINUATION-> publishContinuation()
Constants.PUBLISH_TYPE_CONTINUATION -> publishContinuation()
Comment thread
shashvatgupta-49 marked this conversation as resolved.
Constants.PUBLISH_TYPE_USER_ACCOUNT_MANAGEMENT -> publishUserAccountManagement()
else -> Result.failure()
}
Expand All @@ -74,6 +74,21 @@ class EngageWorker(context: Context, workerParams: WorkerParameters) : Coroutine
return publishAndProvideResult(publishTask)
}

private suspend fun publishContinuation(): Result {
// Empty Continuation Guard: If there is no continuation content,
// we must delete the cluster instead of publishing an empty one on the UI.
if (getContinuationData().isEmpty()) {
val deleteTask = client.deleteContinuationCluster()
return publishAndProvideResult(deleteTask)
}

val publishTask: Task<Void> =
client.publishContinuationCluster(
clusterRequestFactory.constructContinuationClusterRequest()
Comment thread
shashvatgupta-49 marked this conversation as resolved.
)
return publishAndProvideResult(publishTask)
}

private suspend fun publishUserAccountManagement(): Result {
val publishTask: Task<Void>
if (isAccountSignedIn()) {
Expand Down Expand Up @@ -105,6 +120,13 @@ class EngageWorker(context: Context, workerParams: WorkerParameters) : Coroutine
// [END_EXCLUDE]
}

private fun getContinuationData(): List<Any> {
// Implement your app's data loading logic here.
// [START_EXCLUDE]
return emptyList()
// [END_EXCLUDE]
}

private suspend fun publishAndProvideResult(
publishTask: Task<Void>
): Result {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,19 @@ object ItemToEntityConverter {
}
}
// [END android_engage_item_to_entity_converter_implementation]

// [START android_engage_dual_content_rating_example]
fun convertMovie(movie: MovieData): MovieEntity {
val ratingSystem = RatingSystem.Builder()
.setAgencyName("MPAA")
.setRating("PG-13")
.build()
return MovieEntity.Builder()
.setEntityId(movie.id)
.setName(movie.title)
// ... other fields
.addContentRating(ratingSystem) // Recommended API

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to confirm, you want both addContentRating and addContentRatingLegacy to be called on the same builder?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. we do.

.addContentRatingsLegacy(listOf("MPAA:PG-13")) // Legacy API for backward compatibility
.build()
}
// [END android_engage_dual_content_rating_example]
Loading