Skip to content

Commit 3d01a76

Browse files
MOB-5193 HP4U Data API
1 parent 398cef2 commit 3d01a76

16 files changed

+371
-41
lines changed

README.md

Lines changed: 65 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## Preface
44

5-
This project showcases how to integrate Taboola's SDK on Android and use its HomePage capabilities.
5+
This project showcases how to integrate Taboola's SDK on Android and use its HomePage capabilities.
66

77
### Initialization
88

@@ -38,13 +38,27 @@ The parameters you need to pass in are:
3838

3939
2. Next, it is advised to call `fetchContent` as soon as you can so that content will be loaded into the HomePage instance
4040

41-
homePage.fetchContent();
41+
- Home page Swapping approach:
42+
`homePage.fetchContent();`
43+
44+
- Home page Data API approach:
45+
`homePage.fetchContent(object : TBLFetchContentCallback {
46+
override fun onComplete(isHomePageEnabled: Boolean, homePageDataSource: TBLHomePageDataSource) {
47+
// Save recommendations to your data structure
48+
}
49+
override fun onFailure(error: String) {
50+
// Handle the failure
51+
}
52+
});
53+
`
4254

4355
### Swap Articles
4456

45-
To swap articles with content from Taboola,
46-
call the `shouldSwapItemInSection` function in your OnBind methd to get the swapped item's content.
47-
It returns True if the item was swapped, False if it wasn't.
57+
- Home page Swapping approach:
58+
59+
To swap articles with content from Taboola,
60+
call the `shouldSwapItemInSection` function in your OnBind methd to get the swapped item's content.
61+
It returns True if the item was swapped, False if it wasn't.
4862

4963

5064
public boolean shouldSwapItemInSection(
@@ -56,20 +70,54 @@ It returns True if the item was swapped, False if it wasn't.
5670
@Nullable final ImageView thumbnailView,
5771
@Nullable AdditionalViews additionalViews)
5872

59-
The parameters you need to pass in are:
60-
- linePosition: of the cell
61-
- sectionName: representing section
62-
- lineView: view of the cell
63-
- titleView: UI element representing the title of the cell
64-
- contentView: UI element representing the description of the cell
65-
- thumbnailView: UI element representing the image of the cell
66-
- additionalView(optional): UI element representing the all other view in the lineView which aren’t mandatory
73+
The parameters you need to pass in are:
74+
- linePosition: of the cell
75+
- sectionName: representing section
76+
- lineView: view of the cell
77+
- titleView: UI element representing the title of the cell
78+
- contentView: UI element representing the description of the cell
79+
- thumbnailView: UI element representing the image of the cell
80+
- additionalView(optional): UI element representing the all other view in the lineView which aren’t mandatory
81+
82+
- Home page Data API approach:
83+
84+
Invoke `shouldSwapItemInSectionDataApi` for every item in your list after receiving the
85+
recommendations (and only if the home page is enabled).
86+
Invoking this method for all of your items (including those that will not be swapped) is necessary
87+
for Taboola to maintain an accurate map of sections and positions.
88+
The boolean value returned by `shouldSwapItemInSectionDataApi` (true or false) indicates whether
89+
the item should be swapped:
90+
If `true`: Replace the current list item with the available item from Toboola's recommendations list.
91+
If `false`: Invoke addClickUrlForDuplicationCheck to allow Taboola to track if this item is duplicated elsewhere on the homepage.
92+
93+
public boolean shouldSwapItemInSectionDataApi(
94+
String sectionName,
95+
int linePosition,
96+
int sectionStartPosition)
97+
98+
The parameters you need to pass in are:
99+
- sectionName: representing section
100+
- linePosition: of the cell
101+
- sectionStartPosition: position of the first item in the section
67102

