DeviceNodePopupView.m 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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(4, 4, 4, 4);
  51. _bgCellImage1 = [ImageUtil resizableImageWithCapInsets:insets resizingMode:UIImageResizingModeStretch img:[UIImage imageNamed:@"tp_01_img_list_bg_01"]];
  52. _bgCellImage2 = [ImageUtil resizableImageWithCapInsets:insets resizingMode:UIImageResizingModeStretch img:[UIImage imageNamed:@"tp_01_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 = 110.0f;
  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. CommandClassControlNodeView *controlView = _controlViewArray.count > indexPath.row ? _controlViewArray[indexPath.row] : nil;
  108. if (!controlView) {
  109. controlView = [CommandClassControlNodeView viewForCommandClass:subItem.cmdclsType];
  110. controlView.delegate = self;
  111. [_controlViewArray addObject:controlView];
  112. }
  113. controlView.subItem = subItem;
  114. tcell.controlContainer.hidden = !controlView;
  115. if (!tcell.controlContainer.hidden) {
  116. UIView *superview = tcell.controlContainer;
  117. [superview addSubview:controlView];
  118. [controlView mas_makeConstraints:^(MASConstraintMaker *make) {
  119. make.size.mas_equalTo(controlView.frame.size);
  120. make.center.equalTo(superview);
  121. }];
  122. }
  123. if ([_typeCode isEqualToString:ksItemTypeCodeTrigger] || [_typeCode isEqualToString:ksItemTypeCodeCondition]) {//트리거 또는 컨디션
  124. tcell.chkSelect.hidden = YES;
  125. tcell.rdoSelect.value = subItem;
  126. if (indexPath.row == 0 && !_isNotFirstLoading && !_hasSelectedNode) {
  127. _isNotFirstLoading = YES;
  128. tcell.rdoSelect.checked = YES;
  129. } else {
  130. tcell.rdoSelect.checked = [tcell.rdoSelect getRadioStatusFromValue];
  131. }
  132. //추가일 경우, 현재 노드 및 선택된 노드 제외
  133. //수정일 경우, 현재 노드와 선택되지 않은 노드를 선택 가능
  134. if (!_isModifyMode) {
  135. }
  136. // tcell.rdoSelect.enabled = _isModifyMode;
  137. [_rgroup addRadioButton:tcell.rdoSelect];
  138. } else if ([_typeCode isEqualToString:ksItemTypeCodeAction]) {//액션
  139. tcell.rdoSelect.hidden = YES;
  140. tcell.chkSelect.delegate = self;
  141. tcell.chkSelect.value = subItem;
  142. tcell.chkSelect.checked = [tcell.chkSelect getCheckStatusFromValue];
  143. }
  144. return tcell;
  145. }
  146. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  147. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  148. DeviceNodePopupTableViewCell *tcell = (DeviceNodePopupTableViewCell *)[self tableView:tableView cellForRowAtIndexPath:indexPath];
  149. if ([_typeCode isEqualToString:ksItemTypeCodeTrigger] || [_typeCode isEqualToString:ksItemTypeCodeCondition]) {
  150. [_rgroup someRadioButtonTouched:tcell.rdoSelect];
  151. } else if ([_typeCode isEqualToString:ksItemTypeCodeAction]) {
  152. [tcell.chkSelect checkBoxClicked];
  153. }
  154. [_tableView reloadData];
  155. }
  156. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
  157. // Remove seperator inset
  158. if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
  159. [cell setSeparatorInset:UIEdgeInsetsZero];
  160. }
  161. // Prevent the cell from inheriting the Table View's margin settings
  162. if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
  163. [cell setPreservesSuperviewLayoutMargins:NO];
  164. }
  165. // Explictly set your cell's layout margins
  166. if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
  167. [cell setLayoutMargins:UIEdgeInsetsZero];
  168. }
  169. }
  170. #pragma mark - CustomCheckBox Delegate
  171. - (void)didCheckBoxClicked:(id)sender {
  172. [_tableView reloadData];
  173. }
  174. #pragma mark - CommandClassControlNodeView Delegate
  175. - (void)didControlNodeAction:(id)sender {
  176. CommandClassControlNodeView *nodeView = (CommandClassControlNodeView *)sender;
  177. [_tableView enumarateTableViewCellsUsingBlock:^(UITableViewCell *cell, BOOL *stop) {
  178. DeviceNodePopupTableViewCell *tcell = (DeviceNodePopupTableViewCell *)cell;
  179. if ([tcell.controlContainer isEqual:nodeView.superview]) {
  180. [_rgroup someRadioButtonTouched:tcell.rdoSelect];
  181. *stop = YES;
  182. }
  183. }];
  184. }
  185. #pragma mark - UI Events
  186. - (IBAction)btnConfirmTouched:(id)sender {
  187. //validate
  188. if ([_typeCode isEqualToString:ksItemTypeCodeAction]) {//디바이스 멀티
  189. if ([_typeCode isEqualToString:ksItemTypeCodeAction]) {//디바이스 멀티 BOOL isSelected = NO;
  190. _checkedItems = (NSMutableArray<ItemModel> *)[_refDevice.subItems matchedArrayByObjectName:ksCustomCheckBoxStatus condition:YES];
  191. if (!_checkedItems || !_checkedItems.count) {
  192. [[JDFacade facade] alert:NSLocalizedString(@"선택된 항목이 없습니다", @"선택된 항목이 없습니다")];
  193. return;
  194. }
  195. }
  196. } else {//동일한 노드의 액션이 이미 선택되었는지 체크,
  197. ItemSubModel *subItem = _rgroup.valueForChecked;
  198. CmdClsValueModel *cmdclsValue = nil;
  199. for (cmdclsValue in subItem.cmdclsValueList) {
  200. if (!_isModifyMode && cmdclsValue.isSelected) {
  201. [[JDFacade facade] toast:@"이미 추가되어 있습니다"];
  202. return;
  203. }
  204. cmdclsValue.isSelected = [[JDFacade facade] getRadioButtonStatus:cmdclsValue];
  205. }
  206. }
  207. [super btnConfirmTouched:sender];
  208. }
  209. @end