DeviceSelectPopupView.m 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. //
  2. // DeviceSelectPopupView.m
  3. // kneet2
  4. //
  5. // Created by Jason Lee on 11/3/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 "CustomRadioGroup.h"
  18. #import "ValidateUtil.h"
  19. #import "ImageUtil.h"
  20. #import "UIImageView+WebCache.h"
  21. #import "DeviceSelectPopupView.h"
  22. #import "DeviceNodePopupView.h"
  23. @implementation DeviceSelectPopupTableViewCell
  24. @end
  25. @interface DeviceSelectPopupView () <UITableViewDataSource, UITableViewDelegate, CustomCheckBoxDelegate> {
  26. CustomRadioReusableGroup *_rgroup;
  27. BOOL _isNotFirstLoading;
  28. UIImage *_bgCellImage1, *_bgCellImage2;
  29. NSMutableArray<ItemModel> *_checkedDevices;
  30. }
  31. @end
  32. @implementation DeviceSelectPopupView
  33. - (id)initFromNib {
  34. for (UIView *view in [CommonUtil nibViews:@"DeviceSelectPopupView"]) {
  35. if ([view isKindOfClass:[DeviceSelectPopupView class]]) {
  36. self = (DeviceSelectPopupView *)view;
  37. //XIB의 경우, 현재 화면 사이즈로 맞춰줘야 함.
  38. self.frame = [UIScreen mainScreen].bounds;
  39. self.lblTitle.text = NSLocalizedString(@"장치 선택", @"장치 선택");
  40. [self initTableViewAsDefaultStyle:_tableView];
  41. UINib *nib = [UINib nibWithNibName:@"DeviceSelectPopupTableViewCell" bundle:nil];
  42. [_tableView registerNib:nib forCellReuseIdentifier:@"DeviceCellIdentifier"];
  43. //set radio group
  44. _rgroup = [[CustomRadioReusableGroup alloc] init];
  45. _rgroup.tableView = _tableView;
  46. [self.imgTitleBar setImage:[UIImage imageNamed:@"img_popup_bg_head"]inset:UIEdgeInsetsMake(kfTopInset,kfRightInset,kfBottomInset,kfLeftInset)];
  47. [self.btnConfirm setBackgroundImage:[UIImage imageNamed:@"img_popup_btn_right"] forState:UIControlStateNormal capInsets:UIEdgeInsetsMake(kfTopInset,kfRightInset,kfBottomInset,kfLeftInset)];
  48. [self.btnConfirm setBackgroundImage:[UIImage imageNamed:@"img_popup_btn_right_press"] forState:UIControlStateHighlighted capInsets:UIEdgeInsetsMake(kfTopInset,kfRightInset,kfBottomInset,kfLeftInset)];
  49. [self.btnCancel setBackgroundImage:[UIImage imageNamed:@"img_popup_btn_left"] forState:UIControlStateNormal capInsets:UIEdgeInsetsMake(kfTopInset,kfRightInset,kfBottomInset,kfLeftInset)];
  50. [self.btnCancel setBackgroundImage:[UIImage imageNamed:@"img_popup_btn_left_press"] forState:UIControlStateHighlighted capInsets:UIEdgeInsetsMake(kfTopInset,kfRightInset,kfBottomInset,kfLeftInset)];
  51. //Localization
  52. [self.btnConfirm setTitle:NSLocalizedString(@"확인", @"확인") forState:UIControlStateNormal];
  53. [self.btnCancel setTitle:NSLocalizedString(@"취소", @"취소") forState:UIControlStateNormal];
  54. UIEdgeInsets insets = UIEdgeInsetsMake(1, 1, 1, 2);
  55. _bgCellImage1 = [ImageUtil resizableImageWithCapInsets:insets resizingMode:UIImageResizingModeStretch img:[UIImage imageNamed:@"img_list_bg_01"]];
  56. _bgCellImage2 = [ImageUtil resizableImageWithCapInsets:insets resizingMode:UIImageResizingModeStretch img:[UIImage imageNamed:@"img_list_bg_02"]];
  57. }
  58. }
  59. return self;
  60. }
  61. - (id)initFromNibWithRefDevices:(NSMutableArray<ItemModel> *)refDevices
  62. typeCode:(NSString*)typeCode {
  63. id popup = [self initFromNib];
  64. _typeCode = typeCode;
  65. [self setRefDevices:refDevices];
  66. return popup;
  67. }
  68. - (void)initTableViewAsDefaultStyle:(CustomTableView *)tableView {
  69. tableView.dataSource = self;
  70. tableView.delegate = self;
  71. tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  72. tableView.backgroundColor = [UIColor clearColor];
  73. tableView.tableFooterView = [[UIView alloc] init]; //this call table events;
  74. }
  75. - (void)didMoveToSuperview {
  76. NSLog(@"didMoveToSuperview");
  77. NSLog(@"didMoveToSuperview");
  78. NSLog(@"didMoveToSuperview");
  79. NSLog(@"didMoveToSuperview");
  80. NSLog(@"didMoveToSuperview");
  81. }
  82. - (void)setRefDevices:(NSMutableArray<ItemModel> *)refDevices {
  83. _refDevices = refDevices;
  84. for (ItemSubModel *item in _refDevices) {
  85. [[JDFacade facade] setRadioButtonStatus:@NO object:item];
  86. }
  87. [_tableView reloadData];
  88. }
  89. - (NSArray<ItemModel> *)selectedDevices {
  90. NSArray<ItemModel> *devices = nil;
  91. if ([_typeCode isEqualToString:ksItemTypeCodeAction]) {
  92. devices = _checkedDevices;
  93. } else if(_rgroup.valueForChecked != nil) {
  94. devices = (NSArray<ItemModel> *)@[_rgroup.valueForChecked];
  95. }
  96. return devices;
  97. }
  98. #pragma mark - UITableView DataSource & Delegate
  99. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  100. return 1;
  101. }
  102. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  103. return _refDevices ? _refDevices.count : 0;
  104. }
  105. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  106. CGFloat height = 86.0f;
  107. return height;
  108. }
  109. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  110. ItemModel *item = _refDevices[indexPath.row];
  111. UIImageView *selectedBg = [[UIImageView alloc] initWithImage:_bgCellImage1];
  112. UIImageView *defaultBg = [[UIImageView alloc] initWithImage:_bgCellImage2];
  113. DeviceSelectPopupTableViewCell *tcell = (DeviceSelectPopupTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"DeviceCellIdentifier"];
  114. tcell.lblDeviceName.text = item.sourceName;
  115. [tcell.imgvDevice sd_setImageWithURL:[NSURL URLWithString:item.imageFileName] placeholderImage:nil options:SDWebImageRefreshCached];
  116. if ([_typeCode isEqualToString:ksItemTypeCodeTrigger] || [_typeCode isEqualToString:ksItemTypeCodeCondition]) {//트리거 또는 컨디션
  117. tcell.chkSelect.hidden = YES;
  118. tcell.rdoSelect.value = item;
  119. if (indexPath.row == 0 && !_isNotFirstLoading) {
  120. _isNotFirstLoading = YES;
  121. tcell.rdoSelect.checked = YES;
  122. } else {
  123. tcell.rdoSelect.checked = [tcell.rdoSelect getRadioStatusFromValue];
  124. }
  125. [_rgroup addRadioButton:tcell.rdoSelect];
  126. tcell.backgroundView = tcell.rdoSelect.checked ? selectedBg : defaultBg;
  127. } else if ([_typeCode isEqualToString:ksItemTypeCodeAction]) {//액션
  128. tcell.rdoSelect.hidden = YES;
  129. tcell.chkSelect.delegate = self;
  130. tcell.chkSelect.value = item;
  131. tcell.chkSelect.checked = [tcell.chkSelect getCheckStatusFromValue];
  132. tcell.backgroundView = indexPath.row % 2 == 0 ? selectedBg : defaultBg;
  133. }
  134. //set background image
  135. // if (indexPath.row % 2 == 0) {
  136. // tcell.backgroundView = [[UIImageView alloc] initWithImage:_bgCellImage1];
  137. // } else {
  138. // tcell.backgroundView = [[UIImageView alloc] initWithImage:_bgCellImage2];
  139. // }
  140. return tcell;
  141. }
  142. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  143. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  144. DeviceSelectPopupTableViewCell *tcell = (DeviceSelectPopupTableViewCell *)[self tableView:tableView cellForRowAtIndexPath:indexPath];
  145. if ([_typeCode isEqualToString:ksItemTypeCodeTrigger] || [_typeCode isEqualToString:ksItemTypeCodeCondition]) {
  146. [_rgroup someRadioButtonTouched:tcell.rdoSelect];
  147. } else if ([_typeCode isEqualToString:ksItemTypeCodeAction]) {
  148. [tcell.chkSelect checkBoxClicked];
  149. }
  150. [_tableView reloadData];
  151. }
  152. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
  153. // Remove seperator inset
  154. if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
  155. [cell setSeparatorInset:UIEdgeInsetsZero];
  156. }
  157. // Prevent the cell from inheriting the Table View's margin settings
  158. if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
  159. [cell setPreservesSuperviewLayoutMargins:NO];
  160. }
  161. // Explictly set your cell's layout margins
  162. if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
  163. [cell setLayoutMargins:UIEdgeInsetsZero];
  164. }
  165. }
  166. #pragma mark - CustomCheckBox Delegate
  167. - (void)didCheckBoxClicked:(id)sender {
  168. }
  169. #pragma mark - UI Events
  170. - (IBAction)btnConfirm:(id)sender {
  171. //validate
  172. if ([self selectedDevices] == nil) {
  173. return;
  174. }
  175. if ([_typeCode isEqualToString:ksItemTypeCodeAction]) {//디바이스 멀티 BOOL isSelected = NO;
  176. _checkedDevices = (NSMutableArray<ItemModel> *)[_refDevices matchedArrayByObjectName:ksCustomCheckBoxStatus condition:YES];
  177. if (!_checkedDevices || !_checkedDevices.count) {
  178. [[JDFacade facade] alert:NSLocalizedString(@"선택된 항목이 없습니다", @"선택된 항목이 없습니다")];
  179. return;
  180. }
  181. }
  182. [super btnConfirmTouched:sender];
  183. }
  184. @end