RDSRoomCell.m 8.4 KB

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