RDSRoomCell.m 8.4 KB

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