设备详情 UI 业务包

更新时间:2022-06-23 09:23:24下载pdf

涂鸦 智慧居住 设备详情 UI 业务包包含了以下功能:

  • 设备信息编辑:包括设备头像、设备所在房间、名称等
  • 设备信息查询:包括设备 ID、信号等
  • 创建或编辑群组:从 3.25.0 版本 SDK 开始支持,您需要额外接入 设备群组 UI 业务包
  • 备用网络设置
  • 离线提醒功能
  • 删除设备

接入组件

组件集成

在工程的 Podfile 文件中添加业务包组件,并执行 pod update 命令。

source "https://github.com/tuya/TuyaPublicSpecs.git"
source 'https://cdn.cocoapods.org/'

target 'your_target_name' do
  # TuyaSmartResidence SDK
  pod "TuyaSmartResidenceKit"
  # 添加设备详情 UI 业务包
  pod 'TuyaSmartResidenceDeviceDetailBizBundle'
end

权限声明

设备详情 UI 业务包涉及到设备图标上传时,需要使用系统相册和相机权限。因此,会涉及到部分苹果隐私权限的声明。您需要在工程的 info.plist 中添加如下权限声明:

<!-- 相册 -->
<key>NSPhotoLibraryUsageDescription</key>
<string>App 需要您的同意,才能访问相册</string>
<!-- 相机 -->
<key>NSCameraUsageDescription</key>
<string>App 需要您的同意,才能访问相机</string>

配置文件

  1. 在主工程中,新建 configList.json 文件。如果已经存在该文件,则不用再新建。configList.json 配置文件内容大致如下:

    {
    	"deviceDetail": [{"type": "header"}],
    	"其他配置页面 key":[xxxxx]
    }
    
  2. configList.json 中添加以下内容:

    • key:deviceDetail
    • value:
      [
      	{
      		"type": "header"
      	},
      	{
      		"type": "device_info"
      	},
      	{
      		"type": "net_setting"
      	},
      	{
      		"type": "section_off_line_warn"
      	},
      	{
      		"type": "off_line_warn"
      	},
      	{
      		"type": "section_other"
      	},
      	{
      		"type": "group_edit_devices"
      	},
      	{
      		"type": "group_create"
      	},
      	{
      		"type": "help_and_feedback"
      	},
      	{
      		"type": "check_device_network"
      	},
      	{
      		"type": "check_firmware_update"
      	},
      	{
      		"type": "empty",
      		"height":16
      	},
      	{
      		"type": "footer"
      	}
      ]
      
  3. 自定义设备详情。

    deviceDetail 数组 item 的顺序影响设备详情页 item 展示的顺序。如果移除 item,则相应地移除设备详情页子功能。

    deviceDetail 类型 设备详情页子功能
    header 查看修改设备图标,名称,位置
    device_info 设备信息
    net_setting 设备备用网络
    off_line_warn 设备离线提醒
    group_edit_devices 编辑群组
    group_create 创建群组
    footer 移除设备
    section_other 分区头,无实际设备功能
    empty view,无实际设备功能

注意事项

  • 调用 UI 业务包逻辑前,请确保该站点调用过以下方法:

    [self.site fetchSiteDetailWithSuccess:^(TuyaResidenceSiteModel * _Nonnull siteModel) {
    
    } failure:^(NSError *error) {
    
    }];
    
  • 然后实现 TYFamilyProtocol 中的协议方法currentFamilyId

    #import <TuyaSmartBizCore/TuyaSmartBizCore.h>
    #import <TYModuleServices/TYFamilyProtocol.h>
    
    - (void)initCurrentSite {
    	// 注册要实现的协议
    	[[TuyaSmartBizCore sharedInstance] registerService:@protocol(TYFamilyProtocol) withInstance:self];
    }
    
    // 实现对应的协议方法
    - (long long)currentFamilyId {
    	return @"site id";
    }
    

可选功能实现

群组创建和编辑属于可选功能,您需要需要额外接入 设备群组 UI 业务包

方法调用

方法 参数
跳转到网络检测页 设备 ID
跳转到设备详情页,以 Push 方式实现 TuyaSmartDeviceModel/TuyaSmartGroupModel
#import <TuyaSmartBizCore/TuyaSmartBizCore.h>
#import <TYModuleServices/TuyaSmartResidenceDeviceDetailProtocol.h>

id<TuyaSmartResidenceDeviceDetailProtocol> impl = [[TuyaSmartBizCore sharedInstance] serviceOfProtocol:@protocol(TuyaSmartResidenceDeviceDetailProtocol)];

// 跳转到设备网络检测页
[impl gotoDeviceDetailNetworkViewControllerWithDeviceId:@"设备 id"];

// 如果是设备
TuyaSmartDevice *device = [TuyaSmartDevice deviceWithDeviceId:@"设备 id"];
[impl gotoDetailViewControllerWithDevice:device.deviceModel group:nil];

