diff --git a/app/src/main/java/cc/niushuai/project/devcontrol/base/ui/BaseActivity.java b/app/src/main/java/cc/niushuai/project/devcontrol/base/ui/BaseActivity.java index 59a26a9..b62a9c6 100644 --- a/app/src/main/java/cc/niushuai/project/devcontrol/base/ui/BaseActivity.java +++ b/app/src/main/java/cc/niushuai/project/devcontrol/base/ui/BaseActivity.java @@ -154,18 +154,10 @@ public abstract class BaseActivity extends AppCompatActivity { * @author niushuai * @date: 2022/10/25 16:55 */ - protected void activityIconSelectClickListener() { + protected void activityIconSelectClickListener(Integer callbackIconResId) { findViewById(R.id.device_add_select_icon).setOnClickListener(view -> { - IconSelectDialogFragment iconSelectDialogFragment = new IconSelectDialogFragment(); + IconSelectDialogFragment iconSelectDialogFragment = new IconSelectDialogFragment(callbackIconResId); iconSelectDialogFragment.show(getSupportFragmentManager(), IconSelectDialogFragment.class.getName()); - - getSupportFragmentManager().setFragmentResultListener(IconSelectDialogFragment.class.getSimpleName(), - this, (requestKey, result) -> { - int iconResId = result.getInt(requestKey + Keys.ID); - System.out.println(iconResId); - device.setIconId(iconResId); - }); - }); } diff --git a/app/src/main/java/cc/niushuai/project/devcontrol/base/util/CommonUiUtil.java b/app/src/main/java/cc/niushuai/project/devcontrol/base/util/CommonUiUtil.java index 7a13396..9d314ca 100644 --- a/app/src/main/java/cc/niushuai/project/devcontrol/base/util/CommonUiUtil.java +++ b/app/src/main/java/cc/niushuai/project/devcontrol/base/util/CommonUiUtil.java @@ -29,6 +29,8 @@ public class CommonUiUtil { DeviceInfo deviceInfo = new DeviceInfo(); deviceInfo.setId(deviceId); + deviceInfo.setIconId(UiUtil.getAppImageCompatResourceTag(activity, R.id.device_add_select_icon)); + deviceInfo.setDeviceName(UiUtil.getTextViewTextById(activity, R.id.device_add_name)); deviceInfo.setCommandPath(UiUtil.getTextViewTextById(activity, R.id.device_add_param_program)); deviceInfo.setCommandStatus(UiUtil.getTextViewTextById(activity, R.id.device_add_param_status)); @@ -53,6 +55,8 @@ public class CommonUiUtil { Device device = new Device(); device.setId(deviceId); + device.setIconId(UiUtil.getAppImageCompatResourceTag(activity, R.id.device_add_select_icon)); + device.setDeviceName(UiUtil.getTextViewTextById(activity, R.id.device_add_name)); device.setCommandPath(UiUtil.getTextViewTextById(activity, R.id.device_add_param_program)); device.setCommandStatus(UiUtil.getTextViewTextById(activity, R.id.device_add_param_status)); 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 760a11b..32602e1 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 @@ -30,6 +30,7 @@ public class GlobalVariables { for (Device device : deviceList) { DEVICE_INFO_MAP.put(device.getId() + "", DeviceInfo.parseDevice(device)); } + System.out.println(); } public static Class getDeviceAddActivity(String deviceId) { diff --git a/app/src/main/java/cc/niushuai/project/devcontrol/base/util/UiUtil.java b/app/src/main/java/cc/niushuai/project/devcontrol/base/util/UiUtil.java index e054979..7a3127b 100644 --- a/app/src/main/java/cc/niushuai/project/devcontrol/base/util/UiUtil.java +++ b/app/src/main/java/cc/niushuai/project/devcontrol/base/util/UiUtil.java @@ -41,8 +41,14 @@ public class UiUtil { // ((ImageView) view.findViewById(id)).setImageResource(resId); // } + public static int getAppImageCompatResourceTag(Activity activity, int id) { + return (int) ((AppCompatImageView) activity.findViewById(id)).getTag(); + } + public static void setAppImageCompatResource(Activity activity, int id, int resId) { - ((AppCompatImageView) activity.findViewById(id)).setImageResource(resId); + AppCompatImageView appCompatImageView = (AppCompatImageView) activity.findViewById(id); + appCompatImageView.setImageResource(resId); + appCompatImageView.setTag(resId); } // public static void setAppImageCompatResource(View view, int id, int resId) { diff --git a/app/src/main/java/cc/niushuai/project/devcontrol/ui/common/IconSelectDialogFragment.java b/app/src/main/java/cc/niushuai/project/devcontrol/ui/common/IconSelectDialogFragment.java index 901ca7c..badea78 100644 --- a/app/src/main/java/cc/niushuai/project/devcontrol/ui/common/IconSelectDialogFragment.java +++ b/app/src/main/java/cc/niushuai/project/devcontrol/ui/common/IconSelectDialogFragment.java @@ -21,6 +21,7 @@ import java.util.Map; import cc.niushuai.project.devcontrol.R; import cc.niushuai.project.devcontrol.base.enums.IconEnum; +import cc.niushuai.project.devcontrol.base.util.UiUtil; import cc.niushuai.project.devcontrol.databinding.LayoutIconSelectBinding; /** @@ -32,6 +33,7 @@ import cc.niushuai.project.devcontrol.databinding.LayoutIconSelectBinding; public class IconSelectDialogFragment extends DialogFragment { private LayoutIconSelectBinding binding; + private Integer callbackIconResId; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { @@ -43,6 +45,10 @@ public class IconSelectDialogFragment extends DialogFragment { return binding.getRoot(); } + public IconSelectDialogFragment(Integer callbackIconResId) { + this.callbackIconResId = callbackIconResId; + } + /** * 初始化入口 * @@ -64,7 +70,7 @@ public class IconSelectDialogFragment extends DialogFragment { GridView iconListGridView = binding.iconListGridView; - SimpleAdapter simpleAdapter = new SimpleAdapter(getContext(), getIconList(), R.layout.layout_icon_select, + SimpleAdapter simpleAdapter = new SimpleAdapter(getContext(), getIconList(), R.layout.layout_icon_select_item, new String[]{"icon_select_list_key", "icon_select_list_icon", "icon_select_list_text"}, new int[]{R.id.icon_select_list_key, R.id.icon_select_list_icon, R.id.icon_select_list_text} ); @@ -77,7 +83,9 @@ public class IconSelectDialogFragment extends DialogFragment { private void iconListClickListener(AdapterView parent, View view, int position, long id) { // 跳转到相应的activity - String iconId = ((TextView) view.findViewById(R.id.device_type_list_key)).getText().toString(); + String iconId = ((TextView) view.findViewById(R.id.icon_select_list_key)).getText().toString(); + + UiUtil.setAppImageCompatResource(getActivity(), callbackIconResId, Integer.parseInt(iconId)); // 关闭弹出框 this.dismiss(); 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 d04de34..b5749a7 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 @@ -17,7 +17,6 @@ import cc.niushuai.project.devcontrol.base.util.ToastUtil; import cc.niushuai.project.devcontrol.databinding.ActivityDeviceAddPowerSwitchBinding; import cc.niushuai.project.devcontrol.db.DB; import cc.niushuai.project.devcontrol.db.entity.Device; -import cc.niushuai.project.devcontrol.ui.common.IconSelectDialogFragment; import cc.niushuai.project.devcontrol.ui.powerswitch.PowerSwitchActivity; import cn.hutool.core.date.DateUtil; import cn.hutool.core.util.StrUtil; @@ -58,12 +57,7 @@ public class DeviceAddPowerSwitchActivity extends BaseActivity { // 保存设备信息事件 this.activityButtonMoreSetClickListener(this::confirm4SaveDataClickListener); // icon选择器点击事件 - this.activityIconSelectClickListener(); - - } - - protected void iconSelectClickListener(View view) { - + this.activityIconSelectClickListener(R.id.device_add_select_icon); } /** @@ -76,7 +70,7 @@ public class DeviceAddPowerSwitchActivity extends BaseActivity { private void confirm4SaveDataClickListener(View view) { Device device = CommonUiUtil.getDeviceInfo(this, IdWorker.getNextId()); - device.setIconId(R.drawable.ic_device_type_switch); +// device.setIconId(R.drawable.ic_device_type_switch); device.setDeviceType(DeviceTypeEnum.Power_Switch.getValue()); device.setOnOff(OnOffEnum.OFF.getValue()); 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 b0068da..d68799c 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 @@ -46,7 +46,9 @@ public class PowerSwitchSetParamActivity extends BaseActivity { // 隐藏更多按钮 super.activityButtonBackClickListener(this); // 保存监听事件 - super.activityButtonMoreSetClickListener(this::btnSaveParamClickListener); + super.activityButtonMoreSetClickListener(this::btnUpdateParamClickListener); + // icon选择监听事件 + super.activityIconSelectClickListener(R.id.device_add_select_icon); } /** @@ -56,13 +58,13 @@ public class PowerSwitchSetParamActivity extends BaseActivity { * @author niushuai * @date: 2022/10/25 13:56 */ - private void btnSaveParamClickListener(View view) { + private void btnUpdateParamClickListener(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.setIconId(device.getIconId()); deviceInfo.setIsDeleted(device.getIsDeleted()); deviceInfo.setCreateTime(device.getCreateTime()); DB.getDeviceDao().update(deviceInfo.toDevice());