330 lines
11 KiB
Java
330 lines
11 KiB
Java
package com.ckkj.water.device;
|
|
|
|
import android.content.Intent;
|
|
import android.os.Bundle;
|
|
import android.text.TextUtils;
|
|
import android.view.View;
|
|
|
|
import androidx.annotation.Nullable;
|
|
|
|
import com.chad.library.adapter.base.BaseQuickAdapter;
|
|
import com.ckkj.water.EventMessage;
|
|
import com.ckkj.water.R;
|
|
import com.ckkj.water.base.BaseActivity;
|
|
import com.ckkj.water.base.BaseFragment;
|
|
import com.ckkj.water.custom.GridSpacingItemDecoration;
|
|
import com.ckkj.water.custom.view.CustomBubbleAttachPopup;
|
|
import com.ckkj.water.databinding.DeviceControlLayoutBinding;
|
|
import com.ckkj.water.device.adapter.DeviceTimeSelectAdapter;
|
|
import com.ckkj.water.device.mvp.DeviceContract;
|
|
import com.ckkj.water.device.mvp.DeviceImpl;
|
|
import com.ckkj.water.device.mvp.DevicePresenter;
|
|
import com.ckkj.water.index.entity.WaterDeviceEntity;
|
|
import com.ckkj.water.network.BaseBean;
|
|
import com.ckkj.water.plan.entity.WaterPlanWeekEntity;
|
|
import com.ckkj.water.utils.manager.PowerIconManage;
|
|
import com.ckkj.water.utils.show.ToastUtil;
|
|
import com.gyf.immersionbar.ImmersionBar;
|
|
import com.lxj.xpopup.XPopup;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
import de.greenrobot.event.Subscribe;
|
|
import de.greenrobot.event.ThreadMode;
|
|
|
|
/**
|
|
* 设备控制页面
|
|
*/
|
|
public class DeviceControlActivity extends BaseActivity<DeviceControlLayoutBinding, DevicePresenter>
|
|
implements DeviceContract.DeviceView {
|
|
|
|
private List<String> mTimeList = new ArrayList<>();
|
|
private WaterDeviceEntity mDeviceEntity;
|
|
private WaterDeviceEntity mRemoteDeviceEntity;
|
|
private DeviceTimeSelectAdapter mAdapter;
|
|
//用户自定义的浇水时长
|
|
private int mCustomDuration = 0;
|
|
@Override
|
|
public DevicePresenter createPresenter() {
|
|
return new DevicePresenter(new DeviceImpl());
|
|
}
|
|
|
|
@Override
|
|
public void initData(Bundle savedInstanceState) {
|
|
super.initData(savedInstanceState);
|
|
Bundle bundle = getIntent().getExtras();
|
|
if(bundle != null){
|
|
mDeviceEntity = (WaterDeviceEntity) bundle.getSerializable("device_info");
|
|
if(mDeviceEntity != null){
|
|
showLoadingDialog();
|
|
mPresenter.getDeviceDetailInfo(mDeviceEntity.getId());
|
|
viewBinding.includedTitle.tvTitle.setText(mDeviceEntity.getDeviceName());
|
|
if(mDeviceEntity.getWorkStatus().equals("1")){
|
|
viewBinding.tvDeviceStatus.setText(getString(R.string.device_status_working));
|
|
viewBinding.tvDeviceStatus.setTextColor(mContext.getResources().getColor(R.color.color_zhu));
|
|
viewBinding.tvConfirm.setText(getString(R.string.device_control_stop));
|
|
}else{
|
|
viewBinding.tvDeviceStatus.setText(getString(R.string.device_status_not_working));
|
|
viewBinding.tvDeviceStatus.setTextColor(mContext.getResources().getColor(R.color.color_999999));
|
|
viewBinding.tvConfirm.setText(getString(R.string.device_control_start));
|
|
}
|
|
}
|
|
}
|
|
mTimeList.add("1min");
|
|
mTimeList.add("2min");
|
|
mTimeList.add("3min");
|
|
mTimeList.add("4min");
|
|
mTimeList.add("5min");
|
|
mTimeList.add("10min");
|
|
mTimeList.add("15min");
|
|
mTimeList.add("30min");
|
|
mTimeList.add("1h");
|
|
mTimeList.add("2h");
|
|
mTimeList.add("3h");
|
|
mTimeList.add("4h");
|
|
ImmersionBar.with(this)
|
|
.statusBarColor(R.color.mainBackgroundColor) // 设置状态栏颜色
|
|
.statusBarDarkFont(true) // 设置状态栏字体为黑色
|
|
.titleBar(viewBinding.rlTitle)
|
|
.init();
|
|
viewBinding.includedTitle.imgSetting.setVisibility(View.VISIBLE);
|
|
viewBinding.includedTitle.imgSetting.setImageDrawable(getDrawable(R.mipmap.device_ctrl_setting));
|
|
mAdapter = new DeviceTimeSelectAdapter(mContext,R.layout.device_work_time_item,mTimeList);
|
|
viewBinding.rvTimeList.setAdapter(mAdapter);
|
|
viewBinding.rvTimeList.addItemDecoration(new GridSpacingItemDecoration(4, 10, true));
|
|
mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
|
|
@Override
|
|
public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
|
|
mAdapter.setOnSelPosition(position);
|
|
mCustomDuration = 0;
|
|
viewBinding.tvWorkDuartion.setHint(getString(R.string.device_custom_work_time));
|
|
viewBinding.tvWorkDuartion.setText("");
|
|
}
|
|
});
|
|
//用户自定义时间
|
|
if(mCustomDuration == 0){
|
|
viewBinding.tvWorkDuartion.setHint(getString(R.string.device_custom_work_time));
|
|
}
|
|
|
|
initListener();
|
|
}
|
|
|
|
private void initListener() {
|
|
setClick(viewBinding.includedTitle.imgBack);
|
|
setClick(viewBinding.includedTitle.imgSetting);
|
|
setClick(viewBinding.linearBt);
|
|
setClick(viewBinding.linearWifi);
|
|
setClick(viewBinding.linearBat);
|
|
setClick(viewBinding.tvConfirm);
|
|
setClick(viewBinding.tvWorkDuartion);
|
|
}
|
|
|
|
private void setClick(View view) {
|
|
view.setOnClickListener(this::onClick);
|
|
}
|
|
|
|
public void onClick(View v){
|
|
switch (v.getId()){
|
|
case R.id.img_back:
|
|
finish();
|
|
break;
|
|
|
|
case R.id.img_setting:
|
|
Bundle bundle = new Bundle();
|
|
bundle.putSerializable("device_entity",mDeviceEntity);
|
|
goToForResult(mContext, DeviceSettingsActivity.class,REQUEST_EVENT_CODE_10002,bundle);
|
|
break;
|
|
|
|
case R.id.linear_bt:
|
|
|
|
break;
|
|
|
|
case R.id.linear_wifi:
|
|
|
|
break;
|
|
|
|
case R.id.linear_bat:
|
|
new XPopup.Builder(mContext)
|
|
.isTouchThrough(true)
|
|
.isDestroyOnDismiss(true) //对于只使用一次的弹窗,推荐设置这个
|
|
.atView(viewBinding.linearBat)
|
|
.hasShadowBg(false) // 去掉半透明背景
|
|
.asCustom(new CustomBubbleAttachPopup(mContext,"设备电量:"+
|
|
mDeviceEntity.getPowerLevel() + "%" +
|
|
" 更新时间: 05- 11 18:00"))
|
|
.show();
|
|
break;
|
|
|
|
case R.id.tv_work_duartion:
|
|
inputDuration(mCustomDuration);
|
|
break;
|
|
|
|
case R.id.tv_confirm:
|
|
if(mCustomDuration == 0 && mAdapter.getmSelPostion() == -1){
|
|
ToastUtil.showShort(mContext,"请选择浇水时长");
|
|
return;
|
|
}
|
|
showCenterTipDialog(R.mipmap.icon_dialog_success,"开始浇水");//
|
|
String status = mDeviceEntity.getStatus();
|
|
if(status.equals("1")){
|
|
status = "0";
|
|
}else{
|
|
status = "1";
|
|
}
|
|
mPresenter.switchDeviceWork(mDeviceEntity.getId(),status,
|
|
mCustomDuration == 0 ? convertToMinutes(mTimeList.get(mAdapter.getmSelPostion()))
|
|
: mCustomDuration);
|
|
break;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 输入浇水时间
|
|
* @param currentDuration
|
|
*/
|
|
private void inputDuration(int currentDuration) {
|
|
showEditDialog(getString(R.string.device_work_times_title),
|
|
getString(R.string.input_water_plan_time),
|
|
currentDuration == 0 ? "" : currentDuration + "",
|
|
true,
|
|
getString(R.string.cancel_text),
|
|
getString(R.string.confirm_text),
|
|
INPUT_TYPE_NUMBER,
|
|
new BaseActivity.OnDialogClick() {
|
|
@Override
|
|
public void onCancelClick() {}
|
|
|
|
@Override
|
|
public void onConfirmClisk() {}
|
|
|
|
@Override
|
|
public void onConfirmClisk(String content) {
|
|
if (Integer.parseInt(content) <= 0) {
|
|
ToastUtil.showShort(mContext, getString(R.string.input_water_plan_time_error));
|
|
return;
|
|
}
|
|
viewBinding.tvWorkDuartion.setText(content);
|
|
mCustomDuration = Integer.parseInt(content);
|
|
if(mAdapter != null){
|
|
mAdapter.setOnSelPosition(-1);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void onView1Click() {
|
|
|
|
}
|
|
});
|
|
}
|
|
|
|
|
|
/**
|
|
* 将时间字符串转换为分钟数
|
|
*
|
|
* 1min -> 1
|
|
* 10min -> 10
|
|
* 1h -> 60
|
|
* 2h -> 120
|
|
*
|
|
* @param timeValue 时间字符串
|
|
* @return 分钟数
|
|
*/
|
|
public static int convertToMinutes(String timeValue) {
|
|
if (TextUtils.isEmpty(timeValue)) {
|
|
return 0;
|
|
}
|
|
|
|
try {
|
|
if (timeValue.endsWith("min")) {
|
|
return Integer.parseInt(
|
|
timeValue.substring(0, timeValue.length() - 3)
|
|
);
|
|
}
|
|
|
|
if (timeValue.endsWith("h")) {
|
|
return Integer.parseInt(
|
|
timeValue.substring(0, timeValue.length() - 1)
|
|
) * 60;
|
|
}
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
|
|
super.onActivityResult(requestCode, resultCode, data);
|
|
if(resultCode == RESULT_OK){
|
|
switch (requestCode){
|
|
case REQUEST_EVENT_CODE_10002:
|
|
mDeviceEntity = (WaterDeviceEntity) data.getSerializableExtra("device_entity");
|
|
viewBinding.includedTitle.tvTitle.setText(mDeviceEntity.getDeviceName());
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
@Subscribe(threadMode = ThreadMode.MainThread)
|
|
public void onGetMessage(EventMessage message) {
|
|
switch (message.msg){
|
|
case EventMessage.ACTIVITY_FINISH:
|
|
finish();
|
|
break;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 获取设备详情成功
|
|
* @param bean
|
|
*/
|
|
@Override
|
|
public void getDeviceDetailInfo(WaterDeviceEntity bean) {
|
|
hideDialogLoading();
|
|
viewBinding.imgBatStatus.setImageResource(PowerIconManage.getInstance().
|
|
getPowerVerIcon(Integer.parseInt(bean.getPowerLevel())));
|
|
mRemoteDeviceEntity = bean;
|
|
}
|
|
|
|
@Override
|
|
public int getLayoutResId(Bundle savedInstanceState) {
|
|
return 0;
|
|
}
|
|
|
|
@Override
|
|
public void showError(String message) {
|
|
hideDialogLoading();
|
|
ToastUtil.showShort(mContext,message);
|
|
}
|
|
|
|
@Override
|
|
public void logOut(BaseBean bean) {
|
|
|
|
}
|
|
|
|
@Override
|
|
public void addDeviceSuccess(BaseBean bean) {
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
public void editDeviceInfoSuccess(BaseBean bean) {
|
|
|
|
}
|
|
|
|
@Override
|
|
public void deleteDeviceSuccess(BaseBean bean) {
|
|
|
|
}
|
|
|
|
@Override
|
|
public void turnOnOffDeviceSuccess(BaseBean bean) {
|
|
|
|
}
|
|
}
|