HomeModeSettingsViewController.m 13 KB

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