RxMakePromptMessageTimer.java 7.33 KB
Newer Older
huahua committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
package live.nodiyphoneshell.cend.rxbus;
import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.annotation.NonNull;
import com.squareup.picasso.Picasso;
import java.util.List;
import java.util.concurrent.TimeUnit;
import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import live.nodiyphoneshell.cend.config.Config;
import live.nodiyphoneshell.cend.modules.OrderNoticeBean;
import live.nodiyphoneshell.cend.utils.DateUtils;
import live.nodiyphoneshell.cend.utils.ScreenUtils;
/**
 * @author MaYnaghua
 * @version 1.0.0
 * @date 2021/3/4 10:21
 */
public class RxMakePromptMessageTimer {

    private Disposable mDisposable;
    private static volatile RxMakePromptMessageTimer instance;
    private Context context;
    private LinearLayout linearLayout;
    private TextView textView;
    private ImageView imageView;
    private Disposable mDisposableCountDown;
    private int period = 6;
    private int postion = 0;//轮播位置
    public  List<OrderNoticeBean> orderNoticeBeanList;      //之前消息集合
    private boolean hasComeIn = false; //是否首次进入
    private int getWidth;

    public static RxMakePromptMessageTimer getInstance(Context context) {
        if (instance == null) {
            synchronized (RxMakePromptMessageTimer.class) {
                if (instance == null) {
                    instance = new RxMakePromptMessageTimer(context);
                }
            }
        }
        return instance;
    }

    public RxMakePromptMessageTimer(Context context) {
        getWidth=ScreenUtils.dip2px(context, 15);
        this.context = context;
    }


    /*有几个关键:
     1、队列管理,存储数量控制,清空队列;
     2、队列循环,为避免指定时间推送,所以按随机时间段(5、10、15)取个时间间隔
     3、显示规则,每次只显示1S,间隔5秒显示下一条,合计50S内显示完最新队列,接着按2的规则进入下一次循环;*/
    //初始化数据
    public void initData(LinearLayout linearLayout, ImageView imageView, TextView textView) {
        this.linearLayout = linearLayout;
        this.textView = textView;
        this.imageView = imageView;
    }


    //更新集合数据
    public void setUpDataList(List<OrderNoticeBean> orderNoticeBeanList) {
        this.orderNoticeBeanList = orderNoticeBeanList;
        this.postion = 0;
        if (textView != null && linearLayout != null && orderNoticeBeanList != null && orderNoticeBeanList.size() > 0) {
            interval();
        } else {
            cancel();
        }
    }

    /**
     * milliseconds毫秒后执行指定动作
     */
    public synchronized void timer(long SECONDS) {
        Observable.timer(SECONDS, TimeUnit.SECONDS)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Long>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable disposable) {
                        mDisposableCountDown = disposable;
                    }

                    @Override
                    public void onNext(@NonNull Long number) {
                        if (linearLayout != null) {
                            linearLayout.setVisibility(View.GONE);
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }


    /**
     * 每隔milliseconds秒后执行指定动作
     */
    public void interval() {
        if (mDisposable == null && !hasComeIn) {
            if (linearLayout != null) {
                linearLayout.setVisibility(View.GONE);
            }
            hasComeIn = true;
            Observable.interval(period, TimeUnit.SECONDS)
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Observer<Long>() {
                        @Override
                        public void onSubscribe(@NonNull Disposable disposable) {
                            mDisposable = disposable;
                        }

                        @Override
                        public void onNext(@NonNull Long number) {
                            try {
                                if (orderNoticeBeanList != null && orderNoticeBeanList.size() > 0) {
                                    if (linearLayout != null) {
                                        linearLayout.setVisibility(View.VISIBLE);
                                    }
                                    if (postion < orderNoticeBeanList.size() && textView != null) {
                                        OrderNoticeBean orderNoticeBean = orderNoticeBeanList.get(postion);
                                        if (orderNoticeBean != null) {
                                            long getNumber = System.currentTimeMillis() / 1000 - orderNoticeBean.getCreate_time();
                                            textView.setText(DateUtils.getDate(getNumber) + " 前定制了 " + orderNoticeBean.getGoods_name());
                                            String imageUrl = orderNoticeBean.getWorks_image();
                                            if (!TextUtils.isEmpty(imageUrl)) {
                                                imageUrl = imageUrl + Config.IMAGE_OSS_URL +getWidth;
                                                if (context != null && imageView != null) {
                                                    Picasso.get().load(imageUrl).into(imageView);
                                                    /*Picasso.with(context)
                                                            .load(imageUrl).skipMemoryCache()
                                                            .into(imageView);*/
                                                }
                                            }
                                            postion++;
                                        }
                                    }
                                    if (postion > (orderNoticeBeanList.size() - 1)) {//判断位置是否大于条数
                                        postion = 0;
                                    }
                                }
                                timer(3);
                            } catch (Exception e) {

                            }
                        }

                        //16:54:35
                        //17:02:39
                        @Override
                        public void onError(@NonNull Throwable e) {

                        }

                        @Override
                        public void onComplete() {

                        }
                    });
        }
    }

    /**
     * 取消订阅
     */
    public void cancel() {
        postion = 0;
        hasComeIn = false;
        if (mDisposable != null) {
            mDisposable.dispose();
            mDisposable = null;
        }
        if (mDisposableCountDown != null) {
            mDisposableCountDown.dispose();
            mDisposableCountDown = null;
        }
    }

}