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 e73f5eb..59a26a9 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 @@ -13,6 +13,7 @@ import cc.niushuai.project.devcontrol.base.util.ActivityUtil; import cc.niushuai.project.devcontrol.base.util.GlobalVariables; import cc.niushuai.project.devcontrol.base.util.Keys; import cc.niushuai.project.devcontrol.base.util.UiUtil; +import cc.niushuai.project.devcontrol.ui.common.IconSelectDialogFragment; import cc.niushuai.project.devcontrol.vo.DeviceInfo; import cn.hutool.core.util.StrUtil; @@ -147,6 +148,39 @@ public abstract class BaseActivity extends AppCompatActivity { findViewById(R.id.activity_title_more_set).setOnClickListener(onClickListener); } + /** + * 为icon选择绑定点击事件 + * + * @author niushuai + * @date: 2022/10/25 16:55 + */ + protected void activityIconSelectClickListener() { + findViewById(R.id.device_add_select_icon).setOnClickListener(view -> { + IconSelectDialogFragment iconSelectDialogFragment = new IconSelectDialogFragment(); + 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); + }); + + }); + } + + + /** + * 为icon选择绑定点击事件 + * + * @param onClickListener + * @author niushuai + * @date: 2022/10/25 16:55 + */ + protected void activityIconSelectClickListener(View.OnClickListener onClickListener) { + findViewById(R.id.device_add_select_icon).setOnClickListener(onClickListener); + } + /** * 重建缓存 * 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 new file mode 100644 index 0000000..901ca7c --- /dev/null +++ b/app/src/main/java/cc/niushuai/project/devcontrol/ui/common/IconSelectDialogFragment.java @@ -0,0 +1,108 @@ +package cc.niushuai.project.devcontrol.ui.common; + +import android.content.DialogInterface; +import android.graphics.Color; +import android.graphics.drawable.ColorDrawable; +import android.os.Bundle; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.AdapterView; +import android.widget.GridView; +import android.widget.SimpleAdapter; +import android.widget.TextView; + +import androidx.fragment.app.DialogFragment; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import cc.niushuai.project.devcontrol.R; +import cc.niushuai.project.devcontrol.base.enums.IconEnum; +import cc.niushuai.project.devcontrol.databinding.LayoutIconSelectBinding; + +/** + * icon选择弹窗 + * + * @author niushuai233 + * @date 2022/10/25 16:51 + */ +public class IconSelectDialogFragment extends DialogFragment { + + private LayoutIconSelectBinding binding; + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { + + binding = LayoutIconSelectBinding.inflate(getLayoutInflater()); + + this.init(); + + return binding.getRoot(); + } + + /** + * 初始化入口 + * + * @author niushuai + * @date: 2022/10/25 17:21 + */ + private void init() { + + this.initIconList(); + } + + /** + * 加载icon图标列表 + * + * @author niushuai + * @date: 2022/10/25 17:22 + */ + private void initIconList() { + + GridView iconListGridView = binding.iconListGridView; + + SimpleAdapter simpleAdapter = new SimpleAdapter(getContext(), getIconList(), R.layout.layout_icon_select, + 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} + ); + + iconListGridView.setSelector(new ColorDrawable(Color.TRANSPARENT)); + iconListGridView.setOnItemClickListener(this::iconListClickListener); + iconListGridView.setAdapter(simpleAdapter); + } + + 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(); + + // 关闭弹出框 + this.dismiss(); + } + + private List> getIconList() { + IconEnum[] allIcon = IconEnum.values(); + + List> gvData = new ArrayList<>(allIcon.length); + + for (IconEnum icon : allIcon) { + HashMap item = new HashMap<>(3); + item.put("icon_select_list_key", icon.getIconIdDark()); + item.put("icon_select_list_icon", icon.getIconIdDark()); + item.put("icon_select_list_text", icon.getDesc()); + gvData.add(item); + } + + return gvData; + } + + @Override + public void onDismiss(DialogInterface dialog) { + super.onDismiss(dialog); + binding = null; + } + +} diff --git a/app/src/main/java/cc/niushuai/project/devcontrol/ui/common/IconSelectViewModel.java b/app/src/main/java/cc/niushuai/project/devcontrol/ui/common/IconSelectViewModel.java new file mode 100644 index 0000000..7df7270 --- /dev/null +++ b/app/src/main/java/cc/niushuai/project/devcontrol/ui/common/IconSelectViewModel.java @@ -0,0 +1,23 @@ +package cc.niushuai.project.devcontrol.ui.common; + +import androidx.lifecycle.MutableLiveData; +import androidx.lifecycle.ViewModel; + +/** + * TODO + * + * @author niushuai233 + * @date 2022/10/25 17:11 + */ +public class IconSelectViewModel extends ViewModel { + + private final MutableLiveData iconResId = new MutableLiveData<>(); + + public MutableLiveData getIconResId() { + return iconResId; + } + + public void setIconResId(Integer val) { + iconResId.setValue(val); + } +} 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 841a7d6..d04de34 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,6 +17,7 @@ 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; @@ -56,6 +57,13 @@ public class DeviceAddPowerSwitchActivity extends BaseActivity { this.activityButtonBackClickListener(this); // 保存设备信息事件 this.activityButtonMoreSetClickListener(this::confirm4SaveDataClickListener); + // icon选择器点击事件 + this.activityIconSelectClickListener(); + + } + + protected void iconSelectClickListener(View view) { + } /** diff --git a/app/src/main/res/layout/layout_icon_select.xml b/app/src/main/res/layout/layout_icon_select.xml new file mode 100644 index 0000000..e34f0ec --- /dev/null +++ b/app/src/main/res/layout/layout_icon_select.xml @@ -0,0 +1,33 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/layout_icon_select_item.xml b/app/src/main/res/layout/layout_icon_select_item.xml new file mode 100644 index 0000000..47b7643 --- /dev/null +++ b/app/src/main/res/layout/layout_icon_select_item.xml @@ -0,0 +1,59 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values/ids.xml b/app/src/main/res/values/ids.xml index dd80711..0e2c241 100644 --- a/app/src/main/res/values/ids.xml +++ b/app/src/main/res/values/ids.xml @@ -9,6 +9,11 @@ + + + + + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index e9f4335..ccd079a 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -37,6 +37,9 @@ 选择设备类型 + + 选择图标 + 更换图标