// 如果是群组
TuyaSmartGroup *group = [TuyaSmartDevice groupWithGroupId:@"群组 id"];
[impl gotoDetailViewControllerWithDevice:nil group:group.deviceModel];

自定义子功能

自定义子功能可以通过配置 configList.json 来实现。在 configList.json 文件的 deviceDetail 插入自定义 type

type 值必须以 c_ 开头。例如,插入 c_test_insertc_test_async_insert

{
	"deviceDetail": [
	 	{
			"type": "c_test_insert"
		},
		{
			"type": "c_test_async_insert"
		},
		{
			"type": "header"
		},
		{
			"type": "device_info"
		},
		{
			"type": "net_setting"
		},
		{
			"type": "section_off_line_warn"
		},
		{
			"type": "off_line_warn"
		},
		{
			"type": "section_other"
		},
		{
			"type": "help_and_feedback"
		},
		{
			"type": "check_device_network"
		},
		{
			"type": "check_firmware_update"
		},
		{
			"type": "empty",
			"height":16
		},
		{
			"type": "footer"
		}
	]
}

返回 item 数据

同步返回

/// Synchronize callbacks for item insertion
/// @param block Synchronous handling of item insertion callbacks
- (void)insertMenuItem:(ResidenceInsertMenuItemBlock)block;
//@param type type added by configList.json
//@param device device model
//@param group The group model. Determines if the device is a group or not, based on whether the group is nil
//@return An object that complies with the TuyaSmartResidenceDeviceDetailCustomMenuModel protocol. If nil is returned, the item of this type will not be displayed
typedef id<TuyaSmartResidenceDeviceDetailCustomMenuModel> _Nullable (^ResidenceInsertMenuItemBlock)(NSString *  _Nonnull type, TuyaSmartDeviceModel * _Nullable device, TuyaSmartGroupModel * _Nullable group);

异步回调

/// Asynchronous callback for item insertion
/// @param block Callback for asynchronous handling of item insertion
- (void)insertMenuItemAsync:(ResidenceInsertMenuItemAsyncBlock)block;
// Asynchronously process the item insertion, call complete() when the asynchronous operation is finished, call back the data to the device details and refresh the list. Determine if the device is a group or not based on whether the group is nil
typedef void (^ResidenceInsertMenuItemAsyncBlock)(NSString * _Nonnull type, TuyaSmartDeviceModel * _Nullable device, TuyaSmartGroupModel * _Nullable group, ResidenceInsertMenuItemComplete _Nonnull complete);

示例代码

第一步:新建一个 Model 类

//自定义一个类遵守 TuyaSmartResidenceDeviceDetailCustomMenuModel 协议
@interface CustomMenuModel : NSObject<TuyaSmartResidenceDeviceDetailCustomMenuModel>
///标题
@property (nonatomic,copy) NSString *title;
///子标题
@property (nonatomic,copy) NSString *detail;
@end

@implementation CustomMenuModel
@end

第二步:设置数据回调的 block

id<TuyaSmartResidenceDeviceDetailProtocol> impl = [[TuyaSmartBizCore sharedInstance] serviceOfProtocol:@protocol(TuyaSmartResidenceDeviceDetailProtocol)];
[impl insertMenuItem:^ id<TuyaSmartResidenceDeviceDetailCustomMenuModel> (NSString * _Nonnull type, TuyaSmartDeviceModel * _Nonnull device, TuyaSmartGroupModel * _Nonnull group) {
	if ([type isEqualToString:@"c_test_insert"]) {
		CustomMenuModel *model = [CustomMenuModel new];
		if (group) { // 根据 group 是否为 nil,来判断获取到的是设备还是群组
			model.title = type;
			model.detail = @"group";
		}
		else {
			model.title = type;
			model.detail = @"device";
		}
		return model;
	}
	return nil;
}];

[impl insertMenuItemAsync:^(NSString * _Nonnull type, TuyaSmartDeviceModel * _Nullable device, TuyaSmartGroupModel * _Nullable group, ResidenceInsertMenuItemComplete _Nonnull complete) {
	if ([type isEqualToString:@"c_test_async_insert"]) {
		// 耗时操作
		dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
			CustomMenuModel *model = [CustomMenuModel new];
			if (group) { // 根据 group 是否为 nil,来判断获取到的是设备还是群组
				model.title = type;
				model.detail = @"group";
			}
			else {
				model.title = type;
				model.detail = @"device";
			}
			complete(model);
		});
	}
}];

插入 item 点击事件处理

id<TuyaSmartResidenceDeviceDetailProtocol> impl = [[TuyaSmartBizCore sharedInstance] serviceOfProtocol:@protocol(TuyaSmartResidenceDeviceDetailProtocol)];
[impl clickMenuItem: ^(NSString * _Nonnull type, TuyaSmartDeviceModel * _Nullable device ,TuyaSmartGroupModel * _Nullable group) {
	NSLog(@"clickItem: type:%@",type);
}];