导航菜单

页面标题

页面副标题

自健身 v3.3.4 - AiActivity.java 源代码

正在查看: 自健身 v3.3.4 应用的 AiActivity.java JAVA 源代码文件

本页面展示 JAVA 反编译生成的源代码文件,支持语法高亮显示。 仅供安全研究与技术分析使用,严禁用于任何非法用途。请遵守相关法律法规。


package com.mu.gymtrain.Activity;

import android.app.Activity;
import android.content.res.Resources;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.blankj.utilcode.util.AdaptScreenUtils;
import com.blankj.utilcode.util.KeyboardUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.githang.statusbar.StatusBarCompat;
import com.mu.gymtrain.Adapter.AiMsgAdapter;
import com.mu.gymtrain.Base.BaseActivity;
import com.mu.gymtrain.Bean.AiWelcomeBean;
import com.mu.gymtrain.Bean.HistoryMsgBean;
import com.mu.gymtrain.Bean.MsgBean;
import com.mu.gymtrain.Bean.SendMsgBean;
import com.mu.gymtrain.R;
import com.mu.gymtrain.Utils.UrlConfig;
import com.mu.gymtrain.viewmodel.AiViewModel;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

public class AiActivity extends BaseActivity implements OnRefreshListener {
    private AiMsgAdapter adapter;
    private EditText etContent;
    private View llProgress;
    private RecyclerView recyclerView;
    private String token;
    private AiViewModel viewModel;

    public int getContentLayout() {
        return R.layout.activity_ai_layout;
    }

    public void initView() {
        this.viewModel = new AiViewModel();
        ClassicsHeader.REFRESH_HEADER_PULLING = "下拉加载历史数据";
        ClassicsHeader.REFRESH_HEADER_REFRESHING = "正在加载历史数据";
        ClassicsHeader.REFRESH_HEADER_RELEASE = "释放立即加载数据";
        ClassicsHeader.REFRESH_HEADER_FINISH = "加载完成";
        StatusBarCompat.setStatusBarColor((Activity) this, getResources().getColor(R.color.orange), false);
        findViewById(R.id.tvSend).setOnClickListener(this);
        findViewById(R.id.tvClear).setOnClickListener(this);
        findViewById(R.id.imgBack).setOnClickListener(this);
        SmartRefreshLayout findViewById = findViewById(R.id.resfresh);
        findViewById.setEnableLoadMore(false);
        findViewById.setEnableRefresh(false);
        findViewById.setOnRefreshListener(this);
        this.etContent = (EditText) findViewById(R.id.etContent);
        this.llProgress = findViewById(R.id.llProgress);
        RecyclerView findViewById2 = findViewById(2131297022);
        this.recyclerView = findViewById2;
        findViewById2.setLayoutManager(new LinearLayoutManager(this));
        AiMsgAdapter aiMsgAdapter = new AiMsgAdapter(getStringFromSP(UrlConfig.Params.AVATAR));
        this.adapter = aiMsgAdapter;
        this.recyclerView.setAdapter(aiMsgAdapter);
        this.adapter.bindToRecyclerView(this.recyclerView);
    }

    public void initData() {
        this.token = getToken();
        this.llProgress.setVisibility(0);
        this.viewModel.getAiWelcome(this.token, new AiViewModel.OnAiCallBack<AiWelcomeBean>() {
            @Override
            public void onSuccess(AiWelcomeBean result) {
                AiActivity.this.llProgress.setVisibility(8);
                MsgBean msgBean = new MsgBean();
                msgBean.setOwner(false);
                msgBean.setMsg(result.getData());
                AiActivity.this.adapter.addData(msgBean);
            }

            @Override
            public void onFail(String msg) {
                AiActivity.this.llProgress.setVisibility(8);
            }
        });
    }

    public void onClick(View v) {
        int id = v.getId();
        if (id == 2131296609) {
            finish();
        } else if (id == 2131297311) {
            this.adapter.setNewData(null);
        } else {
            if (id != 2131297410) {
                return;
            }
            sendMsg(this.etContent.getText().toString());
        }
    }

    public Resources getResources() {
        return AdaptScreenUtils.adaptWidth(super.getResources(), 1080);
    }

    private void sendMsg(String msg) {
        if (TextUtils.isEmpty(msg)) {
            return;
        }
        MsgBean msgBean = new MsgBean();
        msgBean.setOwner(true);
        msgBean.setTime(TimeUtils.getNowString());
        msgBean.setMsg(msg);
        this.adapter.addData(msgBean);
        this.etContent.setText("");
        KeyboardUtils.hideSoftInput(this.etContent);
        this.recyclerView.scrollToPosition(this.adapter.getItemCount() - 1);
        this.llProgress.setVisibility(0);
        this.viewModel.sendMsg(this.token, msg, new AiViewModel.OnAiCallBack<SendMsgBean>() {
            @Override
            public void onSuccess(SendMsgBean result) {
                AiActivity.this.llProgress.setVisibility(8);
                MsgBean msgBean2 = new MsgBean();
                msgBean2.setOwner(false);
                msgBean2.setTime(result.getData().getCreatedAt());
                msgBean2.setMsg(result.getData().getAnswer());
                msgBean2.setList(result.getData().getAttachments());
                AiActivity.this.adapter.addData(msgBean2);
                AiActivity.this.recyclerView.scrollToPosition(AiActivity.this.adapter.getItemCount() - 1);
            }

            @Override
            public void onFail(String msg2) {
                AiActivity.this.llProgress.setVisibility(8);
            }
        });
    }

    public void onRefresh(final RefreshLayout refreshLayout) {
        this.viewModel.aiHistory(this.token, 0, new AiViewModel.OnAiCallBack<HistoryMsgBean>() {
            @Override
            public void onSuccess(HistoryMsgBean historyMsgBean) {
                refreshLayout.finishRefresh();
            }

            @Override
            public void onFail(String msg) {
                refreshLayout.finishRefresh();
            }
        });
    }
}