Browse Source

增加直播间关闭倒计时逻辑

Pq 3 years ago
parent
commit
e20e366b7e

+ 40 - 3
BaseLibrary/src/main/java/com/cooleshow/base/utils/DateUtil.java

@@ -335,7 +335,8 @@ public class DateUtil {
 
     /**
      * 格式化时间
-     * @param dateStr  yyyy-MM-dd HH:mm:ss
+     *
+     * @param dateStr yyyy-MM-dd HH:mm:ss
      * @return yyyy-MM-dd HH:mm:ss
      */
     public static Date getDate(String dateStr) {
@@ -352,6 +353,41 @@ public class DateUtil {
         return date;
     }
 
+
+    public static String convertDateTime(long targetTime) {
+        if (targetTime <= 0) {
+            return "00:00:00";
+        }
+        long targetTimeMinute = targetTime / 1000;
+        long h = targetTimeMinute / 3600;
+        long m = (targetTimeMinute % 3600) / 60;
+        long s = targetTimeMinute % 60;
+        StringBuilder sb = new StringBuilder();
+        if (h > 0) {
+            if (h < 10) {
+                String hResult = "0" + h;
+                sb.append(hResult).append(":");
+            } else {
+                sb.append(h).append(":");
+            }
+        } else {
+            sb.append("00").append(":");
+        }
+        if (m < 10) {
+            String mResult = "0" + m;
+            sb.append(mResult).append(":");
+        } else {
+            sb.append(m).append(":");
+        }
+        if (s < 10) {
+            String sResult = "0" + s;
+            sb.append(sResult);
+        } else {
+            sb.append(s);
+        }
+        return sb.toString();
+    }
+
     /**
      * 转化时间输入时间与当前时间的间隔
      *
@@ -990,8 +1026,9 @@ public class DateUtil {
     /**
      * 获得分钟差
      * 如果结束时间还没到 返回时间差
+     *
      * @param begin 当前时间
-     * @param end 课程结束时间
+     * @param end   课程结束时间
      * @return -2 课程已经结束  0  时间一致  课程还没结束 返回时间差
      */
     public static long getMinuteDiff(Date begin, Date end) {
@@ -1031,7 +1068,7 @@ public class DateUtil {
     }
 
 
-    public static long forMatLong(String time,SimpleDateFormat datetimeFormat) {
+    public static long forMatLong(String time, SimpleDateFormat datetimeFormat) {
         try {
             datetimeFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
             Date date = datetimeFormat.parse(time);

+ 12 - 1
teacher/src/main/java/com/cooleshow/teacher/bean/LiveRoomInfoBean.java

@@ -31,7 +31,7 @@ public class LiveRoomInfoBean implements Parcelable {
     public int courseId;
     public String coverPic;
     public int likeNum;
-    public String liveEndTime;
+    public String liveEndTime;//2022-07-26 10:12:07
     public String liveRemark;
     public String liveStartTime;
     public int liveState;
@@ -42,8 +42,19 @@ public class LiveRoomInfoBean implements Parcelable {
     public String speakerId;
     public String speakerName;
     public String speakerPic;
+    public int expiredCloseMinute;
     public int subjectId;
     public String type;
+    public String roomType;
+
+    /**
+     * 服务端返回的是分钟,需按毫秒算
+     *
+     * @return
+     */
+    public int getExpiredCloseMinute() {
+        return expiredCloseMinute * 60 * 1000;
+    }
 
     @Override
     public int describeContents() {

+ 98 - 3
teacher/src/main/java/com/cooleshow/teacher/ui/live/TeacherLiveRoomActivity.java

@@ -26,8 +26,12 @@ import com.cooleshow.base.router.RouterPath;
 import com.cooleshow.base.ui.activity.BaseMVPActivity;
 import com.cooleshow.base.utils.AppUtils;
 import com.cooleshow.base.utils.ClickUtils;
+import com.cooleshow.base.utils.DateUtil;
+import com.cooleshow.base.utils.DateUtils;
+import com.cooleshow.base.utils.LogUtils;
 import com.cooleshow.base.utils.PermissionUtils;
 import com.cooleshow.base.utils.SoftKeyboardUtil;
+import com.cooleshow.base.utils.TimeUtils;
 import com.cooleshow.base.utils.ToastUtil;
 import com.cooleshow.base.utils.Utils;
 import com.cooleshow.base.widgets.InputBar;
@@ -62,6 +66,7 @@ import com.rong.io.live.message.RCUserSeatApplyMessage;
 import com.tbruyelle.rxpermissions3.RxPermissions;
 
 import java.util.ArrayList;
+import java.util.Date;
 import java.util.List;
 import java.util.Map;
 
@@ -103,9 +108,13 @@ import io.rong.message.TextMessage;
 @Route(path = RouterPath.LiveCenter.ACTIVITY_LIVE_ROOM_TEACHER)
 public class TeacherLiveRoomActivity extends BaseMVPActivity<ActivityTeacherLiveRoomLayoutBinding, LiveRoomPresenter> implements LiveRoomContract.LiveRoomView, View.OnClickListener, SoftKeyboardUtil.OnSoftInputChangedListener {
     public static final String ROOMID_KEY = "roomid_key";
+    public static final String DEFAULT_TIME_TEXT = "00:00:00";
     public static final int SYNC_ADD_LIKE_TIME = 10000;//同步点赞数时长(兼心跳功能)
     public static final int SEND_APP_BACKGROUND_MSG = 1001;//退到后台消息
     public static final int SHARE_LIVE_REQUEST_CODE = 1002;//分享直播
+    public static final int LIVE_TIME_COUNT = 1003;//直播倒计时
+    public static final int LIVE_TIME_COUNT_END_TIP = 1004;//直播课剩余时间倒计时
+    public static final int LIVE_TIME_COUNT_SPACE = 1000;//直播倒计时间隔
 
 
     ImageView ivReverseCamera;
@@ -189,12 +198,40 @@ public class TeacherLiveRoomActivity extends BaseMVPActivity<ActivityTeacherLive
                 }
                 return;
             }
+            if (what == LIVE_TIME_COUNT) {
+                //倒计时处理
+                countTimeTotal = countTimeTotal - LIVE_TIME_COUNT_SPACE;
+                if (countTimeTotal <= 0) {
+                    updateTimeText(DEFAULT_TIME_TEXT);
+                    if (checkIsNeedRecount()) {
+                        countTimeTotal = mRoomInfoBean.getExpiredCloseMinute();
+                        sendEndTipCountTimeMessage();
+                    }
+                    return;
+                }
+                String s = DateUtil.convertDateTime(countTimeTotal);
+                updateTimeText(s);
+                sendCountTimeMessage();
+                return;
+            }
+            if (what == LIVE_TIME_COUNT_END_TIP) {
+                countTimeTotal = countTimeTotal - LIVE_TIME_COUNT_SPACE;
+                if (countTimeTotal <= 0) {
+                    updateEndTimeTipText(DEFAULT_TIME_TEXT);
+                    return;
+                }
+                String s = DateUtil.convertDateTime(countTimeTotal);
+                updateEndTimeTipText(s);
+                sendEndTipCountTimeMessage();
+                return;
+            }
         }
     };
 
     private LiveRoomManagerDialog mRoomManagerDialog;
     private CommonConfirmDialog mConfirmDialog;
     private boolean isOnResume = false;
+    private long countTimeTotal = 0;
 
     public static void start(Context context, String roomId) {
         Intent intent = new Intent(context, TeacherLiveRoomActivity.class);
@@ -319,6 +356,23 @@ public class TeacherLiveRoomActivity extends BaseMVPActivity<ActivityTeacherLive
                 });
     }
 
+
+    private void updateTimeText(String text) {
+        if (viewBinding.viewMainLayout.tvTimeCount != null) {
+            viewBinding.viewMainLayout.tvTimeCount.setVisibility(View.VISIBLE);
+            viewBinding.viewMainLayout.tvTimeCountEndTip.setVisibility(View.GONE);
+            viewBinding.viewMainLayout.tvTimeCount.setText(text);
+        }
+    }
+
+    private void updateEndTimeTipText(String text) {
+        if (viewBinding.viewMainLayout.tvTimeCountEndTip != null) {
+            viewBinding.viewMainLayout.tvTimeCount.setVisibility(View.GONE);
+            viewBinding.viewMainLayout.tvTimeCountEndTip.setVisibility(View.VISIBLE);
+            viewBinding.viewMainLayout.tvTimeCountEndTip.setText("直播关闭: " + text);
+        }
+    }
+
     private void initListener() {
         IMCenter.getInstance().addConnectionStatusListener(connectStatusListener);
         SoftKeyboardUtil.registerSoftInputChangedListener(getWindow(), this);
@@ -443,6 +497,7 @@ public class TeacherLiveRoomActivity extends BaseMVPActivity<ActivityTeacherLive
             finish();
             return;
         }
+
         mRoomInfoBean = roomInfoBean;
         if (mMessageAdapter != null) {
             mMessageAdapter.setRoomAuthorId(roomInfoBean.speakerId);
@@ -471,6 +526,33 @@ public class TeacherLiveRoomActivity extends BaseMVPActivity<ActivityTeacherLive
         }
     }
 
+    private void handleTimerCount() {
+        if (mRoomInfoBean == null) {
+            return;
+        }
+        Date date = DateUtil.str2Date(mRoomInfoBean.liveEndTime);
+        long currentTime = System.currentTimeMillis();
+        if (date != null) {
+            long timeResult = date.getTime() - currentTime;
+            countTimeTotal = timeResult > 0 ? timeResult : 0;
+            sendCountTimeMessage();
+        }
+    }
+
+    private void sendCountTimeMessage() {
+        mHandler.removeMessages(LIVE_TIME_COUNT);
+        android.os.Message message = android.os.Message.obtain();
+        message.what = LIVE_TIME_COUNT;
+        mHandler.sendMessageDelayed(message, LIVE_TIME_COUNT_SPACE);
+    }
+
+    private void sendEndTipCountTimeMessage() {
+        mHandler.removeMessages(LIVE_TIME_COUNT_END_TIP);
+        android.os.Message message = android.os.Message.obtain();
+        message.what = LIVE_TIME_COUNT_END_TIP;
+        mHandler.sendMessageDelayed(message, LIVE_TIME_COUNT_SPACE);
+    }
+
     /**
      * 是否是预览模式
      *
@@ -817,6 +899,7 @@ public class TeacherLiveRoomActivity extends BaseMVPActivity<ActivityTeacherLive
         Log.i("pq", "onPublishSuccess");
         //通知开启直播间回放
         switchOpsLiveVideoMode(LiveConfig.OPEN_OPS_LIVE_VIDEO_TYPE);
+        handleTimerCount();
         refresh();
     }
 
@@ -1254,9 +1337,9 @@ public class TeacherLiveRoomActivity extends BaseMVPActivity<ActivityTeacherLive
                 String targetId = data.getStringExtra("targetId");
                 int conversationValue = data.getIntExtra("conversation", -1);
                 if (mRoomInfoBean != null && !TextUtils.isEmpty(targetId) && conversationValue != -1) {
-                    OpenChatHelper.sendShareLiveMessage(targetId,conversationValue,mRoomInfoBean.roomUid
-                            ,mRoomInfoBean.speakerPic,
-                            mRoomInfoBean.speakerName,mRoomInfoBean.liveRemark);
+                    OpenChatHelper.sendShareLiveMessage(targetId, conversationValue, mRoomInfoBean.roomUid
+                            , mRoomInfoBean.speakerPic,
+                            mRoomInfoBean.speakerName, mRoomInfoBean.liveRemark);
                 }
             }
         }
@@ -1285,6 +1368,18 @@ public class TeacherLiveRoomActivity extends BaseMVPActivity<ActivityTeacherLive
         }
     }
 
+    /**
+     * 直播课需要有一个额外时间提示
+     */
+    private boolean checkIsNeedRecount() {
+        if (mRoomInfoBean != null) {
+            if (!TextUtils.equals(mRoomInfoBean.roomType, "TEMP") && mRoomInfoBean.expiredCloseMinute != 0) {
+                return true;
+            }
+        }
+        return false;
+    }
+
     private boolean checkActivityIsFinish() {
         if (isFinishing() || isDestroyed()) {
             return true;

+ 34 - 0
teacher/src/main/res/layout/view_live_room_main_layout.xml

@@ -119,6 +119,40 @@
                 tools:text="本场点赞1240000000" />
         </LinearLayout>
 
+        <FrameLayout
+            android:layout_width="wrap_content"
+            android:layout_height="18dp"
+            android:layout_marginTop="10dp"
+            android:background="@drawable/bg_gray_20dp_shape"
+            app:layout_constraintLeft_toLeftOf="@+id/ll_like"
+            app:layout_constraintTop_toBottomOf="@+id/ll_like">
+
+            <TextView
+                android:id="@+id/tv_time_count"
+                android:layout_width="60dp"
+                android:layout_height="15dp"
+                android:layout_gravity="center"
+                android:gravity="center"
+                android:includeFontPadding="false"
+                android:text="00:00:00"
+                android:textColor="@color/white"
+                android:textSize="@dimen/sp_12" />
+
+            <!--            弄2个TextView,是因为倒计时写成wrap_content,
+                            会导致每次都需要重新测量TextView宽度,所以写死宽度-->
+            <TextView
+                android:id="@+id/tv_time_count_end_tip"
+                android:layout_width="120dp"
+                android:layout_height="15dp"
+                android:layout_gravity="center"
+                android:gravity="center"
+                android:includeFontPadding="false"
+                android:text="课程结束 00:00:00"
+                android:textColor="@color/white"
+                android:textSize="@dimen/sp_12"
+                android:visibility="gone" />
+        </FrameLayout>
+
 
         <LinearLayout
             android:id="@+id/ll_live_delay"