DeviceNodePopupView.m 12 KB

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