DeviceNodePopupView.m 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. #pragma mark - Main Logic
  80. #pragma mark - UITableView DataSource & Delegate
  81. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  82. return 1;
  83. }
  84. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  85. return _refDevice.subItems ? _refDevice.subItems.count : 0;
  86. }
  87. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  88. CGFloat height = 110.0f;
  89. return height;
  90. }
  91. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  92. ItemSubModel *subItem = _refDevice.subItems[indexPath.row];
  93. DeviceNodePopupTableViewCell *tcell = (DeviceNodePopupTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"NodeCellIdentifier"];
  94. tcell.lblNodeName.text = subItem.sourceSubName;
  95. //1.set node
  96. //뷰를 초기화함.
  97. [[tcell.controlContainer subviews] enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
  98. UIView *subview = (UIView *)obj;
  99. [subview removeFromSuperview];
  100. }];
  101. CommandClassControlNodeView *controlView = [CommandClassControlNodeView viewForCommandClass:subItem.cmdclsType];
  102. controlView.subItem = subItem;
  103. tcell.controlContainer.hidden = !controlView;
  104. if (!tcell.controlContainer.hidden) {
  105. UIView *superview = tcell.controlContainer;
  106. [superview addSubview:controlView];
  107. [controlView mas_makeConstraints:^(MASConstraintMaker *make) {
  108. make.size.mas_equalTo(controlView.frame.size);
  109. make.center.equalTo(superview);
  110. }];
  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) {
  116. _isNotFirstLoading = YES;
  117. tcell.rdoSelect.checked = YES;
  118. } else {
  119. tcell.rdoSelect.checked = [tcell.rdoSelect getRadioStatusFromValue];
  120. }
  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. return tcell;
  129. }
  130. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  131. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  132. DeviceNodePopupTableViewCell *tcell = (DeviceNodePopupTableViewCell *)[self tableView:tableView cellForRowAtIndexPath:indexPath];
  133. if ([_typeCode isEqualToString:ksItemTypeCodeTrigger] || [_typeCode isEqualToString:ksItemTypeCodeCondition]) {
  134. [_rgroup someRadioButtonTouched:tcell.rdoSelect];
  135. } else if ([_typeCode isEqualToString:ksItemTypeCodeAction]) {
  136. [tcell.chkSelect checkBoxClicked];
  137. }
  138. [_tableView reloadData];
  139. }
  140. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
  141. // Remove seperator inset
  142. if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
  143. [cell setSeparatorInset:UIEdgeInsetsZero];
  144. }
  145. // Prevent the cell from inheriting the Table View's margin settings
  146. if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
  147. [cell setPreservesSuperviewLayoutMargins:NO];
  148. }
  149. // Explictly set your cell's layout margins
  150. if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
  151. [cell setLayoutMargins:UIEdgeInsetsZero];
  152. }
  153. }
  154. #pragma mark - CustomCheckBox Delegate
  155. - (void)didCheckBoxClicked:(id)sender {
  156. [_tableView reloadData];
  157. }
  158. #pragma mark - UI Events
  159. - (IBAction)btnConfirmTouched:(id)sender {
  160. //validate
  161. if ([_typeCode isEqualToString:ksItemTypeCodeAction]) {//디바이스 멀티
  162. if ([_typeCode isEqualToString:ksItemTypeCodeAction]) {//디바이스 멀티 BOOL isSelected = NO;
  163. _checkedItems = (NSMutableArray<ItemModel> *)[_refDevice.subItems matchedArrayByObjectName:ksCustomCheckBoxStatus condition:YES];
  164. if (!_checkedItems || !_checkedItems.count) {
  165. [[JDFacade facade] alert:NSLocalizedString(@"선택된 항목이 없습니다", @"선택된 항목이 없습니다")];
  166. return;
  167. }
  168. }
  169. } else {//동일한 노드의 액션이 이미 선택되었는지 체크,
  170. ItemSubModel *subItem = _rgroup.valueForChecked;
  171. CmdClsValueModel *cmdclsValue = nil;
  172. for (cmdclsValue in subItem.cmdclsValueList) {
  173. if (!_isModifyMode && cmdclsValue.isSelected) {
  174. [[JDFacade facade] toast:@"이미 추가되어 있습니다"];
  175. return;
  176. }
  177. cmdclsValue.isSelected = [[JDFacade facade] getRadioButtonStatus:cmdclsValue];
  178. }
  179. }
  180. [super btnConfirmTouched:sender];
  181. }
  182. @end