package com.yidian.yac.newscontent.adapter

import android.content.Context
import android.os.Build
import android.view.View
import androidx.appcompat.widget.AppCompatImageView
import coil.ImageLoader
import coil.decode.GifDecoder
import coil.decode.ImageDecoderDecoder
import coil.load
import coil.request.CachePolicy
import com.stx.xhb.androidx.XBanner
import com.yidian.yac.newscontent.R
import com.yidian.yac.newscontent.bean.xbrid.BannderBean

/**
 * @description: 自定义bannder适配器
 * @author: huyajun
 * @date:  2021/4/29
 **/
class BannerAdapter(var context: Context) : XBanner.XBannerAdapter {
    val gifImageLoader = context.let {
        ImageLoader.Builder(it).memoryCachePolicy(CachePolicy.ENABLED)
            .diskCachePolicy(CachePolicy.ENABLED)
            .componentRegistry {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
                    add(ImageDecoderDecoder())
                } else {
                    add(GifDecoder())
                }
            }
            .build()
    }

    override fun loadBanner(banner: XBanner?, model: Any?, view: View?, position: Int) {
        (view as AppCompatImageView).load((model as BannderBean).url, gifImageLoader!!) {
            placeholder(R.drawable.ic_launcher_background)
            error(R.drawable.ic_launcher_background)
        }
    }

}
