DeviceNodePopupView.m 10 KB

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