HomeModeSettingsViewController.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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_SCENE_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. [[RequestHandler handler] sendAsyncPostRequestAPIPath:path parameters:parameter modelClass:[SceneModel class] completion:^(id responseObject) {
  111. [self dismissViewControllerAnimated:YES completion:^{
  112. [[JDFacade facade] toast:NSLocalizedString(@"홈모드 설정을 저장했습니다", @"홈모드 설정을 저장했습니다")];
  113. }];
  114. } failure:^(id errorObject) {
  115. JDErrorModel *error = (JDErrorModel *)errorObject;
  116. [[JDFacade facade] alert:error.errorMessage];
  117. }];
  118. }
  119. #pragma mark - UITableView DataSource & Delegate
  120. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  121. return 3;
  122. }
  123. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  124. NSInteger count = 1;
  125. if (section == 1) {
  126. NSArray *matchedSubItems = [_items matchedArrayInSubArrays:@"subItems" objectName:ksCustomRadioButtonStatus condition:YES];
  127. count = matchedSubItems && matchedSubItems.count ? matchedSubItems.count : 0;
  128. }
  129. return count;
  130. }
  131. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  132. CGFloat height = 0;
  133. if (indexPath.section == 0) {
  134. height = 117.0f;
  135. } else if (indexPath.section == 1) {
  136. height = 81.0f;
  137. } else if (indexPath.section == 2) {
  138. height = 162.0f;
  139. }
  140. return height;
  141. }
  142. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  143. UITableViewCell *cell = nil;
  144. NSInteger section = indexPath.section;
  145. if (section == 0) {
  146. HomeSettingsTitleTableViewCell *tcell = (HomeSettingsTitleTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"TitleCellIdentifier"];
  147. // [tcell.imgvMode sd_setImageWithURL:[NSURL URLWithString:_mode.imageFileName] placeholderImage:nil options:SDWebImageRefreshCached];
  148. tcell.imgvMode.image = [UIImage imageNamed:_mode.imgName];
  149. tcell.lblModeTitle.text = [NSString stringWithFormat:@"%@모드 실행시,\n동작할 장치를 선택하세요.",_mode.modeName];
  150. cell = tcell;
  151. } else if (section == 1) {
  152. //노드 액션이 선택된 노드만 출력함.
  153. NSInteger fcount = indexPath.row; //선택된 액션만 찾기 위한 인덱스
  154. NSArray *matchedSubItems = [_items matchedArrayInSubArrays:@"subItems" objectName:ksCustomRadioButtonStatus condition:YES];
  155. ItemSubModel *subItem = nil;
  156. for (subItem in matchedSubItems) {
  157. if (fcount == 0) {
  158. break;
  159. } else {
  160. fcount--;
  161. }
  162. }
  163. HomeSettingsDeviceTableViewCell *tcell = (HomeSettingsDeviceTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"DeviceCellIdentifier"];
  164. [tcell.imgvDevice sd_setImageWithURL:[NSURL URLWithString:subItem.imageFileName] placeholderImage:nil options:SDWebImageRefreshCached];
  165. tcell.lblDeviceName.text = subItem.sourceName;
  166. tcell.lblNodeName.text = subItem.sourceSubName;
  167. //노드의 액션 값을 세팅함.
  168. if (subItem.cmdclsValueList && subItem.cmdclsValueList.count) {//커맨드클래스 밸루 리스트가 있을 경우,
  169. CmdClsValueModel *cmdclsValue = [subItem.cmdclsValueList matchedObjectName:ksCustomRadioButtonStatus condition:YES];
  170. tcell.lblActionName.text = cmdclsValue.cmdclsValueMsg;
  171. } else {
  172. tcell.lblActionName.text = subItem.cmdclsValueMsg;
  173. }
  174. if (![tcell.lblActionName.text isEmptyString]) {
  175. [tcell.lblActionName setUnderLine:tcell.lblActionName.text];
  176. }
  177. tcell.lblActionName.value = subItem;
  178. // [tcell.lblActionName setUnderLine:tcell.lblActionName.text];
  179. if (!tcell.lblActionName.touchHandler) {
  180. [tcell.lblActionName addTouchEventHandler:^(id label) {
  181. [self lblActionNameTouched:label];
  182. }];
  183. }
  184. tcell.btnDelete.value = subItem;
  185. if (![tcell.btnDelete actionsForTarget:self forControlEvent:UIControlEventTouchUpInside]) {
  186. [tcell.btnDelete addTarget:self action:@selector(btnDeleteDeviceTouched:) forControlEvents:UIControlEventTouchUpInside];
  187. }
  188. cell = tcell;
  189. } else if (section == 2) {
  190. HomeSettingsAppendTableViewCell *tcell = (HomeSettingsAppendTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"AppendCellIdentifier"];
  191. if (![tcell.btnAppend actionsForTarget:self forControlEvent:UIControlEventTouchUpInside]) {
  192. [tcell.btnAppend addTarget:self action:@selector(btnAppendTouched:) forControlEvents:UIControlEventTouchUpInside];
  193. }
  194. cell = tcell;
  195. }
  196. return cell;
  197. }
  198. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  199. [super tableView:tableView didSelectRowAtIndexPath:indexPath];
  200. if (indexPath.section == 1) {
  201. HomeSettingsDeviceTableViewCell *tcell = (HomeSettingsDeviceTableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
  202. ItemSubModel *subItem = tcell.lblActionName.value;
  203. [self modifyAction:subItem];
  204. }
  205. }
  206. - (void)btnDeleteDeviceTouched:(id)sender {
  207. CustomButton *btnDelete = (CustomButton *)sender;
  208. ItemSubModel *subItem = (ItemSubModel *)btnDelete.value;
  209. [[JDFacade facade] setRadioButtonStatus:@NO object:subItem];
  210. for (CmdClsValueModel *cmdclsValue in subItem.cmdclsValueList) {
  211. cmdclsValue.isSelected = NO;
  212. }
  213. [_tableView reloadData];
  214. }
  215. //장치 선택
  216. - (void)btnAppendTouched:(id)sender {
  217. DeviceSelectPopupView *popup = [[DeviceSelectPopupView alloc] initFromNib];
  218. popup.refDevices = _items;
  219. popup.typeCode = ksItemTypeCodeTrigger;
  220. [popup showWithCompletion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
  221. if (buttonIndex == 0) {//ok
  222. // todo : 수정하기
  223. NSLog(@"selectedDevices : %@", popup.selectedDevices[0]);
  224. DeviceNodePopupView *npopup = [[DeviceNodePopupView alloc] initFromNib];
  225. npopup.refDevice = popup.selectedDevices[0];
  226. npopup.typeCode = ksItemTypeCodeTrigger;
  227. [npopup showWithCompletion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
  228. if (buttonIndex == 0) {//OK
  229. [_tableView reloadData];
  230. }
  231. }];
  232. }
  233. }];
  234. }
  235. - (void)modifyAction:(ItemSubModel *)subItem {
  236. ItemModel *item = [_items objectKey:@"sourceId" eqaulToString:subItem.sourceId];
  237. //노드 선택 팝럽.
  238. DeviceNodePopupView *npopup = [[DeviceNodePopupView alloc] initFromNib];
  239. npopup.refDevice = item;
  240. npopup.typeCode = ksItemTypeCodeTrigger;
  241. npopup.isModifyMode = YES;
  242. [npopup showWithCompletion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
  243. if (buttonIndex == 0) {//OK
  244. [_tableView reloadData];
  245. }
  246. }];
  247. }
  248. - (void)lblActionNameTouched:(id)sender {
  249. CustomLabel *label = (CustomLabel *)sender;
  250. ItemSubModel *subItem = label.value;
  251. [self modifyAction:subItem];
  252. }
  253. #pragma mark - UI Events
  254. - (IBAction)btnCompleteTouched:(id)sender {
  255. //선택된 노드만 출력함.
  256. NSMutableArray *pSubItems = [[NSMutableArray alloc] init];
  257. NSArray *matchedSubItems = [_items matchedArrayInSubArrays:@"subItems" objectName:ksCustomRadioButtonStatus condition:YES];
  258. for (ItemSubModel *pSubItem in matchedSubItems) {
  259. CmdClsValueModel *pCmdClsValue = [pSubItem.cmdclsValueList matchedObjectName:ksCustomRadioButtonStatus condition:YES];
  260. NSDictionary *dSubITem = @{@"source_id": pSubItem.sourceId,
  261. @"source_sub_id": pSubItem.sourceSubId,
  262. @"cmdcls_value": pCmdClsValue.cmdclsValue};
  263. [pSubItems addObject:dSubITem];
  264. }
  265. if (!pSubItems.count) {
  266. [[JDFacade facade] alert:NSLocalizedString(@"실행할 장치를 선택하세요", @"실행할 장치를 선택하세요")];
  267. return;
  268. }
  269. [self requestRegisterModeAction:pSubItems];
  270. }
  271. - (IBAction)btnCancelTouched:(id)sender {
  272. [[self navigationController] popViewControllerAnimated:YES];
  273. // [[self navigationController] popToRootViewControllerAnimated:YES];
  274. // [self dismissViewControllerAnimated:YES completion:nil];
  275. }
  276. #pragma mark - MemoryWarning
  277. - (void)didReceiveMemoryWarning
  278. {
  279. [super didReceiveMemoryWarning];
  280. // Dispose of any resources that can be recreated.
  281. }
  282. @end