-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f1d0b52
commit f31f1db
Showing
39 changed files
with
759 additions
and
21 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
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
58 changes: 58 additions & 0 deletions
58
...eAdsExample/app/src/main/java/com/yandex/ads/sample/adfoxcarousel/AdfoxCarouselAdapter.kt
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,58 @@ | ||
/* | ||
* This file is a part of the Yandex Advertising Network | ||
* | ||
* Version for Android (C) 2022 YANDEX | ||
* | ||
* You may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at https://legal.yandex.com/partner_ch/ | ||
*/ | ||
|
||
package com.yandex.ads.sample.adfoxcarousel | ||
|
||
import android.util.Log | ||
import android.view.LayoutInflater | ||
import android.view.ViewGroup | ||
import androidx.recyclerview.widget.RecyclerView | ||
import com.yandex.ads.sample.databinding.AdfoxCarouselItemBinding | ||
import com.yandex.mobile.ads.nativeads.NativeAd | ||
import com.yandex.mobile.ads.nativeads.NativeAdException | ||
import com.yandex.mobile.ads.nativeads.NativeAdViewBinder | ||
|
||
class AdfoxCarouselAdapter : RecyclerView.Adapter<NativeAdViewBinderHolder>() { | ||
|
||
private var nativeAds: List<NativeAd> = listOf() | ||
|
||
fun setData(nativeAds: List<NativeAd>) { | ||
this.nativeAds = nativeAds | ||
notifyDataSetChanged() | ||
} | ||
|
||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): NativeAdViewBinderHolder { | ||
val binding = AdfoxCarouselItemBinding.inflate( | ||
LayoutInflater.from(parent.context), parent, false | ||
) | ||
return NativeAdViewBinderHolder(binding) | ||
} | ||
|
||
override fun onBindViewHolder(holder: NativeAdViewBinderHolder, position: Int) { | ||
val nativeAd = nativeAds[position] | ||
bindNativeAd(nativeAd, holder.nativeAdViewBinder) | ||
} | ||
|
||
override fun getItemCount(): Int { | ||
return nativeAds.size | ||
} | ||
|
||
private fun bindNativeAd(nativeAd: NativeAd, viewBinder: NativeAdViewBinder) { | ||
try { | ||
nativeAd.bindNativeAd(viewBinder) | ||
} catch (exception: NativeAdException) { | ||
Log.d(VIEW_PAGER_TAG, "${exception.message}") | ||
} | ||
} | ||
|
||
companion object { | ||
|
||
private const val VIEW_PAGER_TAG = "ViewPagerAdapter" | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
...Example/app/src/main/java/com/yandex/ads/sample/adfoxcarousel/NativeAdViewBinderHolder.kt
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,40 @@ | ||
/* | ||
* This file is a part of the Yandex Advertising Network | ||
* | ||
* Version for Android (C) 2022 YANDEX | ||
* | ||
* You may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at https://legal.yandex.com/partner_ch/ | ||
*/ | ||
|
||
package com.yandex.ads.sample.adfoxcarousel | ||
|
||
import androidx.recyclerview.widget.RecyclerView | ||
import com.yandex.ads.sample.databinding.AdfoxCarouselItemBinding | ||
import com.yandex.mobile.ads.nativeads.NativeAdViewBinder | ||
|
||
class NativeAdViewBinderHolder(binding: AdfoxCarouselItemBinding) : RecyclerView.ViewHolder(binding.root) { | ||
|
||
val nativeAdViewBinder: NativeAdViewBinder = createNativeAdViewBinder(binding) | ||
|
||
private fun createNativeAdViewBinder(binding: AdfoxCarouselItemBinding): NativeAdViewBinder { | ||
return binding.nativeAd.run { | ||
NativeAdViewBinder.Builder(root) | ||
.setAgeView(age) | ||
.setBodyView(body) | ||
.setCallToActionView(callToAction) | ||
.setDomainView(domain) | ||
.setFaviconView(favicon) | ||
.setFeedbackView(feedback) | ||
.setIconView(icon) | ||
.setMediaView(media) | ||
.setPriceView(price) | ||
.setRatingView(rating) | ||
.setReviewCountView(reviewCount) | ||
.setSponsoredView(sponsored) | ||
.setTitleView(title) | ||
.setWarningView(warning) | ||
.build() | ||
} | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
...AdsExample/app/src/main/java/com/yandex/ads/sample/adfoxcarousel/ViewPagerAutoscroller.kt
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,64 @@ | ||
/* | ||
* This file is a part of the Yandex Advertising Network | ||
* | ||
* Version for Android (C) 2022 YANDEX | ||
* | ||
* You may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at https://legal.yandex.com/partner_ch/ | ||
*/ | ||
|
||
package com.yandex.ads.sample.adfoxcarousel | ||
|
||
import androidx.viewpager2.widget.ViewPager2 | ||
import kotlinx.coroutines.Job | ||
import kotlinx.coroutines.MainScope | ||
import kotlinx.coroutines.delay | ||
import kotlinx.coroutines.launch | ||
|
||
class ViewPagerAutoscroller(private val viewPager2: ViewPager2) { | ||
|
||
private val itemCount get() = viewPager2.adapter!!.itemCount | ||
private val currentItem get() = viewPager2.currentItem | ||
|
||
private val scope = MainScope() | ||
private var job: Job? = null | ||
private var lastScroll = 0L | ||
|
||
fun startScrolling() { | ||
stopScrolling() | ||
updateLastScroll() | ||
job = scope.launch { | ||
while (true) { | ||
delay(CHECK_INTERVAL_MILLIS) | ||
if (System.currentTimeMillis() - lastScroll > SCROLL_INTERVAL_MILLIS) { | ||
scroll() | ||
} | ||
} | ||
} | ||
} | ||
|
||
fun updateLastScroll() { | ||
lastScroll = System.currentTimeMillis() | ||
} | ||
|
||
fun stopScrolling() { | ||
job?.cancel() | ||
job = null | ||
} | ||
|
||
private fun scroll() { | ||
openNextPage() | ||
updateLastScroll() | ||
} | ||
|
||
private fun openNextPage() { | ||
val nextItem = (currentItem + 1) % itemCount | ||
viewPager2.setCurrentItem(nextItem, true) | ||
} | ||
|
||
companion object { | ||
|
||
private const val SCROLL_INTERVAL_MILLIS = 5000L | ||
private const val CHECK_INTERVAL_MILLIS = 1000L | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
...ple/app/src/main/java/com/yandex/ads/sample/adfoxcarousel/ViewPagerIndicatorController.kt
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,68 @@ | ||
/* | ||
* This file is a part of the Yandex Advertising Network | ||
* | ||
* Version for Android (C) 2022 YANDEX | ||
* | ||
* You may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at https://legal.yandex.com/partner_ch/ | ||
*/ | ||
|
||
package com.yandex.ads.sample.adfoxcarousel | ||
|
||
import android.widget.ImageView | ||
import android.widget.LinearLayout | ||
import androidx.annotation.DimenRes | ||
import androidx.core.content.ContextCompat | ||
import androidx.core.view.isVisible | ||
import androidx.core.view.setMargins | ||
import com.yandex.ads.sample.R | ||
import kotlin.math.absoluteValue | ||
|
||
class ViewPagerIndicatorController(private val indicatorLayout: LinearLayout) { | ||
private val dotsImageViews = arrayListOf<ImageView>() | ||
|
||
fun setupIndicator(pagesCount: Int, selectedPage: Int) { | ||
indicatorLayout.removeAllViews() | ||
dotsImageViews.clear() | ||
|
||
for (i in 0 until pagesCount) { | ||
val dotView = ImageView(indicatorLayout.context).apply { | ||
setImageDrawable(ContextCompat.getDrawable(indicatorLayout.context, R.drawable.shape_adfox_carousel_indicator_dot)) | ||
} | ||
dotsImageViews.add(dotView) | ||
indicatorLayout.addView(dotsImageViews[i]) | ||
} | ||
changeSelectedPage(selectedPage) | ||
} | ||
|
||
fun changeSelectedPage(selectedPage: Int) { | ||
dotsImageViews.forEachIndexed { page, view -> | ||
val distance = (selectedPage - page).absoluteValue | ||
val size = when { | ||
dotsImageViews.size <= 4 || distance < 2 -> R.dimen.adfox_carousel_indicator_dot_size_large | ||
distance == 2 -> R.dimen.adfox_carousel_indicator_dot_size_medium | ||
else -> R.dimen.adfox_carousel_indicator_dot_size_small | ||
} | ||
view.layoutParams = getLayoutParams(getResource(size)) | ||
view.isVisible = dotsImageViews.size <= 4 || distance <= 3 | ||
view.alpha = if (page == selectedPage) DOT_ALPHA_SELECTED | ||
else DOT_ALPHA_NOT_SELECTED | ||
} | ||
} | ||
|
||
private fun getLayoutParams(dotSize: Int): LinearLayout.LayoutParams { | ||
val layoutParams = LinearLayout.LayoutParams(dotSize, dotSize) | ||
layoutParams.setMargins(getResource(R.dimen.adfox_carousel_indicator_dot_margin)) | ||
return layoutParams | ||
} | ||
|
||
private fun getResource(@DimenRes id: Int): Int { | ||
return indicatorLayout.resources.getDimension(id).toInt() | ||
} | ||
|
||
companion object { | ||
|
||
private const val DOT_ALPHA_SELECTED = 1f | ||
private const val DOT_ALPHA_NOT_SELECTED = 0.6f | ||
} | ||
} |
Oops, something went wrong.