PredefinedDeviceViewController.m 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. //
  2. // RulesRegisterDeviceViewController.m
  3. // kneet
  4. //
  5. // Created by Jason Lee on 3/20/15.
  6. // Copyright (c) 2015 ntels. All rights reserved.
  7. //
  8. @import ObjectiveC.runtime;
  9. #import "RequestHandler.h"
  10. #import "RuleModel.h"
  11. #import "ModeModel.h"
  12. #import "CustomLabel.h"
  13. #import "CustomRadioGroup.h"
  14. #import "CustomImageView.h"
  15. #import "UIImageView+WebCache.h"
  16. #import "PredefinedDeviceViewController.h"
  17. #import "CustomCheckBox.h"
  18. #import "CustomButton.h"
  19. #import "ScenesRegisterViewController.h"
  20. #import "CommandClassControlView.h"
  21. #import "RulesRegisterViewController.h"
  22. #import "NoContentView.h"
  23. @implementation RulesDeviceTitleViewCell
  24. - (void)awakeFromNib {
  25. self.selectionStyle = UITableViewCellSelectionStyleNone;
  26. self.backgroundColor = [UIColor clearColor];
  27. }
  28. @end
  29. @implementation RulesDeviceTableViewCell
  30. - (void)awakeFromNib {
  31. self.selectionStyle = UITableViewCellSelectionStyleNone;
  32. self.backgroundColor = [UIColor clearColor];
  33. }
  34. @end
  35. @interface PredefinedDeviceViewController () <UITableViewDataSource, UITableViewDelegate, CustomRadioButtonDelegate, CustomCheckBoxDelegate> {
  36. NSMutableArray<PredefinedDeviceModel> *_preDeviceList;
  37. NSInteger _triggerCount, _actionCount, _conditionCount;
  38. NSString *_typeCode;
  39. BOOL _isNotFirstLoading;
  40. CustomRadioReusableGroup *_rgroup;
  41. }
  42. @end
  43. #pragma mark - Class Definition
  44. @implementation PredefinedDeviceViewController
  45. - (void)viewDidLoad {
  46. [super viewDidLoad];
  47. // initialize
  48. _rgroup = [[CustomRadioReusableGroup alloc] init];
  49. _rgroup.tableView = _tableView;
  50. [self initUI];
  51. [self prepareViewDidLoad];
  52. }
  53. - (void)initUI {
  54. _tableView.dataSource = self;
  55. _tableView.delegate = self;
  56. _tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
  57. _tableView.separatorColor = kUILineColor2;
  58. _tableView.backgroundColor = [UIColor clearColor];
  59. _tableView.tableFooterView = [[UIView alloc] init]; //this call table events;
  60. //Localization
  61. [_btnSelect setTitle:NSLocalizedString(@"선택", @"선택") forState:UIControlStateNormal];
  62. [_btnCancel setTitle:NSLocalizedString(@"취소", @"취소") forState:UIControlStateNormal];
  63. }
  64. - (void)prepareViewDidLoad {
  65. //itemSubTypeCode가 01-디바이스일 경우,
  66. // if (_predRuleId && ![_predRuleId isEmptyString]) {
  67. // _typeCode = _tmpItem.itemTypeCode;
  68. //
  69. // } else if (_predSceneId && ![_predSceneId isEmptyString]) {
  70. // _typeCode = @"02";
  71. // }
  72. _typeCode = @"02";
  73. [self requestPredefinedDevices];
  74. }
  75. #pragma mark - Main Logic
  76. - (void)requestPredefinedDevices {
  77. NSString *path = nil;
  78. if (_predRuleId && ![_predRuleId isEmptyString]) {
  79. path = [NSString stringWithFormat:API_GET_PRERULE_DEVICES, _tmpItem.predRuleId, _tmpItem.predItemSequence];
  80. } else if (_predSceneId && ![_predSceneId isEmptyString]) {
  81. path = [NSString stringWithFormat:API_GET_PRESCENE_DEVICES, _tmpItem.predSceneId, _tmpItem.predActionSequence];
  82. }
  83. [[RequestHandler handler] sendAsyncGetRequestAPIPath:path parameters:nil modelClass:[PredefinedDeviceListModel class] completion:^(id responseObject) {
  84. if (!responseObject) {//응답결과가 잘못되었거나 없을 경우,
  85. return;
  86. }
  87. PredefinedDeviceListModel *fetchedPreDeviceList = (PredefinedDeviceListModel *)responseObject;
  88. if (fetchedPreDeviceList && fetchedPreDeviceList.list && fetchedPreDeviceList.list.count) {//API 성공 ,
  89. _preDeviceList = (NSMutableArray<PredefinedDeviceModel> *)[[NSMutableArray alloc] initWithArray:fetchedPreDeviceList.list];
  90. for (PredefinedDeviceModel *pdevice in _tmpItem.predDevices) {//이전에 선택된 디바이스와 사전정의 장치 목록을 매칭시킴.
  91. NSInteger indx = [_preDeviceList indexOfObjectPassingTest:^BOOL(PredefinedDeviceModel *obj, NSUInteger idx, BOOL *stop) {
  92. return [pdevice.deviceId isEqualToString:obj.deviceId] && [pdevice.nodeId isEqualToString:obj.nodeId];
  93. }];
  94. if (indx != NSNotFound) {
  95. PredefinedDeviceModel *tdevice = _preDeviceList[indx];
  96. tdevice.cmdclsValue = pdevice.cmdclsValue;
  97. tdevice.enable = YES;
  98. // id checkStatus = [[JDFacade facade] getCheckBoxStatus:pdevice];
  99. // id radioStatus = [[JDFacade facade] getRadioButtonStatus:pdevice];
  100. [[JDFacade facade] setRadioButtonStatus:@YES object:tdevice]; //for radio box
  101. [[JDFacade facade] setCheckBoxStatus:@YES object:tdevice]; //for radio box
  102. // if (checkStatus) {
  103. // [[JDFacade facade] setCheckBoxStatus:checkStatus object:tdevice]; //for radio box
  104. // }
  105. //
  106. // if (radioStatus) {
  107. // [[JDFacade facade] setRadioButtonStatus:radioStatus object:tdevice]; //for radio box
  108. // }
  109. // [_preDeviceList replaceObjectAtIndex:indx withObject:pdevice];
  110. }
  111. }
  112. [self setPredefinedDeviceContents];
  113. } else {
  114. NoContentView *noContentView = [NoContentView viewFromNib];
  115. CGRect nr = noContentView.frame;
  116. nr.size.height = _tableView.frame.size.width;
  117. noContentView.frame = nr;
  118. _tableView.tableFooterView = noContentView;
  119. _tableView.scrollEnabled = NO;
  120. }
  121. } failure:^(id errorObject) {
  122. JDErrorModel *error = (JDErrorModel *)errorObject;
  123. [[JDFacade facade] alert:error.errorMessage];
  124. }];
  125. }
  126. - (void)setPredefinedDeviceContents {
  127. [_tableView reloadData];
  128. }
  129. #pragma mark - UITableView DataSource & Delegate
  130. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  131. return 2;
  132. }
  133. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  134. NSInteger count = 0;
  135. if (section == 0) {
  136. count = 1;
  137. } else if (section == 1) {
  138. count = _preDeviceList && _preDeviceList.count ? _preDeviceList.count : 0;
  139. }
  140. return count;
  141. }
  142. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  143. CGFloat height = 0;
  144. if (indexPath.section == 0) {//title
  145. height = 88;
  146. } else if (indexPath.section == 1) {//devices
  147. PredefinedDeviceModel *pdevice = _preDeviceList[indexPath.row];
  148. height = (!pdevice.predCmdclsValue || [pdevice.predCmdclsValue isEmptyString]) ? 132.0f : 100.0f; //컨트롤을 로드 - 커맨드클래스 밸루 리스트가 있는 장치에 한함.
  149. }
  150. return height;
  151. }
  152. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  153. UITableViewCell *cell = nil;
  154. if (indexPath.section == 0) {//title
  155. RulesDeviceTitleViewCell *tcell = [tableView dequeueReusableCellWithIdentifier:@"TitleCellIdentifier"];
  156. if (tcell == nil) {
  157. tcell = [[RulesDeviceTitleViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"TitleCellIdentifier"];
  158. }
  159. [tcell.btnActionTitle setTitle:_tmpItem.itemName forState:UIControlStateNormal];
  160. tcell.lblSelectCount.text = [NSString stringWithFormat:@"%zd / %zd", [self countForCheckedActions], _preDeviceList.count];
  161. cell = tcell;
  162. } else if (indexPath.section == 1) {//predefined device list
  163. RulesDeviceTableViewCell *tcell = [tableView dequeueReusableCellWithIdentifier:@"CellIdentifier"];
  164. if (tcell == nil) {
  165. tcell = [[RulesDeviceTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CellIdentifier"];
  166. }
  167. PredefinedDeviceModel *pdevice = _preDeviceList[indexPath.row];
  168. [tcell.imgvItem sd_setImageWithURL:[NSURL URLWithString:pdevice.imageFileName] placeholderImage:nil];
  169. tcell.lblItemName.text = pdevice.deviceName;
  170. tcell.lblSubItems.text = pdevice.nodeName;
  171. if ([_typeCode isEqualToString:ksItemTypeCodeTrigger] || [_typeCode isEqualToString:ksItemTypeCodeCondition]) {//트리거 또는 컨디션
  172. tcell.chkSelect.hidden = YES;
  173. tcell.rdoSelect.value = pdevice;
  174. if (indexPath.row == 0 && !_isNotFirstLoading && !_tmpItem.predDevices) {
  175. _isNotFirstLoading = YES;
  176. tcell.rdoSelect.checked = YES;
  177. } else {
  178. tcell.rdoSelect.checked = [tcell.rdoSelect getRadioStatusFromValue];
  179. }
  180. [_rgroup addRadioButton:tcell.rdoSelect];
  181. } else if ([_typeCode isEqualToString:ksItemTypeCodeAction]) {//액션
  182. tcell.rdoSelect.hidden = YES;
  183. tcell.chkSelect.delegate = self;
  184. tcell.chkSelect.value = pdevice;
  185. tcell.chkSelect.checked = [tcell.chkSelect getCheckStatusFromValue];
  186. }
  187. //뷰를 초기화함.
  188. [[tcell.controlContainerView subviews] enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
  189. UIView *subview = (UIView *)obj;
  190. [subview removeFromSuperview];
  191. }];
  192. if (!pdevice.predCmdclsValue || [pdevice.predCmdclsValue isEmptyString]) {//컨트롤을 로드 - 커맨드클래스 밸루 리스트가 있는 장치에 한함.
  193. CommandClassControlView *controlView = [CommandClassControlView viewForCommandClass:pdevice.cmdclsType];
  194. controlView.device = pdevice;
  195. controlView.enable = pdevice.enable;
  196. controlView.isConditionMode = YES;
  197. tcell.controlContainerView.hidden = !controlView;
  198. if (!tcell.controlContainerView.hidden) {
  199. UIView *superview = tcell.controlContainerView;
  200. [superview addSubview:controlView];
  201. [controlView mas_makeConstraints:^(MASConstraintMaker *make) {
  202. make.top.equalTo(superview.mas_top);
  203. make.left.equalTo(superview.mas_left);
  204. make.center.equalTo(superview);
  205. }];
  206. }
  207. }
  208. cell = tcell;
  209. }
  210. return cell;
  211. }
  212. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  213. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  214. if (indexPath.section == 0) //제목 셀인 경우, 리턴
  215. return;
  216. PredefinedDeviceModel *pdevice = _preDeviceList[indexPath.row];
  217. RulesDeviceTableViewCell *tcell = (RulesDeviceTableViewCell *)[self tableView:tableView cellForRowAtIndexPath:indexPath];
  218. if ([_typeCode isEqualToString:ksItemTypeCodeTrigger] || [_typeCode isEqualToString:
  219. ksItemTypeCodeCondition]) {//트리거 또는 컨디션
  220. tcell.rdoSelect.value = pdevice;
  221. tcell.rdoSelect.checked = !tcell.rdoSelect.checked;
  222. [_tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
  223. } else if ([_typeCode isEqualToString:ksItemTypeCodeAction]) {//액션일 경우,
  224. [tcell.chkSelect checkBoxClicked];
  225. }
  226. [_tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationNone];
  227. [_tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
  228. }
  229. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
  230. // Remove seperator inset
  231. if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
  232. [cell setSeparatorInset:UIEdgeInsetsZero];
  233. }
  234. // Prevent the cell from inheriting the Table View's margin settings
  235. if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
  236. [cell setPreservesSuperviewLayoutMargins:NO];
  237. }
  238. // Explictly set your cell's layout margins
  239. if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
  240. [cell setLayoutMargins:UIEdgeInsetsZero];
  241. }
  242. }
  243. #pragma mark - CustomCheckBox Delegate
  244. - (void)didCheckBoxClicked:(id)sender {
  245. CustomCheckBox *chk = (CustomCheckBox *)sender;
  246. PredefinedDeviceModel *pdevice = chk.value;
  247. if (!pdevice.predCmdclsValue || [pdevice.predCmdclsValue isEmptyString]) {//컨트롤을 로드 - 커맨드클래스 밸루 리스트가 있는 장치에 한함.
  248. pdevice.enable = chk.checked;
  249. }
  250. [_tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationNone];
  251. [_tableView reloadSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationNone];
  252. }
  253. #pragma mark - UI Events
  254. - (NSInteger)countForCheckedActions {
  255. NSInteger count = 0;
  256. for (PredefinedDeviceModel *pdevice in _preDeviceList) {
  257. if ([[[JDFacade facade] getCheckBoxStatus:pdevice] boolValue]) {
  258. count++;
  259. }
  260. }
  261. return count;
  262. }
  263. - (void)btnSelectTouched:(id)sender {
  264. if ([_typeCode isEqualToString:ksItemTypeCodeTrigger] || [_typeCode isEqualToString:ksItemTypeCodeCondition]) {//디바이스 1건
  265. PredefinedDeviceModel *predDevice = _rgroup.valueForChecked;
  266. _tmpItem.predDevices = (NSMutableArray<PredefinedDeviceModel> *)[[NSMutableArray alloc] initWithArray:@[predDevice]];
  267. } else if ([_typeCode isEqualToString:ksItemTypeCodeAction]) {//디바이스 멀티
  268. _tmpItem.predDevices = (NSMutableArray<PredefinedDeviceModel> *)[[NSMutableArray alloc] init];
  269. for (PredefinedDeviceModel *pdevice in _preDeviceList) {
  270. if ([[[JDFacade facade] getCheckBoxStatus:pdevice] boolValue]) {
  271. [_tmpItem.predDevices addObject:pdevice];
  272. }
  273. }
  274. if (!_tmpItem.predDevices.count) {
  275. [[JDFacade facade] alert:NSLocalizedString(@"선택된 항목이 없습니다", @"선택된 항목이 없습니다")];
  276. return;
  277. }
  278. }
  279. if (_isCustomCreation) {//자유 선택
  280. if (_tmpItem.predSceneId && ![_tmpItem.predSceneId isEmptyString]) {//씬일 경우,
  281. ScenesRegisterViewController *vc = [[JDFacade facade] viewControllerOnNaviationController:[ScenesRegisterViewController class]];
  282. [vc addItem:_tmpItem];
  283. } else if (_tmpItem.predRuleId && ![_tmpItem.predRuleId isEmptyString]) {//룰일 경우,
  284. RulesRegisterViewController *vc = [[JDFacade facade] viewControllerOnNaviationController:[RulesRegisterViewController class]];
  285. [vc addItem:_tmpItem];
  286. }
  287. }
  288. // [[JDFacade facade] toast:@"추가되었습니다."];
  289. [self.navigationController popViewControllerAnimated:YES];
  290. }
  291. - (void)btnCancelTouched:(id)sender {
  292. [self.navigationController popViewControllerAnimated:YES];
  293. }
  294. #pragma mark - MemoryWarning
  295. - (void)didReceiveMemoryWarning
  296. {
  297. [super didReceiveMemoryWarning];
  298. // Dispose of any resources that can be recreated.
  299. }
  300. @end