HomeModeSettingsViewController.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. //
  2. // HomeModeSettingsViewController.m
  3. // kneet2
  4. //
  5. // Created by Jason Lee on 11/3/15.
  6. // Copyright © 2015 ntels. All rights reserved.
  7. //
  8. #import "JDObject.h"
  9. #import "RequestHandler.h"
  10. #import "ModeModel.h"
  11. #import "SceneModel.h"
  12. #import "ItemModel.h"
  13. #import "CustomTableView.h"
  14. #import "CustomImageView.h"
  15. #import "CustomLabel.h"
  16. #import "CustomButton.h"
  17. #import "UIImageView+WebCache.h"
  18. #import "DeviceSelectPopupView.h"
  19. #import "DeviceNodePopupView.h"
  20. #import "HomeModeSettingsViewController.h"
  21. @implementation HomeSettingsTitleTableViewCell
  22. @end
  23. @implementation HomeSettingsDeviceTableViewCell
  24. @end
  25. @implementation HomeSettingsAppendTableViewCell
  26. @end
  27. @interface HomeModeSettingsViewController () {
  28. NSMutableArray<ItemModel> *_items;
  29. NSMutableArray<ItemSubModel> *_subItems;
  30. }
  31. @end
  32. #pragma mark - Class Definition
  33. @implementation HomeModeSettingsViewController
  34. - (void)viewDidLoad {
  35. [super viewDidLoad];
  36. // Do any additional setup after loading the view.
  37. [self initUI];
  38. [self prepareViewDidLoad];
  39. }
  40. - (void)initUI {
  41. [self initTableViewAsDefaultStyle:_tableView];
  42. }
  43. - (void)prepareViewDidLoad {
  44. [self requestDeviceListForHome];
  45. }
  46. #pragma mark - Main Logic
  47. - (void)requestDeviceListForHome {
  48. //parameters
  49. NSDictionary *parameter = @{@"item_type_code": ksItemTypeCodeAction};
  50. NSString *path = [NSString stringWithFormat:API_GET_ITEM_DEVICES, ksItemTypeCodeAction];
  51. [[RequestHandler handler] sendAsyncGetRequestAPIPath:path parameters:parameter modelClass:[ItemListModel class] completion:^(id responseObject) {
  52. if (!responseObject) {//응답결과가 잘못되었거나 없을 경우,
  53. return;
  54. }
  55. ItemListModel *fetchedItemList = (ItemListModel *)responseObject;
  56. if (fetchedItemList && fetchedItemList.list && fetchedItemList.list.count) {//API 성공 ,
  57. _items = fetchedItemList.list;
  58. }
  59. [self requestSceneDetailsByHomeMode];
  60. } failure:^(id errorObject) {
  61. JDErrorModel *error = (JDErrorModel *)errorObject;
  62. [[JDFacade facade] alert:error.errorMessage];
  63. }];
  64. }
  65. //현재 모드에 설정된 액션 리스트를 조회함
  66. - (void)requestSceneDetailsByHomeMode {
  67. NSString *path = [NSString stringWithFormat:API_GET_SCENE_DETAIL_HOMEMODE, _mode.modeId];
  68. [[RequestHandler handler] sendAsyncGetRequestAPIPath:path parameters:nil modelClass:[ItemSubListModel class] completion:^(id responseObject) {
  69. if (!responseObject) {//응답결과가 잘못되었거나 없을 경우,
  70. return;
  71. }
  72. ItemSubListModel *fetchedSubItems = (ItemSubListModel *) responseObject;
  73. if (fetchedSubItems && fetchedSubItems.list && fetchedSubItems.list.count) {//API 성공 ,
  74. _subItems = (NSMutableArray<ItemSubModel> *)[[NSMutableArray alloc] initWithArray:fetchedSubItems.list];
  75. }
  76. [self matchItemListWithSubItemsForMode];
  77. } failure:^(id errorObject) {
  78. JDErrorModel *error = (JDErrorModel *)errorObject;
  79. [[JDFacade facade] alert:error.errorMessage];
  80. }];
  81. }
  82. //장치리스트와 현재 모드설정을 매칭함.
  83. - (void)matchItemListWithSubItemsForMode {
  84. for (ItemSubModel *subItem in _subItems) {//scene detail
  85. //모드에 설정된 디바이스와 장치 리스트의 매칭 및 커맨드 클래스 밸루 설정
  86. NSString *filter = @"sourceId == %@ && sourceSubId == %@";
  87. NSArray *matchedSubitems = [_items matchedArrayInSubArrays:@"subItems" predicateFormat:filter, subItem.sourceId, subItem.sourceSubId];
  88. for (ItemSubModel *pSubItem in matchedSubitems) {
  89. [[JDFacade facade] setRadioButtonStatus:@YES object:pSubItem]; //해당 노드를 선택 표시
  90. NSString *vfilter = @"cmdclsValue == %@";
  91. NSArray *matchedValues = [pSubItem.cmdclsValueList filteredArrayUsingPredicateFormat:vfilter, subItem.cmdclsValue];
  92. if (matchedValues && matchedValues.count) {//설정 초기화
  93. CmdClsValueModel *cmdclsValue = matchedValues[0];
  94. [[JDFacade facade] setRadioButtonStatus:@YES object:cmdclsValue];
  95. cmdclsValue.isSelected = YES;
  96. }
  97. }
  98. }
  99. [_tableView reloadData];
  100. }
  101. - (void)requestRegisterModeAction:(NSArray *)subItems {
  102. //parameters
  103. NSDictionary *parameter = @{@"item_sub": subItems};
  104. NSString *path = [NSString stringWithFormat:API_POST_SCENE_HOMEMODE, _mode.modeId];
  105. [[RequestHandler handler] sendAsyncPostRequestAPIPath:path parameters:parameter modelClass:[SceneModel class] completion:^(id responseObject) {
  106. [self dismissViewControllerAnimated:YES completion:^{
  107. [[JDFacade facade] toast:NSLocalizedString(@"홈모드 설정을 저장했습니다", @"홈모드 설정을 저장했습니다")];
  108. }];
  109. } failure:^(id errorObject) {
  110. JDErrorModel *error = (JDErrorModel *)errorObject;
  111. [[JDFacade facade] alert:error.errorMessage];
  112. }];
  113. }
  114. #pragma mark - UITableView DataSource & Delegate
  115. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  116. return 3;
  117. }
  118. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  119. NSInteger count = 1;
  120. if (section == 1) {
  121. NSArray *matchedSubItems = [_items matchedArrayInSubArrays:@"subItems" objectName:ksCustomRadioButtonStatus condition:YES];
  122. count = matchedSubItems && matchedSubItems.count ? matchedSubItems.count : 0;
  123. }
  124. return count;
  125. }
  126. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  127. CGFloat height = 110.0f;
  128. if (indexPath.section == 0) {
  129. height = 110.0f;
  130. } else if (indexPath.section == 1 || indexPath.section == 2) {
  131. height = 95.0f;
  132. }
  133. return height;
  134. }
  135. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  136. UITableViewCell *cell = nil;
  137. NSInteger section = indexPath.section;
  138. if (section == 0) {
  139. HomeSettingsTitleTableViewCell *tcell = (HomeSettingsTitleTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"TitleCellIdentifier"];
  140. [tcell.imgvMode sd_setImageWithURL:[NSURL URLWithString:_mode.imageFileName] placeholderImage:nil options:SDWebImageRefreshCached];
  141. tcell.lblModeTitle.text = _mode.modeName;
  142. cell = tcell;
  143. } else if (section == 1) {
  144. //노드 액션이 선택된 노드만 출력함.
  145. NSInteger fcount = indexPath.row; //선택된 액션만 찾기 위한 인덱스
  146. NSArray *matchedSubItems = [_items matchedArrayInSubArrays:@"subItems" objectName:ksCustomRadioButtonStatus condition:YES];
  147. ItemSubModel *subItem = nil;
  148. for (subItem in matchedSubItems) {
  149. if (fcount == 0) {
  150. break;
  151. } else {
  152. fcount--;
  153. }
  154. }
  155. HomeSettingsDeviceTableViewCell *tcell = (HomeSettingsDeviceTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"DeviceCellIdentifier"];
  156. [tcell.imgvDevice sd_setImageWithURL:[NSURL URLWithString:subItem.imageFileName] placeholderImage:nil options:SDWebImageRefreshCached];
  157. tcell.lblDeviceName.text = subItem.sourceName;
  158. tcell.lblNodeName.text = subItem.sourceSubName;
  159. //노트의 액션 값을 세팅함.
  160. if (subItem.cmdclsValueList && subItem.cmdclsValueList.count) {//커맨드클래스 밸루 리스트가 있을 경우,
  161. CmdClsValueModel *cmdclsValue = [subItem.cmdclsValueList matchedObjectName:ksCustomRadioButtonStatus condition:YES];
  162. tcell.lblActionName.text = cmdclsValue.cmdclsValueMsg;
  163. } else {
  164. tcell.lblActionName.text = subItem.cmdclsValueMsg;
  165. }
  166. tcell.lblActionName.value = subItem;
  167. [tcell.lblActionName setUnderLine:tcell.lblActionName.text];
  168. if (!tcell.lblActionName.touchHandler) {
  169. [tcell.lblActionName addTouchEventHandler:^(id label) {
  170. [self lblActionNameTouched:label];
  171. }];
  172. }
  173. tcell.btnDelete.value = subItem;
  174. if (![tcell.btnDelete actionsForTarget:self forControlEvent:UIControlEventTouchUpInside]) {
  175. [tcell.btnDelete addTarget:self action:@selector(btnDeleteDeviceTouched:) forControlEvents:UIControlEventTouchUpInside];
  176. }
  177. cell = tcell;
  178. } else if (section == 2) {
  179. HomeSettingsAppendTableViewCell *tcell = (HomeSettingsAppendTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"AppendCellIdentifier"];
  180. if (![tcell.btnAppend actionsForTarget:self forControlEvent:UIControlEventTouchUpInside]) {
  181. [tcell.btnAppend addTarget:self action:@selector(btnAppendTouched:) forControlEvents:UIControlEventTouchUpInside];
  182. }
  183. cell = tcell;
  184. }
  185. return cell;
  186. }
  187. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  188. [super tableView:tableView didSelectRowAtIndexPath:indexPath];
  189. if (indexPath.section == 1) {
  190. HomeSettingsDeviceTableViewCell *tcell = (HomeSettingsDeviceTableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
  191. ItemSubModel *subItem = tcell.lblActionName.value;
  192. [self modifyAction:subItem];
  193. }
  194. }
  195. - (void)btnDeleteDeviceTouched:(id)sender {
  196. CustomButton *btnDelete = (CustomButton *)sender;
  197. ItemSubModel *subItem = (ItemSubModel *)btnDelete.value;
  198. [[JDFacade facade] setRadioButtonStatus:@NO object:subItem];
  199. for (CmdClsValueModel *cmdclsValue in subItem.cmdclsValueList) {
  200. cmdclsValue.isSelected = NO;
  201. }
  202. [_tableView reloadData];
  203. }
  204. //장치 선택
  205. - (void)btnAppendTouched:(id)sender {
  206. DeviceSelectPopupView *popup = [[DeviceSelectPopupView alloc] initFromNib];
  207. popup.items = _items;
  208. popup.typeCode = ksItemTypeCodeTrigger;
  209. [popup showWithCompletion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
  210. if (buttonIndex == 0) {//ok
  211. DeviceNodePopupView *npopup = [[DeviceNodePopupView alloc] initFromNib];
  212. npopup.item = popup.selectedItems[0];
  213. npopup.typeCode = ksItemTypeCodeTrigger;
  214. [npopup showWithCompletion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
  215. [_tableView reloadData];
  216. }];
  217. }
  218. }];
  219. }
  220. - (void)modifyAction:(ItemSubModel *)subItem {
  221. ItemModel *item = [_items objectKey:@"sourceId" eqaulToString:subItem.sourceId];
  222. //노드 선택 팝럽.
  223. DeviceNodePopupView *npopup = [[DeviceNodePopupView alloc] initFromNib];
  224. npopup.item = item;
  225. npopup.typeCode = ksItemTypeCodeTrigger;
  226. npopup.isModifyMode = YES;
  227. [npopup showWithCompletion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
  228. [_tableView reloadData];
  229. }];
  230. }
  231. - (void)lblActionNameTouched:(id)sender {
  232. CustomLabel *label = (CustomLabel *)sender;
  233. ItemSubModel *subItem = label.value;
  234. [self modifyAction:subItem];
  235. }
  236. #pragma mark - UI Events
  237. - (IBAction)btnCompleteTouched:(id)sender {
  238. //선택된 노드만 출력함.
  239. NSMutableArray *pSubItems = [[NSMutableArray alloc] init];
  240. NSArray *matchedSubItems = [_items matchedArrayInSubArrays:@"subItems" objectName:ksCustomRadioButtonStatus condition:YES];
  241. for (ItemSubModel *pSubItem in matchedSubItems) {
  242. CmdClsValueModel *pCmdClsValue = [pSubItem.cmdclsValueList matchedObjectName:ksCustomRadioButtonStatus condition:YES];
  243. NSDictionary *dSubITem = @{@"source_id": pSubItem.sourceId,
  244. @"source_sub_id": pSubItem.sourceSubId,
  245. @"cmdcls_value": pCmdClsValue.cmdclsValue};
  246. [pSubItems addObject:dSubITem];
  247. }
  248. if (!pSubItems.count) {
  249. [[JDFacade facade] alert:NSLocalizedString(@"실행할 장치를 선택하세요", @"실행할 장치를 선택하세요")];
  250. return;
  251. }
  252. [self requestRegisterModeAction:pSubItems];
  253. }
  254. - (IBAction)btnCancelTouched:(id)sender {
  255. [self dismissViewControllerAnimated:YES completion:nil];
  256. }
  257. #pragma mark - MemoryWarning
  258. - (void)didReceiveMemoryWarning
  259. {
  260. [super didReceiveMemoryWarning];
  261. // Dispose of any resources that can be recreated.
  262. }
  263. @end