ThingsDetailViewController.m 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  1. //
  2. // ThingsDetailViewController.m
  3. // kneet
  4. //
  5. // Created by Jason Lee on 3/16/15.
  6. // Copyright (c) 2015 ntels. All rights reserved.
  7. //
  8. #import "RequestHandler.h"
  9. #import "DeviceModel.h"
  10. #import "CustomImageView.h"
  11. #import "CustomLabel.h"
  12. #import "CustomTextField.h"
  13. #import "CustomButton.h"
  14. #import "UIImageView+WebCache.h"
  15. #import "CommandClassControlDetailView.h"
  16. #import "ThingsModifyViewController.h"
  17. #import "ThingsInfoViewController.h"
  18. #import "ThingsDetailViewController.h"
  19. #import "ValidateUtil.h"
  20. #import "ThingsViewController.h"
  21. #import "CustomAlertView.h"
  22. #import "ModifySecureKeyPopupView.h"
  23. #import "ModifyStreamDataPopupView.h"
  24. @implementation ThingsDetailImageViewCell
  25. - (void)awakeFromNib {
  26. self.backgroundColor = [UIColor clearColor];
  27. self.selectionStyle = UITableViewCellSelectionStyleNone;
  28. }
  29. @end
  30. @implementation ThingsDetailControlViewCell
  31. - (void)awakeFromNib {
  32. self.backgroundColor = [UIColor clearColor];
  33. self.selectionStyle = UITableViewCellSelectionStyleNone;
  34. }
  35. @end
  36. @interface ThingsDetailViewController () <UITableViewDataSource, UITableViewDelegate> {
  37. DeviceDetailModel *_deviceDetail;
  38. ThingsModifyViewController *_mvc;
  39. ThingsInfoViewController *_ivc;
  40. CGFloat _containerBottom;
  41. BOOL _isNotFirstLoading;
  42. CustomTextField2 *_txtDeviceName;
  43. CustomImageView *_imgvDevice;
  44. ModifyStreamDataPopupView *_dpopup;
  45. ModifySecureKeyPopupView *_kpopup;
  46. }
  47. @end
  48. #pragma mark - Class Definition
  49. @implementation ThingsDetailViewController
  50. - (void)viewDidLoad {
  51. [super viewDidLoad];
  52. // Do any additional setup after loading the view.
  53. [self initUI];
  54. [self prepareViewDidLoad];
  55. }
  56. - (void)viewWillAppear:(BOOL)animated {
  57. [super viewWillAppear:animated];
  58. self.title = NSLocalizedString(@"내 장치 상세",nil);
  59. }
  60. - (void)initUI {
  61. [self generateOptionButton];
  62. //initialize tableView
  63. _tableView.dataSource = self;
  64. _tableView.delegate = self;
  65. _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  66. _tableView.backgroundColor = [UIColor clearColor];
  67. _tableView.tableFooterView = [[UIView alloc] init]; //this call table events;
  68. _txtDeviceName.keyboardType = UIKeyboardTypeDefault;
  69. _txtDeviceName.returnKeyType = UIReturnKeyDone;
  70. }
  71. - (void)setThingsDetailPopoverOptions {
  72. //set Popover Contents
  73. __weak typeof(self) weakSelf = self;
  74. _popooverOptionArray = [[NSMutableArray alloc] init];
  75. [_popooverOptionArray addObject:@{@"menuName" : NSLocalizedString(@"새로 고침",nil),
  76. @"iconName": @"tp_01_img_bg_morepopup_icon_refresh",
  77. @"target": weakSelf,
  78. @"selector": [NSValue valueWithPointer:@selector(requestDeviceDetails)]}];
  79. [_popooverOptionArray addObject:@{@"menuName" : NSLocalizedString(@"장치 정보 보기",nil),
  80. @"iconName": @"tp_01_img_bg_morepopup_icon_detail",
  81. @"target": weakSelf,
  82. @"selector": [NSValue valueWithPointer:@selector(showThingsDetailInfo)]}];
  83. if ([JDFacade facade].loginHomeGroup.level > 10) {
  84. [_popooverOptionArray addObject:@{@"menuName" : NSLocalizedString(@"이름/아이콘 변경",nil),
  85. @"iconName": @"tp_01_img_bg_morepopup_icon_edit",
  86. @"target": weakSelf,
  87. @"selector": [NSValue valueWithPointer:@selector(modifyThingsName)]}];
  88. if ([_deviceDetail.deviceProtocolType isEqualToString:@"FOSCAM_CAMERA_CGI"]) {
  89. [_popooverOptionArray addObject:@{@"menuName" : NSLocalizedString(@"연결정보 변경", @"연결정보 변경"),
  90. @"iconName": @"tp_01_img_bg_morepopup_icon_edit",
  91. @"target": weakSelf,
  92. @"selector": [NSValue valueWithPointer:@selector(modifyStreamData)]}];
  93. }
  94. if (_deviceDetail.activationCode && ![_deviceDetail.activationCode isEmptyString]){//device_type = controller_primary
  95. [_popooverOptionArray addObject:@{@"menuName" : NSLocalizedString(@"장치등록 보안키 변경", @"장치등록 보안키 변경"),
  96. @"iconName": @"tp_01_img_bg_morepopup_icon_edit",
  97. @"target": weakSelf,
  98. @"selector": [NSValue valueWithPointer:@selector(modifySecureKey)]}];
  99. }
  100. if ([_deviceDetail.deviceProfileId isEqualToString:ksDeviceProfileIdUnknown]) {//장치가 언노운일 경우,
  101. [_popooverOptionArray addObject:@{@"menuName" : NSLocalizedString(@"장치유형 선택", @"장치유형 선택"),
  102. @"iconName": @"tp_01_img_bg_morepopup_icon_dtype",
  103. @"target": weakSelf,
  104. @"selector": [NSValue valueWithPointer:@selector(modifyDeviceType)]}];
  105. }
  106. //FIXME : for demo
  107. // if ([_deviceDetail.removableYn boolValue]) {//삭제 가능일 경우,
  108. if ([_deviceDetail.deviceProfileId isEqualToString:@"20028"] || [_deviceDetail.deviceProfileId isEqualToString:@"20029"]) {
  109. [_popooverOptionArray addObject:@{@"menuName" : NSLocalizedString(@"장치 삭제",nil),
  110. @"iconName": @"tp_01_img_bg_morepopup_icon_del",
  111. @"target": weakSelf,
  112. @"selector": [NSValue valueWithPointer:@selector(removeThings)]}];
  113. }
  114. }
  115. }
  116. - (void)prepareViewDidLoad {
  117. [self performSelector:@selector(requestDeviceDetails) withObject:nil afterDelay:0.0f];
  118. }
  119. #pragma mark - Main Logic
  120. - (void)requestDeviceDetails {
  121. NSString *path = [NSString stringWithFormat:API_GET_DEVICE_DETAIL, _device.deviceId];
  122. [[RequestHandler handler] sendAsyncGetRequestAPIPath:path parameters:nil modelClass:[DeviceDetailModel class] completion:^(id responseObject) {
  123. if (!responseObject) {//응답결과가 잘못되었거나 없을 경우,
  124. return;
  125. }
  126. _deviceDetail = (DeviceDetailModel *)responseObject;
  127. if (_deviceDetail) {//API 성공 ,
  128. [self resetOptions]; //옵션 메뉴 초기화
  129. [self setThingsDetailPopoverOptions];
  130. [self setDeviceContents];
  131. }
  132. } failure:^(id errorObject) {
  133. JDErrorModel *error = (JDErrorModel *)errorObject;
  134. [[JDFacade facade] alert:error.errorMessage];
  135. }];
  136. }
  137. - (void)setDeviceContents {
  138. [_tableView reloadData];
  139. }
  140. - (void)requestDeviceUpdate {
  141. //parameters
  142. NSDictionary *parameter = @{@"image_sequence": _mvc && _mvc.selectedIcon ? _mvc.selectedIcon.imageSequence : ksEmptyString,
  143. @"device_name": _txtDeviceName.text};
  144. NSString *path = [NSString stringWithFormat:API_GET_DEVICE_UPDATE, _deviceDetail.deviceId];
  145. [[RequestHandler handler] sendAsyncPostRequestAPIPath:path parameters:parameter modelClass:[JDJSONModel class] completion:^(id responseObject) {
  146. _device.deviceName = _deviceDetail.deviceName = _txtDeviceName.text;
  147. _device.imageFileName = _deviceDetail.imageFileName = _mvc && _mvc.selectedIcon ? _mvc.selectedIcon.imageFileName : _device.imageFileName;
  148. [self closeChildViewController:_mvc completionHandler:nil];
  149. [_tableView reloadData];
  150. //리스트를 다시 로드함.
  151. ThingsViewController *vc = [[JDFacade facade] viewControllerOnNaviationController:[ThingsViewController class]];
  152. [vc.tableView reloadData];
  153. } failure:^(id errorObject) {
  154. JDErrorModel *error = (JDErrorModel *)errorObject;
  155. [[JDFacade facade] alert:error.errorMessage];
  156. }];
  157. }
  158. - (void)requestDeleteDevice {
  159. //parameters
  160. NSString *path = [NSString stringWithFormat:API_DELETE_DEVICE, _deviceDetail.deviceId];
  161. [[RequestHandler handler] sendAsyncRequestAPIPath:path method:ksHTTPRequestDELETE parameters:nil modelClass:[JDJSONModel class] showLoadingView:YES completion:^(id responseObject) {
  162. [[JDFacade facade] toast:NSLocalizedString(@"장치가 삭제되었습니다", @"장치가 삭제되었습니다")];
  163. if ([JDFacade facade].loginHomeGroup.deviceId && ![[JDFacade facade].loginHomeGroup.deviceId isEmptyString]) {
  164. if ([[JDFacade facade].loginHomeGroup.deviceId isEqualToString:_deviceDetail.deviceId]) {//현재 홈의 스마튼폰일 경우,
  165. [JDFacade facade].loginHomeGroup.deviceId = nil;
  166. [JDFacade facade].loginHomeGroup.deviceAuthorization = nil;
  167. [JDFacade facade].loginHomeGroup.deviceKey = nil;
  168. }
  169. }
  170. //리스트를 다시 로드함.
  171. ThingsViewController *vc = [[JDFacade facade] viewControllerOnNaviationController:[ThingsViewController class]];
  172. [vc prepareViewDidLoad];
  173. [self.navigationController popToRootViewControllerAnimated:YES];
  174. } failure:^(id errorObject) {
  175. JDErrorModel *error = (JDErrorModel *)errorObject;
  176. [[JDFacade facade] alert:error.errorMessage];
  177. }];
  178. }
  179. //장치등록 보안키 변경
  180. - (void)requestModifySecureKey {
  181. //parameters
  182. NSDictionary *parameter = @{@"device_id": _deviceDetail.deviceId,
  183. @"change_device_password": _kpopup.txtSecureCode.text};
  184. NSString *path = [NSString stringWithFormat:API_POST_PARTNER_PASSWD];
  185. [[RequestHandler handler] sendAsyncPostRequestAPIPath:path parameters:parameter modelClass:[JDJSONModel class] completion:^(id responseObject) {
  186. [[JDFacade facade] toast:NSLocalizedString(@"변경되었습니다", @"변경되었습니다")];
  187. } failure:^(id errorObject) {
  188. JDErrorModel *error = (JDErrorModel *)errorObject;
  189. [[JDFacade facade] alert:error.errorMessage];
  190. }];
  191. }
  192. //연결정보 변경
  193. - (void)requestModifyStreamData {
  194. //parameters
  195. NSDictionary *parameter = @{@"camera_id": _dpopup.txtFoscamId.text,
  196. @"camera_password": _dpopup.txtFoscamPwd.text,
  197. @"url": _dpopup.txtFoscamURL.text,
  198. @"port": _dpopup.txtFoscamPort.text};
  199. NSString *path = [NSString stringWithFormat:API_POST_CAMERA_UPDATE, _deviceDetail.deviceId];
  200. [[RequestHandler handler] sendAsyncPostRequestAPIPath:path parameters:parameter modelClass:[JDJSONModel class] completion:^(id responseObject) {
  201. _deviceDetail.cameraId = _dpopup.txtFoscamId.text;
  202. _deviceDetail.cameraPassword = _dpopup.txtFoscamPwd.text;
  203. _deviceDetail.ip = _dpopup.txtFoscamURL.text;
  204. _deviceDetail.port = _dpopup.txtFoscamPort.text;
  205. [[JDFacade facade] toast:NSLocalizedString(@"변경되었습니다", @"변경되었습니다")];
  206. } failure:^(id errorObject) {
  207. JDErrorModel *error = (JDErrorModel *)errorObject;
  208. [[JDFacade facade] alert:error.errorMessage];
  209. }];
  210. }
  211. #pragma mark - UITableView DataSource & Delegate
  212. - (ThingsDetailImageViewCell *)imageCell {
  213. return (ThingsDetailImageViewCell *)[_tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
  214. }
  215. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  216. return 1;
  217. }
  218. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  219. return _deviceDetail.nodes ? _deviceDetail.nodes.count + 1 : 0; //append for ImageCell
  220. }
  221. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  222. CGFloat height = 0.0f, titlePadding = 0.0f;
  223. if (indexPath.row == 0) {
  224. height = 152.0f;
  225. } else {
  226. NodeModel *node = _deviceDetail.nodes[indexPath.row-1];
  227. //다원플러그일 경우,
  228. BOOL isControlDAWONPlug = [_deviceDetail.deviceMfId isEqualToString:@"DAWON"] && [_deviceDetail.deviceProfileId isEqualToString:@"20004"];
  229. CommandClassControlView *controlView = nil;
  230. if (node.cmdclsType == CmdClsTypeMeterCurrent && isControlDAWONPlug) {
  231. controlView = [CommandClassControlDetailView viewForCommandClass:CmdClsTypeMeterCurrentForDAWON];
  232. } else if (node.cmdclsType == CmdClsTypeMeterTotal && isControlDAWONPlug) {
  233. controlView = [CommandClassControlDetailView viewForCommandClass:CmdClsTypeMeterTotalForDAWON];
  234. titlePadding = indexPath.row < _deviceDetail.nodes.count ? 25 : 50;
  235. } else {
  236. controlView = [CommandClassControlDetailView viewForCommandClass:node.cmdclsType];
  237. titlePadding = 50.0f;
  238. }
  239. controlView.node = node;
  240. height = titlePadding + [controlView sizeForIntrinsic].height; //title label + margin
  241. }
  242. return height;
  243. }
  244. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  245. NSString *cellIdentifier = indexPath.row == 0 ? @"ImageCellIdentifier" : @"CellIdentifier";
  246. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
  247. if (cell == nil) {
  248. if (indexPath.row == 0) {
  249. ThingsDetailImageViewCell *icell = [[ThingsDetailImageViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
  250. cell = icell;
  251. } else {
  252. ThingsDetailControlViewCell *cCell = [[ThingsDetailControlViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
  253. cell = cCell;
  254. }
  255. }
  256. if (indexPath.row == 0) {//디바이스 이미지 및 네임.
  257. ThingsDetailImageViewCell *icell = (ThingsDetailImageViewCell *)cell;
  258. icell.lblDeviceName.text = _deviceDetail.deviceName;
  259. icell.txtDeviceName.text = _deviceDetail.deviceName;
  260. [icell.imgvDevice sd_setImageWithURL:[NSURL URLWithString:_deviceDetail.imageFileName] placeholderImage:nil];
  261. if (_deviceDetail.networkYn && ![_deviceDetail.networkYn isEqualToString:@"none"] && ![_deviceDetail.networkYn boolValue]) {
  262. if (!icell.imgvDevice.subviews.count) {
  263. UIImageView *imgvOffline = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tp_01_img_things_offline_cover"]];
  264. CGRect ir = icell.imgvDevice.frame;
  265. ir.origin = (CGPoint){.x=0, .y=0};
  266. imgvOffline.frame = ir;
  267. [icell.imgvDevice addSubview:imgvOffline];
  268. } else {
  269. [icell.imgvDevice bringSubviewToFront:icell.imgvDevice.subviews.firstObject];
  270. }
  271. } else {
  272. if (icell.imgvDevice.subviews.count) {
  273. [icell.imgvDevice.subviews.firstObject removeFromSuperview];
  274. }
  275. }
  276. if (!_txtDeviceName) {
  277. _txtDeviceName = icell.txtDeviceName;
  278. }
  279. if (!_imgvDevice) {
  280. _imgvDevice = icell.imgvDevice;
  281. }
  282. icell.txtDeviceName.alpha = 0.0f;
  283. } else {
  284. NodeModel *node = _deviceDetail.nodes[indexPath.row-1];
  285. ThingsDetailControlViewCell *cCell = (ThingsDetailControlViewCell *)cell;
  286. //뷰를 초기화함.
  287. [[cCell.controlContainerView subviews] enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
  288. UIView *subview = (UIView *)obj;
  289. [subview removeFromSuperview];
  290. }];
  291. cCell.lblNodeTitle.text = node.nodeName;
  292. //다원플러그일 경우,
  293. BOOL isControlDAWONPlug = [_deviceDetail.deviceMfId isEqualToString:@"DAWON"] && [_deviceDetail.deviceProfileId isEqualToString:@"20004"];
  294. CommandClassControlView *controlView = nil;
  295. if (node.cmdclsType == CmdClsTypeMeterCurrent && isControlDAWONPlug) {//미터 커렌트 - 그래프
  296. controlView = [CommandClassControlDetailView viewForCommandClass:CmdClsTypeMeterCurrentForDAWON];
  297. cCell.constraintContainerTop.constant = 0;
  298. cCell.constraintNodeTitleLeft.constant = 0;
  299. cCell.constraintNodeTitleTop.constant = ([controlView sizeForIntrinsic].height / 2) - (25/2) - 15;
  300. cCell.lblNodeTitle.textAlignment = NSTextAlignmentCenter;
  301. } else if (node.cmdclsType == CmdClsTypeMeterTotal && isControlDAWONPlug) {//미터 토탈 - 아이콘
  302. controlView = [CommandClassControlDetailView viewForCommandClass:CmdClsTypeMeterTotalForDAWON];
  303. cCell.constraintContainerTop.constant = 25;
  304. cCell.constraintNodeTitleLeft.constant = 120;
  305. cCell.constraintNodeTitleTop.constant = 28;
  306. cCell.lblNodeTitle.textAlignment = NSTextAlignmentLeft;
  307. } else {
  308. controlView = [CommandClassControlDetailView viewForCommandClass:node.cmdclsType];
  309. cCell.constraintContainerTop.constant = 25;
  310. cCell.constraintNodeTitleLeft.constant = 0;
  311. cCell.constraintNodeTitleTop.constant = 0;
  312. cCell.lblNodeTitle.textAlignment = NSTextAlignmentCenter;
  313. }
  314. node.deviceId = _deviceDetail.deviceId;
  315. controlView.node = node;
  316. if (node.cmdclsType == CmdClsTypeCameraStream) {//카메라 스트림일 경우,
  317. controlView.device = _deviceDetail;
  318. if (!_isNotFirstLoading) {
  319. [[JDFacade facade] alert:MSG_NOT_WIFI_STREAM];
  320. _isNotFirstLoading = YES;
  321. }
  322. }
  323. cCell.controlContainerView.hidden = !controlView;
  324. if (!cCell.controlContainerView.hidden) {
  325. UIView *superview = cCell.controlContainerView;
  326. cCell.constraintContainerHeight.constant = [controlView sizeForIntrinsic].height;
  327. [superview addSubview:controlView];
  328. [controlView mas_makeConstraints:^(MASConstraintMaker *make) {
  329. make.size.mas_equalTo(controlView.frame.size);
  330. make.center.equalTo(superview);
  331. }];
  332. // NSLog(@"%s\n %@", __PRETTY_FUNCTION__, cCell.controlContainerView);
  333. }
  334. }
  335. return cell;
  336. }
  337. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  338. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  339. }
  340. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
  341. // Remove seperator inset
  342. if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
  343. [cell setSeparatorInset:UIEdgeInsetsZero];
  344. }
  345. // Prevent the cell from inheriting the Table View's margin settings
  346. if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
  347. [cell setPreservesSuperviewLayoutMargins:NO];
  348. }
  349. // Explictly set your cell's layout margins
  350. if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
  351. [cell setLayoutMargins:UIEdgeInsetsZero];
  352. }
  353. }
  354. #pragma mark - UI Events
  355. //상세 보기
  356. - (void)showThingsDetailInfo {
  357. if ([_mvc.view superview]) {
  358. [self closeChildViewController:_mvc completionHandler:^{
  359. [self showThingsDetailInfo];
  360. }];
  361. return;
  362. }
  363. _tableView.scrollEnabled = NO;
  364. if (!_ivc) {
  365. _ivc = (ThingsInfoViewController *)[CommonUtil instantiateViewControllerWithIdentifier:@"ThingsInfoViewController" storyboardName:@"Things"];
  366. _ivc.deviceDetail = _deviceDetail;
  367. [self addChildViewController:_ivc];
  368. [_ivc didMoveToParentViewController:self];
  369. }
  370. [_containerView addSubview:_ivc.view];
  371. if (![_ivc.btnClose actionsForTarget:self forControlEvent:UIControlEventTouchUpInside]) {
  372. __weak typeof(self) weakSelf = self;
  373. [_ivc.btnClose addTarget:weakSelf action:@selector(btnCloseTouched:) forControlEvents:UIControlEventTouchUpInside];
  374. }
  375. _containerBottom = _constraintContainerBottom.constant;
  376. _constraintContainerBottom.constant = 0.0f;
  377. [UIView animateWithDuration:ANIMATION_DUR animations:^{
  378. [_containerView layoutIfNeeded];
  379. }];
  380. }
  381. //장치명 변경
  382. - (void)modifyThingsName {
  383. if ([_ivc.view superview]) {
  384. [self closeChildViewController:_ivc completionHandler:^{
  385. [self modifyThingsName];
  386. }];
  387. return;
  388. }
  389. _tableView.scrollEnabled = NO;
  390. if (!_mvc) {
  391. _mvc = (ThingsModifyViewController *)[CommonUtil instantiateViewControllerWithIdentifier:@"ThingsModifyViewController" storyboardName:@"Things"];
  392. _mvc.device = _deviceDetail;
  393. [self addChildViewController:_mvc];
  394. [_mvc didMoveToParentViewController:self];
  395. }
  396. [_containerView addSubview:_mvc.view];
  397. if (![_mvc.btnSave actionsForTarget:self forControlEvent:UIControlEventTouchUpInside]) {
  398. __weak typeof(self) weakSelf = self;
  399. [_mvc.btnSave addTarget:weakSelf action:@selector(btnSaveTouched:) forControlEvents:UIControlEventTouchUpInside];
  400. [_mvc.btnCancel addTarget:weakSelf action:@selector(btnCancelTouched:) forControlEvents:UIControlEventTouchUpInside];
  401. }
  402. _containerBottom = _constraintContainerBottom.constant;
  403. _constraintContainerBottom.constant = 0.0f;
  404. [UIView animateWithDuration:ANIMATION_DUR animations:^{
  405. [self imageCell].txtDeviceName.alpha = 1.0f;
  406. [self imageCell].lblDeviceName.alpha = 0.0f;
  407. [_containerView layoutIfNeeded];
  408. }];
  409. }
  410. //장치등록 보안키 변경
  411. - (void)modifySecureKey {
  412. if (!_kpopup) {
  413. _kpopup = [[ModifySecureKeyPopupView alloc] initFromNib];
  414. }
  415. [_kpopup showWithCompletion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
  416. if (buttonIndex == 0) {//OK
  417. [self requestModifySecureKey];
  418. }
  419. }];
  420. }
  421. //장치 유형 변경
  422. - (void)modifyDeviceType {
  423. //goto choose device type
  424. }
  425. //카메라 연결정보 변경
  426. - (void)modifyStreamData {
  427. if (!_dpopup) {
  428. _dpopup = [[ModifyStreamDataPopupView alloc] initFromNib];
  429. }
  430. _dpopup.deviceDetail = _deviceDetail;
  431. [_dpopup showWithCompletion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
  432. if (buttonIndex == 0) {//OK
  433. [self requestModifyStreamData];
  434. }
  435. }];
  436. }
  437. //디바이스 삭제
  438. - (void)removeThings {
  439. CustomAlertView *alert = [[CustomAlertView alloc] initWithTitle:NSLocalizedString(@"알림", @"알림") message:NSLocalizedString(@"장치를 삭제하시겠습니까?\n이 장치에 속한 하위 장치가 있을 경우 함께 삭제됩니다\n\n이 장치를 사용중인 홈 규칙과\n멀티 제어가 비활성 상태로 변경됩니다\n\n신중하게 결정하세요", @"장치를 삭제하시겠습니까?\n이 장치에 속한 하위 장치가 있을 경우 함께 삭제됩니다\n\n이 장치를 사용중인 홈 규칙과\n멀티 제어가 비활성 상태로 변경됩니다\n\n신중하게 결정하세요") delegate:nil OKButtonTitle:NSLocalizedString(@"삭제", @"삭제") cancelButtonTitle:NSLocalizedString(@"취소", @"취소")];
  440. [alert showWithCompletion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
  441. if (buttonIndex == 0) {//OK
  442. //삭제 후 장치목록으로 이동요
  443. [self requestDeleteDevice];
  444. }
  445. }];
  446. }
  447. - (void)btnSaveTouched:(id)sender {
  448. //validate
  449. if (!_txtDeviceName || ![ValidateUtil validateTextfiled:_txtDeviceName type:ValidateTypeNull title:NSLocalizedString(@"장치명", @"장치명")]) {
  450. return;
  451. }
  452. //TOOD : request
  453. [self requestDeviceUpdate];
  454. }
  455. - (void)btnCancelTouched:(id)sender {
  456. [self closeChildViewController:_mvc completionHandler:nil];
  457. }
  458. - (void)closeChildViewController:(UIViewController *)vc completionHandler:(completionCallBackHandler)completionHander {
  459. _constraintContainerBottom.constant = _containerBottom;
  460. [UIView animateWithDuration:ANIMATION_DUR animations:^{
  461. [self imageCell].txtDeviceName.alpha = 0.0f;
  462. [self imageCell].lblDeviceName.alpha = 1.0f;
  463. [self.view layoutIfNeeded];
  464. } completion:^(BOOL finished) {
  465. _tableView.scrollEnabled = YES;
  466. [vc.view removeFromSuperview];
  467. if (completionHander) {
  468. completionHander();
  469. }
  470. }];
  471. }
  472. - (void)btnCloseTouched:(id)sender {
  473. [self closeChildViewController:_ivc completionHandler:nil];
  474. }
  475. #pragma mark - MemoryWarning
  476. - (void)didReceiveMemoryWarning
  477. {
  478. [super didReceiveMemoryWarning];
  479. // Dispose of any resources that can be recreated.
  480. }
  481. @end