DeviceNodePopupView.m 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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> {
  26. CustomRadioReusableGroup *_rgroup;
  27. BOOL _isNotFirstLoading;
  28. UIImage *_bgCellImage1, *_bgCellImage2;
  29. NSMutableArray<ItemModel> *_checkedItems;
  30. }
  31. @end
  32. @implementation DeviceNodePopupView
  33. - (id)initFromNib {
  34. for (UIView *view in [CommonUtil nibViews:@"DeviceNodePopupView"]) {
  35. if ([view isKindOfClass:[DeviceNodePopupView class]]) {
  36. self = (DeviceNodePopupView *)view;
  37. //XIB의 경우, 현재 화면 사이즈로 맞춰줘야 함.
  38. self.frame = [UIScreen mainScreen].bounds;
  39. [self initTableViewAsDefaultStyle:_tableView];
  40. UINib *nib = [UINib nibWithNibName:@"DeviceNodePopupTableViewCell" bundle:nil];
  41. [_tableView registerNib:nib forCellReuseIdentifier:@"NodeCellIdentifier"];
  42. //set radio group
  43. _rgroup = [[CustomRadioReusableGroup alloc] init];
  44. _rgroup.tableView = _tableView;
  45. //Localization
  46. [self.btnConfirm setTitle:NSLocalizedString(@"확인", @"확인") forState:UIControlStateNormal];
  47. [self.btnCancel setTitle:NSLocalizedString(@"취소", @"취소") forState:UIControlStateNormal];
  48. UIEdgeInsets insets = UIEdgeInsetsMake(4, 4, 4, 4);
  49. _bgCellImage1 = [ImageUtil resizableImageWithCapInsets:insets resizingMode:UIImageResizingModeStretch img:[UIImage imageNamed:@"tp_01_img_list_bg_01"]];
  50. _bgCellImage2 = [ImageUtil resizableImageWithCapInsets:insets resizingMode:UIImageResizingModeStretch img:[UIImage imageNamed:@"tp_01_img_list_bg_02"]];
  51. }
  52. }
  53. return self;
  54. }
  55. - (void)initTableViewAsDefaultStyle:(CustomTableView *)tableView {
  56. tableView.dataSource = self;
  57. tableView.delegate = self;
  58. tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  59. tableView.backgroundColor = [UIColor clearColor];
  60. tableView.tableFooterView = [[UIView alloc] init]; //this call table events;
  61. }
  62. - (void)didMoveToSuperview {
  63. }
  64. - (void)setRefDevice:(ItemModel *)refDevice {
  65. _refDevice = refDevice;
  66. self.lblTitle.text = _refDevice.sourceName;
  67. [self.imgvDevice sd_setImageWithURL:[NSURL URLWithString:_refDevice.imageFileName] placeholderImage:nil options:SDWebImageRefreshCached];
  68. [_tableView reloadData];
  69. }
  70. - (NSArray<ItemModel> *)selectedItems {
  71. NSArray<ItemModel> *items = nil;
  72. if ([_typeCode isEqualToString:ksItemTypeCodeAction]) {
  73. items = _checkedItems;
  74. } else {
  75. items = (NSArray<ItemModel> *)@[_rgroup.valueForChecked];
  76. }
  77. return items;
  78. }
  79. - (ItemSubModel *)selectedNode {
  80. return _rgroup.valueForChecked;
  81. }
  82. #pragma mark - Main Logic
  83. #pragma mark - UITableView DataSource & Delegate
  84. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  85. return 1;
  86. }
  87. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  88. return _refDevice.subItems ? _refDevice.subItems.count : 0;
  89. }
  90. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  91. CGFloat height = 110.0f;
  92. return height;
  93. }
  94. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  95. ItemSubModel *subItem = _refDevice.subItems[indexPath.row];
  96. DeviceNodePopupTableViewCell *tcell = (DeviceNodePopupTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"NodeCellIdentifier"];
  97. tcell.lblNodeName.text = subItem.sourceSubName;
  98. //1.set node
  99. //뷰를 초기화함.
  100. [[tcell.controlContainer subviews] enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
  101. UIView *subview = (UIView *)obj;
  102. [subview removeFromSuperview];
  103. }];
  104. CommandClassControlNodeView *controlView = [CommandClassControlNodeView viewForCommandClass:subItem.cmdclsType];
  105. controlView.subItem = subItem;
  106. tcell.controlContainer.hidden = !controlView;
  107. if (!tcell.controlContainer.hidden) {
  108. UIView *superview = tcell.controlContainer;
  109. [superview addSubview:controlView];
  110. [controlView mas_makeConstraints:^(MASConstraintMaker *make) {
  111. make.size.mas_equalTo(controlView.frame.size);
  112. make.center.equalTo(superview);
  113. }];
  114. }
  115. if ([_typeCode isEqualToString:ksItemTypeCodeTrigger] || [_typeCode isEqualToString:ksItemTypeCodeCondition]) {//트리거 또는 컨디션
  116. tcell.chkSelect.hidden = YES;
  117. tcell.rdoSelect.value = subItem;
  118. if (indexPath.row == 0 && !_isNotFirstLoading) {
  119. _isNotFirstLoading = YES;
  120. tcell.rdoSelect.checked = YES;
  121. } else {
  122. tcell.rdoSelect.checked = [tcell.rdoSelect getRadioStatusFromValue];
  123. }
  124. [_rgroup addRadioButton:tcell.rdoSelect];
  125. } else if ([_typeCode isEqualToString:ksItemTypeCodeAction]) {//액션
  126. tcell.rdoSelect.hidden = YES;
  127. tcell.chkSelect.delegate = self;
  128. tcell.chkSelect.value = subItem;
  129. tcell.chkSelect.checked = [tcell.chkSelect getCheckStatusFromValue];
  130. }
  131. return tcell;
  132. }
  133. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  134. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  135. DeviceNodePopupTableViewCell *tcell = (DeviceNodePopupTableViewCell *)[self tableView:tableView cellForRowAtIndexPath:indexPath];
  136. if ([_typeCode isEqualToString:ksItemTypeCodeTrigger] || [_typeCode isEqualToString:ksItemTypeCodeCondition]) {
  137. [_rgroup someRadioButtonTouched:tcell.rdoSelect];
  138. } else if ([_typeCode isEqualToString:ksItemTypeCodeAction]) {
  139. [tcell.chkSelect checkBoxClicked];
  140. }
  141. [_tableView reloadData];
  142. }
  143. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
  144. // Remove seperator inset
  145. if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
  146. [cell setSeparatorInset:UIEdgeInsetsZero];
  147. }
  148. // Prevent the cell from inheriting the Table View's margin settings
  149. if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
  150. [cell setPreservesSuperviewLayoutMargins:NO];
  151. }
  152. // Explictly set your cell's layout margins
  153. if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
  154. [cell setLayoutMargins:UIEdgeInsetsZero];
  155. }
  156. }
  157. #pragma mark - CustomCheckBox Delegate
  158. - (void)didCheckBoxClicked:(id)sender {
  159. [_tableView reloadData];
  160. }
  161. #pragma mark - UI Events
  162. - (IBAction)btnConfirmTouched:(id)sender {
  163. //validate
  164. if ([_typeCode isEqualToString:ksItemTypeCodeAction]) {//디바이스 멀티
  165. if ([_typeCode isEqualToString:ksItemTypeCodeAction]) {//디바이스 멀티 BOOL isSelected = NO;
  166. _checkedItems = (NSMutableArray<ItemModel> *)[_refDevice.subItems matchedArrayByObjectName:ksCustomCheckBoxStatus condition:YES];
  167. if (!_checkedItems || !_checkedItems.count) {
  168. [[JDFacade facade] alert:NSLocalizedString(@"선택된 항목이 없습니다", @"선택된 항목이 없습니다")];
  169. return;
  170. }
  171. }
  172. } else {//동일한 노드의 액션이 이미 선택되었는지 체크,
  173. ItemSubModel *subItem = _rgroup.valueForChecked;
  174. CmdClsValueModel *cmdclsValue = nil;
  175. for (cmdclsValue in subItem.cmdclsValueList) {
  176. if (!_isModifyMode && cmdclsValue.isSelected) {
  177. [[JDFacade facade] toast:@"이미 추가되어 있습니다"];
  178. return;
  179. }
  180. cmdclsValue.isSelected = [[JDFacade facade] getRadioButtonStatus:cmdclsValue];
  181. }
  182. }
  183. [super btnConfirmTouched:sender];
  184. }
  185. @end