DeviceNodePopupView.m 7.9 KB

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