From 28b4a368006b47a944657c57086a0e1157f8fac7 Mon Sep 17 00:00:00 2001 From: zlzw <583819556@qq.com> Date: Wed, 7 Dec 2022 15:36:23 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E9=9A=8F=E6=9C=BAPK=E6=A8=A1?= =?UTF-8?q?=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/http/live/LiveNetManager.java | 2 +- .../yunbao/common/manager/IMRTCManager.java | 14 +- .../common/manager/RandomPkManager.java | 272 ++++++++++++++++++ config.gradle | 2 +- .../live/activity/LiveRyAnchorActivity.java | 58 ++-- .../live/dialog/FreePkDialogFragment.java | 271 +++++++++++++++++ .../live/dialog/RandomPkDialogFragment.java | 248 +++++----------- .../presenter/LiveRyLinkMicPkPresenter.java | 5 +- .../yunbao/live/socket/SocketRyClient.java | 6 +- .../yunbao/live/utils/RandomPkManager.java | 103 ------- .../live/views/LivePushRyViewHolder.java | 1 + .../yunbao/live/views/LiveRoomViewHolder.java | 15 +- .../drawable/bg_live_room_random_pk_timer.xml | 9 + .../layout/dialog_live_free_pk_function.xml | 108 +++++++ .../layout/dialog_live_random_pk_function.xml | 64 +---- live/src/main/res/layout/view_live_room.xml | 17 +- live/src/main/res/values/strings.xml | 1 + 17 files changed, 824 insertions(+), 372 deletions(-) create mode 100644 common/src/main/java/com/yunbao/common/manager/RandomPkManager.java create mode 100644 live/src/main/java/com/yunbao/live/dialog/FreePkDialogFragment.java delete mode 100644 live/src/main/java/com/yunbao/live/utils/RandomPkManager.java create mode 100644 live/src/main/res/drawable/bg_live_room_random_pk_timer.xml create mode 100644 live/src/main/res/layout/dialog_live_free_pk_function.xml diff --git a/common/src/main/java/com/yunbao/common/http/live/LiveNetManager.java b/common/src/main/java/com/yunbao/common/http/live/LiveNetManager.java index ec0257553..21540654c 100644 --- a/common/src/main/java/com/yunbao/common/http/live/LiveNetManager.java +++ b/common/src/main/java/com/yunbao/common/http/live/LiveNetManager.java @@ -598,7 +598,7 @@ public class LiveNetManager { @Override public void accept(ResponseModel responseModel) throws Exception { if (callback != null) { - callback.onSuccess("36956"); + callback.onSuccess("98196"); // callback.onSuccess(responseModel.getData().getInfo()); } } diff --git a/common/src/main/java/com/yunbao/common/manager/IMRTCManager.java b/common/src/main/java/com/yunbao/common/manager/IMRTCManager.java index 42d76cbba..bffa567cd 100644 --- a/common/src/main/java/com/yunbao/common/manager/IMRTCManager.java +++ b/common/src/main/java/com/yunbao/common/manager/IMRTCManager.java @@ -1,5 +1,7 @@ package com.yunbao.common.manager; +import com.yunbao.common.utils.ToastUtil; + import cn.rongcloud.rtc.api.RCRTCRoom; import cn.rongcloud.rtc.api.callback.IRCRTCResultCallback; import cn.rongcloud.rtc.base.RTCErrorCode; @@ -71,7 +73,17 @@ public class IMRTCManager { 2:无论为true或false,双方都可以使用RCRTCLiveInfo.setMixConfig(RCRTCMixConfig, IRCRTCResultCallback) 方法主动设置合流布局。一旦主动设置过合流布局,后续音视频直播过程中设置的自动合流参数将失效。 extra - 扩展字段,默认为空 */ - rtcRoom.getLocalUser().requestJoinOtherRoom(liveUid,liveUid,inviterAutoMix,extra,callback); + rtcRoom.getLocalUser().requestJoinOtherRoom(liveUid, liveUid, inviterAutoMix, extra, new IRCRTCResultCallback() { + @Override + public void onSuccess() { + callback.onSuccess(); + } + + @Override + public void onFailed(RTCErrorCode errorCode) { + callback.onFailed(errorCode); + } + }); }else{ callback.onFailed(RTCErrorCode.RongRTCCodeIMError); } diff --git a/common/src/main/java/com/yunbao/common/manager/RandomPkManager.java b/common/src/main/java/com/yunbao/common/manager/RandomPkManager.java new file mode 100644 index 000000000..d16825de6 --- /dev/null +++ b/common/src/main/java/com/yunbao/common/manager/RandomPkManager.java @@ -0,0 +1,272 @@ +package com.yunbao.common.manager; + +import android.os.Handler; +import android.os.Looper; + +import com.yunbao.common.CommonAppContext; +import com.yunbao.common.http.base.HttpCallback; +import com.yunbao.common.http.live.LiveNetManager; +import com.yunbao.common.utils.ToastUtil; + +import java.util.ArrayList; +import java.util.List; +import java.util.Locale; +import java.util.Timer; +import java.util.TimerTask; + +public class RandomPkManager { + public final static int PK_STATUS_DEFAULT = -1; + public final static int PK_STATUS_REQUEST = 0; + public final static int PK_STATUS_START = 1; + public final static int PK_STATUS_CLOSE = 2; + public final static int PK_STATUS_REFUSE = 3; + public final static int PK_STATUS_EXIT_ING = 4; + + private static RandomPkManager manager; + private int min = -1; + private int sec = -1; + private int status = PK_STATUS_DEFAULT; + private Timer timer; + private TimerTask task; + private List randomPkTimer = new ArrayList<>(); + Handler handler = new Handler(Looper.getMainLooper()); + + private RandomPkManager() { + initTask(); + } + + public static RandomPkManager getInstance() { + if (manager == null) { + manager = new RandomPkManager(); + } + return manager; + } + + public void addOnRandomPkTimer(OnRandomPkTimer randomPkTimer) { + if (randomPkTimer != null) { + this.randomPkTimer.add(randomPkTimer); + } + } + + public void unregisterOnRandomPkTimer(OnRandomPkTimer randomPkTimer) { + if (randomPkTimer != null) { + this.randomPkTimer.remove(randomPkTimer); + } + } + + private void initVal() { + min = -1; + sec = -1; + } + + private void initTask() { + if (task != null) { + return; + } + task = new TimerTask() { + + @Override + public void run() { + status = PK_STATUS_REQUEST; + sec++; + if (sec % 60 == 0) { + min++; + sec = 0; + } + handler.post(() -> { + for (OnRandomPkTimer onRandomPkTimer : randomPkTimer) { + onRandomPkTimer.onTimer(String.format(Locale.CHINA, "%02d:%02d", min, sec)); + } + if (sec == 8) { + nextPk(); + } + }); + } + }; + } + + public String getTimer() { + return String.format(Locale.CHINA, "%02d:%02d", min, sec); + } + + private void nextPk() { + LiveNetManager.get(CommonAppContext.getTopActivity()) + .randomPK(new HttpCallback() { + @Override + public void onSuccess(String data) { + for (OnRandomPkTimer pkTimer : randomPkTimer) { + pkTimer.onStartPK(data); + } + } + + @Override + public void onError(String error) { + + } + }); + } + + public boolean start() { + if (status == PK_STATUS_START) { + ToastUtil.show("PK中"); + return false; + } + if (status == PK_STATUS_EXIT_ING) { + return false; + } + if (task != null) { + task.cancel(); + } + task = null; + initTask(); + initVal(); + timer = new Timer(); + timer.schedule(task, 0, 1000); + return true; + } + + private void callEndPkStart() { + for (OnRandomPkTimer pkTimer : randomPkTimer) { + pkTimer.onPkEndStart(); + } + } + + private void callEndPkTimer(String time) { + for (OnRandomPkTimer pkTimer : randomPkTimer) { + pkTimer.onPkEndTimer(time); + } + } + + private void callEndPkSuccess() { + for (OnRandomPkTimer pkTimer : randomPkTimer) { + pkTimer.onPkEndSuccess(); + } + } + + private int exitTimer; + + public void exitPk() { + if (status == PK_STATUS_EXIT_ING) { + return; + } + callEndPkStart(); + status = PK_STATUS_EXIT_ING; + exitTimer = 11; + new Timer().schedule(new TimerTask() { + + Handler handler = new Handler(Looper.getMainLooper()); + + @Override + public void run() { + + handler.post(() -> { + exitTimer--; + callEndPkTimer(exitTimer + ""); + if (exitTimer == 0) { + end(); + callEndPkSuccess(); + cancel(); + status = PK_STATUS_DEFAULT; + } + }); + } + }, 0, 1000); + } + + public void end() { + if (task != null) { + task.cancel(); + } + task = null; + status = PK_STATUS_DEFAULT; + callEnd(); + } + + public boolean isPking() { + return status == PK_STATUS_REQUEST; + } + + private void callEnd() { + if (task != null) { + task.cancel(); + task = null; + } + handler.post(() -> { + for (OnRandomPkTimer pkTimer : randomPkTimer) { + pkTimer.onPkEnd(); + } + }); + } + + private void callStart() { + if (task != null) { + task.cancel(); + task = null; + } + handler.post(() -> { + for (OnRandomPkTimer pkTimer : randomPkTimer) { + pkTimer.onPking(); + } + }); + } + + private void callRefuse() { + handler.post(() -> { + for (OnRandomPkTimer pkTimer : randomPkTimer) { + pkTimer.onRefuse(); + } + }); + } + + public void setPkStatus(int status) { + this.status = status; + switch (status) { + case PK_STATUS_START: + callStart(); + break; + case PK_STATUS_CLOSE: + callEnd(); + break; + case PK_STATUS_REFUSE: + callRefuse(); + //nextPk(); + break; + } + } + + public boolean isExiting() { + return status == PK_STATUS_EXIT_ING; + } + + public String getExitTimer() { + return exitTimer + ""; + } + + + public static abstract class OnRandomPkTimer { + public void onTimer(String time) { + } + + public void onStartPK(String pkUid) { + } + + public void onPking() { + } + + public void onPkEnd() { + } + + public void onRefuse() { + } + + public void onPkEndTimer(String time) { + + } + + public void onPkEndStart() { + } + + public void onPkEndSuccess() { + } + } +} diff --git a/config.gradle b/config.gradle index b1defc16c..d9d2c3a12 100644 --- a/config.gradle +++ b/config.gradle @@ -5,7 +5,7 @@ ext { minSdkVersion : 21, targetSdkVersion : 31, versionCode : 310, - versionName : "6.4.5" + versionName : "6.4.7" ] manifestPlaceholders = [ //正式 diff --git a/live/src/main/java/com/yunbao/live/activity/LiveRyAnchorActivity.java b/live/src/main/java/com/yunbao/live/activity/LiveRyAnchorActivity.java index d8d201b6d..478e5b8ec 100644 --- a/live/src/main/java/com/yunbao/live/activity/LiveRyAnchorActivity.java +++ b/live/src/main/java/com/yunbao/live/activity/LiveRyAnchorActivity.java @@ -7,7 +7,6 @@ import static com.yunbao.common.Constants.SOCKET_LIVE_DRPK; import static com.yunbao.common.Constants.SOCKET_LIVE_DRPK_RANDOM; import static com.yunbao.live.views.AbsRyLivePushViewHolder.mPreView; import static com.yunbao.live.views.LivePushRyViewHolder.dr_pk_view; -import static com.yunbao.live.views.LivePushRyViewHolder.rtcRoom; import static com.yunbao.live.views.LiveRyAnchorViewHolder.btn_dr_pk_nub; import static com.yunbao.live.views.LiveRyAnchorViewHolder.btn_start_dr_pk; import static com.yunbao.live.views.LiveRyAnchorViewHolder.btn_start_dr_pk_view; @@ -49,7 +48,6 @@ import com.yunbao.common.http.CommonHttpConsts; import com.yunbao.common.http.CommonHttpUtil; import com.yunbao.common.http.HttpCallback; import com.yunbao.common.http.HttpClient; -import com.yunbao.common.http.live.LiveNetManager; import com.yunbao.common.manager.IMLoginManager; import com.yunbao.common.manager.IMRTCManager; import com.yunbao.common.utils.BitmapUtil; @@ -71,7 +69,6 @@ import com.yunbao.live.bean.LiveReceiveGiftBean; import com.yunbao.common.bean.LiveUserGiftBean; import com.yunbao.common.bean.MicUserBean; import com.yunbao.live.dialog.LiveLinkMicListDialogFragment; -import com.yunbao.live.dialog.LiveMicAnchorDialogFragment; import com.yunbao.live.dialog.LiveNewFunctionDialogFragment; import com.yunbao.live.dialog.LiveNewWishListDialogFragment; import com.yunbao.live.dialog.LiveWishListDialogFragment4Audience; @@ -90,7 +87,7 @@ import com.yunbao.live.socket.SocketRyChatUtil; import com.yunbao.live.socket.SocketRyClient; import com.yunbao.live.socket.SocketSendBean; import com.yunbao.common.utils.MicStatusManager; -import com.yunbao.live.utils.RandomPkManager; +import com.yunbao.common.manager.RandomPkManager; import com.yunbao.live.views.LiveEndViewHolder; import com.yunbao.live.views.LiveMusicViewHolder; import com.yunbao.live.views.LiveNewReadyRyViewHolder; @@ -102,9 +99,7 @@ import org.greenrobot.eventbus.Subscribe; import org.greenrobot.eventbus.ThreadMode; import java.io.File; -import java.util.ArrayList; import java.util.List; -import java.util.TreeMap; import cn.rongcloud.rtc.api.RCRTCEngine; import cn.rongcloud.rtc.api.callback.IRCRTCResultCallback; @@ -158,13 +153,35 @@ public class LiveRyAnchorActivity extends LiveActivity implements LiveFunctionCl private final RandomPkManager.OnRandomPkTimer onRandomPkTimer = new RandomPkManager.OnRandomPkTimer() { @Override public void onTimer(String time) { - + if (mLiveRoomViewHolder != null) { + mLiveRoomViewHolder.setRandomPkTimer(String.format(WordUtil.getString(R.string.random_pk_info_btn_ing), time)); + } } @Override public void onStartPK(String pkUid) { - ToastUtil.show("发起随机PK:"+pkUid); - linkDrMicAnchorApply(pkUid, pkUid, SOCKET_LIVE_DRPK_RANDOM); + ToastUtil.show("发起随机PK:" + pkUid); + JSONObject msg1 = new JSONObject(); + msg1.put("uid", CommonAppConfig.getInstance().getUid()); + msg1.put("pkuid", CommonAppConfig.getInstance().getUid()); + msg1.put("pkname", CommonAppConfig.getInstance().getUserBean().getUserNiceName()); + msg1.put("pkhead", CommonAppConfig.getInstance().getUserBean().getAvatar()); + msg1.put("randomPk", true); + linkMicAnchorApply(pkUid, pkUid, msg1.toString()); + } + + @Override + public void onPking() { + if (mLiveRoomViewHolder != null) { + mLiveRoomViewHolder.setRandomPkTimer(null); + } + } + + @Override + public void onPkEnd() { + if (mLiveRoomViewHolder != null) { + mLiveRoomViewHolder.setRandomPkTimer(null); + } } }; @@ -1047,7 +1064,16 @@ public class LiveRyAnchorActivity extends LiveActivity implements LiveFunctionCl * @param pkUid 对方主播的uid * @param stream 对方主播的stream */ - public void linkMicAnchorApply(final String pkUid, String stream) { + public void linkMicAnchorApply(String pkUid, String stream) { + JSONObject msg1 = new JSONObject(); + msg1.put("uid", CommonAppConfig.getInstance().getUid()); + msg1.put("pkuid", CommonAppConfig.getInstance().getUid()); + msg1.put("pkname", CommonAppConfig.getInstance().getUserBean().getUserNiceName()); + msg1.put("pkhead", CommonAppConfig.getInstance().getUserBean().getAvatar()); + linkMicAnchorApply(pkUid, stream, msg1.toString()); + } + + public void linkMicAnchorApply(final String pkUid, String stream, String extra) { LiveHttpUtil.livePkCheckLive(pkUid, stream, mStream, new HttpCallback() { @Override @@ -1056,15 +1082,11 @@ public class LiveRyAnchorActivity extends LiveActivity implements LiveFunctionCl JSONObject obj = JSON.parseObject(info[0]); if (obj != null) { if (obj.getString("ispk").equals("0")) { - JSONObject msg1 = new JSONObject(); - msg1.put("uid", CommonAppConfig.getInstance().getUid()); - msg1.put("pkuid", CommonAppConfig.getInstance().getUid()); - msg1.put("pkname", CommonAppConfig.getInstance().getUserBean().getUserNiceName()); - msg1.put("pkhead", CommonAppConfig.getInstance().getUserBean().getAvatar()); - rtcRoom.getLocalUser().requestJoinOtherRoom(pkUid, pkUid, true, msg1.toString(), new IRCRTCResultCallback() { + + IMRTCManager.getInstance().requestJoinOtherRoom(pkUid, true, extra, new IRCRTCResultCallback() { @Override public void onSuccess() { - ToastUtil.show("邀请 " + pkUid + " 发送成功"); + // ToastUtil.show("邀请 " + pkUid + " 发送成功"); runOnUiThread(new Runnable() { @Override public void run() { @@ -1125,7 +1147,7 @@ public class LiveRyAnchorActivity extends LiveActivity implements LiveFunctionCl if (obj != null) { if (obj.getString("ispk").equals("0")) { - IMRTCManager.getInstance().requestJoinOtherRoom(pkUid, true, extra, new IRCRTCResultCallback() { + IMRTCManager.getInstance().requestJoinOtherRoom(pkUid, true, extra, new IRCRTCResultCallback() { @Override public void onSuccess() { if (!extra.equals(SOCKET_LIVE_DRPK_RANDOM)) { diff --git a/live/src/main/java/com/yunbao/live/dialog/FreePkDialogFragment.java b/live/src/main/java/com/yunbao/live/dialog/FreePkDialogFragment.java new file mode 100644 index 000000000..d87a6b158 --- /dev/null +++ b/live/src/main/java/com/yunbao/live/dialog/FreePkDialogFragment.java @@ -0,0 +1,271 @@ +package com.yunbao.live.dialog; + +import android.app.Dialog; +import android.os.Bundle; +import android.os.Handler; +import android.os.Looper; +import android.text.Editable; +import android.text.TextWatcher; +import android.util.Log; +import android.view.Gravity; +import android.view.View; +import android.view.Window; +import android.view.WindowManager; +import android.widget.EditText; +import android.widget.ImageView; +import android.widget.TextView; + +import androidx.annotation.Nullable; +import androidx.recyclerview.widget.LinearLayoutManager; + +import com.alibaba.fastjson.JSON; +import com.google.android.material.tabs.TabLayout; +import com.yunbao.common.adapter.RefreshAdapter; +import com.yunbao.common.bean.HttpCallbackModel; +import com.yunbao.common.bean.RandomPkUserBean; +import com.yunbao.common.custom.CommonRefreshView; +import com.yunbao.common.dialog.AbsDialogFragment; +import com.yunbao.common.glide.ImgLoader; +import com.yunbao.common.http.HttpCallback; +import com.yunbao.common.http.live.LiveNetManager; +import com.yunbao.common.manager.RandomPkManager; +import com.yunbao.common.utils.DialogUitl; +import com.yunbao.common.utils.ToastUtil; +import com.yunbao.common.utils.WordUtil; +import com.yunbao.common.utils.WordsTypeUtil; +import com.yunbao.live.R; +import com.yunbao.live.adapter.RandomPkRecyclerAdapter; +import com.yunbao.live.bean.LivePkBean; +import com.yunbao.live.http.LiveHttpUtil; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Timer; +import java.util.TimerTask; + +/** + * 自由PK + */ +public class FreePkDialogFragment extends AbsDialogFragment implements View.OnClickListener { + private static final String TAG = "自由PK"; + private int mLiveUid; + private TabLayout tabLayout; + private View reset; + private CommonRefreshView mRecyclerView; + private View mPkInfoLayout; + private View mSearchLayout; + private EditText mSearch; + private ImageView mClear; + private RandomPkRecyclerAdapter adapter; + private DataHelper helper; + + + + @Override + protected int getLayoutId() { + return R.layout.dialog_live_free_pk_function; + } + + @Override + protected int getDialogStyle() { + return R.style.dialog2; + } + + @Override + protected boolean canCancel() { + return true; + } + + @Override + protected void setWindowAttributes(Window window) { + WindowManager.LayoutParams params = window.getAttributes(); + window.setWindowAnimations(R.style.bottomToTopAnim); + params.width = WindowManager.LayoutParams.MATCH_PARENT; + params.height = WindowManager.LayoutParams.WRAP_CONTENT; + params.gravity = Gravity.BOTTOM; + window.setAttributes(params); + } + + public void setLiveUid(int mLiveUid) { + this.mLiveUid = mLiveUid; + } + + @Override + public void onDestroy() { + super.onDestroy(); + } + + @Override + public void onActivityCreated(@Nullable Bundle savedInstanceState) { + super.onActivityCreated(savedInstanceState); + initView(); + initTab(); + initData(); + } + + private void initView() { + tabLayout = (TabLayout) findViewById(R.id.menu_tab); + reset = findViewById(R.id.menu_reset); + mRecyclerView = (CommonRefreshView) findViewById(R.id.random_container_view); + mPkInfoLayout = findViewById(R.id.layout_random_pk_info); + mSearchLayout = findViewById(R.id.random_pk_search_layout); + mSearch = (EditText) findViewById(R.id.search_edit); + mClear = (ImageView) findViewById(R.id.search_clear); + mClear.setOnClickListener(this); + mSearch.addTextChangedListener(new TextWatcher() { + @Override + public void beforeTextChanged(CharSequence s, int start, int count, int after) { + + } + + @Override + public void onTextChanged(CharSequence s, int start, int before, int count) { + helper.search(s.toString()); + } + + @Override + public void afterTextChanged(Editable s) { + + } + }); + + initRecycler(); + } + + private void initRecycler() { + helper = new DataHelper(); + adapter = new RandomPkRecyclerAdapter(mContext); + mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false)); + mRecyclerView.setDataHelper(helper); + mRecyclerView.initData(); + } + + + private void initData() { + } + + private void initTab() { + TabLayout.Tab searchTag = tabLayout.newTab(); + TabLayout.Tab listTag = tabLayout.newTab(); + searchTag.setTag(1); + searchTag.setText("主播搜索"); + listTag.setTag(2); + listTag.setText("關注列表"); + tabLayout.addTab(searchTag); + tabLayout.addTab(listTag); + tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { + @Override + public void onTabSelected(TabLayout.Tab tab) { + if (tab.getTag() != null) { + mPkInfoLayout.setVisibility(View.GONE); + mRecyclerView.setVisibility(View.GONE); + mSearchLayout.setVisibility(View.INVISIBLE); + switch ((int) tab.getTag()) { + case 1: + mSearchLayout.setVisibility(View.VISIBLE); + mRecyclerView.setVisibility(View.VISIBLE); + mRecyclerView.initData(); + break; + case 2: + mRecyclerView.setVisibility(View.VISIBLE); + mRecyclerView.initData(); + break; + } + } + } + + @Override + public void onTabUnselected(TabLayout.Tab tab) { + + } + + @Override + public void onTabReselected(TabLayout.Tab tab) { + + } + }); + } + + + + + @Override + public void onClick(View v) { + int id = v.getId(); + if (id == R.id.live_random_pk_switch) { + + } else if (id == R.id.search_clear) { + mSearch.setText(""); + } + } + + public class DataHelper implements CommonRefreshView.DataHelper { + + public void search(String key) { + LiveNetManager.get(mContext) + .randomPkSearchUser(key, WordsTypeUtil.changeTraditional(key), new com.yunbao.common.http.base.HttpCallback>() { + @Override + public void onSuccess(List data) { + List list = new ArrayList<>(); + list.add(null); + list.addAll(data); + adapter.setList(list); + adapter.notifyDataSetChanged(); + } + + @Override + public void onError(String error) { + } + }); + } + + @Override + public RefreshAdapter getAdapter() { + return adapter; + } + + @Override + public void loadData(int p, HttpCallback callback) { + LiveHttpUtil.getLivePkList(p, callback); + } + + @Override + public List processData(String[] info) { + Log.i(TAG, "processData: 获取数据成功"); + List beans = JSON.parseArray(Arrays.toString(info), LivePkBean.class); + List list = new ArrayList<>(); + for (LivePkBean bean : beans) { + RandomPkUserBean userBean = new RandomPkUserBean(); + userBean.setId(bean.getUid()); + userBean.setSex(bean.getSex()); + userBean.setUserNiceName(bean.getUserNiceName()); + userBean.setAvatar(bean.getAvatar()); + userBean.setPk(bean.getPkUid().equals("0") ? 0 : 1); + userBean.setAttention("0"); + list.add(userBean); + } + return list; + } + + @Override + public void onRefreshSuccess(List list, int listCount) { + + } + + @Override + public void onRefreshFailure() { + + } + + @Override + public void onLoadMoreSuccess(List loadItemList, int loadItemCount) { + + } + + @Override + public void onLoadMoreFailure() { + + } + } +} diff --git a/live/src/main/java/com/yunbao/live/dialog/RandomPkDialogFragment.java b/live/src/main/java/com/yunbao/live/dialog/RandomPkDialogFragment.java index 1b0f03d5e..aceef4c12 100644 --- a/live/src/main/java/com/yunbao/live/dialog/RandomPkDialogFragment.java +++ b/live/src/main/java/com/yunbao/live/dialog/RandomPkDialogFragment.java @@ -2,11 +2,12 @@ package com.yunbao.live.dialog; import android.app.Dialog; import android.os.Bundle; +import android.os.Handler; +import android.os.Looper; import android.text.Editable; import android.text.TextWatcher; import android.util.Log; import android.view.Gravity; -import android.view.KeyEvent; import android.view.View; import android.view.Window; import android.view.WindowManager; @@ -18,7 +19,6 @@ import androidx.annotation.Nullable; import androidx.recyclerview.widget.LinearLayoutManager; import com.alibaba.fastjson.JSON; -import com.alibaba.fastjson.JSONArray; import com.google.android.material.tabs.TabLayout; import com.yunbao.common.adapter.RefreshAdapter; import com.yunbao.common.bean.HttpCallbackModel; @@ -36,43 +36,69 @@ import com.yunbao.live.R; import com.yunbao.live.adapter.RandomPkRecyclerAdapter; import com.yunbao.live.bean.LivePkBean; import com.yunbao.live.http.LiveHttpUtil; -import com.yunbao.live.utils.RandomPkManager; +import com.yunbao.common.manager.RandomPkManager; import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import java.util.Timer; +import java.util.TimerTask; /** * 随机PK */ public class RandomPkDialogFragment extends AbsDialogFragment implements View.OnClickListener { private static final String TAG = "随机PK"; - private int mLiveUid; - private TabLayout tabLayout; - private View reset; - private CommonRefreshView mRecyclerView; - private View mPkInfoLayout; - private View mSearchLayout; private View mPkBtn; private TextView mPkBtnTitle; private TextView mPkBtnDesc; private ImageView mRandomPkSwitch; - private EditText mSearch; - private ImageView mClear; - private RandomPkRecyclerAdapter adapter; - private DataHelper helper; private final RandomPkManager.OnRandomPkTimer randomPkTimer = new RandomPkManager.OnRandomPkTimer() { @Override public void onTimer(String time) { - Log.i(TAG, "onTimer: "+time); + if (!mPkBtn.isEnabled()) { + return; + } + Log.i(TAG, "onTimer: " + time); mPkBtnTitle.setText(String.format(WordUtil.getString(R.string.random_pk_info_btn_ing), time)); + mPkBtnDesc.setText(R.string.random_pk_info_btn_end_desc); + mPkBtnDesc.setVisibility(View.VISIBLE); + mPkBtn.setBackgroundResource(R.drawable.bg_live_room_random_pk_timer); } @Override - public void onStartPK(String pkUid) { + public void onPking() { + resetPkBtn(); + dismiss(); + } + @Override + public void onPkEnd() { + resetPkBtn(); + } + + @Override + public void onPkEndStart() { + super.onPkEndStart(); + mPkBtn.setEnabled(false); + } + + @Override + public void onPkEndTimer(String time) { + super.onPkEndTimer(time); + mPkBtnTitle.setText(String.format(WordUtil.getString(R.string.random_pk_info_btn_end), time)); + mPkBtnDesc.setText(R.string.random_pk_info_btn_end_desc); + mPkBtn.setEnabled(false); + } + + @Override + public void onPkEndSuccess() { + super.onPkEndSuccess(); + resetPkBtn(); + mPkBtn.setTag(false); + mPkBtn.setEnabled(true); } }; @@ -101,9 +127,6 @@ public class RandomPkDialogFragment extends AbsDialogFragment implements View.On window.setAttributes(params); } - public void setLiveUid(int mLiveUid) { - this.mLiveUid = mLiveUid; - } @Override public void onDestroy() { @@ -115,110 +138,34 @@ public class RandomPkDialogFragment extends AbsDialogFragment implements View.On public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); initView(); - initTab(); initData(); } private void initView() { - tabLayout = (TabLayout) findViewById(R.id.menu_tab); - reset = findViewById(R.id.menu_reset); - mRecyclerView = (CommonRefreshView) findViewById(R.id.random_container_view); - mPkInfoLayout = findViewById(R.id.layout_random_pk_info); - mSearchLayout = findViewById(R.id.random_pk_search_layout); mPkBtn = findViewById(R.id.random_pk_info_btn); mPkBtnTitle = (TextView) findViewById(R.id.random_pk_btn_title); mPkBtnDesc = (TextView) findViewById(R.id.random_pk_btn_desc); mRandomPkSwitch = (ImageView) findViewById(R.id.live_random_pk_switch); - mSearch = (EditText) findViewById(R.id.search_edit); - mClear = (ImageView) findViewById(R.id.search_clear); - mClear.setOnClickListener(this); mRandomPkSwitch.setTag(true); mRandomPkSwitch.setOnClickListener(this); mPkBtn.setOnClickListener(this); - mSearch.addTextChangedListener(new TextWatcher() { - @Override - public void beforeTextChanged(CharSequence s, int start, int count, int after) { - - } - - @Override - public void onTextChanged(CharSequence s, int start, int before, int count) { - helper.search(s.toString()); - } - - @Override - public void afterTextChanged(Editable s) { - - } - }); - - initRecycler(); } - private void initRecycler() { - helper = new DataHelper(); - adapter = new RandomPkRecyclerAdapter(mContext); - mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false)); - mRecyclerView.setDataHelper(helper); - mRecyclerView.initData(); - } - - private void initData() { RandomPkManager.getInstance().addOnRandomPkTimer(randomPkTimer); initPkSwitch(); + if (RandomPkManager.getInstance().isPking()) { + mPkBtnTitle.setText(String.format(WordUtil.getString(R.string.random_pk_info_btn_ing), RandomPkManager.getInstance().getTimer())); + mPkBtnDesc.setText(R.string.random_pk_info_btn_end_desc); + mPkBtnDesc.setVisibility(View.VISIBLE); + mPkBtn.setBackgroundResource(R.drawable.bg_live_room_random_pk_timer); + }else if(RandomPkManager.getInstance().isExiting()){ + mPkBtnTitle.setText(String.format(WordUtil.getString(R.string.random_pk_info_btn_end), RandomPkManager.getInstance().getExitTimer())); + mPkBtnDesc.setText(R.string.random_pk_info_btn_end_desc); + mPkBtn.setEnabled(false); + } } - private void initTab() { - TabLayout.Tab randPkTag = tabLayout.newTab(); - TabLayout.Tab searchTag = tabLayout.newTab(); - TabLayout.Tab listTag = tabLayout.newTab(); - randPkTag.setTag(1); - randPkTag.setText("隨機PK"); - searchTag.setTag(2); - searchTag.setText("主播搜索"); - listTag.setTag(3); - listTag.setText("關注列表"); - tabLayout.addTab(randPkTag); - tabLayout.addTab(searchTag); - tabLayout.addTab(listTag); - tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { - @Override - public void onTabSelected(TabLayout.Tab tab) { - if (tab.getTag() != null) { - mPkInfoLayout.setVisibility(View.GONE); - mRecyclerView.setVisibility(View.GONE); - mSearchLayout.setVisibility(View.INVISIBLE); - switch ((int) tab.getTag()) { - case 1: - mSearch.setText(""); - mSearchLayout.setVisibility(View.GONE); - mPkInfoLayout.setVisibility(View.VISIBLE); - break; - case 2: - mSearchLayout.setVisibility(View.VISIBLE); - mRecyclerView.setVisibility(View.VISIBLE); - mRecyclerView.initData(); - break; - case 3: - mRecyclerView.setVisibility(View.VISIBLE); - mRecyclerView.initData(); - break; - } - } - } - - @Override - public void onTabUnselected(TabLayout.Tab tab) { - - } - - @Override - public void onTabReselected(TabLayout.Tab tab) { - - } - }); - } /** * 设置随机PK开关状态 @@ -287,22 +234,28 @@ public class RandomPkDialogFragment extends AbsDialogFragment implements View.On }); } + private void randomPk() { - if (mPkBtn.getTag() != null && (boolean) mPkBtn.getTag()) { - RandomPkManager.getInstance().end(); - resetPkBtn(); - mPkBtn.setTag(false); + if (mRandomPkSwitch.getTag() == null || !(boolean) mRandomPkSwitch.getTag()) { + ToastUtil.show("未开启随机PK开关"); return; } - mPkBtnDesc.setVisibility(View.VISIBLE); - mPkBtnDesc.setText(R.string.random_pk_info_btn_ing_desc); - RandomPkManager.getInstance().start(); - mPkBtn.setTag(true); + if ((mPkBtn.getTag() != null && (boolean) mPkBtn.getTag()) || RandomPkManager.getInstance().isPking()) { + RandomPkManager.getInstance().exitPk(); + return; + } + + if (RandomPkManager.getInstance().start()) { + mPkBtnDesc.setVisibility(View.VISIBLE); + mPkBtnDesc.setText(R.string.random_pk_info_btn_ing_desc); + mPkBtn.setTag(true); + } } private void resetPkBtn() { mPkBtnDesc.setVisibility(View.GONE); mPkBtnTitle.setText(R.string.random_pk_info_btn_start); + mPkBtn.setBackgroundResource(R.drawable.bg_live_random_pk_info_btn); } @Override @@ -314,79 +267,8 @@ public class RandomPkDialogFragment extends AbsDialogFragment implements View.On } else { setPkSwitch(1); } - } else if (id == R.id.search_clear) { - mSearch.setText(""); } else if (id == R.id.random_pk_info_btn) { randomPk(); } } - - public class DataHelper implements CommonRefreshView.DataHelper { - - public void search(String key) { - LiveNetManager.get(mContext) - .randomPkSearchUser(key, WordsTypeUtil.changeTraditional(key), new com.yunbao.common.http.base.HttpCallback>() { - @Override - public void onSuccess(List data) { - List list = new ArrayList<>(); - list.add(null); - list.addAll(data); - adapter.setList(list); - adapter.notifyDataSetChanged(); - } - - @Override - public void onError(String error) { - } - }); - } - - @Override - public RefreshAdapter getAdapter() { - return adapter; - } - - @Override - public void loadData(int p, HttpCallback callback) { - LiveHttpUtil.getLivePkList(p, callback); - } - - @Override - public List processData(String[] info) { - Log.i(TAG, "processData: 获取数据成功"); - List beans = JSON.parseArray(Arrays.toString(info), LivePkBean.class); - List list = new ArrayList<>(); - for (LivePkBean bean : beans) { - RandomPkUserBean userBean = new RandomPkUserBean(); - userBean.setId(bean.getUid()); - userBean.setSex(bean.getSex()); - userBean.setUserNiceName(bean.getUserNiceName()); - userBean.setAvatar(bean.getAvatar()); - userBean.setPk(bean.getPkUid().equals("0") ? 0 : 1); - userBean.setAttention("0"); - list.add(userBean); - } - return list; - } - - @Override - public void onRefreshSuccess(List list, int listCount) { - - } - - @Override - public void onRefreshFailure() { - - } - - @Override - public void onLoadMoreSuccess(List loadItemList, int loadItemCount) { - - } - - @Override - public void onLoadMoreFailure() { - - } - } } diff --git a/live/src/main/java/com/yunbao/live/presenter/LiveRyLinkMicPkPresenter.java b/live/src/main/java/com/yunbao/live/presenter/LiveRyLinkMicPkPresenter.java index 949ad09a8..f8e9d3078 100644 --- a/live/src/main/java/com/yunbao/live/presenter/LiveRyLinkMicPkPresenter.java +++ b/live/src/main/java/com/yunbao/live/presenter/LiveRyLinkMicPkPresenter.java @@ -27,6 +27,7 @@ import com.yunbao.common.bean.UserBean; import com.yunbao.common.http.HttpCallback; import com.yunbao.common.http.HttpClient; import com.yunbao.common.manager.IMRTCManager; +import com.yunbao.common.manager.RandomPkManager; import com.yunbao.common.utils.Bus; import com.yunbao.common.utils.DpUtil; import com.yunbao.common.utils.ScreenDimenUtil; @@ -599,7 +600,7 @@ public class LiveRyLinkMicPkPresenter implements View.OnClickListener { * @param callBack 加入房间回调 * @group 房间管理 */ - + RandomPkManager.getInstance().setPkStatus(RandomPkManager.PK_STATUS_START); RCRTCEngine.getInstance().joinOtherRoom(u.getId(), new IRCRTCResultDataCallback() { @Override public void onSuccess(RCRTCOtherRoom rcrtcOtherRoom) { @@ -1996,7 +1997,7 @@ public class LiveRyLinkMicPkPresenter implements View.OnClickListener { if (mIsAnchor) { ((LiveRyAnchorActivity) mContext).setPkBtnVisible(true); } - ToastUtil.show(R.string.link_mic_refuse_pk); + // ToastUtil.show(R.string.link_mic_refuse_pk); } /** diff --git a/live/src/main/java/com/yunbao/live/socket/SocketRyClient.java b/live/src/main/java/com/yunbao/live/socket/SocketRyClient.java index fc9d7ff9d..b802242c8 100644 --- a/live/src/main/java/com/yunbao/live/socket/SocketRyClient.java +++ b/live/src/main/java/com/yunbao/live/socket/SocketRyClient.java @@ -19,7 +19,6 @@ import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.blankj.utilcode.util.GsonUtils; -import com.google.gson.reflect.TypeToken; import com.yunbao.common.CommonAppConfig; import com.yunbao.common.CommonAppContext; import com.yunbao.common.Constants; @@ -50,6 +49,7 @@ import com.yunbao.live.bean.LiveLuckGiftWinBean; import com.yunbao.live.bean.LivePKUserListBean; import com.yunbao.live.bean.LiveReceiveGiftBean; import com.yunbao.live.event.LiveAudienceEvent; +import com.yunbao.common.manager.RandomPkManager; import com.yunbao.live.views.LiveEndViewHolder; import com.yunbao.live.views.LivePlayKsyViewHolder; import com.yunbao.live.views.LivePlayRyViewHolder; @@ -1048,17 +1048,21 @@ public class SocketRyClient { mListener.onLinkMicPkApply(u, map.getString("stream"), 1); break; case 2://收到对方主播PK回调 + RandomPkManager.getInstance().setPkStatus(RandomPkManager.PK_STATUS_START); mListener.onLinkMicToPk(map.getString("uid"), map.getString("pkhead"), map.getString("pkname")); mListener.onLinkMicPkStart(map.getString("uid"), map.getString("pkhead"), map.getString("pkname"), map.getString("is_ladders"));// mListener.onLinkMicPkStart(map.getString("uid")); break; case 3://对方主播拒绝PK的回调 + RandomPkManager.getInstance().setPkStatus(RandomPkManager.PK_STATUS_REFUSE); mListener.onLinkMicPkRefuse(); break; case 4://所有人收到PK开始址的回调 + // RandomPkManager.getInstance().setPkStatus(RandomPkManager.PK_STATUS_START); mListener.onLinkMicPkStart(map.getString("pkuid"), map.getString("pkhead"), map.getString("pkname"), map.getString("is_ladders")); break; case 5://PK时候断开连麦的回调 if (rtcRoom != null) { + RandomPkManager.getInstance().setPkStatus(RandomPkManager.PK_STATUS_CLOSE); if (LivePushRyViewHolder.mPreView1 != null) { LivePushRyViewHolder.mPreView1.removeAllViews(); LivePushRyViewHolder.mPreView1.setVisibility(View.GONE); diff --git a/live/src/main/java/com/yunbao/live/utils/RandomPkManager.java b/live/src/main/java/com/yunbao/live/utils/RandomPkManager.java deleted file mode 100644 index 59dbc4249..000000000 --- a/live/src/main/java/com/yunbao/live/utils/RandomPkManager.java +++ /dev/null @@ -1,103 +0,0 @@ -package com.yunbao.live.utils; - -import android.content.Context; -import android.os.Handler; -import android.os.Looper; - -import com.yunbao.common.CommonAppContext; -import com.yunbao.common.http.base.HttpCallback; -import com.yunbao.common.http.live.LiveNetManager; - -import java.util.ArrayList; -import java.util.List; -import java.util.Locale; -import java.util.Timer; -import java.util.TimerTask; - -public class RandomPkManager { - private static RandomPkManager manager; - private int min = -1; - private int sec = -1; - private Timer timer; - private TimerTask task; - private List randomPkTimer = new ArrayList<>(); - - private RandomPkManager() { - initTask(); - } - - public static RandomPkManager getInstance() { - if (manager == null) { - manager = new RandomPkManager(); - } - return manager; - } - - public void addOnRandomPkTimer(OnRandomPkTimer randomPkTimer) { - this.randomPkTimer.add(randomPkTimer); - } - - public void unregisterOnRandomPkTimer(OnRandomPkTimer randomPkTimer) { - this.randomPkTimer.remove(randomPkTimer); - } - - private void initVal() { - min = -1; - sec = -1; - } - - private void initTask() { - task = new TimerTask() { - Handler handler = new Handler(Looper.getMainLooper()); - - @Override - public void run() { - sec++; - if (sec % 60 == 0) { - min++; - sec = 0; - } - handler.post(() -> { - for (OnRandomPkTimer onRandomPkTimer : randomPkTimer) { - onRandomPkTimer.onTimer(String.format(Locale.CHINA, "%02d:%02d", min, sec)); - } - if (sec == 5) { - LiveNetManager.get(CommonAppContext.getTopActivity()) - .randomPK(new HttpCallback() { - @Override - public void onSuccess(String data) { - for (OnRandomPkTimer pkTimer : randomPkTimer) { - pkTimer.onStartPK(data); - } - } - - @Override - public void onError(String error) { - - } - }); - } - }); - } - }; - } - - public void start() { - task.cancel(); - initTask(); - initVal(); - timer = new Timer(); - timer.schedule(task, 0, 1000); - - } - - public void end() { - task.cancel(); - } - - public interface OnRandomPkTimer { - void onTimer(String time); - - void onStartPK(String pkUid); - } -} diff --git a/live/src/main/java/com/yunbao/live/views/LivePushRyViewHolder.java b/live/src/main/java/com/yunbao/live/views/LivePushRyViewHolder.java index 4e0b1595e..b519801fc 100644 --- a/live/src/main/java/com/yunbao/live/views/LivePushRyViewHolder.java +++ b/live/src/main/java/com/yunbao/live/views/LivePushRyViewHolder.java @@ -36,6 +36,7 @@ import com.yunbao.common.bean.UserBean; import com.yunbao.common.http.HttpCallback; import com.yunbao.common.http.HttpClient; import com.yunbao.common.manager.IMRTCManager; +import com.yunbao.common.manager.RandomPkManager; import com.yunbao.common.utils.DialogUitl; import com.yunbao.common.utils.DpUtil; import com.yunbao.common.utils.L; diff --git a/live/src/main/java/com/yunbao/live/views/LiveRoomViewHolder.java b/live/src/main/java/com/yunbao/live/views/LiveRoomViewHolder.java index d8b52023f..6681a5ac6 100644 --- a/live/src/main/java/com/yunbao/live/views/LiveRoomViewHolder.java +++ b/live/src/main/java/com/yunbao/live/views/LiveRoomViewHolder.java @@ -86,6 +86,7 @@ import com.yunbao.common.http.live.LiveNetManager; import com.yunbao.common.interfaces.CommonCallback; import com.yunbao.common.interfaces.OnItemClickListener; import com.yunbao.common.manager.IMLoginManager; +import com.yunbao.common.manager.RandomPkManager; import com.yunbao.common.utils.Bus; import com.yunbao.common.utils.DeviceUtils; import com.yunbao.common.utils.DialogUitl; @@ -96,6 +97,7 @@ import com.yunbao.common.utils.SVGAViewUtils; import com.yunbao.common.utils.ScreenDimenUtil; import com.yunbao.common.utils.SpUtil; import com.yunbao.common.utils.StringUtil; +import com.yunbao.common.utils.ToastUtil; import com.yunbao.common.utils.WordUtil; import com.yunbao.common.utils.formatBigNum; import com.yunbao.common.views.AbsViewHolder; @@ -323,6 +325,9 @@ public class LiveRoomViewHolder extends AbsViewHolder implements View.OnClickLis //接口整合新加参数 private GuardUserModel guardUserModel; private ViewFlipper flipper; + private TextView mRandomPkTimer; + + public LiveRoomViewHolder(boolean isRys, int forActivity, Context context, ViewGroup parentView, GifImageView gifImageView, SVGAImageView svgaImageView, ViewGroup liveGiftPrizePoolContainer, WindowManager windowManager) { super(context, parentView); @@ -736,6 +741,7 @@ public class LiveRoomViewHolder extends AbsViewHolder implements View.OnClickLis lt_trickery.setOnClickListener(this); dialog = new Dialog(mContext, R.style.dialog); + mRandomPkTimer = (TextView) findViewById(R.id.random_pk_timer); if (!SpUtil.getInstance().getBooleanValue("private_chat_message_switch")) { msgLayout.setVisibility(View.GONE); @@ -1180,7 +1186,14 @@ public class LiveRoomViewHolder extends AbsViewHolder implements View.OnClickLis showBanner2(); } } - + public void setRandomPkTimer(String timer){ + if(timer==null){ + mRandomPkTimer.setVisibility(View.GONE); + return; + } + mRandomPkTimer.setVisibility(View.VISIBLE); + mRandomPkTimer.setText(timer); + } private synchronized void showBanner2() { if (mBannerList2 != null && mBanner2 != null) { btn_event2.setVisibility(View.VISIBLE); diff --git a/live/src/main/res/drawable/bg_live_room_random_pk_timer.xml b/live/src/main/res/drawable/bg_live_room_random_pk_timer.xml new file mode 100644 index 000000000..f20a8dac9 --- /dev/null +++ b/live/src/main/res/drawable/bg_live_room_random_pk_timer.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/live/src/main/res/layout/dialog_live_free_pk_function.xml b/live/src/main/res/layout/dialog_live_free_pk_function.xml new file mode 100644 index 000000000..5c2d57dae --- /dev/null +++ b/live/src/main/res/layout/dialog_live_free_pk_function.xml @@ -0,0 +1,108 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/live/src/main/res/layout/dialog_live_random_pk_function.xml b/live/src/main/res/layout/dialog_live_random_pk_function.xml index 54024bda4..9bef25c80 100644 --- a/live/src/main/res/layout/dialog_live_random_pk_function.xml +++ b/live/src/main/res/layout/dialog_live_random_pk_function.xml @@ -23,69 +23,13 @@ android:gravity="center" android:orientation="horizontal"> - - - - - - - - - - - - - - + android:text="@string/random_pk_info_view_title" + android:textColor="#F6F7FB" + android:textSize="16sp" /> + + diff --git a/live/src/main/res/values/strings.xml b/live/src/main/res/values/strings.xml index bd1c7c127..813807f9a 100644 --- a/live/src/main/res/values/strings.xml +++ b/live/src/main/res/values/strings.xml @@ -32,6 +32,7 @@ 已踢下麥 已發出邀請,等待用戶接受 已同意請求,等待用戶接受 + 隨機PK PK主要規則說明: 1、開始匹配後,系統將會自動給您挑選對手進行PK; 2、每場PK總時長10分鍾,懲罰時間為1分鍾;