// // ThingsDetailViewController.m // kneet // // Created by Jason Lee on 3/16/15. // Copyright (c) 2015 ntels. All rights reserved. // #import "RequestHandler.h" #import "DeviceModel.h" #import "CustomImageView.h" #import "CustomLabel.h" #import "CustomTextField.h" #import "CustomButton.h" #import "UIImageView+WebCache.h" #import "CustomTableView.h" #import "ValidateUtil.h" #import "CommandClassControlDetailView.h" #import "ThingsViewController.h" #import "ThingsDetailViewController.h" #import "ModifyDeviceNamePopupView.h" @implementation ThingsDetailTitleTableViewCell @end @implementation ThingsDetailControlTableViewCell @end @implementation ThingsDetailOptionTableViewCell @end @interface ThingsDetailViewController () { DeviceDetailModel *_deviceDetail; ModifyDeviceNamePopupView *_mpopup; } @end #pragma mark - Class Definition @implementation ThingsDetailViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. [self initUI]; [self prepareViewDidLoad]; } - (void)initUI { //initialize tableView [self initTableViewAsDefaultStyle:_tableView]; _tableView.tableFooterView = [[UIView alloc] init]; //TODO : gray view - this call table events; [self setThingsDetailPopoverOptions]; } - (void)setThingsDetailPopoverOptions { //set Popover Contents __weak typeof(self) weakSelf = self; _popooverOptionArray = [[NSMutableArray alloc] init]; [_popooverOptionArray addObject:@{@"menuName" : NSLocalizedString(@"새로 고침",nil), @"iconName": @"tp_01_img_bg_morepopup_icon_refresh", @"target": weakSelf, @"selector": [NSValue valueWithPointer:@selector(requestDeviceDetail)]}]; if ([JDFacade facade].loginUser.level > 10) { [_popooverOptionArray addObject:@{@"menuName" : NSLocalizedString(@"이름 변경", @"이름 변경"), @"iconName": @"tp_01_img_bg_morepopup_icon_edit", @"target": weakSelf, @"selector": [NSValue valueWithPointer:@selector(modifyDeviceName)]}]; } } - (void)prepareViewDidLoad { [self requestDeviceDetail]; } - (void)modifyDeviceName { _mpopup = [[ModifyDeviceNamePopupView alloc] initFromNib]; _mpopup.txtDeviceName.text = _deviceDetail.deviceName; [_mpopup showWithCompletion:^(CustomAlertView *alertView, NSInteger buttonIndex) { if (buttonIndex == 0) {//OK [self requestDeviceUpdate]; } }]; } - (void)updateTitle { if (![JDFacade facade].loginUser.isHomehubOnline) { _lblTitle.text = @"홈허브 오프라인"; [_lblTitle setColor:kUITextColor02 text:_lblTitle.text]; return; } if (_deviceDetail && !_deviceDetail.isOnline) { _lblTitle.text = @"장치 상태를 확인하세요"; [_lblTitle setColor:kUITextColor02 text:_lblTitle.text]; return; } } #pragma mark - Main Logic - (void)requestDeviceDetail { NSString *path = [NSString stringWithFormat:API_GET_DEVICE_DETAIL, _refDevice.deviceId]; [[RequestHandler handler] sendAsyncGetRequestAPIPath:path parameters:nil modelClass:[DeviceDetailModel class] completion:^(id responseObject) { if (!responseObject) {//응답결과가 잘못되었거나 없을 경우, return; } _deviceDetail = (DeviceDetailModel *)responseObject; if (_deviceDetail) {//API 성공 , [self setDeviceContents]; } } failure:^(id errorObject) { JDErrorModel *error = (JDErrorModel *)errorObject; [[JDFacade facade] alert:error.errorMessage]; }]; } - (void)setDeviceContents { [self updateTitle]; [_tableView reloadData]; } - (void)requestDeviceUpdate { //parameters NSDictionary *parameter = @{@"device_name": _mpopup.txtDeviceName.text}; NSString *path = [NSString stringWithFormat:API_GET_DEVICE_UPDATE, _deviceDetail.deviceId]; [[RequestHandler handler] sendAsyncPostRequestAPIPath:path parameters:parameter modelClass:[JDJSONModel class] completion:^(id responseObject) { _refDevice.deviceName = _deviceDetail.deviceName = _mpopup.txtDeviceName.text; [_tableView reloadData]; //리스트를 다시 로드함. ThingsViewController *vc = [[JDFacade facade] viewControllerOnNaviationController:[ThingsViewController class]]; [vc.collectionView reloadData]; } failure:^(id errorObject) { JDErrorModel *error = (JDErrorModel *)errorObject; [[JDFacade facade] alert:error.errorMessage]; }]; } - (void)requestDeleteDevice { //parameters NSString *path = [NSString stringWithFormat:API_DELETE_DEVICE, _deviceDetail.deviceId]; [[RequestHandler handler] sendAsyncRequestAPIPath:path method:ksHTTPRequestDELETE parameters:nil modelClass:[JDJSONModel class] showLoadingView:YES completion:^(id responseObject) { [[JDFacade facade] toast:NSLocalizedString(@"장치가 삭제되었습니다", @"장치가 삭제되었습니다")]; // if ([JDFacade facade].loginHomeGroup.deviceId && ![[JDFacade facade].loginHomeGroup.deviceId isEmptyString]) { // if ([[JDFacade facade].loginHomeGroup.deviceId isEqualToString:_deviceDetail.deviceId]) {//현재 홈의 스마튼폰일 경우, // [JDFacade facade].loginHomeGroup.deviceId = nil; // [JDFacade facade].loginHomeGroup.deviceAuthorization = nil; // [JDFacade facade].loginHomeGroup.deviceKey = nil; // } // } //리스트를 다시 로드함. ThingsViewController *vc = [[JDFacade facade] viewControllerOnNaviationController:[ThingsViewController class]]; [vc prepareViewDidLoad]; [self.navigationController popToRootViewControllerAnimated:YES]; } failure:^(id errorObject) { JDErrorModel *error = (JDErrorModel *)errorObject; [[JDFacade facade] alert:error.errorMessage]; }]; } #pragma mark - UITableView DataSource & Delegate - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 3; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { NSInteger count = 1; if (section == 2) {//optional nodes count = _deviceDetail.nodes.count - 1; //두번째 이후부터 옵셔널 노드임. } return count; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { CGFloat height = 80; NSInteger section = indexPath.section; if (section == 1) { height = 375.0f; } else if (section == 2) { height = 110.0f; } return height; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = nil; NSInteger section = indexPath.section; if (section == 0) {//title ThingsDetailTitleTableViewCell *tcell = (ThingsDetailTitleTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"TitleCellIdentifier"]; tcell.lblDeviceName.text = _deviceDetail.deviceName; [tcell.imgvDevice sd_setImageWithURL:[NSURL URLWithString:_deviceDetail.imageFileName] placeholderImage:nil options:SDWebImageRefreshCached]; cell = tcell; } else if (section == 1) {//device control ThingsDetailControlTableViewCell *tcell = (ThingsDetailControlTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"ControlCellIdentifier"]; //뷰를 초기화함. [[tcell.controlContainer subviews] enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { UIView *subview = (UIView *)obj; [subview removeFromSuperview]; }]; NodeModel *node = _deviceDetail.nodes.firstObject; CommandClassControlView *controlView = [CommandClassControlDetailView viewForCommandClass:node.cmdclsType]; node.deviceId = _deviceDetail.deviceId; controlView.node = node; tcell.controlContainer.hidden = !controlView; if (!tcell.controlContainer.hidden) { UIView *superview = tcell.controlContainer; [superview addSubview:controlView]; [controlView mas_makeConstraints:^(MASConstraintMaker *make) { make.size.mas_equalTo(controlView.frame.size); make.center.equalTo(superview); }]; } cell = tcell; } else if (section == 2) {//option control ThingsDetailOptionTableViewCell *tcell = (ThingsDetailOptionTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"OptionCellIdentifier"]; NodeModel *node = _deviceDetail.nodes[indexPath.row + 1]; tcell.lblNodeName.text = node.nodeName; tcell.lblNodeValue.text = node.cmdclsValueMsg; cell = tcell; } return cell; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [super tableView:tableView didSelectRowAtIndexPath:indexPath]; } #pragma mark - UI Events - (IBAction)btnOptionTouched:(id)sender { [self toggleOptions:sender]; } - (IBAction)btnCloseTouched:(id)sender { [[JDFacade facade] dismissModalStack:YES completion:^{ //TODO: - list refresh }]; } #pragma mark - MemoryWarning - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end