DeviceSelectPopupView.m 8.3 KB

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