HomeModeSettingsViewController.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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 = 0;
  128. if (indexPath.section == 0) {
  129. height = 101.0f;
  130. } else if (indexPath.section == 1 || indexPath.section == 2) {
  131. height = 86.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. if (![tcell.lblActionName.text isEmptyString]) {
  167. [tcell.lblActionName setUnderLine:tcell.lblActionName.text];
  168. }
  169. tcell.lblActionName.value = subItem;
  170. // [tcell.lblActionName setUnderLine:tcell.lblActionName.text];
  171. if (!tcell.lblActionName.touchHandler) {
  172. [tcell.lblActionName addTouchEventHandler:^(id label) {
  173. [self lblActionNameTouched:label];
  174. }];
  175. }
  176. tcell.btnDelete.value = subItem;
  177. if (![tcell.btnDelete actionsForTarget:self forControlEvent:UIControlEventTouchUpInside]) {
  178. [tcell.btnDelete addTarget:self action:@selector(btnDeleteDeviceTouched:) forControlEvents:UIControlEventTouchUpInside];
  179. }
  180. cell = tcell;
  181. } else if (section == 2) {
  182. HomeSettingsAppendTableViewCell *tcell = (HomeSettingsAppendTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"AppendCellIdentifier"];
  183. if (![tcell.btnAppend actionsForTarget:self forControlEvent:UIControlEventTouchUpInside]) {
  184. [tcell.btnAppend addTarget:self action:@selector(btnAppendTouched:) forControlEvents:UIControlEventTouchUpInside];
  185. }
  186. cell = tcell;
  187. }
  188. return cell;
  189. }
  190. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  191. [super tableView:tableView didSelectRowAtIndexPath:indexPath];
  192. if (indexPath.section == 1) {
  193. HomeSettingsDeviceTableViewCell *tcell = (HomeSettingsDeviceTableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
  194. ItemSubModel *subItem = tcell.lblActionName.value;
  195. [self modifyAction:subItem];
  196. }
  197. }
  198. - (void)btnDeleteDeviceTouched:(id)sender {
  199. CustomButton *btnDelete = (CustomButton *)sender;
  200. ItemSubModel *subItem = (ItemSubModel *)btnDelete.value;
  201. [[JDFacade facade] setRadioButtonStatus:@NO object:subItem];
  202. for (CmdClsValueModel *cmdclsValue in subItem.cmdclsValueList) {
  203. cmdclsValue.isSelected = NO;
  204. }
  205. [_tableView reloadData];
  206. }
  207. //장치 선택
  208. - (void)btnAppendTouched:(id)sender {
  209. DeviceSelectPopupView *popup = [[DeviceSelectPopupView alloc] initFromNib];
  210. popup.refDevices = _items;
  211. popup.typeCode = ksItemTypeCodeTrigger;
  212. [popup showWithCompletion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
  213. if (buttonIndex == 0) {//ok
  214. DeviceNodePopupView *npopup = [[DeviceNodePopupView alloc] initFromNib];
  215. npopup.refDevice = popup.selectedDevices[0];
  216. npopup.typeCode = ksItemTypeCodeTrigger;
  217. [npopup showWithCompletion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
  218. if (buttonIndex == 0) {//OK
  219. [_tableView reloadData];
  220. }
  221. }];
  222. }
  223. }];
  224. }
  225. - (void)modifyAction:(ItemSubModel *)subItem {
  226. ItemModel *item = [_items objectKey:@"sourceId" eqaulToString:subItem.sourceId];
  227. //노드 선택 팝럽.
  228. DeviceNodePopupView *npopup = [[DeviceNodePopupView alloc] initFromNib];
  229. npopup.refDevice = item;
  230. npopup.typeCode = ksItemTypeCodeTrigger;
  231. npopup.isModifyMode = YES;
  232. [npopup showWithCompletion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
  233. if (buttonIndex == 0) {//OK
  234. [_tableView reloadData];
  235. }
  236. }];
  237. }
  238. - (void)lblActionNameTouched:(id)sender {
  239. CustomLabel *label = (CustomLabel *)sender;
  240. ItemSubModel *subItem = label.value;
  241. [self modifyAction:subItem];
  242. }
  243. #pragma mark - UI Events
  244. - (IBAction)btnCompleteTouched:(id)sender {
  245. //선택된 노드만 출력함.
  246. NSMutableArray *pSubItems = [[NSMutableArray alloc] init];
  247. NSArray *matchedSubItems = [_items matchedArrayInSubArrays:@"subItems" objectName:ksCustomRadioButtonStatus condition:YES];
  248. for (ItemSubModel *pSubItem in matchedSubItems) {
  249. CmdClsValueModel *pCmdClsValue = [pSubItem.cmdclsValueList matchedObjectName:ksCustomRadioButtonStatus condition:YES];
  250. NSDictionary *dSubITem = @{@"source_id": pSubItem.sourceId,
  251. @"source_sub_id": pSubItem.sourceSubId,
  252. @"cmdcls_value": pCmdClsValue.cmdclsValue};
  253. [pSubItems addObject:dSubITem];
  254. }
  255. if (!pSubItems.count) {
  256. [[JDFacade facade] alert:NSLocalizedString(@"실행할 장치를 선택하세요", @"실행할 장치를 선택하세요")];
  257. return;
  258. }
  259. [self requestRegisterModeAction:pSubItems];
  260. }
  261. - (IBAction)btnCancelTouched:(id)sender {
  262. [self dismissViewControllerAnimated:YES completion:nil];
  263. }
  264. #pragma mark - MemoryWarning
  265. - (void)didReceiveMemoryWarning
  266. {
  267. [super didReceiveMemoryWarning];
  268. // Dispose of any resources that can be recreated.
  269. }
  270. @end