387 lines
15 KiB
Java
387 lines
15 KiB
Java
package com.yunbao.live.dialog;
|
|
|
|
import android.app.Dialog;
|
|
import android.os.Bundle;
|
|
import android.text.TextUtils;
|
|
import android.util.Log;
|
|
import android.view.Gravity;
|
|
import android.view.View;
|
|
import android.view.Window;
|
|
import android.view.WindowManager;
|
|
import android.widget.Button;
|
|
import android.widget.LinearLayout;
|
|
import android.widget.RelativeLayout;
|
|
import android.widget.TextView;
|
|
|
|
import androidx.annotation.Nullable;
|
|
import androidx.recyclerview.widget.LinearLayoutManager;
|
|
import androidx.recyclerview.widget.RecyclerView;
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
import com.alibaba.fastjson.JSONObject;
|
|
import com.makeramen.roundedimageview.RoundedImageView;
|
|
import com.opensource.svgaplayer.SVGAImageView;
|
|
import com.yunbao.common.CommonAppConfig;
|
|
import com.yunbao.common.Constants;
|
|
import com.yunbao.common.bean.UserBean;
|
|
import com.yunbao.common.dialog.AbsDialogFragment;
|
|
import com.yunbao.common.glide.ImgLoader;
|
|
import com.yunbao.common.http.HttpCallback;
|
|
import com.yunbao.common.utils.DialogUitl;
|
|
import com.yunbao.common.utils.RouteUtil;
|
|
import com.yunbao.common.utils.ToastUtil;
|
|
import com.yunbao.common.utils.WordUtil;
|
|
import com.yunbao.live.R;
|
|
import com.yunbao.live.activity.LiveActivity;
|
|
import com.yunbao.live.adapter.GuardRightAdapter;
|
|
import com.yunbao.live.adapter.LiveNewGuardBuyItemsAdapter;
|
|
import com.yunbao.live.bean.GuardBuyBean;
|
|
import com.yunbao.live.bean.GuardRightBean;
|
|
import com.yunbao.live.bean.LiveGuardInfo;
|
|
import com.yunbao.live.http.LiveHttpConsts;
|
|
import com.yunbao.live.http.LiveHttpUtil;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
/**
|
|
* Created by cxf on 2018/11/6.
|
|
* 直播间购买守护弹窗
|
|
* 改版后效果
|
|
*/
|
|
|
|
public class LiveNewGuardBuyDialogFragment extends AbsDialogFragment implements View.OnClickListener {
|
|
|
|
private RecyclerView mRights;//权益列表
|
|
private RecyclerView mBuys;//购买列表
|
|
private RoundedImageView userIcon, userIconOne, userIconTwo;
|
|
private SVGAImageView iconCover;
|
|
private TextView anchorName;
|
|
private TextView guardCoin2;
|
|
private TextView guardBuy;
|
|
private TextView guard_tips;
|
|
private Button submit;
|
|
private LinearLayout buy2;
|
|
private RelativeLayout buy1;
|
|
private TextView mCoin;
|
|
private String mCoinName;
|
|
private TextView mCoinNameTextView;
|
|
private View mBtnBuy;
|
|
private List<GuardRightBean> mRightList = new ArrayList<>();//权限列表
|
|
private List<GuardBuyBean> mBuyList;//商品列表
|
|
private long mCoinVal;//余额
|
|
private String mLiveUid;
|
|
private String mStream;
|
|
private String mAncherName;
|
|
private String mAncherIcon;
|
|
private String mUserIcon;
|
|
private LiveGuardInfo mLiveGuardInfo;
|
|
private GuardBuyBean mTargetBuyBean;
|
|
|
|
private LiveNewGuardBuyItemsAdapter buyItemsAdapter;
|
|
private GuardRightAdapter mGuardRightAdapter;
|
|
|
|
|
|
@Override
|
|
protected int getLayoutId() {
|
|
return R.layout.dialog_new_guard_buy;
|
|
}
|
|
|
|
@Override
|
|
protected int getDialogStyle() {
|
|
return R.style.dialog2;
|
|
}
|
|
|
|
@Override
|
|
protected boolean canCancel() {
|
|
return true;
|
|
}
|
|
|
|
@Override
|
|
protected void setWindowAttributes(Window window) {
|
|
window.setWindowAnimations(R.style.bottomToTopAnim);
|
|
WindowManager.LayoutParams params = window.getAttributes();
|
|
params.width = WindowManager.LayoutParams.MATCH_PARENT;
|
|
params.height = WindowManager.LayoutParams.WRAP_CONTENT;
|
|
params.gravity = Gravity.BOTTOM;
|
|
window.setAttributes(params);
|
|
}
|
|
|
|
public void setLiveGuardInfo(LiveGuardInfo info) {
|
|
mLiveGuardInfo = info;
|
|
}
|
|
|
|
@Override
|
|
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
|
|
super.onActivityCreated(savedInstanceState);
|
|
|
|
mCoinNameTextView = mRootView.findViewById(R.id.guard_coin_name);
|
|
mCoin = mRootView.findViewById(R.id.guard_coin);
|
|
mBtnBuy = mRootView.findViewById(R.id.guard_btn_buy);
|
|
mRights = mRootView.findViewById(R.id.guard_rights);
|
|
mBuys = mRootView.findViewById(R.id.guard_buys);
|
|
anchorName = mRootView.findViewById(R.id.guard_anchor_name);
|
|
userIcon = mRootView.findViewById(R.id.guard_imageView);
|
|
userIconOne = mRootView.findViewById(R.id.guard_imageView_one);
|
|
userIconTwo = mRootView.findViewById(R.id.guard_imageView_two);
|
|
iconCover = mRootView.findViewById(R.id.gift_svga);
|
|
guardCoin2 = mRootView.findViewById(R.id.guard_coin2);
|
|
guardBuy = mRootView.findViewById(R.id.guard_buy);
|
|
submit = mRootView.findViewById(R.id.guard_submit);
|
|
buy1 = mRootView.findViewById(R.id.buy1);
|
|
buy2 = mRootView.findViewById(R.id.buy2);
|
|
guard_tips = mRootView.findViewById(R.id.guard_textView2);
|
|
|
|
iconCover.setImageResource(R.drawable.guardian_img_wings);
|
|
|
|
mBtnBuy.setOnClickListener(this);
|
|
submit.setOnClickListener(this);
|
|
guardBuy.setOnClickListener(this);
|
|
mRights.setHasFixedSize(true);
|
|
mRights.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
|
|
mBuys.setHasFixedSize(true);
|
|
mBuys.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false) {
|
|
@Override
|
|
public boolean canScrollHorizontally() {//购买列表禁止滑动
|
|
return false;
|
|
}
|
|
});
|
|
mBuys.setItemViewCacheSize(3);
|
|
|
|
buyItemsAdapter = new LiveNewGuardBuyItemsAdapter(mContext);
|
|
mGuardRightAdapter = new GuardRightAdapter(mContext, mRightList);
|
|
mRights.setAdapter(mGuardRightAdapter);
|
|
mBuys.setAdapter(buyItemsAdapter);
|
|
buyItemsAdapter.setOnItemSelectListener(this::refreshList);
|
|
|
|
Bundle bundle = getArguments();
|
|
if (bundle != null) {
|
|
mLiveUid = bundle.getString(Constants.LIVE_UID);
|
|
mStream = bundle.getString(Constants.STREAM);
|
|
mAncherName = bundle.getString(Constants.LIVE_ANCHER_NAME);
|
|
mAncherIcon = bundle.getString(Constants.LIVE_ANCHER_ICON);
|
|
mUserIcon = bundle.getString(Constants.USER_ICON);
|
|
String coinName = WordUtil.getString(R.string.diamond);
|
|
mCoinName = coinName;
|
|
// mCoinNameTextView.setText(WordUtil.getString(R.string.guard_my) + coinName + ":");
|
|
mCoinNameTextView.setText("快为您喜欢的主播开通守护吧");
|
|
anchorName.setText(String.format("成爲%s的守護", mAncherName));
|
|
}
|
|
LiveHttpUtil.getGuardBuyList(new HttpCallback() {
|
|
@Override
|
|
public void onSuccess(int code, String msg, String[] info) {
|
|
if (code == 0 && info.length > 0) {
|
|
JSONObject obj = JSON.parseObject(info[0]);
|
|
mRightList = JSON.parseArray(obj.getString("privilege"), GuardRightBean.class);
|
|
mBuyList = JSON.parseArray(obj.getString("list"), GuardBuyBean.class);
|
|
try {
|
|
mCoinVal = obj.getLongValue("coin");
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
mCoin.setText(String.valueOf(mCoinVal));
|
|
guardCoin2.setText(WordUtil.getString(R.string.guard_my) + mCoinName + ":" + mCoinVal);
|
|
buyItemsAdapter.setItems(mBuyList);
|
|
mGuardRightAdapter.setList(mRightList);
|
|
buyItemsAdapter.notifyDataSetChanged();
|
|
|
|
refreshList(1);
|
|
|
|
} else {
|
|
ToastUtil.show(msg);
|
|
}
|
|
}
|
|
});
|
|
initView();
|
|
}
|
|
|
|
/**
|
|
* 根据守护类型初始化不同的底部布局
|
|
*/
|
|
private void initView() {
|
|
int guardType = mLiveGuardInfo.getMyGuardType();
|
|
if (guardType == Constants.GUARD_TYPE_NONE) {
|
|
iconCover.setBackgroundResource(R.drawable.guardian_img_wings);
|
|
buy1.setVisibility(View.VISIBLE);
|
|
buy2.setVisibility(View.GONE);
|
|
} else {
|
|
buy2.setVisibility(View.VISIBLE);
|
|
buy1.setVisibility(View.GONE);
|
|
userIconOne.setVisibility(View.VISIBLE);
|
|
userIconTwo.setVisibility(View.VISIBLE);
|
|
iconCover.setImageResource(R.drawable.guardian_img_two);
|
|
ImgLoader.display(mContext, mAncherIcon, userIconOne);
|
|
ImgLoader.display(mContext, mUserIcon, userIconTwo);
|
|
String title = "";
|
|
switch (guardType) {
|
|
case Constants.GUARD_TYPE_DAY:
|
|
title = "您是當前主播的周守護";
|
|
break;
|
|
case Constants.GUARD_TYPE_MONTH:
|
|
title = "您是當前主播的月守護";
|
|
break;
|
|
case Constants.GUARD_TYPE_YEAR:
|
|
title = "您是當前主播的年守護";
|
|
break;
|
|
}
|
|
anchorName.setText(title);
|
|
guard_tips.setText("守護日期截止到:"+ mLiveGuardInfo.getMyGuardEndTime());
|
|
}
|
|
|
|
}
|
|
|
|
|
|
private void refreshList(int index) {
|
|
if (!mRightList.isEmpty()) {
|
|
GuardBuyBean guardBuyBean = mBuyList.get(index - 1);
|
|
mTargetBuyBean = guardBuyBean;
|
|
for (GuardRightBean bean : mRightList) {
|
|
bean.setChecked(false);
|
|
}
|
|
for (int i : guardBuyBean.getPrivilege()) {
|
|
if(i<mRightList.size()) {
|
|
mRightList.get(i).setChecked(true);
|
|
}
|
|
}
|
|
mGuardRightAdapter.notifyDataSetChanged();
|
|
// mBtnBuy.setEnabled(mCoinVal >= guardBuyBean.getCoin());
|
|
}
|
|
|
|
}
|
|
|
|
|
|
@Override
|
|
public void onClick(View view) {
|
|
int i = view.getId();
|
|
if (i == R.id.guard_btn_buy || i == R.id.guard_submit) {
|
|
clickBuyGuard();
|
|
} else if (i == R.id.guard_buy) {
|
|
forwardMyCoin();
|
|
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 跳转到我的钻石
|
|
*/
|
|
private void forwardMyCoin() {
|
|
dismiss();
|
|
RouteUtil.forwardMyCoin(mContext);
|
|
}
|
|
|
|
/**
|
|
* 点击购买守护按钮
|
|
*/
|
|
private void clickBuyGuard() {
|
|
if (TextUtils.isEmpty(mLiveUid) || TextUtils.isEmpty(mStream) || mLiveGuardInfo == null || mTargetBuyBean == null) {
|
|
return;
|
|
}
|
|
Log.d("ffffff", "" + mLiveGuardInfo.getMyGuardType() + " " + mTargetBuyBean.getType());
|
|
if (mLiveGuardInfo.getMyGuardType() == Constants.GUARD_TYPE_MONTH && mTargetBuyBean.getType() == Constants.GUARD_TYPE_DAY) {
|
|
DialogUitl.showSimpleTipDialog(mContext, WordUtil.getString(R.string.guard_buy_tip));
|
|
return;
|
|
} else if (mLiveGuardInfo.getMyGuardType() == Constants.GUARD_TYPE_YEAR && mTargetBuyBean.getType() == Constants.GUARD_TYPE_DAY) {
|
|
DialogUitl.showSimpleTipDialog(mContext, "您為當前主播的年守護無法開通周守護");
|
|
return;
|
|
} else if (mLiveGuardInfo.getMyGuardType() == Constants.GUARD_TYPE_YEAR && mTargetBuyBean.getType() == Constants.GUARD_TYPE_MONTH) {
|
|
DialogUitl.showSimpleTipDialog(mContext, "您為當前主播的年守護無法開通月守護");
|
|
return;
|
|
} else {
|
|
if (mLiveGuardInfo.getMyGuardType() == Constants.GUARD_TYPE_MONTH
|
|
&& mTargetBuyBean.getType() == Constants.GUARD_TYPE_YEAR) {
|
|
DialogUitl.showSimpleDialog(mContext, WordUtil.getString(R.string.guard_buy_tip_2), new DialogUitl.SimpleCallback() {
|
|
@Override
|
|
public void onConfirmClick(Dialog dialog, String content) {
|
|
doBuyGuard();
|
|
}
|
|
});
|
|
return;
|
|
} else if (mLiveGuardInfo.getMyGuardType() == Constants.GUARD_TYPE_DAY
|
|
&& mTargetBuyBean.getType() == Constants.GUARD_TYPE_MONTH) {
|
|
DialogUitl.showSimpleDialog(mContext, "您為當前主播的周守護,開通月守護將覆蓋您的周守護時長,是否開通?", new DialogUitl.SimpleCallback() {
|
|
@Override
|
|
public void onConfirmClick(Dialog dialog, String content) {
|
|
doBuyGuard();
|
|
}
|
|
});
|
|
return;
|
|
} else if (mLiveGuardInfo.getMyGuardType() == Constants.GUARD_TYPE_DAY
|
|
&& mTargetBuyBean.getType() == Constants.GUARD_TYPE_YEAR) {
|
|
DialogUitl.showSimpleDialog(mContext, "您為當前主播的周守護,開通年守護將覆蓋您的周守護時長,是否開通?", new DialogUitl.SimpleCallback() {
|
|
@Override
|
|
public void onConfirmClick(Dialog dialog, String content) {
|
|
doBuyGuard();
|
|
}
|
|
});
|
|
return;
|
|
}
|
|
|
|
}
|
|
buyGuard();
|
|
}
|
|
|
|
/**
|
|
* 购买守护
|
|
*/
|
|
private void buyGuard() {
|
|
if (mTargetBuyBean == null) {
|
|
return;
|
|
}
|
|
DialogUitl.showSimpleDialog(mContext,
|
|
String.format(WordUtil.getString(R.string.guard_buy_tip_3), mTargetBuyBean.getCoin(), mCoinName, mTargetBuyBean.getShopName()),
|
|
new DialogUitl.SimpleCallback() {
|
|
|
|
@Override
|
|
public void onConfirmClick(Dialog dialog, String content) {
|
|
doBuyGuard();
|
|
}
|
|
});
|
|
}
|
|
|
|
/**
|
|
* 购买守护
|
|
*/
|
|
private void doBuyGuard() {
|
|
if (mTargetBuyBean == null) {
|
|
return;
|
|
}
|
|
LiveHttpUtil.buyGuard(mLiveUid, mStream, mTargetBuyBean.getId(), new HttpCallback() {
|
|
@Override
|
|
public void onSuccess(int code, String msg, String[] info) {
|
|
if (code == 0 && info.length > 0) {
|
|
JSONObject obj = JSON.parseObject(info[0]);
|
|
String votes = obj.getString("votestotal");//主播当前的映票数
|
|
int guardNum = obj.getIntValue("guard_nums");//主播当前的守护人数
|
|
int guardType = obj.getIntValue("type");
|
|
String ancherName = obj.getString("ancherName");
|
|
String liveuid = obj.getString("liveuid");
|
|
if (mLiveGuardInfo != null) {
|
|
mLiveGuardInfo.setMyGuardType(guardType);
|
|
mLiveGuardInfo.setMyGuardEndTime(obj.getString("endtime"));
|
|
mLiveGuardInfo.setGuardNum(guardNum);
|
|
}
|
|
mCoinVal = obj.getLongValue("coin");
|
|
String coinString = String.valueOf(mCoinVal);
|
|
mCoin.setText(coinString);
|
|
UserBean u = CommonAppConfig.getInstance().getUserBean();
|
|
if (u != null) {
|
|
u.setCoin(coinString);
|
|
u.setLevel(obj.getIntValue("level"));
|
|
}
|
|
((LiveActivity) mContext).sendBuyGuardMessage2(votes, guardNum, guardType, ancherName, liveuid);
|
|
dismiss();
|
|
}
|
|
ToastUtil.show(msg);
|
|
}
|
|
});
|
|
}
|
|
|
|
|
|
@Override
|
|
public void onDestroy() {
|
|
mLiveGuardInfo = null;
|
|
LiveHttpUtil.cancel(LiveHttpConsts.GET_GUARD_BUY_LIST);
|
|
super.onDestroy();
|
|
}
|
|
}
|