OpenLotteryBoxDialog.java 7.92 KB
package com.chudiangameplay.android.dialog;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chudiangameplay.android.R;
import com.chudiangameplay.android.adapter.LotteryResultListAdapter;
import com.chudiangameplay.android.bean.GiftInfo;
import com.chudiangameplay.android.bean.LotteryInfo;
import com.chudiangameplay.android.event.EventOneMore;
import com.chudiangameplay.android.interface_.CommCallBack;
import com.chudiangameplay.android.interface_.OkHttpCallBack;
import com.chudiangameplay.android.manager.API_RoomManager;
import com.chudiangameplay.android.module_im.ui.view.IM_EmojiGridViewV2;
import com.chudiangameplay.android.responce.BaseResponce;
import com.chudiangameplay.android.responce.LotteryResultRespson;
import com.chudiangameplay.android.tool.CommToast;
import com.chudiangameplay.android.ui.view.GridSpacingItemDecoration;
import com.chudiangameplay.android.util.Util;
import com.opensource.svgaplayer.SVGACallback;
import com.opensource.svgaplayer.SVGADrawable;
import com.opensource.svgaplayer.SVGAImageView;
import com.opensource.svgaplayer.SVGAParser;
import com.opensource.svgaplayer.SVGAVideoEntity;

import org.greenrobot.eventbus.EventBus;
import org.jetbrains.annotations.NotNull;

import java.io.Serializable;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by 0 on 2019/8/22.
 */

public class OpenLotteryBoxDialog extends DialogFragment implements View.OnClickListener {

    RecyclerView recyclerview;
    SVGAImageView svga_iv;
    List<LotteryResultRespson.LotteryResult> lotteryResults;
    View fl_result;
    static LotteryResultRespson lotteryResultRespson;

    static String roomNo, number;
    static LotteryInfo info;
    static CommCallBack callBack;

    public static void open(final FragmentActivity context, String roomNo, LotteryInfo info, String number, final CommCallBack callBack) {
        OpenLotteryBoxDialog.roomNo = roomNo;
        OpenLotteryBoxDialog.info = info;
        OpenLotteryBoxDialog.number = number;
        OpenLotteryBoxDialog.callBack = callBack;

        API_RoomManager.lottery_activity(context, roomNo, info.id, number, new OkHttpCallBack() {
            @Override
            public void onSuccess(BaseResponce baseResponce) {
                if (BaseResponce.Status_Success.equals(baseResponce.status)) {
                    OpenLotteryBoxDialog.lotteryResultRespson = (LotteryResultRespson) baseResponce;

                    Bundle args = new Bundle();
                    args.putSerializable("lotteryResults", (Serializable) lotteryResultRespson.data);
                    OpenLotteryBoxDialog fragment = new OpenLotteryBoxDialog();
                    fragment.setArguments(args);
                    fragment.show(context.getSupportFragmentManager(), null);
                } else {
                    CommToast.showToast(context, baseResponce.msg);
                }
            }

            @Override
            public void onFailure(BaseResponce baseResponce) {

            }
        });
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(DialogFragment.STYLE_NO_FRAME, R.style.myDialog);
        lotteryResults = (List<LotteryResultRespson.LotteryResult>) getArguments().getSerializable("lotteryResults");
    }

    @Override
    public void onStart() {
        super.onStart();
        Window win = getDialog().getWindow();
        // 一定要设置Background,如果不设置,window属性设置无效
        win.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

        DisplayMetrics dm = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);

        WindowManager.LayoutParams params = win.getAttributes();
        // 使用ViewGroup.LayoutParams,以便Dialog 宽度充满整个屏幕
        params.width = ViewGroup.LayoutParams.MATCH_PARENT;
        params.height = ViewGroup.LayoutParams.MATCH_PARENT;
        win.setAttributes(params);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.dialog_open_lottery, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(view);
    }

    private void initView(View view) {
        recyclerview = view.findViewById(R.id.recyclerview);
        svga_iv = view.findViewById(R.id.svga_iv);
        fl_result = view.findViewById(R.id.fl_result);
        view.findViewById(R.id.view_bg).setOnClickListener(this);
        view.findViewById(R.id.tv_ok).setOnClickListener(this);

        TextView tv_one_more = view.findViewById(R.id.tv_one_more);
        tv_one_more.setOnClickListener(this);
        tv_one_more.setText("再开" + number + "个");


        GridLayoutManager manager = new GridLayoutManager(getContext(), lotteryResults.size() > 1 ? 5 : 1);
        recyclerview.setLayoutManager(manager);
        recyclerview.addItemDecoration(new GridSpacingItemDecoration(5, Util.dip2px(getContext(), 8), false));

        if (lotteryResults.size()>10){
            recyclerview.getLayoutParams().height = Util.dip2px(getActivity(),168);
        }

        LotteryResultListAdapter adapter = new LotteryResultListAdapter(getActivity(), null);
        recyclerview.setAdapter(adapter);

        adapter.setData(lotteryResults);
        if (info != null) {
            try {
                loadSvga(info.objectGifUrl);
            } catch (MalformedURLException e) {
                e.printStackTrace();
            }
        }
    }

    private void loadSvga(String objectGifUrl) throws MalformedURLException {
        new SVGAParser(getActivity()).decodeFromURL(new URL(objectGifUrl), new SVGAParser.ParseCompletion() {
            @Override
            public void onComplete(@NotNull SVGAVideoEntity svgaVideoEntity) {
                SVGADrawable drawable = new SVGADrawable(svgaVideoEntity);
                svga_iv.setImageDrawable(drawable);
                svga_iv.setCallback(new SVGACallback() {
                    @Override
                    public void onPause() {

                    }

                    @Override
                    public void onFinished() {
                        fl_result.setVisibility(View.VISIBLE);
                        if (callBack != null) {
                            callBack.onResult(lotteryResultRespson.data);
                        }
                    }

                    @Override
                    public void onRepeat() {

                    }

                    @Override
                    public void onStep(int i, double v) {

                    }
                });
                svga_iv.startAnimation();
            }

            @Override
            public void onError() {
                fl_result.setVisibility(View.VISIBLE);
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_ok:
            case R.id.fl_result:
                dismiss();
                break;
            case R.id.tv_one_more:
                open(getActivity(), roomNo, info, number, callBack);
                dismiss();
                break;
        }
    }
}