HomeModeSettingsViewController.m 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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. _lbTitle.text = [NSString stringWithFormat:@"%@ 모드 설정", _mode.modeName];
  45. [self requestDeviceListForHome];
  46. }
  47. #pragma mark - Main Logic
  48. - (void)requestDeviceListForHome {
  49. //parameters
  50. // NSDictionary *parameter = @{@"item_type_code": ksItemTypeCodeAction};
  51. NSArray *arr = @[ksItemTypeCodeAction];
  52. // NSString *path = [NSString stringWithFormat:API_GET_ITEM_DEVICES, ksItemTypeCodeAction];
  53. NSString *path = [[JDFacade facade] getUrlWithCustAndGroupID:API_GET_ITEM_DEVICES arguments:arr];
  54. NSLog(@"path : %@", path);
  55. [[RequestHandler handler] sendAsyncGetRequestAPIPath:path parameters:nil modelClass:[ItemListModel class] completion:^(id responseObject) {
  56. if (!responseObject) {//응답결과가 잘못되었거나 없을 경우,
  57. return;
  58. }
  59. NSLog(@"_items : %@", responseObject);
  60. ItemListModel *fetchedItemList = (ItemListModel *)responseObject;
  61. if (fetchedItemList && fetchedItemList.list && fetchedItemList.list.count) {//API 성공 ,
  62. _items = fetchedItemList.list;
  63. }
  64. [self requestSceneDetailsByHomeMode];
  65. } failure:^(id errorObject) {
  66. JDErrorModel *error = (JDErrorModel *)errorObject;
  67. [[JDFacade facade] alert:error.errorMessage];
  68. }];
  69. }
  70. //현재 모드에 설정된 액션 리스트를 조회함
  71. - (void)requestSceneDetailsByHomeMode {
  72. NSArray *arr = @[_mode.modeId];
  73. NSString *path = [[JDFacade facade] getUrlWithCustAndGroupID:API_GET_DETAIL_HOMEMODE arguments:arr];
  74. // NSString *path = [NSString stringWithFormat:API_GET_SCENE_DETAIL_HOMEMODE, _mode.modeId];
  75. [[RequestHandler handler] sendAsyncGetRequestAPIPath:path parameters:nil modelClass:[ItemSubListModel class] completion:^(id responseObject) {
  76. if (!responseObject) {//응답결과가 잘못되었거나 없을 경우,
  77. return;
  78. }
  79. ItemSubListModel *fetchedSubItems = (ItemSubListModel *) responseObject;
  80. // NSLog(@"Item Sub Model : %@", fetchedSubItems);
  81. if (fetchedSubItems && fetchedSubItems.list && fetchedSubItems.list.count) {//API 성공 ,
  82. _subItems = (NSMutableArray<ItemSubModel> *)[[NSMutableArray alloc] initWithArray:fetchedSubItems.list];
  83. }
  84. // NSLog(@"Item Sub Model : %@", _subItems);
  85. [self matchItemListWithSubItemsForMode];
  86. } failure:^(id errorObject) {
  87. JDErrorModel *error = (JDErrorModel *)errorObject;
  88. [[JDFacade facade] alert:error.errorMessage];
  89. }];
  90. }
  91. //장치리스트와 현재 모드설정을 매칭함.
  92. - (void)matchItemListWithSubItemsForMode {
  93. for (ItemSubModel *subItem in _subItems) {//scene detail
  94. //모드에 설정된 디바이스와 장치 리스트의 매칭 및 커맨드 클래스 밸루 설정
  95. NSString *filter = @"sourceId == %@ && sourceSubId == %@";
  96. NSArray *matchedSubitems = [_items matchedArrayInSubArrays:@"subItems" predicateFormat:filter, subItem.sourceId, subItem.sourceSubId];
  97. for (ItemSubModel *pSubItem in matchedSubitems) {
  98. [[JDFacade facade] setRadioButtonStatus:@YES object:pSubItem]; //해당 노드를 선택 표시
  99. NSString *vfilter = @"cmdclsValue == %@";
  100. NSArray *matchedValues = [pSubItem.cmdclsValueList filteredArrayUsingPredicateFormat:vfilter, subItem.cmdclsValue];
  101. if (matchedValues && matchedValues.count) {//설정 초기화
  102. CmdClsValueModel *cmdclsValue = matchedValues[0];
  103. [[JDFacade facade] setRadioButtonStatus:@YES object:cmdclsValue];
  104. cmdclsValue.isSelected = YES;
  105. }
  106. }
  107. }
  108. [_tableView reloadData];
  109. }
  110. - (void)requestRegisterModeAction:(NSArray *)subItems {
  111. //parameters
  112. NSDictionary *parameter = @{@"item_sub": subItems};
  113. NSArray *arr = @[_mode.modeId];
  114. NSString *path = [[JDFacade facade] getUrlWithCustAndGroupID:API_POST_MODIFY_HOMEMODE arguments:arr];
  115. // NSString *path = [NSString stringWithFormat:API_POST_SCENE_HOMEMODE, _mode.modeId];
  116. [[RequestHandler handler] sendAsyncPostRequestAPIPath:path parameters:parameter modelClass:[SceneModel class] completion:^(id responseObject) {
  117. [self dismissViewControllerAnimated:YES completion:^{
  118. [[JDFacade facade] toast:NSLocalizedString(@"홈모드 설정을 저장했습니다", @"홈모드 설정을 저장했습니다")];
  119. }];
  120. } failure:^(id errorObject) {
  121. JDErrorModel *error = (JDErrorModel *)errorObject;
  122. [[JDFacade facade] alert:error.errorMessage];
  123. }];
  124. }
  125. #pragma mark - UITableView DataSource & Delegate
  126. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  127. return 3;
  128. }
  129. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  130. NSInteger count = 1;
  131. if (section == 1) {
  132. NSArray *matchedSubItems = [_items matchedArrayInSubArrays:@"subItems" objectName:ksCustomRadioButtonStatus condition:YES];
  133. count = matchedSubItems && matchedSubItems.count ? matchedSubItems.count : 0;
  134. }
  135. return count;
  136. }
  137. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  138. CGFloat height = 0;
  139. if (indexPath.section == 0) {
  140. height = 117.0f;
  141. } else if (indexPath.section == 1) {
  142. height = 81.0f;
  143. } else if (indexPath.section == 2) {
  144. height = 162.0f;
  145. }
  146. return height;
  147. }
  148. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  149. UITableViewCell *cell = nil;
  150. NSInteger section = indexPath.section;
  151. if (section == 0) {
  152. HomeSettingsTitleTableViewCell *tcell = (HomeSettingsTitleTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"TitleCellIdentifier"];
  153. // [tcell.imgvMode sd_setImageWithURL:[NSURL URLWithString:_mode.imageFileName] placeholderImage:nil options:SDWebImageRefreshCached];
  154. tcell.imgvMode.image = [UIImage imageNamed:_mode.imgName];
  155. tcell.lblModeTitle.text = [NSString stringWithFormat:@"%@모드 실행시,\n동작할 장치를 선택하세요.",_mode.modeName];
  156. cell = tcell;
  157. } else if (section == 1) {
  158. //노드 액션이 선택된 노드만 출력함.
  159. NSInteger fcount = indexPath.row; //선택된 액션만 찾기 위한 인덱스
  160. NSArray *matchedSubItems = [_items matchedArrayInSubArrays:@"subItems" objectName:ksCustomRadioButtonStatus condition:YES];
  161. // NSLog(@"SubInfo : %@", matchedSubItems);
  162. ItemSubModel *subItem = nil;
  163. for (subItem in matchedSubItems) {
  164. if (fcount == 0) {
  165. break;
  166. } else {
  167. fcount--;
  168. }
  169. }
  170. for (ItemModel *info in _items) {
  171. if ([info.sourceId isEqualToString:subItem.sourceId]) {
  172. subItem.imageFileName = info.imageFileName;
  173. subItem.imageFileViewName = info.imageFileViewName;
  174. break;
  175. }
  176. }
  177. HomeSettingsDeviceTableViewCell *tcell = (HomeSettingsDeviceTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"DeviceCellIdentifier"];
  178. // [tcell.imgvDevice sd_setImageWithURL:[NSURL URLWithString:subItem.imageFileName] placeholderImage:nil options:SDWebImageRefreshCached];
  179. // TODO : 이미지 표시하기.......
  180. // NSLog(@"SubInfo : %@", subItem);
  181. // NSString *imageName = nil;
  182. // NSArray *tmpArr = [subItem.imageFileViewName explode:@"/"];
  183. // NSArray *tmpArr2 = [tmpArr.lastObject explode:@"."];
  184. NSString *imageName = [CommonUtil getImageNameFormUrlString:subItem.imageFileName];
  185. if (imageName) {
  186. tcell.imgvDevice.image = [UIImage imageNamed:imageName];
  187. }
  188. // NSLog(@"Image Name : %@", subItem.imageFileViewName);
  189. tcell.lblDeviceName.text = subItem.sourceName;
  190. tcell.lblNodeName.text = subItem.sourceSubName;
  191. //노드의 액션 값을 세팅함.
  192. if (subItem.cmdclsValueList && subItem.cmdclsValueList.count) {//커맨드클래스 밸루 리스트가 있을 경우,
  193. CmdClsValueModel *cmdclsValue = [subItem.cmdclsValueList matchedObjectName:ksCustomRadioButtonStatus condition:YES];
  194. tcell.lblActionName.text = cmdclsValue.cmdclsValueMsg;
  195. } else {
  196. tcell.lblActionName.text = subItem.cmdclsValueMsg;
  197. }
  198. if (![tcell.lblActionName.text isEmptyString]) {
  199. [tcell.lblActionName setUnderLine:tcell.lblActionName.text];
  200. }
  201. tcell.lblActionName.value = subItem;
  202. // [tcell.lblActionName setUnderLine:tcell.lblActionName.text];
  203. if (!tcell.lblActionName.touchHandler) {
  204. [tcell.lblActionName addTouchEventHandler:^(id label) {
  205. [self lblActionNameTouched:label];
  206. }];
  207. }
  208. tcell.btnDelete.value = subItem;
  209. if (![tcell.btnDelete actionsForTarget:self forControlEvent:UIControlEventTouchUpInside]) {
  210. [tcell.btnDelete addTarget:self action:@selector(btnDeleteDeviceTouched:) forControlEvents:UIControlEventTouchUpInside];
  211. }
  212. cell = tcell;
  213. } else if (section == 2) {
  214. HomeSettingsAppendTableViewCell *tcell = (HomeSettingsAppendTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"AppendCellIdentifier"];
  215. if (![tcell.btnAppend actionsForTarget:self forControlEvent:UIControlEventTouchUpInside]) {
  216. [tcell.btnAppend addTarget:self action:@selector(btnAppendTouched:) forControlEvents:UIControlEventTouchUpInside];
  217. }
  218. cell = tcell;
  219. }
  220. return cell;
  221. }
  222. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  223. [super tableView:tableView didSelectRowAtIndexPath:indexPath];
  224. if (indexPath.section == 1) {
  225. HomeSettingsDeviceTableViewCell *tcell = (HomeSettingsDeviceTableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
  226. ItemSubModel *subItem = tcell.lblActionName.value;
  227. [self modifyAction:subItem];
  228. }
  229. }
  230. - (void)btnDeleteDeviceTouched:(id)sender {
  231. CustomButton *btnDelete = (CustomButton *)sender;
  232. ItemSubModel *subItem = (ItemSubModel *)btnDelete.value;
  233. [[JDFacade facade] setRadioButtonStatus:@NO object:subItem];
  234. for (CmdClsValueModel *cmdclsValue in subItem.cmdclsValueList) {
  235. cmdclsValue.isSelected = NO;
  236. }
  237. [_tableView reloadData];
  238. }
  239. //장치 선택
  240. - (void)btnAppendTouched:(id)sender {
  241. DeviceSelectPopupView *popup = [[DeviceSelectPopupView alloc] initFromNib];
  242. popup.refDevices = _items;
  243. popup.typeCode = ksItemTypeCodeTrigger;
  244. [popup showWithCompletion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
  245. if (buttonIndex == 0) {//ok
  246. // todo : 수정하기
  247. NSLog(@"selectedDevices : %@", popup.selectedDevices[0]);
  248. DeviceNodePopupView *npopup = [[DeviceNodePopupView alloc] initFromNib];
  249. npopup.refDevice = popup.selectedDevices[0];
  250. npopup.typeCode = ksItemTypeCodeTrigger;
  251. [npopup showWithCompletion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
  252. if (buttonIndex == 0) {//OK
  253. [_tableView reloadData];
  254. }
  255. }];
  256. }
  257. }];
  258. }
  259. - (void)modifyAction:(ItemSubModel *)subItem {
  260. ItemModel *item = [_items objectKey:@"sourceId" eqaulToString:subItem.sourceId];
  261. //노드 선택 팝럽.
  262. DeviceNodePopupView *npopup = [[DeviceNodePopupView alloc] initFromNib];
  263. npopup.refDevice = item;
  264. npopup.typeCode = ksItemTypeCodeTrigger;
  265. npopup.isModifyMode = YES;
  266. [npopup showWithCompletion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
  267. if (buttonIndex == 0) {//OK
  268. [_tableView reloadData];
  269. }
  270. }];
  271. }
  272. - (void)lblActionNameTouched:(id)sender {
  273. CustomLabel *label = (CustomLabel *)sender;
  274. ItemSubModel *subItem = label.value;
  275. [self modifyAction:subItem];
  276. }
  277. #pragma mark - UI Events
  278. - (IBAction)btnCompleteTouched:(id)sender {
  279. //선택된 노드만 출력함.
  280. NSMutableArray *pSubItems = [[NSMutableArray alloc] init];
  281. NSArray *matchedSubItems = [_items matchedArrayInSubArrays:@"subItems" objectName:ksCustomRadioButtonStatus condition:YES];
  282. for (ItemSubModel *pSubItem in matchedSubItems) {
  283. CmdClsValueModel *pCmdClsValue = [pSubItem.cmdclsValueList matchedObjectName:ksCustomRadioButtonStatus condition:YES];
  284. NSDictionary *dSubITem = @{@"source_id": pSubItem.sourceId,
  285. @"source_sub_id": pSubItem.sourceSubId,
  286. @"cmdcls_value": pCmdClsValue.cmdclsValue};
  287. [pSubItems addObject:dSubITem];
  288. }
  289. if (!pSubItems.count) {
  290. [[JDFacade facade] alert:NSLocalizedString(@"실행할 장치를 선택하세요", @"실행할 장치를 선택하세요")];
  291. return;
  292. }
  293. [self requestRegisterModeAction:pSubItems];
  294. }
  295. - (IBAction)btnCancelTouched:(id)sender {
  296. [[self navigationController] popViewControllerAnimated:YES];
  297. // [[self navigationController] popToRootViewControllerAnimated:YES];
  298. // [self dismissViewControllerAnimated:YES completion:nil];
  299. }
  300. #pragma mark - MemoryWarning
  301. - (void)didReceiveMemoryWarning
  302. {
  303. [super didReceiveMemoryWarning];
  304. // Dispose of any resources that can be recreated.
  305. }
  306. @end