DeviceSelectPopupView.m 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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)setRefDevices:(NSMutableArray<ItemModel> *)refDevices {
  77. _refDevices = refDevices;
  78. for (ItemSubModel *item in _refDevices) {
  79. [[JDFacade facade] setRadioButtonStatus:@NO object:item];
  80. }
  81. [_tableView reloadData];
  82. }
  83. - (NSArray<ItemModel> *)selectedDevices {
  84. NSArray<ItemModel> *devices = nil;
  85. if ([_typeCode isEqualToString:ksItemTypeCodeAction]) {
  86. devices = _checkedDevices;
  87. } else {
  88. devices = (NSArray<ItemModel> *)@[_rgroup.valueForChecked];
  89. }
  90. return devices;
  91. }
  92. #pragma mark - Main Logic
  93. #pragma mark - UITableView DataSource & Delegate
  94. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  95. return 1;
  96. }
  97. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  98. return _refDevices ? _refDevices.count : 0;
  99. }
  100. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  101. CGFloat height = 86.0f;
  102. return height;
  103. }
  104. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  105. ItemModel *item = _refDevices[indexPath.row];
  106. DeviceSelectPopupTableViewCell *tcell = (DeviceSelectPopupTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"DeviceCellIdentifier"];
  107. tcell.lblDeviceName.text = item.sourceName;
  108. [tcell.imgvDevice sd_setImageWithURL:[NSURL URLWithString:item.imageFileName] placeholderImage:nil options:SDWebImageRefreshCached];
  109. if ([_typeCode isEqualToString:ksItemTypeCodeTrigger] || [_typeCode isEqualToString:ksItemTypeCodeCondition]) {//트리거 또는 컨디션
  110. tcell.chkSelect.hidden = YES;
  111. tcell.rdoSelect.value = item;
  112. if (indexPath.row == 0 && !_isNotFirstLoading) {
  113. _isNotFirstLoading = YES;
  114. tcell.rdoSelect.checked = YES;
  115. } else {
  116. tcell.rdoSelect.checked = [tcell.rdoSelect getRadioStatusFromValue];
  117. }
  118. [_rgroup addRadioButton:tcell.rdoSelect];
  119. } else if ([_typeCode isEqualToString:ksItemTypeCodeAction]) {//액션
  120. tcell.rdoSelect.hidden = YES;
  121. tcell.chkSelect.delegate = self;
  122. tcell.chkSelect.value = item;
  123. tcell.chkSelect.checked = [tcell.chkSelect getCheckStatusFromValue];
  124. }
  125. //set background image
  126. if (indexPath.row % 2 == 0) {
  127. tcell.backgroundView = [[UIImageView alloc] initWithImage:_bgCellImage1];
  128. } else {
  129. tcell.backgroundView = [[UIImageView alloc] initWithImage:_bgCellImage2];
  130. }
  131. return tcell;
  132. }
  133. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  134. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  135. DeviceSelectPopupTableViewCell *tcell = (DeviceSelectPopupTableViewCell *)[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. }
  160. #pragma mark - UI Events
  161. - (IBAction)btnConfirmTouched:(id)sender {
  162. //validate
  163. if ([_typeCode isEqualToString:ksItemTypeCodeAction]) {//디바이스 멀티 BOOL isSelected = NO;
  164. _checkedDevices = (NSMutableArray<ItemModel> *)[_refDevices matchedArrayByObjectName:ksCustomCheckBoxStatus condition:YES];
  165. if (!_checkedDevices || !_checkedDevices.count) {
  166. [[JDFacade facade] alert:NSLocalizedString(@"선택된 항목이 없습니다", @"선택된 항목이 없습니다")];
  167. return;
  168. }
  169. }
  170. [super btnConfirmTouched:sender];
  171. }
  172. @end