diff --git a/app/src/main/java/cc/niushuai/project/devcontrol/MainActivity.java b/app/src/main/java/cc/niushuai/project/devcontrol/MainActivity.java index f375fca..d8d2408 100644 --- a/app/src/main/java/cc/niushuai/project/devcontrol/MainActivity.java +++ b/app/src/main/java/cc/niushuai/project/devcontrol/MainActivity.java @@ -1,6 +1,5 @@ package cc.niushuai.project.devcontrol; -import android.content.Context; import android.os.Bundle; import androidx.appcompat.app.AppCompatActivity; diff --git a/app/src/main/java/cc/niushuai/project/devcontrol/base/entity/device/DeviceInfo.java b/app/src/main/java/cc/niushuai/project/devcontrol/base/entity/device/DeviceInfo.java index bfae979..43601e9 100644 --- a/app/src/main/java/cc/niushuai/project/devcontrol/base/entity/device/DeviceInfo.java +++ b/app/src/main/java/cc/niushuai/project/devcontrol/base/entity/device/DeviceInfo.java @@ -4,6 +4,8 @@ import java.util.ArrayList; import java.util.List; import cc.niushuai.project.devcontrol.base.enums.DeviceTypeEnum; +import cc.niushuai.project.devcontrol.base.enums.OnOffEnum; +import cc.niushuai.project.devcontrol.base.util.GlobalVariables; import cn.hutool.core.util.IdUtil; import cn.hutool.core.util.StrUtil; @@ -30,6 +32,11 @@ public class DeviceInfo { */ private DeviceTypeEnum type; + /** + * 开关状态 + */ + private OnOffEnum onOff; + /** * 设备描述信息 */ @@ -80,6 +87,14 @@ public class DeviceInfo { this.type = type; } + public OnOffEnum getOnOff() { + return onOff; + } + + public void setOnOff(OnOffEnum onOff) { + this.onOff = onOff; + } + public String getDescription() { return description; } @@ -134,9 +149,12 @@ public class DeviceInfo { device.setIconId(iconId); device.setDescription("卧室灯开关-树莓派"); device.setType(DeviceTypeEnum.Switch); + device.setOnOff(OnOffEnum.OFF); device.setCommandPath("/path/file"); device.setCommandArgs("-c light -t 1"); list.add(device); + + GlobalVariables.DEVICE_INFO_MAP.put(device.getId(), device); } return list; diff --git a/app/src/main/java/cc/niushuai/project/devcontrol/base/enums/OnOffEnum.java b/app/src/main/java/cc/niushuai/project/devcontrol/base/enums/OnOffEnum.java new file mode 100644 index 0000000..a758b90 --- /dev/null +++ b/app/src/main/java/cc/niushuai/project/devcontrol/base/enums/OnOffEnum.java @@ -0,0 +1,6 @@ +package cc.niushuai.project.devcontrol.base.enums; + +public enum OnOffEnum { + + ON, OFF; +} 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 new file mode 100644 index 0000000..e513fbc --- /dev/null +++ b/app/src/main/java/cc/niushuai/project/devcontrol/base/util/GlobalVariables.java @@ -0,0 +1,17 @@ +package cc.niushuai.project.devcontrol.base.util; + +import java.util.HashMap; +import java.util.Map; + +import cc.niushuai.project.devcontrol.base.entity.device.DeviceInfo; + +public class GlobalVariables { + + /** + * 全部设备列表map + * id为key elem为value + */ + public static final Map DEVICE_INFO_MAP = new HashMap<>(16); + + +} diff --git a/app/src/main/java/cc/niushuai/project/devcontrol/base/util/Keys.java b/app/src/main/java/cc/niushuai/project/devcontrol/base/util/Keys.java new file mode 100644 index 0000000..4d18dd6 --- /dev/null +++ b/app/src/main/java/cc/niushuai/project/devcontrol/base/util/Keys.java @@ -0,0 +1,6 @@ +package cc.niushuai.project.devcontrol.base.util; + +public interface Keys { + + String ID = "id"; +} diff --git a/app/src/main/java/cc/niushuai/project/devcontrol/ui/device/DeviceActivity.java b/app/src/main/java/cc/niushuai/project/devcontrol/ui/device/DeviceActivity.java index 1d2ccbf..6febb9f 100644 --- a/app/src/main/java/cc/niushuai/project/devcontrol/ui/device/DeviceActivity.java +++ b/app/src/main/java/cc/niushuai/project/devcontrol/ui/device/DeviceActivity.java @@ -1,13 +1,23 @@ package cc.niushuai.project.devcontrol.ui.device; import android.os.Bundle; +import android.view.View; +import android.widget.TextView; +import androidx.appcompat.widget.AppCompatImageView; + +import cc.niushuai.project.devcontrol.R; import cc.niushuai.project.devcontrol.base.activity.BaseActivity; +import cc.niushuai.project.devcontrol.base.entity.device.DeviceInfo; +import cc.niushuai.project.devcontrol.base.enums.OnOffEnum; +import cc.niushuai.project.devcontrol.base.util.GlobalVariables; +import cc.niushuai.project.devcontrol.base.util.Keys; import cc.niushuai.project.devcontrol.databinding.DeviceActivityBinding; public class DeviceActivity extends BaseActivity { private DeviceActivityBinding binding; + private DeviceInfo device; @Override public void onCreate(Bundle savedInstanceState) { @@ -17,5 +27,73 @@ public class DeviceActivity extends BaseActivity { binding = DeviceActivityBinding.inflate(getLayoutInflater()); setContentView(binding.getRoot()); + + // 初始化页面数据 + init(); + // 添加点击事件 + addListener(); } + + /** + * 初始化页面数据 + * + * @author niushuai + * @date: 2022/10/19 11:18 + */ + public void init() { + String deviceId = getIntent().getStringExtra(Keys.ID); + + DeviceInfo data = GlobalVariables.DEVICE_INFO_MAP.get(deviceId); + if (null != data) { + this.device = data; + // 标题名称 + TextView titleTextView = findViewById(R.id.device_activity_title_name); + titleTextView.setText(device.getName()); + + // 副标题名称 + TextView descTextView = findViewById(R.id.device_activity_title_description); + descTextView.setText(device.getDescription()); + + // 开关底部的名称 + TextView contentTextTextView = findViewById(R.id.device_activity_content_text); + contentTextTextView.setText(device.getName()); + } + } + + /** + * 添加点击事件 + * + * @author niushuai + * @date: 2022/10/19 11:49 + */ + private void addListener() { + + binding.deviceActivityContentSwitch.setOnClickListener(this::switchClickListener); + } + + /** + * 开关点击事件处理 + * + * @param view 开关view + * @author niushuai + * @date: 2022/10/19 11:52 + */ + private void switchClickListener(View view) { + AppCompatImageView appCompatImageView = (AppCompatImageView) view; + + int switchImageId, iconImageId; + if (OnOffEnum.OFF.equals(device.getOnOff())) { + device.setOnOff(OnOffEnum.ON); + switchImageId = R.drawable.img_switch_open; + iconImageId = R.drawable.ic_device_light_1_on; + } else { + device.setOnOff(OnOffEnum.OFF); + switchImageId = R.drawable.img_switch_close; + iconImageId = R.drawable.ic_device_light_1_close; + } + appCompatImageView.setImageResource(switchImageId); + ((AppCompatImageView) findViewById(R.id.device_activity_content_icon)).setImageResource(iconImageId); + } + + } diff --git a/app/src/main/java/cc/niushuai/project/devcontrol/ui/device/DeviceViewModel.java b/app/src/main/java/cc/niushuai/project/devcontrol/ui/device/DeviceViewModel.java new file mode 100644 index 0000000..2f2413b --- /dev/null +++ b/app/src/main/java/cc/niushuai/project/devcontrol/ui/device/DeviceViewModel.java @@ -0,0 +1,22 @@ +package cc.niushuai.project.devcontrol.ui.device; + +import androidx.lifecycle.MutableLiveData; +import androidx.lifecycle.ViewModel; + +import cc.niushuai.project.devcontrol.base.entity.device.DeviceInfo; + +public class DeviceViewModel extends ViewModel { + + private MutableLiveData deviceInfo; + + public DeviceViewModel() { + } + + public MutableLiveData getDeviceInfo() { + return deviceInfo; + } + + public void setDeviceInfo(MutableLiveData deviceInfo) { + this.deviceInfo = deviceInfo; + } +} 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 4d81033..411bd09 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 @@ -22,6 +22,7 @@ import java.util.List; import cc.niushuai.project.devcontrol.R; import cc.niushuai.project.devcontrol.base.entity.device.DeviceInfo; +import cc.niushuai.project.devcontrol.base.util.Keys; import cc.niushuai.project.devcontrol.databinding.DeviceItemBinding; import cc.niushuai.project.devcontrol.databinding.MainNavFragmentDeviceBinding; import cc.niushuai.project.devcontrol.ui.device.DeviceActivity; @@ -63,19 +64,7 @@ public class NavDeviceFragment extends Fragment { GridView deviceGv = deviceBinding.deviceGv; deviceGv.setSelector(new ColorDrawable(Color.TRANSPARENT)); - deviceGv.setOnItemClickListener(new AdapterView.OnItemClickListener() { - @Override - public void onItemClick(AdapterView parent, View view, int position, long id) { - View text = view.findViewById(R.id.device_item_cardView_id); - String x = ";"; - if (text != null) { - x = ((TextView) text).getText().toString(); - } - Toast.makeText(getContext(), "Item Clicked" + x, Toast.LENGTH_SHORT).show(); - - startActivity(new Intent(getActivity(), DeviceActivity.class)); - } - }); + deviceGv.setOnItemClickListener(this::onItemClick); // SimpleAdapter gvAdapter = new SimpleAdapter(getContext(), dataItem, R.layout.device_item, // new String[]{"device_item_cardView_text"}, new int[]{R.id.device_item_cardView_text}); @@ -115,10 +104,29 @@ public class NavDeviceFragment extends Fragment { return gvData; } + /** + * 条目点击事件 + * + * @param parent 父view + * @param view 被点击的view + * @param position 当前view中的位置顺序 + * @param id 组件id + * @author niushuai + * @date: 2022/10/19 11:13 + */ + private void onItemClick(AdapterView parent, View view, int position, long id) { + TextView textId = view.findViewById(R.id.device_item_cardView_id); + + Intent intent = new Intent(getActivity(), DeviceActivity.class); + intent.putExtra(Keys.ID, textId.getText()); + startActivity(intent); + } + @Override public void onDestroyView() { super.onDestroyView(); deviceBinding = null; deviceItemBinding = null; } + } \ No newline at end of file diff --git a/app/src/main/res/drawable/ic_device_light_1_close.xml b/app/src/main/res/drawable/ic_device_light_1_close.xml new file mode 100644 index 0000000..dfa3cea --- /dev/null +++ b/app/src/main/res/drawable/ic_device_light_1_close.xml @@ -0,0 +1,15 @@ + + + + + diff --git a/app/src/main/res/layout/device_activity.xml b/app/src/main/res/layout/device_activity.xml index 3a08a66..3343a70 100644 --- a/app/src/main/res/layout/device_activity.xml +++ b/app/src/main/res/layout/device_activity.xml @@ -7,13 +7,13 @@ tools:context=".ui.device.DeviceActivity"> + android:layout_marginTop="35dp" + android:src="@drawable/ic_device_light_1_close" /> + android:text="@string/test_hello_blank_textview_1" /> diff --git a/app/src/main/res/values/ids.xml b/app/src/main/res/values/ids.xml index f4b5169..7c50440 100644 --- a/app/src/main/res/values/ids.xml +++ b/app/src/main/res/values/ids.xml @@ -17,20 +17,20 @@ - - - + + + - - - - + + + + - - - + + + diff --git a/iconfront/灯具-关.svg b/iconfront/灯具-关.svg new file mode 100644 index 0000000..0388a54 --- /dev/null +++ b/iconfront/灯具-关.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/iconfront/灯具-开.svg b/iconfront/灯具-开.svg index 4890883..a3b6c34 100644 --- a/iconfront/灯具-开.svg +++ b/iconfront/灯具-开.svg @@ -1 +1 @@ - \ No newline at end of file + \ No newline at end of file