68103
#### How does the swapping take place?
69-
When you call `shouldSwapItemInSection`, Taboola verifies that this item is allowed to be swapped and validates the fields of the content, then performs a swap with a recommendation.
70-
Taboola will handle the views the publisher desires to swap.
71-
It will validate the fields of the content and the swapped content as well.
72-
After a successful validation - Taboola will swap the publisher’s content with Taboola recommendations, and return a boolean that indicates if the swapping process did occur.
104+
105+
- Home page Swapping approach:
106+
107+
When you call `shouldSwapItemInSection`, Taboola verifies that this item is allowed to be swapped and validates the fields of the content, then performs a swap with a recommendation.
108+
Taboola will handle the views the publisher desires to swap.
109+
It will validate the fields of the content and the swapped content as well.
110+
After a successful validation - Taboola will swap the publisher’s content with Taboola recommendations, and return a boolean that indicates if the swapping process did occur.
111+
112+
- Home page Data API approach:
113+
114+
After calling `shouldSwapItemInSectionDataApi`, you must report the result calling `reportSwapDataApi'
115+
116+
If swap succeeds: Pass `true` for `itemHasBeenSwapped`
117+
`homePage.reportSwapDataApi(sectionName, position, true);`
118+
119+
If swap fails: Pass `false` for `itemHasBeenSwapped`
120+
`homePage.reportSwapDataApi(sectionName, position, false);`
73121

74122
### Additional HomePage functionality
75123

app/build.gradle

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ plugins {
44
}
55

66
android {
7-
compileSdk 32
7+
compileSdk 34
88

99
defaultConfig {
1010
applicationId "com.taboola.hp4udemoapplication"
1111
minSdk 21
12-
targetSdk 32
12+
targetSdk 34
1313
versionCode 1
1414
versionName "1.0"
1515

@@ -35,6 +35,7 @@ android {
3535
kotlinOptions {
3636
jvmTarget = '1.8'
3737
}
38+
namespace 'com.taboola.hp4udemoapplication'
3839
}
3940

4041
dependencies {
@@ -50,7 +51,7 @@ dependencies {
5051
implementation 'androidx.core:core-splashscreen:1.0.0-rc01'
5152
implementation 'com.squareup.picasso:picasso:2.8'
5253
//Taboola SDK
53-
implementation 'com.taboola:android-sdk:3.8.13'
54+
implementation 'com.taboola:android-sdk:4.0.17'
5455

5556
testImplementation 'junit:junit:4.13.2'
5657
androidTestImplementation 'androidx.test.ext:junit:1.1.3'

app/src/main/java/com/taboola/hp4udemoapplication/adapters/articles/ArticleDiffCallback.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class ArticleDiffCallback(
2626
} else {
2727
((oldItem as Article).title == (newItem as Article).title
2828
&& oldItem.content == newItem.content
29-
&& oldItem.imageUrl == newItem.imageUrl
29+
&& oldItem.imageResourceId == newItem.imageResourceId
3030
&& oldItem.url == newItem.url
3131
&& oldItem.category == newItem.category)
3232
}

app/src/main/java/com/taboola/hp4udemoapplication/adapters/articles/HomePageAdapter.kt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import com.taboola.hp4udemoapplication.model.Header
1313

1414
class HomePageAdapter(
1515
private var homePage: TBLHomePage?,
16+
private val isHomePageDataApiMode: Boolean,
1617
private val onItemClickListener: HomePageItemClickListener
1718
) :
1819
RecyclerView.Adapter<RecyclerView.ViewHolder>() {
@@ -47,7 +48,7 @@ class HomePageAdapter(
4748
return mainItemViewHolder
4849
}
4950
DEFAULT_ARTICLE -> {
50-
val viewHolder = HomePageItemViewHolder(view)
51+
val viewHolder = if (isHomePageDataApiMode) HomePageDataApiItemViewHolder(view) else HomePageItemViewHolder(view)
5152
view.setOnClickListener {
5253
val url = (data[viewHolder.adapterPosition] as Article).url
5354
onItemClickListener.onClick(url)
@@ -65,11 +66,15 @@ class HomePageAdapter(
6566
(holder as MainHomePageItemViewHolder).onBind(data[position] as Article)
6667
}
6768
DEFAULT_ARTICLE -> if (data[position] is Article) {
68-
(holder as HomePageItemViewHolder).onBind(
69+
(holder as? HomePageItemViewHolder)?.onBind(
6970
homePage,
7071
position,
7172
data[position] as Article
7273
)
74+
75+
(holder as? HomePageDataApiItemViewHolder)?.onBind(
76+
data[position] as Article
77+
)
7378
}
7479
HEADER -> if (data[position] is Header) {
7580
(holder as HeaderViewHolder).onBind(data[position] as Header)
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.taboola.hp4udemoapplication.adapters.articles
2+
3+
import android.view.View
4+
import android.widget.ImageView
5+
import android.widget.TextView
6+
import androidx.core.view.isVisible
7+
import androidx.recyclerview.widget.RecyclerView
8+
import com.squareup.picasso.Picasso
9+
import com.taboola.hp4udemoapplication.R
10+
import com.taboola.hp4udemoapplication.model.Article
11+
import com.taboola.hp4udemoapplication.view.AnimatedBackgroundTextView
12+
13+
class HomePageDataApiItemViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
14+
15+
private val title: TextView?
16+
get() = itemView.findViewById(R.id.title)
17+
18+
private val content: TextView?
19+
get() = itemView.findViewById(R.id.content)
20+
21+
private val image: ImageView?
22+
get() = itemView.findViewById(R.id.image)
23+
24+
private val animatedBackgroundTextView: AnimatedBackgroundTextView?
25+
get() = itemView.findViewById(R.id.swapped_indication)
26+
27+
fun onBind(article: Article) {
28+
animatedBackgroundTextView?.isVisible = article.isSwapped
29+
title?.text = article.title
30+
content?.text = article.content
31+
if (article.isSwapped) {
32+
Picasso.get().load(article.url).into(image)
33+
} else {
34+
Picasso.get().load(article.imageResourceId).into(image)
35+
}
36+
}
37+
}

app/src/main/java/com/taboola/hp4udemoapplication/adapters/articles/HomePageItemViewHolder.kt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,14 @@ class HomePageItemViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView)
3131
itemView,
3232
title,
3333
content,
34-
image,
35-
null
34+
image
3635
)
3736
if (swappedPerformed == null || !swappedPerformed) {
3837
animatedBackgroundTextView?.visibility = View.GONE
3938
title?.text = article.title
4039
content?.text = article.content
41-
if (article.imageUrl != 0) {
42-
Picasso.get().load(article.imageUrl).into(image)
40+
if (article.imageResourceId != 0) {
41+
Picasso.get().load(article.imageResourceId).into(image)
4342
}
4443
} else {
4544
animatedBackgroundTextView?.visibility = View.VISIBLE

app/src/main/java/com/taboola/hp4udemoapplication/adapters/articles/MainHomePageItemViewHolder.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ class MainHomePageItemViewHolder(itemView: View) : RecyclerView.ViewHolder(itemV
2222
fun onBind(article: Article) {
2323
title?.text = article.title
2424
content?.text = article.content
25-
if (article.imageUrl != 0) {
26-
Picasso.get().load(article.imageUrl).into(image)
25+
if (article.imageResourceId != 0) {
26+
Picasso.get().load(article.imageResourceId).into(image)
2727
}
2828
}
2929
}

app/src/main/java/com/taboola/hp4udemoapplication/model/Article.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ package com.taboola.hp4udemoapplication.model
33
data class Article(
44
var title: String,
55
var content: String,
6-
var imageUrl: Int,
6+
var imageResourceId: Int,
77
var url: String,
88
var category: String,
9-
var sectionName: String
10-
) : BaseItem(ARTICLE_TYPE)
9+
var sectionName: String,
10+
var isSwapped: Boolean = false
11+
) : BaseItem(ARTICLE_TYPE)

0 commit comments

Comments
 (0)