diff --git a/app/src/main/java/cc/niushuai/project/devcontrol/base/util/GlobalVariables.java b/app/src/main/java/cc/niushuai/project/devcontrol/base/util/GlobalVariables.java index 542fae3..4e5bc21 100644 --- a/app/src/main/java/cc/niushuai/project/devcontrol/base/util/GlobalVariables.java +++ b/app/src/main/java/cc/niushuai/project/devcontrol/base/util/GlobalVariables.java @@ -28,7 +28,7 @@ public class GlobalVariables { List deviceList = DB.getDeviceDao().loadAll(); for (Device device : deviceList) { - DEVICE_INFO_MAP.put(device.getId() + "", DeviceInfo.convert(device)); + DEVICE_INFO_MAP.put(device.getId() + "", DeviceInfo.parseDevice(device)); } } diff --git a/app/src/main/java/cc/niushuai/project/devcontrol/ui/deviceadd/DeviceAddPowerSwitchActivity.java b/app/src/main/java/cc/niushuai/project/devcontrol/ui/deviceadd/DeviceAddPowerSwitchActivity.java index 1429efd..2256a91 100644 --- a/app/src/main/java/cc/niushuai/project/devcontrol/ui/deviceadd/DeviceAddPowerSwitchActivity.java +++ b/app/src/main/java/cc/niushuai/project/devcontrol/ui/deviceadd/DeviceAddPowerSwitchActivity.java @@ -9,6 +9,7 @@ import androidx.annotation.NonNull; import cc.niushuai.project.devcontrol.R; import cc.niushuai.project.devcontrol.base.enums.YesNoEnum; +import cc.niushuai.project.devcontrol.base.util.CommonUiUtil; import cc.niushuai.project.devcontrol.base.util.IdWorker; import cc.niushuai.project.devcontrol.base.util.UiUtil; import cc.niushuai.project.devcontrol.db.DB; @@ -72,18 +73,12 @@ public class DeviceAddPowerSwitchActivity extends BaseActivity { * @date: 2022/10/21 14:20 */ private void confirm4SaveDataClickListener(View view) { - Device device = new Device(); + Device device = CommonUiUtil.getDeviceInfo(this, IdWorker.getNextId()); - device.setId(IdWorker.getNextId()); device.setIconId(R.drawable.ic_device_type_switch); device.setDeviceType(DeviceTypeEnum.Power_Switch.getValue()); device.setOnOff(OnOffEnum.OFF.getValue()); - device.setDeviceName(UiUtil.getTextViewTextById(this, R.id.device_add_name)); - device.setCommandPath(UiUtil.getTextViewTextById(this,R.id.device_add_param_program)); - device.setCommandStatus(UiUtil.getTextViewTextById(this,R.id.device_add_param_status)); - device.setCommandOpen(UiUtil.getTextViewTextById(this,R.id.device_add_param_open)); - device.setCommandClose(UiUtil.getTextViewTextById(this,R.id.device_add_param_close)); - device.setRemark(UiUtil.getTextViewTextById(this,R.id.device_add_param_remark)); + device.setCreateTime(DateUtil.now()); device.setOrder(1); device.setIsDeleted(YesNoEnum.NO.getIntegerValue()); diff --git a/app/src/main/java/cc/niushuai/project/devcontrol/ui/nav/device/NavDeviceFragment.java b/app/src/main/java/cc/niushuai/project/devcontrol/ui/nav/device/NavDeviceFragment.java index c1ac0ef..8a13d8a 100644 --- a/app/src/main/java/cc/niushuai/project/devcontrol/ui/nav/device/NavDeviceFragment.java +++ b/app/src/main/java/cc/niushuai/project/devcontrol/ui/nav/device/NavDeviceFragment.java @@ -77,7 +77,12 @@ public class NavDeviceFragment extends Fragment { deviceInfoList = GlobalVariables.DEVICE_INFO_MAP.values() .stream() - .sorted(Comparator.comparingInt(DeviceInfo::getOrder)) + .sorted((before, after) -> { + if (null == before.getOrder() || null == after.getOrder()) { + return 99999; + } + return before.getOrder() - after.getOrder(); + }) .collect(Collectors.toList()); // GridLayout deviceGridLayout = binding.deviceGridLayout; diff --git a/app/src/main/java/cc/niushuai/project/devcontrol/ui/powerswitch/PowerSwitchSetParamActivity.java b/app/src/main/java/cc/niushuai/project/devcontrol/ui/powerswitch/PowerSwitchSetParamActivity.java index ebc73ab..89129f3 100644 --- a/app/src/main/java/cc/niushuai/project/devcontrol/ui/powerswitch/PowerSwitchSetParamActivity.java +++ b/app/src/main/java/cc/niushuai/project/devcontrol/ui/powerswitch/PowerSwitchSetParamActivity.java @@ -7,13 +7,20 @@ import android.widget.Toast; import androidx.annotation.Nullable; +import java.util.Date; + import cc.niushuai.project.devcontrol.R; +import cc.niushuai.project.devcontrol.base.enums.DeviceTypeEnum; +import cc.niushuai.project.devcontrol.base.enums.OnOffEnum; import cc.niushuai.project.devcontrol.base.ui.BaseActivity; +import cc.niushuai.project.devcontrol.base.util.CommonUiUtil; import cc.niushuai.project.devcontrol.base.util.GlobalVariables; import cc.niushuai.project.devcontrol.base.util.ToastUtil; import cc.niushuai.project.devcontrol.base.util.UiUtil; import cc.niushuai.project.devcontrol.databinding.ActivityPowerSwitchSetParamBinding; - +import cc.niushuai.project.devcontrol.db.DB; +import cc.niushuai.project.devcontrol.db.entity.Device; +import cc.niushuai.project.devcontrol.vo.DeviceInfo; public class PowerSwitchSetParamActivity extends BaseActivity { @@ -36,27 +43,12 @@ public class PowerSwitchSetParamActivity extends BaseActivity { this.setTitle(null, getString(R.string.power_switch_set_paramSet), null, R.drawable.ic_confrim_32); - this.echoData(); + // 回显数据 + CommonUiUtil.echoDeviceInfo(this, device); } - /** - * 回显数据 - * - * @author niushuai - * @date: 2022/10/25 9:01 - */ - private void echoData() { - UiUtil.setTextViewTextById(this, R.id.device_add_name, device.getDeviceName()); - UiUtil.setTextViewTextById(this, R.id.device_add_param_program, device.getCommandPath()); - UiUtil.setTextViewTextById(this, R.id.device_add_param_status, device.getCommandStatus()); - UiUtil.setTextViewTextById(this, R.id.device_add_param_open, device.getCommandOpen()); - UiUtil.setTextViewTextById(this, R.id.device_add_param_close, device.getCommandClose()); - UiUtil.setTextViewTextById(this, R.id.device_add_param_remark, device.getRemark()); - - UiUtil.setAppImageCompatResource(this, R.id.device_add_select_icon, device.getIconId()); - } @Override protected void addListener() { @@ -66,9 +58,29 @@ public class PowerSwitchSetParamActivity extends BaseActivity { super.activityButtonMoreSetClickListener(this::btnSaveParamClickListener); } + /** + * 更新数据操作 + * + * @param view + * @author niushuai + * @date: 2022/10/25 13:56 + */ private void btnSaveParamClickListener(View view) { + // 构建更新数据 + DeviceInfo deviceInfo = CommonUiUtil.getDeviceInfo(this, device.getId()); + deviceInfo.setDeviceType(device.getDeviceType()); + deviceInfo.setOnOff(device.getOnOff()); + deviceInfo.setOrder(device.getOrder()); + deviceInfo.setIconId(device.getIconId()); + deviceInfo.setIsDeleted(device.getIsDeleted()); + deviceInfo.setCreateTime(device.getCreateTime()); + DB.getDeviceDao().update(deviceInfo.toDevice()); - ToastUtil.show(this, "已保存"); + // 对当前设备引用重新赋值 + device = deviceInfo; + + // 返回上一层 + this.finish(); } @Override diff --git a/app/src/main/java/cc/niushuai/project/devcontrol/vo/DeviceInfo.java b/app/src/main/java/cc/niushuai/project/devcontrol/vo/DeviceInfo.java index e3c89aa..ca77560 100644 --- a/app/src/main/java/cc/niushuai/project/devcontrol/vo/DeviceInfo.java +++ b/app/src/main/java/cc/niushuai/project/devcontrol/vo/DeviceInfo.java @@ -6,10 +6,10 @@ import java.util.List; import cc.niushuai.project.devcontrol.base.entity.BaseVO; import cc.niushuai.project.devcontrol.base.enums.DeviceTypeEnum; import cc.niushuai.project.devcontrol.base.enums.OnOffEnum; +import cc.niushuai.project.devcontrol.base.util.DateFormatUtil; import cc.niushuai.project.devcontrol.base.util.GlobalVariables; import cc.niushuai.project.devcontrol.base.util.IdWorker; import cc.niushuai.project.devcontrol.db.entity.Device; -import cn.hutool.core.date.DateUtil; /** * 设备信息实体数据类 @@ -181,7 +181,15 @@ public class DeviceInfo extends BaseVO { return list; } - public static DeviceInfo convert(Device device) { + /** + * device to deviceInfo + * + * @param device + * @author niushuai + * @date: 2022/10/25 14:13 + * @return: {@link DeviceInfo} + */ + public static DeviceInfo parseDevice(Device device) { DeviceInfo _this = new DeviceInfo(); _this.setId(device.getId() + ""); @@ -196,8 +204,35 @@ public class DeviceInfo extends BaseVO { _this.setCommandClose(device.getCommandClose()); _this.setOrder(device.getOrder()); _this.setIsDeleted(device.getIsDeleted()); - _this.setCreateTime(DateUtil.parseDateTime(device.getCreateTime())); + _this.setCreateTime(DateFormatUtil.parseDateTime(device.getCreateTime())); return _this; } + + + /** + * deviceInfo to device + * + * @author niushuai + * @date: 2022/10/25 14:13 + * @return: {@link Device} + */ + public Device toDevice() { + Device to = new Device(); + to.setId(Long.valueOf(this.getId())); + to.setDeviceName(this.getDeviceName()); + to.setDeviceType(this.getDeviceType().getValue()); + to.setOnOff(this.getOnOff().getValue()); + to.setRemark(this.getRemark()); + to.setIconId(this.getIconId()); + to.setCommandPath(this.getCommandPath()); + to.setCommandStatus(this.getCommandStatus()); + to.setCommandOpen(this.getCommandOpen()); + to.setCommandClose(this.getCommandClose()); + to.setOrder(this.getOrder()); + to.setIsDeleted(this.getIsDeleted()); + to.setCreateTime(DateFormatUtil.formatDateTime(this.getCreateTime())); + + return to; + } }