DeviceNodePopupView.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. //
  2. // DeviceNodePopupView.m
  3. // kneet2
  4. //
  5. // Created by Jason Lee on 11/5/15.
  6. // Copyright (c) 2015 ntels. All rights reserved.
  7. //
  8. #import "JDObject.h"
  9. #import "RequestHandler.h"
  10. #import "JDJSONModel.h"
  11. #import "CustomLabel.h"
  12. #import "CustomTextField.h"
  13. #import "CustomTableView.h"
  14. #import "CustomImageView.h"
  15. #import "CustomButton.h"
  16. #import "CustomCheckBox.h"
  17. #import "ValidateUtil.h"
  18. #import "ImageUtil.h"
  19. #import "UIImageView+WebCache.h"
  20. #import "DeviceNodePopupView.h"
  21. #import "CustomRadioGroup.h"
  22. #import "CommandClassControlNodeView.h"
  23. @implementation DeviceNodePopupTableViewCell
  24. @end
  25. @interface DeviceNodePopupView () <UITableViewDataSource, UITableViewDelegate, CustomCheckBoxDelegate, CommandClassControlNodeViewDelegate> {
  26. CustomRadioReusableGroup *_rgroup;
  27. BOOL _isNotFirstLoading, _hasSelectedNode;
  28. UIImage *_bgCellImage1, *_bgCellImage2;
  29. NSMutableArray<ItemModel> *_checkedItems;
  30. NSMutableArray *_controlViewArray;
  31. }
  32. @property (weak, nonatomic) IBOutlet NSLayoutConstraint *constTableHeight;
  33. @end
  34. @implementation DeviceNodePopupView
  35. - (id)initFromNib {
  36. for (UIView *view in [CommonUtil nibViews:@"DeviceNodePopupView"]) {
  37. if ([view isKindOfClass:[DeviceNodePopupView class]]) {
  38. self = (DeviceNodePopupView *)view;
  39. //XIB의 경우, 현재 화면 사이즈로 맞춰줘야 함.
  40. self.frame = [UIScreen mainScreen].bounds;
  41. [self initTableViewAsDefaultStyle:_tableView];
  42. UINib *nib = [UINib nibWithNibName:@"DeviceNodePopupTableViewCell" bundle:nil];
  43. [_tableView registerNib:nib forCellReuseIdentifier:@"NodeCellIdentifier"];
  44. _controlViewArray = [[NSMutableArray alloc] init];
  45. //set radio group
  46. _rgroup = [[CustomRadioReusableGroup alloc] init];
  47. _rgroup.tableView = _tableView;
  48. [self.imgTitleBar setImage:[UIImage imageNamed:@"img_popup_bg_head"]inset:UIEdgeInsetsMake(kfTopInset,kfRightInset,kfBottomInset,kfLeftInset)];
  49. [self.btnConfirm setBackgroundImage:[UIImage imageNamed:@"img_popup_btn_right"] forState:UIControlStateNormal capInsets:UIEdgeInsetsMake(kfTopInset,kfRightInset,kfBottomInset,kfLeftInset)];
  50. [self.btnConfirm setBackgroundImage:[UIImage imageNamed:@"img_popup_btn_right_press"] forState:UIControlStateHighlighted capInsets:UIEdgeInsetsMake(kfTopInset,kfRightInset,kfBottomInset,kfLeftInset)];
  51. [self.btnCancel setBackgroundImage:[UIImage imageNamed:@"img_popup_btn_left"] forState:UIControlStateNormal capInsets:UIEdgeInsetsMake(kfTopInset,kfRightInset,kfBottomInset,kfLeftInset)];
  52. [self.btnCancel setBackgroundImage:[UIImage imageNamed:@"img_popup_btn_left_press"] forState:UIControlStateHighlighted capInsets:UIEdgeInsetsMake(kfTopInset,kfRightInset,kfBottomInset,kfLeftInset)];
  53. //Localization
  54. [self.btnConfirm setTitle:NSLocalizedString(@"확인", @"확인") forState:UIControlStateNormal];
  55. [self.btnCancel setTitle:NSLocalizedString(@"취소", @"취소") forState:UIControlStateNormal];
  56. UIEdgeInsets insets = UIEdgeInsetsMake(1, 1, 1, 2);
  57. _bgCellImage1 = [ImageUtil resizableImageWithCapInsets:insets resizingMode:UIImageResizingModeStretch img:[UIImage imageNamed:@"img_list_bg_01"]];
  58. _bgCellImage2 = [ImageUtil resizableImageWithCapInsets:insets resizingMode:UIImageResizingModeStretch img:[UIImage imageNamed:@"img_list_bg_02"]];
  59. }
  60. }
  61. return self;
  62. }
  63. - (void)initTableViewAsDefaultStyle:(CustomTableView *)tableView {
  64. tableView.dataSource = self;
  65. tableView.delegate = self;
  66. tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  67. tableView.backgroundColor = [UIColor clearColor];
  68. tableView.tableFooterView = [[UIView alloc] init]; //this call table events;
  69. }
  70. - (void)didMoveToSuperview {
  71. if (_refDevice.subItems.count == 1) {
  72. _constTableHeight.constant = 130.f;
  73. }
  74. }
  75. - (void)setRefDevice:(ItemModel *)refDevice {
  76. _refDevice = refDevice;
  77. //NSLog(@"refDevice : %@", refDevice);
  78. self.lblTitle.text = _refDevice.sourceName;
  79. // [self.imgvDevice sd_setImageWithURL:[NSURL URLWithString:_refDevice.imageFileName] placeholderImage:nil options:SDWebImageRefreshCached];
  80. _hasSelectedNode = [_refDevice.subItems matchedObjectName:ksCustomRadioButtonStatus condition:YES] != nil; //이미 선택된 노드가 있는 지 확인,
  81. [_tableView reloadData];
  82. }
  83. - (NSArray<ItemModel> *)selectedItems {
  84. NSArray<ItemModel> *items = nil;
  85. if ([_typeCode isEqualToString:ksItemTypeCodeAction]) {
  86. items = _checkedItems;
  87. } else {
  88. items = (NSArray<ItemModel> *)@[_rgroup.valueForChecked];
  89. }
  90. return items;
  91. }
  92. - (ItemSubModel *)selectedNode {
  93. return _rgroup.valueForChecked;
  94. }
  95. #pragma mark - Main Logic
  96. #pragma mark - UITableView DataSource & Delegate
  97. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  98. return 1;
  99. }
  100. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  101. return _refDevice.subItems ? _refDevice.subItems.count : 0;
  102. }
  103. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  104. CGFloat height = 130;
  105. return height;
  106. }
  107. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  108. ItemSubModel *subItem = _refDevice.subItems[indexPath.row];
  109. DeviceNodePopupTableViewCell *tcell = (DeviceNodePopupTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"NodeCellIdentifier"];
  110. tcell.lblNodeName.text = subItem.sourceSubName;
  111. //뷰를 초기화함.
  112. [[tcell.controlContainer subviews] enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
  113. UIView *subview = (UIView *)obj;
  114. [subview removeFromSuperview];
  115. }];
  116. if ([_typeCode isEqualToString:ksItemTypeCodeTrigger] || [_typeCode isEqualToString:ksItemTypeCodeCondition]) {//트리거 또는 컨디션
  117. tcell.chkSelect.hidden = YES;
  118. tcell.rdoSelect.value = subItem;
  119. if (indexPath.row == 0 && !_isNotFirstLoading && !_hasSelectedNode) {
  120. _isNotFirstLoading = YES;
  121. tcell.rdoSelect.checked = YES;
  122. } else {
  123. tcell.rdoSelect.checked = [tcell.rdoSelect getRadioStatusFromValue];
  124. }
  125. //추가일 경우, 현재 노드 및 선택된 노드 제외
  126. //수정일 경우, 현재 노드와 선택되지 않은 노드를 선택 가능
  127. if (!_isModifyMode) {
  128. }
  129. // tcell.rdoSelect.enabled = _isModifyMode;
  130. [_rgroup addRadioButton:tcell.rdoSelect];
  131. } else if ([_typeCode isEqualToString:ksItemTypeCodeAction]) {//액션
  132. tcell.rdoSelect.hidden = YES;
  133. tcell.chkSelect.delegate = self;
  134. tcell.chkSelect.value = subItem;
  135. tcell.chkSelect.checked = [tcell.chkSelect getCheckStatusFromValue];
  136. }
  137. CommandClassControlNodeView *controlView = _controlViewArray.count > indexPath.row ? _controlViewArray[indexPath.row] : nil;
  138. //가스락인데 실행조건>장치상태 일때 온오프 할 수 있을 수 있다. 동작할 장치에서는 불가능
  139. NSInteger type = subItem.cmdclsType;
  140. if (type == 17 && subItem.cmdclsValueList.count == 2) {
  141. type = 1; //토글
  142. }
  143. if (!controlView) {
  144. controlView = [CommandClassControlNodeView viewForCommandClass:type];
  145. controlView.delegate = self;
  146. [_controlViewArray addObject:controlView];
  147. }
  148. controlView.nodeEnable = tcell.rdoSelect.checked;
  149. controlView.subItem = subItem;
  150. tcell.controlContainer.hidden = !controlView;
  151. if (!tcell.controlContainer.hidden) {
  152. UIView *superview = tcell.controlContainer;
  153. [superview addSubview:controlView];
  154. [controlView fillToSuperView];
  155. // [controlView mas_makeConstraints:^(MASConstraintMaker *make) {
  156. //
  157. // make.size.mas_equalTo(controlView.frame.size);
  158. // make.left.equalTo(superview);
  159. // make.top.equalTo(superview);
  160. // }];
  161. }
  162. //set background image
  163. if (indexPath.row % 2 == 0) {
  164. tcell.backgroundView = [[UIImageView alloc] initWithImage:_bgCellImage1];
  165. } else {
  166. tcell.backgroundView = [[UIImageView alloc] initWithImage:_bgCellImage2];
  167. }
  168. return tcell;
  169. }
  170. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  171. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  172. DeviceNodePopupTableViewCell *tcell = (DeviceNodePopupTableViewCell *)[self tableView:tableView cellForRowAtIndexPath:indexPath];
  173. NSLog(@"didSelectRowAtIndexPath");
  174. if ([_typeCode isEqualToString:ksItemTypeCodeTrigger] || [_typeCode isEqualToString:ksItemTypeCodeCondition]) {
  175. [_rgroup someRadioButtonTouched:tcell.rdoSelect];
  176. } else if ([_typeCode isEqualToString:ksItemTypeCodeAction]) {
  177. [tcell.chkSelect checkBoxClicked];
  178. }
  179. [_tableView reloadData];
  180. }
  181. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
  182. // Remove seperator inset
  183. if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
  184. [cell setSeparatorInset:UIEdgeInsetsZero];
  185. }
  186. // Prevent the cell from inheriting the Table View's margin settings
  187. if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
  188. [cell setPreservesSuperviewLayoutMargins:NO];
  189. }
  190. // Explictly set your cell's layout margins
  191. if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
  192. [cell setLayoutMargins:UIEdgeInsetsZero];
  193. }
  194. }
  195. #pragma mark - CustomCheckBox Delegate
  196. - (void)didCheckBoxClicked:(id)sender {
  197. [_tableView reloadData];
  198. }
  199. #pragma mark - CommandClassControlNodeView Delegate
  200. - (void)didControlNodeAction:(id)sender {
  201. NSLog(@"didControlNodeAction");
  202. CommandClassControlNodeView *nodeView = (CommandClassControlNodeView *)sender;
  203. [_tableView enumarateTableViewCellsUsingBlock:^(UITableViewCell *cell, BOOL *stop) {
  204. DeviceNodePopupTableViewCell *tcell = (DeviceNodePopupTableViewCell *)cell;
  205. if ([tcell.controlContainer isEqual:nodeView.superview]) {
  206. [_rgroup someRadioButtonTouched:tcell.rdoSelect];
  207. *stop = YES;
  208. }
  209. }];
  210. }
  211. #pragma mark - UI Events
  212. - (IBAction)btnConfirmTouched:(id)sender {
  213. //validate
  214. if ([_typeCode isEqualToString:ksItemTypeCodeAction]) {//디바이스 멀티
  215. if ([_typeCode isEqualToString:ksItemTypeCodeAction]) {//디바이스 멀티 BOOL isSelected = NO;
  216. _checkedItems = (NSMutableArray<ItemModel> *)[_refDevice.subItems matchedArrayByObjectName:ksCustomCheckBoxStatus condition:YES];
  217. if (!_checkedItems || !_checkedItems.count) {
  218. [[JDFacade facade] alert:NSLocalizedString(@"선택된 항목이 없습니다", @"선택된 항목이 없습니다")];
  219. return;
  220. }
  221. }
  222. }
  223. else {//동일한 노드의 액션이 이미 선택되었는지 체크,
  224. ItemSubModel *subItem = _rgroup.valueForChecked;
  225. // CmdClsValueModel *cmdclsValue = nil;
  226. for (CmdClsValueModel *cmdclsValue in subItem.cmdclsValueList) {
  227. if (!_passAlreadyAdded && !_isModifyMode && cmdclsValue.isSelected) {
  228. [[JDFacade facade] toast:@"이미 추가되어 있습니다"];
  229. return;
  230. }
  231. cmdclsValue.isSelected = [[[JDFacade facade] getRadioButtonStatus:cmdclsValue] boolValue];
  232. if (cmdclsValue.isSelected) {
  233. subItem.cmdclsValue = cmdclsValue.cmdclsValue;
  234. subItem.cmdclsValueMsg = cmdclsValue.cmdclsValueMsg;
  235. }
  236. }
  237. }
  238. [super btnConfirmTouched:sender];
  239. }
  240. @end