DeviceNodePopupView.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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. self.lblTitle.text = _refDevice.sourceName;
  74. // [self.imgvDevice sd_setImageWithURL:[NSURL URLWithString:_refDevice.imageFileName] placeholderImage:nil options:SDWebImageRefreshCached];
  75. _hasSelectedNode = [_refDevice.subItems matchedObjectName:ksCustomRadioButtonStatus condition:YES] != nil; //이미 선택된 노드가 있는 지 확인,
  76. [_tableView reloadData];
  77. }
  78. - (NSArray<ItemModel> *)selectedItems {
  79. NSArray<ItemModel> *items = nil;
  80. if ([_typeCode isEqualToString:ksItemTypeCodeAction]) {
  81. items = _checkedItems;
  82. } else {
  83. items = (NSArray<ItemModel> *)@[_rgroup.valueForChecked];
  84. }
  85. return items;
  86. }
  87. - (ItemSubModel *)selectedNode {
  88. return _rgroup.valueForChecked;
  89. }
  90. #pragma mark - Main Logic
  91. #pragma mark - UITableView DataSource & Delegate
  92. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  93. return 1;
  94. }
  95. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  96. return _refDevice.subItems ? _refDevice.subItems.count : 0;
  97. }
  98. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  99. CGFloat height = 130;
  100. return height;
  101. }
  102. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  103. ItemSubModel *subItem = _refDevice.subItems[indexPath.row];
  104. DeviceNodePopupTableViewCell *tcell = (DeviceNodePopupTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"NodeCellIdentifier"];
  105. tcell.lblNodeName.text = subItem.sourceSubName;
  106. //1.set node
  107. //뷰를 초기화함.
  108. [[tcell.controlContainer subviews] enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
  109. UIView *subview = (UIView *)obj;
  110. [subview removeFromSuperview];
  111. }];
  112. if ([_typeCode isEqualToString:ksItemTypeCodeTrigger] || [_typeCode isEqualToString:ksItemTypeCodeCondition]) {//트리거 또는 컨디션
  113. tcell.chkSelect.hidden = YES;
  114. tcell.rdoSelect.value = subItem;
  115. if (indexPath.row == 0 && !_isNotFirstLoading && !_hasSelectedNode) {
  116. _isNotFirstLoading = YES;
  117. tcell.rdoSelect.checked = YES;
  118. } else {
  119. tcell.rdoSelect.checked = [tcell.rdoSelect getRadioStatusFromValue];
  120. }
  121. //추가일 경우, 현재 노드 및 선택된 노드 제외
  122. //수정일 경우, 현재 노드와 선택되지 않은 노드를 선택 가능
  123. if (!_isModifyMode) {
  124. }
  125. // tcell.rdoSelect.enabled = _isModifyMode;
  126. [_rgroup addRadioButton:tcell.rdoSelect];
  127. } else if ([_typeCode isEqualToString:ksItemTypeCodeAction]) {//액션
  128. tcell.rdoSelect.hidden = YES;
  129. tcell.chkSelect.delegate = self;
  130. tcell.chkSelect.value = subItem;
  131. tcell.chkSelect.checked = [tcell.chkSelect getCheckStatusFromValue];
  132. }
  133. CommandClassControlNodeView *controlView = _controlViewArray.count > indexPath.row ? _controlViewArray[indexPath.row] : nil;
  134. if (!controlView) {
  135. controlView = [CommandClassControlNodeView viewForCommandClass:subItem.cmdclsType];
  136. controlView.delegate = self;
  137. [_controlViewArray addObject:controlView];
  138. }
  139. controlView.nodeEnable = tcell.rdoSelect.checked;
  140. controlView.subItem = subItem;
  141. tcell.controlContainer.hidden = !controlView;
  142. if (!tcell.controlContainer.hidden) {
  143. UIView *superview = tcell.controlContainer;
  144. [superview addSubview:controlView];
  145. [controlView mas_makeConstraints:^(MASConstraintMaker *make) {
  146. make.size.mas_equalTo(controlView.frame.size);
  147. make.left.equalTo(superview);
  148. make.top.equalTo(superview);
  149. }];
  150. }
  151. //set background image
  152. if (indexPath.row % 2 == 0) {
  153. tcell.backgroundView = [[UIImageView alloc] initWithImage:_bgCellImage1];
  154. } else {
  155. tcell.backgroundView = [[UIImageView alloc] initWithImage:_bgCellImage2];
  156. }
  157. return tcell;
  158. }
  159. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  160. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  161. DeviceNodePopupTableViewCell *tcell = (DeviceNodePopupTableViewCell *)[self tableView:tableView cellForRowAtIndexPath:indexPath];
  162. if ([_typeCode isEqualToString:ksItemTypeCodeTrigger] || [_typeCode isEqualToString:ksItemTypeCodeCondition]) {
  163. [_rgroup someRadioButtonTouched:tcell.rdoSelect];
  164. } else if ([_typeCode isEqualToString:ksItemTypeCodeAction]) {
  165. [tcell.chkSelect checkBoxClicked];
  166. }
  167. [_tableView reloadData];
  168. }
  169. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
  170. // Remove seperator inset
  171. if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
  172. [cell setSeparatorInset:UIEdgeInsetsZero];
  173. }
  174. // Prevent the cell from inheriting the Table View's margin settings
  175. if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
  176. [cell setPreservesSuperviewLayoutMargins:NO];
  177. }
  178. // Explictly set your cell's layout margins
  179. if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
  180. [cell setLayoutMargins:UIEdgeInsetsZero];
  181. }
  182. }
  183. #pragma mark - CustomCheckBox Delegate
  184. - (void)didCheckBoxClicked:(id)sender {
  185. [_tableView reloadData];
  186. }
  187. #pragma mark - CommandClassControlNodeView Delegate
  188. - (void)didControlNodeAction:(id)sender {
  189. CommandClassControlNodeView *nodeView = (CommandClassControlNodeView *)sender;
  190. [_tableView enumarateTableViewCellsUsingBlock:^(UITableViewCell *cell, BOOL *stop) {
  191. DeviceNodePopupTableViewCell *tcell = (DeviceNodePopupTableViewCell *)cell;
  192. if ([tcell.controlContainer isEqual:nodeView.superview]) {
  193. [_rgroup someRadioButtonTouched:tcell.rdoSelect];
  194. *stop = YES;
  195. }
  196. }];
  197. }
  198. #pragma mark - UI Events
  199. - (IBAction)btnConfirmTouched:(id)sender {
  200. //validate
  201. if ([_typeCode isEqualToString:ksItemTypeCodeAction]) {//디바이스 멀티
  202. if ([_typeCode isEqualToString:ksItemTypeCodeAction]) {//디바이스 멀티 BOOL isSelected = NO;
  203. _checkedItems = (NSMutableArray<ItemModel> *)[_refDevice.subItems matchedArrayByObjectName:ksCustomCheckBoxStatus condition:YES];
  204. if (!_checkedItems || !_checkedItems.count) {
  205. [[JDFacade facade] alert:NSLocalizedString(@"선택된 항목이 없습니다", @"선택된 항목이 없습니다")];
  206. return;
  207. }
  208. }
  209. } else {//동일한 노드의 액션이 이미 선택되었는지 체크,
  210. ItemSubModel *subItem = _rgroup.valueForChecked;
  211. CmdClsValueModel *cmdclsValue = nil;
  212. for (cmdclsValue in subItem.cmdclsValueList) {
  213. if (!_isModifyMode && cmdclsValue.isSelected) {
  214. [[JDFacade facade] toast:@"이미 추가되어 있습니다"];
  215. return;
  216. }
  217. cmdclsValue.isSelected = [[[JDFacade facade] getRadioButtonStatus:cmdclsValue] boolValue];
  218. if (cmdclsValue.isSelected) {
  219. subItem.cmdclsValue = cmdclsValue.cmdclsValue;
  220. subItem.cmdclsValueMsg = cmdclsValue.cmdclsValueMsg;
  221. }
  222. }
  223. }
  224. [super btnConfirmTouched:sender];
  225. }
  226. @end