CommandClassListPopupView.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. //
  2. // CommandClassListPopupView.h
  3. // kneet
  4. //
  5. // Created by Jason Lee on 5/22/15.
  6. // Copyright (c) 2015 ntels. All rights reserved.
  7. //
  8. #import "JDObject.h"
  9. #import "RequestHandler.h"
  10. #import "ItemModel.h"
  11. #import "CustomLabel.h"
  12. #import "CustomRadioGroup.h"
  13. #import "UIImageView+WebCache.h"
  14. #import "CustomImageView.h"
  15. #import "CustomButton.h"
  16. #import "CommandClassListPopupView.h"
  17. #import "CommandClassControlView.h"
  18. #import "NoContentView.h"
  19. #define kfPopTableViewCellHeight 100.0f
  20. @implementation CommandClassListCell
  21. - (void)awakeFromNib {
  22. self.selectionStyle = UITableViewCellSelectionStyleNone;
  23. }
  24. @end
  25. @interface CommandClassListPopupView () <UITableViewDataSource, UITableViewDelegate, CustomRadioGroupDelegate> {
  26. NSMutableArray<PredefinedDeviceModel> *_preDeviceList;
  27. UIImage *_bgCellImage;
  28. BOOL _isNotFirstLoading;
  29. CustomRadioReusableGroup *_rgroup;
  30. }
  31. @end
  32. @implementation CommandClassListPopupView
  33. - (id)initFromNib {
  34. for (UIView *view in [CommonUtil nibViews:@"CommandClassListPopupView"]) {
  35. if ([view isKindOfClass:[CommandClassListPopupView class]]) {
  36. self = (CommandClassListPopupView *)view;
  37. //XIB의 경우, 현재 화면 사이즈로 맞춰줘야 함.
  38. self.frame = [UIScreen mainScreen].bounds;
  39. if (!self.tableView.delegate) {
  40. _tableView.dataSource = self;
  41. _tableView.delegate = self;
  42. }
  43. _tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
  44. _tableView.separatorColor = kUILineColor2;
  45. _tableView.backgroundColor = [UIColor clearColor];
  46. _tableView.scrollEnabled = YES;
  47. _tableView.tableFooterView = [[UIView alloc] init];
  48. UINib *nib = [UINib nibWithNibName:@"CommandClassListCell" bundle:nil];
  49. [_tableView registerNib:nib forCellReuseIdentifier:@"CellIdentifier"];
  50. UIEdgeInsets insets = UIEdgeInsetsMake(4, 4, 4, 4);
  51. _bgCellImage = [CommonUtil resizableImageWithCapInsets:insets resizingMode:UIImageResizingModeStretch img:[UIImage imageNamed:@"tp_01_img_list_bg_01"]];
  52. _rgroup = [[CustomRadioReusableGroup alloc] init];
  53. _rgroup.tableView = _tableView;
  54. //Localization
  55. [self.btnConfirm setTitle:NSLocalizedString(@"선택", @"선택") forState:UIControlStateNormal];
  56. [self.btnCancel setTitle:NSLocalizedString(@"취소", @"취소") forState:UIControlStateNormal];
  57. }
  58. }
  59. return self;
  60. }
  61. - (void)didMoveToSuperview {
  62. //상위 함수를 호출하지 않음.
  63. }
  64. - (void)show {
  65. [super show];
  66. //FIXME : 수정!!
  67. if (!self.tableView.delegate) {
  68. self.tableView.delegate = self;
  69. self.tableView.dataSource= self;
  70. }
  71. [_tableView reloadData];
  72. [_tableView scrollsToTop];
  73. }
  74. - (void)setTmpItem:(ItemModel *)tmpItem {
  75. _tmpItem = tmpItem;
  76. self.lblTitle.text = _tmpItem.itemName;
  77. [self requestPredefinedDevicesByItem];
  78. }
  79. #pragma mark - Main Logic
  80. //여길 호출해줘야됨.
  81. - (void)requestPredefinedDevicesByItem {
  82. //parameters
  83. // NSDictionary *parameter = @{@"homegrp_scene_id": _homeGroupSceneId ? _homeGroupSceneId : ksEmptyString};
  84. NSString *path = path = [NSString stringWithFormat:API_GET_PRERULE_DEVICES, _tmpItem.predRuleId, _tmpItem.predItemSequence];
  85. [[RequestHandler handler] sendAsyncGetRequestAPIPath:path parameters:nil modelClass:[PredefinedDeviceListModel class] completion:^(id responseObject) {
  86. if (!responseObject) {//응답결과가 잘못되었거나 없을 경우,
  87. return;
  88. }
  89. PredefinedDeviceListModel *fetchedPreDeviceList = (PredefinedDeviceListModel *)responseObject;
  90. if (fetchedPreDeviceList && fetchedPreDeviceList.list && fetchedPreDeviceList.list.count) {//API 성공 ,
  91. _preDeviceList = (NSMutableArray<PredefinedDeviceModel> *)[[NSMutableArray alloc] initWithArray:fetchedPreDeviceList.list];
  92. [self matchPredefinedDeviceContentsForItem];
  93. } else {
  94. NoContentView *noContentView = [NoContentView viewFromNib];
  95. CGRect nr = noContentView.frame;
  96. nr.size.height = _tableView.frame.size.width;
  97. noContentView.frame = nr;
  98. _tableView.tableFooterView = noContentView;
  99. _tableView.scrollEnabled = NO;
  100. }
  101. } failure:^(id errorObject) {
  102. JDErrorModel *error = (JDErrorModel *)errorObject;
  103. [[JDFacade facade] alert:error.errorMessage];
  104. }];
  105. }
  106. - (void)matchPredefinedDeviceContentsForItem {
  107. //이전에 선택된 액션과 액션리스트를 매칭시킴.
  108. for (PredefinedDeviceModel *pdevice in _tmpItem.predDevices) {//이전에 선택된 디바이스와 사전정의 장치 목록을 매칭시킴.
  109. NSInteger indx = [_preDeviceList indexOfObjectPassingTest:^BOOL(PredefinedDeviceModel *obj, NSUInteger idx, BOOL *stop) {
  110. return [pdevice.deviceId isEqualToString:obj.deviceId] && [pdevice.nodeId isEqualToString:obj.nodeId];
  111. }];
  112. if (indx != NSNotFound) {
  113. PredefinedDeviceModel *tdevice = _preDeviceList[indx];
  114. tdevice.cmdclsValue = pdevice.cmdclsValue;
  115. tdevice.enable = YES;
  116. [[JDFacade facade] setRadioButtonStatus:@YES object:tdevice]; //for radio box
  117. }
  118. }
  119. [_tableView reloadData];
  120. }
  121. #pragma mark - UITableView DataSource & Delegate
  122. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  123. return 1;
  124. }
  125. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  126. return _preDeviceList.count;
  127. }
  128. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  129. CGFloat height = 0;
  130. PredefinedDeviceModel *pdevice = _preDeviceList[indexPath.row];
  131. height = (!pdevice.predCmdclsValue || [pdevice.predCmdclsValue isEmptyString]) ? 169.0f : 100.0f; //컨트롤을 로드 - 커맨드클래스 밸루 리스트가 있는 장치에 한함.
  132. return height;
  133. }
  134. - (NSArray<CmdClsValueModel> *)makeCmdClsValueList {
  135. CmdClsValueModel *cmdCmsValue = [[CmdClsValueModel alloc] init];
  136. cmdCmsValue.cmdclsValue = @"02";
  137. cmdCmsValue.cmdclsValueMsg = NSLocalizedString(@"미만 시", @"미만 시");
  138. CmdClsValueModel *cmdCmsValue2 = [[CmdClsValueModel alloc] init];
  139. cmdCmsValue2.cmdclsValue = @"04";
  140. cmdCmsValue2.cmdclsValueMsg = NSLocalizedString(@"초과 시", @"초과 시");
  141. return (NSMutableArray<CmdClsValueModel> *)[[NSMutableArray alloc] initWithArray:@[cmdCmsValue, cmdCmsValue2]];
  142. }
  143. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  144. static NSString *CellIdentifier = @"CellIdentifier";
  145. CommandClassListCell *tcell = (CommandClassListCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  146. PredefinedDeviceModel *pdevice = _preDeviceList[indexPath.row];
  147. [tcell.imgvItem sd_setImageWithURL:[NSURL URLWithString:pdevice.imageFileName] placeholderImage:nil];
  148. tcell.lblItemName.text = pdevice.deviceName;
  149. tcell.lblSubItems.text = pdevice.nodeName;
  150. tcell.rdoSelect.value = pdevice;
  151. if (indexPath.row == 0 && !_isNotFirstLoading && ! _tmpItem.predDevices) {
  152. _isNotFirstLoading = YES;
  153. tcell.rdoSelect.checked = YES;
  154. } else {
  155. tcell.rdoSelect.checked = [tcell.rdoSelect getRadioStatusFromValue];
  156. }
  157. pdevice.enable = tcell.rdoSelect.checked;
  158. [_rgroup addRadioButton:tcell.rdoSelect];
  159. //뷰를 초기화함.
  160. [[tcell.controlContainerView subviews] enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
  161. UIView *subview = (UIView *)obj;
  162. [subview removeFromSuperview];
  163. }];
  164. [[tcell.controlContainerView2 subviews] enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
  165. UIView *subview = (UIView *)obj;
  166. [subview removeFromSuperview];
  167. }];
  168. if (!pdevice.predCmdclsValue || [pdevice.predCmdclsValue isEmptyString]) {//컨트롤을 로드 - 커맨드클래스 밸루 리스트가 있는 장치에 한함.
  169. CommandClassControlView *controlView = [CommandClassControlView viewForCommandClass:CmdClsTypeThermostatSetPoint];
  170. controlView.device = pdevice;
  171. controlView.enable = pdevice.enable;
  172. controlView.isConditionMode = YES;
  173. tcell.controlContainerView.hidden = !controlView;
  174. if (!tcell.controlContainerView.hidden) {
  175. UIView *superview = tcell.controlContainerView;
  176. [superview addSubview:controlView];
  177. [controlView mas_makeConstraints:^(MASConstraintMaker *make) {
  178. make.top.equalTo(superview.mas_top);
  179. make.left.equalTo(superview.mas_left);
  180. make.center.equalTo(superview);
  181. }];
  182. }
  183. CommandClassControlView *controlView2 = [CommandClassControlView viewForCommandClass:CmdClsTypeThermostatMode];
  184. if (!pdevice.cmdclsValueList || !pdevice.cmdclsValueList.count) {
  185. pdevice.cmdclsValueList = [self makeCmdClsValueList];
  186. }
  187. controlView2.device = pdevice; //02, 04
  188. controlView2.isConditionMode = YES;
  189. controlView2.isConditionTypeMode = YES;
  190. controlView2.enable = pdevice.enable;
  191. tcell.controlContainerView2.hidden = !controlView2;
  192. if (!tcell.controlContainerView.hidden) {
  193. UIView *superview = tcell.controlContainerView2;
  194. [superview addSubview:controlView2];
  195. [controlView2 mas_makeConstraints:^(MASConstraintMaker *make) {
  196. make.top.equalTo(superview.mas_top);
  197. make.left.equalTo(superview.mas_left);
  198. make.center.equalTo(superview);
  199. }];
  200. }
  201. }
  202. tcell.backgroundColor = [UIColor clearColor];
  203. tcell.backgroundView = [[UIImageView alloc] initWithImage:_bgCellImage];
  204. return tcell;
  205. }
  206. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  207. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  208. PredefinedDeviceModel *pdevice = _preDeviceList[indexPath.row];
  209. CommandClassListCell *tcell = (CommandClassListCell *)[self tableView:tableView cellForRowAtIndexPath:indexPath];
  210. tcell.rdoSelect.value = pdevice;
  211. [_rgroup someRadioButtonTouched:tcell.rdoSelect];
  212. }
  213. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
  214. // Remove seperator inset
  215. if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
  216. [cell setSeparatorInset:UIEdgeInsetsZero];
  217. }
  218. // Prevent the cell from inheriting the Table View's margin settings
  219. if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
  220. [cell setPreservesSuperviewLayoutMargins:NO];
  221. }
  222. // Explictly set your cell's layout margins
  223. if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
  224. [cell setLayoutMargins:UIEdgeInsetsZero];
  225. }
  226. }
  227. #pragma mark - UI Events
  228. - (void)btnConfirmTouched:(id)sender {
  229. if (_rgroup.values && _rgroup.values.count) {
  230. PredefinedDeviceModel *predDevice = _rgroup.valueForChecked;
  231. _tmpItem.predDevices = (NSMutableArray<PredefinedDeviceModel> *)[[NSMutableArray alloc] initWithArray:@[predDevice]];
  232. }
  233. self.tableView.delegate = nil;
  234. self.tableView.dataSource = nil;
  235. [super btnConfirmTouched:sender];
  236. }
  237. - (void)btnCancelTouched:(id)sender {
  238. self.tableView.delegate = nil;
  239. self.tableView.dataSource = nil;
  240. // self.tableView = nil;
  241. [super btnCancelTouched:sender];
  242. }
  243. @end