ScenesDetailViewController.m 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. //
  2. // ScenesDetailViewController.m
  3. // kneet
  4. //
  5. // Created by Jason Lee on 4/3/15.
  6. // Copyright (c) 2015 ntels. All rights reserved.
  7. //
  8. #import "JDObject.h"
  9. #import "RequestHandler.h"
  10. #import "SceneModel.h"
  11. #import "CustomLabel.h"
  12. #import "CustomImageView.h"
  13. #import "CustomAlertView.h"
  14. #import "CustomTextView.h"
  15. #import "CustomButton.h"
  16. #import "RulesDetailViewController.h"
  17. #import "ScenesDetailViewController.h"
  18. #import "ScenesRegisterViewController.h"
  19. #import "RulesRegisterViewController.h"
  20. @implementation SceneDetailActivationViewCell
  21. - (void)awakeFromNib {
  22. _lblRunTitle.text = NSLocalizedString(@"지금 실행", @"지금 실행");
  23. }
  24. @end
  25. @implementation ScenesDetailDisableViewCell
  26. - (void)awakeFromNib {
  27. _lblDesc.text = NSLocalizedString(@"일부 장치가 삭제되어 비활성 되었습니다\n편집 후 다시 사용해주세요", @"일부 장치가 삭제되어 비활성 되었습니다\n편집 후 다시 사용해주세요");
  28. }
  29. @end
  30. @interface ScenesDetailViewController () <UITableViewDataSource, UITableViewDelegate> {
  31. SceneDetailModel *_sceneDetail;
  32. NSInteger _actionCount;
  33. BOOL _isNotFirstLoading;
  34. UIImage *_iconAction;
  35. NSMutableArray *_arrayForHeader;
  36. UIImage *_runImageNormal, *_runImageDisable;
  37. }
  38. @end
  39. #pragma mark - Class Definition
  40. @implementation ScenesDetailViewController
  41. - (void)viewDidLoad {
  42. [super viewDidLoad];
  43. // Do any additional setup after loading the view.
  44. [self initUI];
  45. [self prepareViewDidLoad];
  46. }
  47. - (void)initUI {
  48. _tableView.dataSource = self;
  49. _tableView.delegate = self;
  50. _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  51. _tableView.backgroundColor = [UIColor clearColor];
  52. _tableView.tableFooterView = [[UIView alloc] init]; //this call table events;
  53. _iconAction = [UIImage imageNamed:@"tp_01_img_rule_rbox_icon_action"];
  54. if ([JDFacade facade].loginUser.level <= 10) {//심플멤버일 경우,
  55. _constraintActionTabHeight.constant = 0;
  56. }
  57. _runImageNormal = [UIImage imageNamed:@"tp_01_img_scene_btn_play"];
  58. _runImageDisable = [UIImage imageNamed:@"tp_01_img_scene_btn_play_disable"];
  59. [_btnEdit setTitle:NSLocalizedString(@"편집", @"편집") forState:UIControlStateNormal];
  60. [_btnDelete setTitle:NSLocalizedString(@"삭제", @"삭제") forState:UIControlStateNormal];
  61. }
  62. - (void)prepareViewDidLoad {
  63. }
  64. - (void)viewWillAppear:(BOOL)animated {
  65. [super viewWillAppear:animated];
  66. self.title = NSLocalizedString(@"멀티 제어 상세",nil);
  67. //룰 상세 조회
  68. [self requestSceneDetail];
  69. }
  70. #pragma mark - Main Logic
  71. - (void)requestSceneDetail {
  72. //parameters
  73. NSString *path = [NSString stringWithFormat:API_GET_SCENE_DETAIL, _scene.homegrpSceneId];
  74. [[RequestHandler handler] sendAsyncGetRequestAPIPath:path parameters:nil modelClass:[SceneDetailModel class] completion:^(id responseObject) {
  75. if (!responseObject) {//응답결과가 잘못되었거나 없을 경우,
  76. return;
  77. }
  78. _sceneDetail = (SceneDetailModel *) responseObject;
  79. if (_sceneDetail) {//API 성공 ,
  80. [self setSceneDetailContents];
  81. }
  82. } failure:^(id errorObject) {
  83. JDErrorModel *error = (JDErrorModel *)errorObject;
  84. [[JDFacade facade] alert:error.errorMessage];
  85. }];
  86. }
  87. - (void)setSceneDetailContents {
  88. _actionCount = _sceneDetail.actions && _sceneDetail.actions.count ? _sceneDetail.actions.count : 0;
  89. [RulesDetailViewController cloneSubItemsForItemSubType:_sceneDetail.actions conditions:nil];
  90. [_tableView reloadData];
  91. }
  92. //씬 실행
  93. - (void)requestExecuteScene {
  94. //parameters
  95. NSString *path = [NSString stringWithFormat:API_POST_SCENE_EXECUTE, _sceneDetail.homegrpSceneId];
  96. [[RequestHandler handler] sendAsyncPostRequestAPIPath:path parameters:nil modelClass:[JDJSONModel class] completion:^(id responseObject) {
  97. if (!responseObject) {//응답결과가 잘못되었거나 없을 경우,
  98. return;
  99. }
  100. JDJSONModel *result = (JDJSONModel *) responseObject;
  101. if (result) {//API 성공 ,
  102. if (result.msg && ![result.msg isEmptyString]) {
  103. [[JDFacade facade] alert:result.msg];
  104. } else {
  105. [[JDFacade facade] toast:NSLocalizedString(@"실행되었습니다", @"실행되었습니다")];
  106. }
  107. }
  108. } failure:^(id errorObject) {
  109. JDErrorModel *error = (JDErrorModel *)errorObject;
  110. [[JDFacade facade] alert:error.errorMessage];
  111. }];
  112. }
  113. //씬 삭제
  114. - (void)requestDeleteScene {
  115. NSString *path = [NSString stringWithFormat:API_DELETE_SCENE, _sceneDetail.homegrpSceneId];
  116. [[RequestHandler handler] sendAsyncRequestAPIPath:path method:ksHTTPRequestDELETE parameters:nil modelClass:[JDJSONModel class] showLoadingView:@(YES) completion:^(id responseObject) {
  117. [[JDFacade facade] toast:NSLocalizedString(@"삭제되었습니다", @"삭제되었습니다")];
  118. [[JDFacade facade] gotoWishMenu:KNMenuIdScenes];
  119. } failure:^(id errorObject) {
  120. JDErrorModel *error = (JDErrorModel *)errorObject;
  121. [[JDFacade facade] alert:error.errorMessage];
  122. }];
  123. }
  124. #pragma mark - UITableView DataSource & Delegate
  125. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  126. return 3 + ([_sceneDetail.deleteCnt boolValue] ? 1 : 0);
  127. }
  128. - (NSString *)titleForSectionHeader:(NSInteger)section {
  129. NSString *title = nil;
  130. switch (section) {
  131. case 1:
  132. title = NSLocalizedString(@"무엇을 실행할까요?", @"무엇을 실행할까요?");
  133. break;
  134. }
  135. return title;
  136. }
  137. - (UIImage *)imageForSection:(NSInteger)section {
  138. UIImage *image = nil;
  139. switch (section) {
  140. case 1:
  141. image = _iconAction;
  142. break;
  143. }
  144. return image;
  145. }
  146. - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
  147. UIView *view = _arrayForHeader.count > section ? _arrayForHeader[section] : nil;
  148. if (!view) {
  149. RulesCreateHeaderTableViewCell *hcell = (RulesCreateHeaderTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"HeaderCellIdentifier"];
  150. CGFloat height = [self tableView:tableView heightForHeaderInSection:section];
  151. hcell.frame = CGRectMake(0, 0, IPHONE_WIDTH, height);
  152. if (section == 0) {//titleCell
  153. hcell.imgvTop.hidden = YES;
  154. hcell.constraintHeaderTitleTop.constant = 10;
  155. }
  156. view = [[UIView alloc] initWithFrame:hcell.frame];
  157. [view addSubview:hcell];
  158. hcell.lblHeaderTitle.text = [self titleForSectionHeader:section];
  159. [_arrayForHeader insertObject:view atIndex:section];
  160. }
  161. return view;
  162. }
  163. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
  164. if (section == 1) {
  165. return 72.0f;
  166. }
  167. return 0.01f;
  168. }
  169. - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
  170. return 0.01f;
  171. }
  172. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  173. NSInteger count = 1;
  174. if (section == 1) {//actions
  175. count = _actionCount;
  176. }
  177. return count;
  178. }
  179. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  180. CGFloat height = 0;
  181. if (indexPath.section == 0) {//title
  182. height = 80;
  183. } else if (indexPath.section == 1) {//actions
  184. //calcualate subItemsText
  185. NSArray *items = _sceneDetail.actions;
  186. ItemModel *item = items[indexPath.row];
  187. CGFloat adjustHeight = [RulesRegisterViewController heightForSubItems:item isTrigger:NO];
  188. adjustHeight = adjustHeight > 0 ? adjustHeight + 15 + 20 : adjustHeight;
  189. height = 60 + adjustHeight;
  190. } else if (indexPath.section == 2) {//activation
  191. height = 90.0f;
  192. } else if (indexPath.section == 3) {//disable
  193. height = 52.0f;
  194. }
  195. return height;
  196. }
  197. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  198. UITableViewCell *cell = nil;
  199. if (indexPath.section == 0) {//title
  200. RulesDetailTitleViewCell *tcell = [tableView dequeueReusableCellWithIdentifier:@"TitleCellIdentifier"];
  201. if (tcell == nil) {
  202. tcell = [[RulesDetailTitleViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"TitleCellIdentifier"];
  203. }
  204. tcell.lblRuleTitle.text = _sceneDetail.sceneName;
  205. tcell.lblRuleOwner.text = [NSString stringWithFormat:@"by %@", [JDFacade facade].loginUser.nickname];
  206. cell = tcell;
  207. } else if (indexPath.section == 2) {//activation
  208. SceneDetailActivationViewCell *tcell = [tableView dequeueReusableCellWithIdentifier:@"ActivationCellIdentifier"];
  209. if (tcell == nil) {
  210. tcell = [[SceneDetailActivationViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ActivationCellIdentifier"];
  211. }
  212. //활성/비활성 이미지 적용
  213. UIImage *nimage = ![_sceneDetail.deleteCnt boolValue] ? _runImageNormal : _runImageDisable;
  214. [tcell.btnRun setImage:nimage forState:UIControlStateNormal];
  215. [tcell.btnRun addTarget:self action:@selector(btnRunSceneTouched:) forControlEvents:UIControlEventTouchUpInside];
  216. cell = tcell;
  217. } else if (indexPath.section == 3) {//disable
  218. cell = [tableView dequeueReusableCellWithIdentifier:@"DisableCellIdentifier"];
  219. } else {//actions
  220. RulesDetailTableViewCell *tcell = [tableView dequeueReusableCellWithIdentifier:@"CellIdentifier"];
  221. if (tcell == nil) {
  222. tcell = [[RulesDetailTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CellIdentifier"];
  223. }
  224. NSArray *items = nil;
  225. if (indexPath.section == 1) {
  226. items = _sceneDetail.actions;
  227. }
  228. ItemModel *item = items[indexPath.row];
  229. [RulesRegisterViewController fillCell:tcell item:item isTrigger:NO];
  230. cell = tcell;
  231. }
  232. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  233. return cell;
  234. }
  235. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  236. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  237. }
  238. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
  239. // Remove seperator inset
  240. if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
  241. [cell setSeparatorInset:UIEdgeInsetsZero];
  242. }
  243. // Prevent the cell from inheriting the Table View's margin settings
  244. if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
  245. [cell setPreservesSuperviewLayoutMargins:NO];
  246. }
  247. // Explictly set your cell's layout margins
  248. if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
  249. [cell setLayoutMargins:UIEdgeInsetsZero];
  250. }
  251. }
  252. #pragma mark - UI Events
  253. - (void)btnRunSceneTouched:(id)sender {
  254. if ([_sceneDetail.deleteCnt boolValue]) {
  255. [[JDFacade facade] toast:NSLocalizedString(@"일부 장치가 삭제되어 비활성 되었습니다\n편집 후 다시 사용해주세요", @"일부 장치가 삭제되어 비활성 되었습니다\n편집 후 다시 사용해주세요")];
  256. return;
  257. }
  258. [self requestExecuteScene];
  259. }
  260. - (void)gotoModifyScene {
  261. ScenesRegisterViewController *vc = (ScenesRegisterViewController *)[CommonUtil instantiateViewControllerWithIdentifier:@"ScenesRegisterViewController" storyboardName:@"Scenes"];
  262. vc.tmpSceneDetail = _sceneDetail;
  263. vc.title = NSLocalizedString(@"멀티 제어 상세",nil);
  264. [self.navigationController pushViewController:vc animated:YES];
  265. }
  266. - (IBAction)btnEditTouched:(id)sender {
  267. if ([_sceneDetail.deleteCnt boolValue]) {//삭제된 장치가 있을 경우,
  268. NSMutableString *deletedDevices = [[NSMutableString alloc] init];
  269. for (ItemModel *item in _sceneDetail.actions) {
  270. if ([item.itemSubTypeCode isEqualToString:ksItemSubTypeCodeDevice]) {//디바이스일 경우,
  271. for (PredefinedDeviceModel *pdevice in item.predDevices) {
  272. if ([pdevice.deleteYn boolValue]) {//삭제된 장치를 추가
  273. NSString *prefix = [deletedDevices isEmptyString] ? @"" : @", ";
  274. [deletedDevices appendFormat:@"%@%@", prefix, pdevice.deviceName];
  275. }
  276. }
  277. }
  278. }
  279. NSString *msg = [NSString stringWithFormat:NSLocalizedString(@"삭제된 장치를 정리 후 진행합니다\n\n%@", @"삭제된 장치를 정리 후 진행합니다\n\n%@"), deletedDevices];
  280. CustomAlertView *alert = [[CustomAlertView alloc] initWithTitle:NSLocalizedString(@"알림", @"알림") message:msg delegate:nil OKButtonTitle:NSLocalizedString(@"확인", @"확인") cancelButtonTitle:NSLocalizedString(@"취소", @"취소")];
  281. [alert showWithCompletion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
  282. if (buttonIndex == 0) {//OK
  283. [self gotoModifyScene];
  284. }
  285. }];
  286. return;
  287. }
  288. [self gotoModifyScene];
  289. }
  290. - (IBAction)btnDeleteTouched:(id)sender {
  291. NSString *messge = NSLocalizedString(@"삭제하시겠습니까?", @"삭제하시겠습니까?");
  292. CustomAlertView *alert = [[CustomAlertView alloc] initWithTitle:NSLocalizedString(@"알림", @"알림") message:messge delegate:self OKButtonTitle:NSLocalizedString(@"삭제", @"삭제") cancelButtonTitle:NSLocalizedString(@"취소", @"취소")];
  293. [alert showWithCompletion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
  294. if (buttonIndex == 0) {
  295. [self requestDeleteScene];
  296. }
  297. }];
  298. }
  299. #pragma mark - MemoryWarning
  300. - (void)didReceiveMemoryWarning
  301. {
  302. [super didReceiveMemoryWarning];
  303. // Dispose of any resources that can be recreated.
  304. }
  305. @end