mirror of
https://github.com/niushuai233/DevControl.git
synced 2024-10-27 22:43:20 +08:00
feat: 开关操作详情页
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
package cc.niushuai.project.devcontrol;
|
||||
|
||||
import android.content.Context;
|
||||
import android.os.Bundle;
|
||||
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
package cc.niushuai.project.devcontrol.base.enums;
|
||||
|
||||
public enum OnOffEnum {
|
||||
|
||||
ON, OFF;
|
||||
}
|
||||
@@ -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<String, DeviceInfo> DEVICE_INFO_MAP = new HashMap<>(16);
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package cc.niushuai.project.devcontrol.base.util;
|
||||
|
||||
public interface Keys {
|
||||
|
||||
String ID = "id";
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -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> deviceInfo;
|
||||
|
||||
public DeviceViewModel() {
|
||||
}
|
||||
|
||||
public MutableLiveData<DeviceInfo> getDeviceInfo() {
|
||||
return deviceInfo;
|
||||
}
|
||||
|
||||
public void setDeviceInfo(MutableLiveData<DeviceInfo> deviceInfo) {
|
||||
this.deviceInfo = deviceInfo;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user