PopUpsUtils.java 4.18 KB
package com.wd.common.dialog;

import android.content.Context;


import com.wd.base.log.Logger;
import com.wd.foundation.wdkitcore.tools.HostUtil;
import com.wd.common.utils.ProcessUtils;
import com.wd.foundation.bean.custom.content.ContentBean;
import com.wd.foundation.bean.pop.PopUpsBean;
import com.wd.foundation.wdkit.utils.SpUtils;
import com.wd.foundation.bean.utils.TimeUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 彩蛋工具类
 * @author baozhaoxin
 * @version [V1.0.0, 2022/8/20]
 */
public class PopUpsUtils {

    private static String TAG = "PopUpsUtils";

    /**
     *
     * @param hasPopUp 是否有彩蛋
     * @param originalList  原始数据集合
     * @return
     */
    public static PopUpsBean handlerPopUps(boolean hasPopUp, List<PopUpsBean> originalList, String type){
        if(!hasPopUp){
            //没有彩蛋
            return null;
        }
        if(originalList==null||originalList.size()==0){
            //彩蛋数据为空
            return null;
        }

        List<PopUpsBean> resultlList = new ArrayList<>();

        //按时间和exposureOnce过滤数据
        handlerByDataAndExposureOnce(resultlList,originalList,type);

        if(resultlList!=null&&resultlList.size()>0){
            //返回符合条件的第一条数据
            return resultlList.get(0);
        }
        return null;
    }

    /**
     *按时间和exposureOnce过滤数据
     * @param resultlList
     * @param originalList
     */
    private static void handlerByDataAndExposureOnce(List<PopUpsBean> resultlList,List<PopUpsBean> originalList,String type){
        long currentTimeStamp = HostUtil.serverTime;
        for (PopUpsBean bean : originalList) {
            PopUpsBean popUpsBean = SpUtils.getPopUpById(bean.getId());
            if(popUpsBean != null && (
                    popUpsBean.getAlreadyDailyExposureTimes() == popUpsBean.getDailyExposureTimes() ||
                            popUpsBean.getAlreadyDailyExposureTimes() > popUpsBean.getDailyExposureTimes())){
                //当日看到N次彩蛋后,当日不会再看到彩蛋
                continue;
            }
            try {
                long startTimeStamp = TimeUtil.time2timestamp(bean.getStartTime());
                long endTimeStamp = TimeUtil.time2timestamp(bean.getEndTime());
                if (currentTimeStamp >= startTimeStamp
                        && currentTimeStamp <= endTimeStamp) {
                    resultlList.add(bean);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 显示彩蛋
     */
    public static EasterEggsDialog showEasterEggsDialog(Context context,PopUpsBean mPopUpsBean,String type,
                                             EasterEggsDialog.DialogClickListener mListener) {
        if (mPopUpsBean != null) {
            EasterEggsDialog easterEggsDialog = new EasterEggsDialog(context).builder()
                    .setMaterialUrl(mPopUpsBean.getMaterialUrl())
                    .setDurations(mPopUpsBean.getDurations())
                    .setClickListener(mListener)
                    .show();
            Logger.t(TAG).d("easterEggsResult=" + mPopUpsBean.toString());
            SpUtils.savePopUpToCache(mPopUpsBean);
            return easterEggsDialog;
        }
        return null;
    }

    /**
     * 彩蛋点击跳转
     *
     * @param mPopUpsBean
     */
    public static void easterEggsDialogJump(PopUpsBean mPopUpsBean) {
        if(mPopUpsBean==null){
            return;
        }
        ContentBean contentBean = new ContentBean();
        contentBean.setPageId(mPopUpsBean.getPageId());
        contentBean.setLinkUrl(mPopUpsBean.getLinkUrl());
        contentBean.setObjectId(mPopUpsBean.getObjectId());
        contentBean.setObjectType(mPopUpsBean.getObjectType());
        contentBean.setObjectLevel(mPopUpsBean.getObjectLevel());
        contentBean.setBottomNavId(mPopUpsBean.getBottomNavId());
        contentBean.setRelId(mPopUpsBean.getRelId());
        //专题模板 --->专题样式 1:常规, 2:作者, 3:时间线
       // contentBean.setTopicTemplate(mPopUpsBean.getTopicTemplate());
        ProcessUtils.processPage(contentBean);
    }
}