DeviceSelectPopupView.m 7.3 KB

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