DeviceSelectPopupView.m 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. NSLog(@"refDevices : %@", refDevices);
  67. return popup;
  68. }
  69. - (void)initTableViewAsDefaultStyle:(CustomTableView *)tableView {
  70. tableView.dataSource = self;
  71. tableView.delegate = self;
  72. tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  73. tableView.backgroundColor = [UIColor clearColor];
  74. tableView.tableFooterView = [[UIView alloc] init]; //this call table events;
  75. }
  76. - (void)didMoveToSuperview {
  77. }
  78. - (void)setRefDevices:(NSMutableArray<ItemModel> *)refDevices {
  79. _refDevices = refDevices;
  80. for (ItemSubModel *item in _refDevices) {
  81. [[JDFacade facade] setRadioButtonStatus:@NO object:item];
  82. }
  83. [_tableView reloadData];
  84. }
  85. - (NSArray<ItemModel> *)selectedDevices {
  86. NSArray<ItemModel> *devices = nil;
  87. if ([_typeCode isEqualToString:ksItemTypeCodeAction]) {
  88. devices = _checkedDevices;
  89. } else {
  90. devices = (NSArray<ItemModel> *)@[_rgroup.valueForChecked];
  91. }
  92. return devices;
  93. }
  94. #pragma mark - Main Logic
  95. #pragma mark - UITableView DataSource & Delegate
  96. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  97. return 1;
  98. }
  99. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  100. return _refDevices ? _refDevices.count : 0;
  101. }
  102. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  103. CGFloat height = 86.0f;
  104. return height;
  105. }
  106. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  107. ItemModel *item = _refDevices[indexPath.row];
  108. UIImageView *selectedBg = [[UIImageView alloc] initWithImage:_bgCellImage1];
  109. UIImageView *defaultBg = [[UIImageView alloc] initWithImage:_bgCellImage2];
  110. DeviceSelectPopupTableViewCell *tcell = (DeviceSelectPopupTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"DeviceCellIdentifier"];
  111. tcell.lblDeviceName.text = item.sourceName;
  112. [tcell.imgvDevice sd_setImageWithURL:[NSURL URLWithString:item.imageFileName] placeholderImage:nil options:SDWebImageRefreshCached];
  113. if ([_typeCode isEqualToString:ksItemTypeCodeTrigger] || [_typeCode isEqualToString:ksItemTypeCodeCondition]) {//트리거 또는 컨디션
  114. tcell.chkSelect.hidden = YES;
  115. tcell.rdoSelect.value = item;
  116. if (indexPath.row == 0 && !_isNotFirstLoading) {
  117. _isNotFirstLoading = YES;
  118. tcell.rdoSelect.checked = YES;
  119. } else {
  120. tcell.rdoSelect.checked = [tcell.rdoSelect getRadioStatusFromValue];
  121. }
  122. [_rgroup addRadioButton:tcell.rdoSelect];
  123. tcell.backgroundView = tcell.rdoSelect.checked ? selectedBg : defaultBg;
  124. } else if ([_typeCode isEqualToString:ksItemTypeCodeAction]) {//액션
  125. tcell.rdoSelect.hidden = YES;
  126. tcell.chkSelect.delegate = self;
  127. tcell.chkSelect.value = item;
  128. tcell.chkSelect.checked = [tcell.chkSelect getCheckStatusFromValue];
  129. tcell.backgroundView = indexPath.row % 2 == 0 ? selectedBg : defaultBg;
  130. }
  131. //set background image
  132. // if (indexPath.row % 2 == 0) {
  133. // tcell.backgroundView = [[UIImageView alloc] initWithImage:_bgCellImage1];
  134. // } else {
  135. // tcell.backgroundView = [[UIImageView alloc] initWithImage:_bgCellImage2];
  136. // }
  137. return tcell;
  138. }
  139. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  140. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  141. DeviceSelectPopupTableViewCell *tcell = (DeviceSelectPopupTableViewCell *)[self tableView:tableView cellForRowAtIndexPath:indexPath];
  142. if ([_typeCode isEqualToString:ksItemTypeCodeTrigger] || [_typeCode isEqualToString:ksItemTypeCodeCondition]) {
  143. [_rgroup someRadioButtonTouched:tcell.rdoSelect];
  144. } else if ([_typeCode isEqualToString:ksItemTypeCodeAction]) {
  145. [tcell.chkSelect checkBoxClicked];
  146. }
  147. [_tableView reloadData];
  148. }
  149. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
  150. // Remove seperator inset
  151. if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
  152. [cell setSeparatorInset:UIEdgeInsetsZero];
  153. }
  154. // Prevent the cell from inheriting the Table View's margin settings
  155. if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
  156. [cell setPreservesSuperviewLayoutMargins:NO];
  157. }
  158. // Explictly set your cell's layout margins
  159. if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
  160. [cell setLayoutMargins:UIEdgeInsetsZero];
  161. }
  162. }
  163. #pragma mark - CustomCheckBox Delegate
  164. - (void)didCheckBoxClicked:(id)sender {
  165. }
  166. #pragma mark - UI Events
  167. - (IBAction)btnConfirmTouched:(id)sender {
  168. //validate
  169. if ([_typeCode isEqualToString:ksItemTypeCodeAction]) {//디바이스 멀티 BOOL isSelected = NO;
  170. _checkedDevices = (NSMutableArray<ItemModel> *)[_refDevices matchedArrayByObjectName:ksCustomCheckBoxStatus condition:YES];
  171. if (!_checkedDevices || !_checkedDevices.count) {
  172. [[JDFacade facade] alert:NSLocalizedString(@"선택된 항목이 없습니다", @"선택된 항목이 없습니다")];
  173. return;
  174. }
  175. }
  176. [super btnConfirmTouched:sender];
  177. }
  178. @end