RDSRoomCell.m 8.7 KB

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