RDSRoomCell.m 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. //
  2. // RDSRoomCell.m
  3. // Temperature
  4. //
  5. // Created by RD on 2022/10/31.
  6. //
  7. #import "RDSRoomCell.h"
  8. #import <SDWebImage/SDWebImage.h>
  9. @interface RDSRoomCell()
  10. @property (nonatomic, strong) UIView *containerView;
  11. @property (nonatomic, strong) UIImageView *iconView;
  12. @property (nonatomic, strong) UILabel *roomNameLabel;
  13. @property (nonatomic, strong) UILabel *deviceTypeLabel;
  14. @property (nonatomic, strong) UIView *lineView;
  15. @property (nonatomic, strong) UIView *pointView;
  16. @property (nonatomic, strong) UILabel *statusLabel;
  17. @property (nonatomic, strong) UISwitch *deviceSwitch;
  18. @property (nonatomic, strong) UIButton *deleteButton;
  19. @property (nonatomic, strong) NSDictionary *codeToNameDic;
  20. @end
  21. @implementation RDSRoomCell
  22. - (instancetype)initWithFrame:(CGRect)frame {
  23. self = [super initWithFrame:frame];
  24. if (self) {
  25. [self configSubviews];
  26. }
  27. return self;
  28. }
  29. - (void)setModel:(RDSRoomModel *)model {
  30. _model = model;
  31. self.roomNameLabel.text = model.name;
  32. // if (isEmptyString(model.code)) {
  33. // self.iconView.image = [UIImage imageNamed:@"icon_fluorine_sub_controller"];
  34. // } else {
  35. // self.iconView.image = [UIImage imageNamed:self.codeToNameDic[model.code]];
  36. // }
  37. [self.iconView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@", TheApiHelper.baseUrlHeadImg, model.icon]] placeholderImage:[UIImage imageNamed:@"icon_fluorine_sub_controller"]];
  38. if (isEmptyString(model.device_type_name)) {
  39. self.deviceTypeLabel.text = @"分控制器";
  40. } else {
  41. self.deviceTypeLabel.text = model.device_type_name;
  42. }
  43. // 如果是永续精灵则隐藏相应的组件显示
  44. if (model.deviceType == RDSDeviceType_YongXuSpirit) {
  45. self.deviceSwitch.hidden = true;
  46. } else {
  47. self.deviceSwitch.hidden = false;
  48. self.lineView.hidden = false;
  49. self.statusLabel.hidden = false;
  50. self.pointView.hidden = false;
  51. self.deviceSwitch.on = model.power;
  52. }
  53. // RDSHomeModel *currentHome = TheDataManager.detailHome;
  54. if (model.is_online) {
  55. self.statusLabel.text = [NSString stringWithFormat:@"温度%@°C 湿度%@%%", @(model.temperature), @(model.humidity)];
  56. self.statusLabel.textColor = [UIColor colorWithHexString:@"#14C9C8"];
  57. self.pointView.backgroundColor = [UIColor colorWithHexString:@"#14C9C8"];
  58. if (model.deviceType == RDSDeviceType_YongXuSpirit) {
  59. self.statusLabel.text = @"在线";
  60. }
  61. } else {
  62. self.statusLabel.text = @"已离线";
  63. self.statusLabel.textColor = [UIColor colorWithHexString:@"#FAAD14"];
  64. self.pointView.backgroundColor = [UIColor colorWithHexString:@"#FAAD14"];
  65. }
  66. }
  67. - (void)setIsEdit:(BOOL)isEdit {
  68. _isEdit = isEdit;
  69. if (isEdit) {
  70. [self.deviceSwitch removeFromSuperview];
  71. [self.contentView addSubview:self.deleteButton];
  72. [self.deleteButton mas_makeConstraints:^(MASConstraintMaker *make) {
  73. make.size.mas_equalTo(CGSizeMake(48, 48));
  74. make.right.equalTo(self.containerView).offset(-16);
  75. make.centerY.mas_equalTo(0);
  76. }];
  77. [self.roomNameLabel mas_updateConstraints:^(MASConstraintMaker *make) {
  78. make.right.equalTo(self.deleteButton.mas_left).offset(-12);
  79. }];
  80. } else {
  81. [self.deleteButton removeFromSuperview];
  82. [self.contentView addSubview:self.deviceSwitch];
  83. [self.deviceSwitch mas_makeConstraints:^(MASConstraintMaker *make) {
  84. make.size.mas_equalTo(CGSizeMake(48, 24));
  85. make.centerY.mas_equalTo(0);
  86. make.right.equalTo(self.containerView).offset(-16);
  87. }];
  88. [self.roomNameLabel mas_updateConstraints:^(MASConstraintMaker *make) {
  89. make.right.equalTo(self.deviceSwitch.mas_left).offset(-12);
  90. }];
  91. }
  92. }
  93. - (void)onPowerChanged:(UISwitch *)powerSwitch {
  94. BLOCK_SAFE_RUN(_onPowerClick, powerSwitch.isOn);
  95. }
  96. - (void)deleteButtonClicked {
  97. BLOCK_SAFE_RUN(_onDelClick);
  98. }
  99. - (void)configSubviews {
  100. [self.contentView addSubview:self.containerView];
  101. [self.containerView addSubview:self.iconView];
  102. [self.containerView addSubview:self.roomNameLabel];
  103. [self.containerView addSubview:self.deviceTypeLabel];
  104. [self.containerView addSubview:self.lineView];
  105. [self.containerView addSubview:self.pointView];
  106. [self.containerView addSubview:self.statusLabel];
  107. [self.containerView addSubview:self.deviceSwitch];
  108. [self.containerView mas_makeConstraints:^(MASConstraintMaker *make) {
  109. make.edges.mas_equalTo(UIEdgeInsetsZero);
  110. }];
  111. [self.iconView mas_makeConstraints:^(MASConstraintMaker *make) {
  112. make.left.top.equalTo(self.containerView).offset(20);
  113. make.size.mas_equalTo(CGSizeMake(40, 40));
  114. make.bottom.equalTo(self.containerView).offset(-20);
  115. }];
  116. [self.deviceSwitch mas_makeConstraints:^(MASConstraintMaker *make) {
  117. make.size.mas_equalTo(CGSizeMake(48, 24));
  118. make.centerY.mas_equalTo(0);
  119. make.right.equalTo(self.containerView).offset(-16);
  120. }];
  121. [self.roomNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  122. make.left.equalTo(self.iconView.mas_right).offset(12);
  123. make.top.equalTo(self.containerView).offset(18);
  124. make.right.equalTo(self.deviceSwitch.mas_left).offset(-12);
  125. make.height.mas_equalTo(23);
  126. }];
  127. [self.deviceTypeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  128. make.left.equalTo(self.roomNameLabel);
  129. make.bottom.equalTo(self.containerView).offset(-18);
  130. make.height.mas_equalTo(17);
  131. }];
  132. [self.deviceTypeLabel setContentHuggingPriority:UILayoutPriorityDefaultHigh forAxis:UILayoutConstraintAxisHorizontal];
  133. [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
  134. make.size.mas_equalTo(CGSizeMake(1, 8));
  135. make.centerY.equalTo(self.deviceTypeLabel);
  136. make.left.equalTo(self.deviceTypeLabel.mas_right).offset(8);
  137. }];
  138. [self.pointView mas_makeConstraints:^(MASConstraintMaker *make) {
  139. make.size.mas_equalTo(CGSizeMake(5, 5));
  140. make.centerY.equalTo(self.deviceTypeLabel);
  141. make.left.equalTo(self.lineView.mas_right).offset(8);
  142. }];
  143. [self.statusLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  144. make.bottom.height.equalTo(self.deviceTypeLabel);
  145. make.left.equalTo(self.pointView.mas_right).offset(5);
  146. make.right.equalTo(self.roomNameLabel);
  147. }];
  148. }
  149. - (UIView *)containerView {
  150. if (!_containerView) {
  151. _containerView = [[UIView alloc] init];
  152. _containerView.backgroundColor = UIColor.whiteColor;
  153. _containerView.layer.cornerRadius = 6;
  154. }
  155. return _containerView;
  156. }
  157. - (UIImageView *)iconView {
  158. if (!_iconView) {
  159. _iconView = [[UIImageView alloc] init];
  160. }
  161. return _iconView;
  162. }
  163. - (UILabel *)roomNameLabel {
  164. if (!_roomNameLabel) {
  165. _roomNameLabel = [[UILabel alloc] init];
  166. _roomNameLabel.font = [UIFont pingFangSCMediumOfSize:16];
  167. _roomNameLabel.textColor = [UIColor colorWithHexString:@"#333333"];
  168. }
  169. return _roomNameLabel;
  170. }
  171. - (UILabel *)deviceTypeLabel {
  172. if (!_deviceTypeLabel) {
  173. _deviceTypeLabel = [[UILabel alloc] init];
  174. _deviceTypeLabel.font = [UIFont pingFangSCRegularOfSize:12];
  175. _deviceTypeLabel.textColor = [UIColor colorWithHexString:@"#999999"];
  176. }
  177. return _deviceTypeLabel;
  178. }
  179. - (UIView *)lineView {
  180. if (!_lineView) {
  181. _lineView = [[UIView alloc] init];
  182. _lineView.backgroundColor = [UIColor colorWithHexString:@"#999999"];
  183. }
  184. return _lineView;
  185. }
  186. - (UIView *)pointView {
  187. if (!_pointView) {
  188. _pointView = [[UIView alloc] init];
  189. _pointView.layer.cornerRadius = 2.5;
  190. }
  191. return _pointView;
  192. }
  193. - (UILabel *)statusLabel {
  194. if (!_statusLabel) {
  195. _statusLabel = [[UILabel alloc] init];
  196. _statusLabel.font = [UIFont pingFangSCRegularOfSize:12];
  197. }
  198. return _statusLabel;
  199. }
  200. - (UISwitch *)deviceSwitch {
  201. if (!_deviceSwitch) {
  202. _deviceSwitch = [[UISwitch alloc] init];
  203. _deviceSwitch.onTintColor = [UIColor colorWithHexString:@"#14C9C8"];
  204. _deviceSwitch.tintColor = [UIColor colorWithHexString:@"#D9D9D9"];
  205. [_deviceSwitch addTarget:self action:@selector(onPowerChanged:) forControlEvents:UIControlEventValueChanged];
  206. }
  207. return _deviceSwitch;
  208. }
  209. - (UIButton *)deleteButton {
  210. if (!_deleteButton) {
  211. _deleteButton = [[UIButton alloc] init];
  212. [_deleteButton setImage:[UIImage imageNamed:@"icon_delete"] forState:UIControlStateNormal];
  213. [_deleteButton addTarget:self action:@selector(deleteButtonClicked) forControlEvents:UIControlEventTouchUpInside];
  214. }
  215. return _deleteButton;
  216. }
  217. - (NSDictionary *)codeToNameDic {
  218. if (!_codeToNameDic) {
  219. _codeToNameDic = @{@"YXK-Z/86-FG-A": @"icon_fluorine_main_controller",
  220. @"YXK-F/86-FG-A": @"icon_fluorine_sub_controller",
  221. @"YXK-Z/86-FC-B": @"icon_water_main_controller",
  222. @"YXK-F/86-FC-B": @"icon_water_sub_controller",
  223. @"YXK-P/DN-JF-8": @"icon_diverter_water_collector"};
  224. }
  225. return _codeToNameDic;
  226. }
  227. @end