From 404d45893f2a7b0f7462f8862637d150be9bfeb9 Mon Sep 17 00:00:00 2001 From: 18401019693 Date: Sat, 24 Dec 2022 14:23:40 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9C=BA=E5=99=A8=E4=BA=BA=E8=87=AA=E5=8A=A8?= =?UTF-8?q?=E5=8F=91=E8=A8=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/adapter/RobotSayHelloAdapter.java | 51 +++++++ .../common/event/LiveRobotSayHelloEvent.java | 16 +++ .../views/LiveRobotSayHelloCustomPopup.java | 128 ++++++++++++++++++ .../views/LiveRobotSettingCustomPopup.java | 71 +++++++--- .../common/views/RobotSayHelloViewHoler.java | 77 +++++++++++ .../layout/dialog_live_robot_say_hello.xml | 106 +++++++++++++++ .../res/layout/dialog_live_robot_setting.xml | 1 + common/src/main/res/values/strings.xml | 2 + 8 files changed, 434 insertions(+), 18 deletions(-) create mode 100644 common/src/main/java/com/yunbao/common/adapter/RobotSayHelloAdapter.java create mode 100644 common/src/main/java/com/yunbao/common/event/LiveRobotSayHelloEvent.java create mode 100644 common/src/main/java/com/yunbao/common/views/LiveRobotSayHelloCustomPopup.java create mode 100644 common/src/main/java/com/yunbao/common/views/RobotSayHelloViewHoler.java create mode 100644 common/src/main/res/layout/dialog_live_robot_say_hello.xml diff --git a/common/src/main/java/com/yunbao/common/adapter/RobotSayHelloAdapter.java b/common/src/main/java/com/yunbao/common/adapter/RobotSayHelloAdapter.java new file mode 100644 index 000000000..adeacfa4e --- /dev/null +++ b/common/src/main/java/com/yunbao/common/adapter/RobotSayHelloAdapter.java @@ -0,0 +1,51 @@ +package com.yunbao.common.adapter; + +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; + +import androidx.annotation.NonNull; +import androidx.recyclerview.widget.RecyclerView; + +import com.yunbao.common.R; +import com.yunbao.common.bean.LiveAiRobotBean; +import com.yunbao.common.views.RobotSayHelloViewHoler; + +import java.util.ArrayList; +import java.util.List; + +public class RobotSayHelloAdapter extends RecyclerView.Adapter { + private List messageList = new ArrayList<>(); + + @NonNull + @Override + public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { + View robotSayHelloView = LayoutInflater.from(parent.getContext()).inflate(R.layout.view_robot_message, parent, false); + return new RobotSayHelloViewHoler(robotSayHelloView); + } + + @Override + public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) { + RobotSayHelloViewHoler robotSayHelloViewHoler = (RobotSayHelloViewHoler) holder; + robotSayHelloViewHoler.setData(messageList.get(position), position + 1); + } + + @Override + public int getItemCount() { + return messageList.size(); + } + + + public void setDataAll(List messages) { + messageList.clear(); + messageList.addAll(messages); + notifyDataSetChanged(); + } + + // 删除数据 + public void removeData(int position) { + messageList.remove(position); + //删除动画 + notifyItemRemoved(position); + } +} diff --git a/common/src/main/java/com/yunbao/common/event/LiveRobotSayHelloEvent.java b/common/src/main/java/com/yunbao/common/event/LiveRobotSayHelloEvent.java new file mode 100644 index 000000000..6e3e192a5 --- /dev/null +++ b/common/src/main/java/com/yunbao/common/event/LiveRobotSayHelloEvent.java @@ -0,0 +1,16 @@ +package com.yunbao.common.event; + +import com.yunbao.common.bean.BaseModel; + +public class LiveRobotSayHelloEvent extends BaseModel { + private int index = -1; + + public int getIndex() { + return index; + } + + public LiveRobotSayHelloEvent setIndex(int index) { + this.index = index; + return this; + } +} diff --git a/common/src/main/java/com/yunbao/common/views/LiveRobotSayHelloCustomPopup.java b/common/src/main/java/com/yunbao/common/views/LiveRobotSayHelloCustomPopup.java new file mode 100644 index 000000000..93e61f37c --- /dev/null +++ b/common/src/main/java/com/yunbao/common/views/LiveRobotSayHelloCustomPopup.java @@ -0,0 +1,128 @@ +package com.yunbao.common.views; + +import android.content.Context; +import android.util.Log; +import android.view.View; + +import androidx.annotation.NonNull; +import androidx.recyclerview.widget.LinearLayoutManager; +import androidx.recyclerview.widget.RecyclerView; + +import com.lxj.xpopup.XPopup; +import com.lxj.xpopup.core.BottomPopupView; +import com.yunbao.common.R; +import com.yunbao.common.adapter.RobotSayHelloAdapter; +import com.yunbao.common.bean.HttpCallbackModel; +import com.yunbao.common.bean.LiveAiRobotBean; +import com.yunbao.common.event.LiveRobotSayHelloEvent; +import com.yunbao.common.http.base.HttpCallback; +import com.yunbao.common.http.live.LiveNetManager; +import com.yunbao.common.utils.Bus; +import com.yunbao.common.utils.ToastUtil; + +import org.greenrobot.eventbus.Subscribe; +import org.greenrobot.eventbus.ThreadMode; + +import java.util.List; + +public class LiveRobotSayHelloCustomPopup extends BottomPopupView { + private RecyclerView robotMessages; + private RobotSayHelloAdapter robotMessageAdapter; + private static String TAG = "AI机器人"; + + public LiveRobotSayHelloCustomPopup(@NonNull Context context) { + super(context); + } + + + // 返回自定义弹窗的布局 + @Override + protected int getImplLayoutId() { + return R.layout.dialog_live_robot_say_hello; + } + + // 执行初始化操作,比如:findView,设置点击,或者任何你弹窗内的业务逻辑 + @Override + protected void onCreate() { + super.onCreate(); + Bus.getOn(this); + initView(); + initDate(); + } + + @Subscribe(threadMode = ThreadMode.MAIN) + public void onLiveRobotSayHelloEvent(LiveRobotSayHelloEvent event) { + if (event.getIndex() > 0) { + robotMessageAdapter.removeData(event.getIndex()); + } else { + initDate(); + } + + } + + @Override + public void onDestroy() { + super.onDestroy(); + Bus.getOff(this); + } + + private void initView() { + robotMessages = (RecyclerView) findViewById(R.id.robot_messages); + robotMessageAdapter = new RobotSayHelloAdapter(); + robotMessages.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false)); + robotMessages.setAdapter(robotMessageAdapter); + findViewById(R.id.bt_cancel).setOnClickListener(new OnClickListener() { + @Override + public void onClick(View v) { + dismiss(); + } + }); + //添加消息语 + findViewById(R.id.robot_add_content).setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + new XPopup.Builder(getContext()) + .asCustom(new InputCustomPopup(getContext()).setListener(new InputCustomPopup.InputCustomListener() { + @Override + public void onConfirm(String text) { + //添加消息语 + LiveNetManager.get(getContext()) + .addAiRobotBody(LiveAiRobotBean.Message.TYPE_SAY_HI, text, new HttpCallback() { + @Override + public void onSuccess(HttpCallbackModel data) { + initDate(); + } + + @Override + public void onError(String error) { + ToastUtil.show(error); + } + }); + } + })) + .show(); + } + }); + } + + + private void initDate() { + LiveNetManager.get(getContext()) + .getAiRobotBody(LiveAiRobotBean.Message.TYPE_SAY_HI, new HttpCallback>() { + @Override + public void onSuccess(List data) { + Log.i(TAG, "onSuccess 自动打招呼 : " + data.size()); + for (LiveAiRobotBean.Message message : data) { + Log.i(TAG, "onSuccess: message=" + message.toString()); + } + robotMessageAdapter.setDataAll(data); + + } + + @Override + public void onError(String error) { + + } + }); + } +} diff --git a/common/src/main/java/com/yunbao/common/views/LiveRobotSettingCustomPopup.java b/common/src/main/java/com/yunbao/common/views/LiveRobotSettingCustomPopup.java index de72c505c..f954a8ce7 100644 --- a/common/src/main/java/com/yunbao/common/views/LiveRobotSettingCustomPopup.java +++ b/common/src/main/java/com/yunbao/common/views/LiveRobotSettingCustomPopup.java @@ -61,6 +61,7 @@ public class LiveRobotSettingCustomPopup extends BottomPopupView implements View automaticGreetingNumber = findViewById(R.id.automatic_greeting_number); automaticMessageNumber = findViewById(R.id.automatic_message_number); findViewById(R.id.automatic_message_sending).setOnClickListener(this); + findViewById(R.id.automatic_greeting_setting).setOnClickListener(this); robotNameText.setOnClickListener(this); robotState.setOnClickListener(this); @@ -167,10 +168,10 @@ public class LiveRobotSettingCustomPopup extends BottomPopupView implements View public void onSuccess(HttpCallbackModel data) { if (finalState == 1) { robotState.setImageResource(R.mipmap.special_icon_on); - robotStateInt=1; + robotStateInt = 1; } else { robotState.setImageResource(R.mipmap.special_icon_off); - robotStateInt=0; + robotStateInt = 0; } } @@ -179,6 +180,56 @@ public class LiveRobotSettingCustomPopup extends BottomPopupView implements View ToastUtil.show(error); } }); + } else if (viewID == R.id.automatic_greeting_setting) { + new XPopup.Builder(getContext()) + .setPopupCallback(new XPopupCallback() { + @Override + public void onCreated(BasePopupView popupView) { + + } + + @Override + public void beforeShow(BasePopupView popupView) { + + } + + @Override + public void onShow(BasePopupView popupView) { + + } + + @Override + public void onDismiss(BasePopupView popupView) { + initDate(); + } + + @Override + public void beforeDismiss(BasePopupView popupView) { + + } + + @Override + public boolean onBackPressed(BasePopupView popupView) { + return false; + } + + @Override + public void onKeyBoardStateChanged(BasePopupView popupView, int height) { + + } + + @Override + public void onDrag(BasePopupView popupView, int value, float percent, boolean upOrLeft) { + + } + + @Override + public void onClickOutside(BasePopupView popupView) { + + } + }) + .asCustom(new LiveRobotSayHelloCustomPopup(getContext())) + .show(); } } @@ -209,23 +260,7 @@ public class LiveRobotSettingCustomPopup extends BottomPopupView implements View private void initListType1() { - LiveNetManager.get(getContext()) - .getAiRobotBody(LiveAiRobotBean.Message.TYPE_SAY_HI, new HttpCallback>() { - @Override - public void onSuccess(List data) { - Log.i(TAG, "onSuccess 自动打招呼 : " + data.size()); - for (LiveAiRobotBean.Message message : data) { - Log.i(TAG, "onSuccess: message=" + message.toString()); - } - - } - - @Override - public void onError(String error) { - - } - }); } private void initListType2() { diff --git a/common/src/main/java/com/yunbao/common/views/RobotSayHelloViewHoler.java b/common/src/main/java/com/yunbao/common/views/RobotSayHelloViewHoler.java new file mode 100644 index 000000000..359f7ba1a --- /dev/null +++ b/common/src/main/java/com/yunbao/common/views/RobotSayHelloViewHoler.java @@ -0,0 +1,77 @@ +package com.yunbao.common.views; + +import android.view.View; +import android.widget.ImageView; +import android.widget.TextView; + +import androidx.annotation.NonNull; +import androidx.recyclerview.widget.RecyclerView; + +import com.lxj.xpopup.XPopup; +import com.yunbao.common.R; +import com.yunbao.common.bean.HttpCallbackModel; +import com.yunbao.common.bean.LiveAiRobotBean; +import com.yunbao.common.event.LiveRobotSayHelloEvent; +import com.yunbao.common.http.base.HttpCallback; +import com.yunbao.common.http.live.LiveNetManager; +import com.yunbao.common.utils.Bus; +import com.yunbao.common.utils.ToastUtil; + +public class RobotSayHelloViewHoler extends RecyclerView.ViewHolder { + private TextView serialNumber, messageText; + private ImageView btnDelete; + + public RobotSayHelloViewHoler(@NonNull View itemView) { + super(itemView); + serialNumber = itemView.findViewById(R.id.serial_number); + messageText = itemView.findViewById(R.id.message_text); + btnDelete = itemView.findViewById(R.id.btn_delete); + } + + + public void setData(LiveAiRobotBean.Message message, int index) { + serialNumber.setText(String.valueOf(index)); + messageText.setText(message.getContent()); + btnDelete.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + LiveNetManager.get(itemView.getContext()) + .delAiRobotBody(LiveAiRobotBean.Message.TYPE_SAY_HI, message.getId(), new HttpCallback() { + @Override + public void onSuccess(HttpCallbackModel data) { + Bus.get().post(new LiveRobotSayHelloEvent().setIndex(index - 1)); + } + + @Override + public void onError(String error) { + ToastUtil.show(error); + } + }); + } + }); + messageText.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + new XPopup.Builder(itemView.getContext()) + .asCustom(new InputCustomPopup(itemView.getContext(), message.getContent()).setListener(new InputCustomPopup.InputCustomListener() { + @Override + public void onConfirm(String text) { + LiveNetManager.get(itemView.getContext()) + .updateAiRobotBody(LiveAiRobotBean.Message.TYPE_SAY_HI, message.getId(), text, new HttpCallback() { + @Override + public void onSuccess(HttpCallbackModel data) { + Bus.get().post(new LiveRobotSayHelloEvent()); + } + + @Override + public void onError(String error) { + ToastUtil.show(error); + } + }); + } + })) + .show(); + } + }); + } +} diff --git a/common/src/main/res/layout/dialog_live_robot_say_hello.xml b/common/src/main/res/layout/dialog_live_robot_say_hello.xml new file mode 100644 index 000000000..0dc1bda30 --- /dev/null +++ b/common/src/main/res/layout/dialog_live_robot_say_hello.xml @@ -0,0 +1,106 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/common/src/main/res/layout/dialog_live_robot_setting.xml b/common/src/main/res/layout/dialog_live_robot_setting.xml index 9e4b2e54c..0cedd53b9 100644 --- a/common/src/main/res/layout/dialog_live_robot_setting.xml +++ b/common/src/main/res/layout/dialog_live_robot_setting.xml @@ -158,6 +158,7 @@ 填寫內容推薦,如:求送心願單、加粉絲團等 自動發言間隔時間(分鐘) 最少間隔5分鐘1次 + 當有用戶進入直播間時,機器人會@該用戶並自動 + 隨機以下一句話。最少設置1條,最多20條。