DeviceConditionPopupView.m 7.1 KB

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