机器人自动发言

This commit is contained in:
18401019693 2022-12-24 14:23:40 +08:00
parent ed6e95f380
commit 404d45893f
8 changed files with 434 additions and 18 deletions

View File

@ -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<LiveAiRobotBean.Message> 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<LiveAiRobotBean.Message> messages) {
messageList.clear();
messageList.addAll(messages);
notifyDataSetChanged();
}
// 删除数据
public void removeData(int position) {
messageList.remove(position);
//删除动画
notifyItemRemoved(position);
}
}

View File

@ -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;
}
}

View File

@ -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<HttpCallbackModel>() {
@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<List<LiveAiRobotBean.Message>>() {
@Override
public void onSuccess(List<LiveAiRobotBean.Message> 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) {
}
});
}
}

View File

@ -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<List<LiveAiRobotBean.Message>>() {
@Override
public void onSuccess(List<LiveAiRobotBean.Message> 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() {

View File

@ -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<HttpCallbackModel>() {
@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<HttpCallbackModel>() {
@Override
public void onSuccess(HttpCallbackModel data) {
Bus.get().post(new LiveRobotSayHelloEvent());
}
@Override
public void onError(String error) {
ToastUtil.show(error);
}
});
}
}))
.show();
}
});
}
}

View File

@ -0,0 +1,106 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="400dp"
android:background="#000002">
<LinearLayout
android:id="@+id/line1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:id="@+id/bt_cancel"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_marginStart="5.33dp"
android:layout_marginTop="7.76dp"
android:layout_marginBottom="6.33dp"
android:src="@mipmap/icon_back" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginStart="6.67dp"
android:text="@string/automatic_greeting_setting"
android:textColor="#CCCCCC"
android:textSize="15sp" />
<View
android:layout_width="0dp"
android:layout_height="1dp"
android:layout_weight="1" />
<TextView
android:id="@+id/robot_add_content"
android:layout_width="67.67dp"
android:layout_height="23.33dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="11dp"
android:background="@drawable/background_81c16d"
android:gravity="center"
android:text="@string/robot_add_content"
android:textColor="#FFFFFF"
android:textSize="10sp" />
</LinearLayout>
<LinearLayout
android:id="@+id/line2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/line1"
android:layout_marginStart="10.33dp"
android:layout_marginEnd="10.67dp"
android:background="@drawable/bg_prank_coin"
android:orientation="horizontal"
android:paddingTop="8.67dp"
android:paddingBottom="7.67dp">
<ImageView
android:layout_width="26dp"
android:layout_height="26dp"
android:layout_gravity="center_vertical"
android:layout_marginStart="13.67dp"
android:src="@mipmap/icon_tip" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginStart="8.33dp"
android:text="@string/robot_add_content_hint3"
android:textColor="#CCCCCC"
android:textSize="12sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginStart="8.33dp"
android:layout_marginTop="6dp"
android:text="@string/robot_add_content_hint4"
android:textColor="#CCCCCC"
android:textSize="12sp" />
</LinearLayout>
</LinearLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/robot_messages"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/line2" />
</RelativeLayout>

View File

@ -158,6 +158,7 @@
<TextView
android:id="@+id/automatic_greeting_setting"
android:layout_width="96dp"
android:layout_height="27dp"
android:layout_gravity="center_vertical"

View File

@ -993,4 +993,6 @@
<string name="robot_add_content_hint2" translatable="false">填寫內容推薦,如:求送心願單、加粉絲團等</string>
<string name="robot_automatic_speech_interval" translatable="false">自動發言間隔時間(分鐘)</string>
<string name="robot_minimum_interval" translatable="false">最少間隔5分鐘1次</string>
<string name="robot_add_content_hint3" translatable="false">當有用戶進入直播間時,機器人會@該用戶並自動</string>
<string name="robot_add_content_hint4" translatable="false">隨機以下一句話。最少設置1條最多20條。</string>
</resources>