HomeModeSettingsViewController.m 14 KB

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