ThingsDetailViewController.m 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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 "CustomTableView.h"
  16. #import "ValidateUtil.h"
  17. #import "CommandClassControlDetailView.h"
  18. #import "ThingsViewController.h"
  19. #import "ThingsDetailViewController.h"
  20. #import "ModifyDeviceNamePopupView.h"
  21. @implementation ThingsDetailTitleTableViewCell
  22. @end
  23. @implementation ThingsDetailControlTableViewCell
  24. @end
  25. @implementation ThingsDetailOptionTableViewCell
  26. @end
  27. @interface ThingsDetailViewController () <UITableViewDataSource, UITableViewDelegate> {
  28. DeviceDetailModel *_deviceDetail;
  29. ModifyDeviceNamePopupView *_mpopup;
  30. }
  31. @end
  32. #pragma mark - Class Definition
  33. @implementation ThingsDetailViewController
  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. //initialize tableView
  42. [self initTableViewAsDefaultStyle:_tableView];
  43. _tableView.tableFooterView = [[UIView alloc] init]; //TODO : gray view - this call table events;
  44. [self setThingsDetailPopoverOptions];
  45. }
  46. - (void)setThingsDetailPopoverOptions {
  47. //set Popover Contents
  48. __weak typeof(self) weakSelf = self;
  49. _popooverOptionArray = [[NSMutableArray alloc] init];
  50. [_popooverOptionArray addObject:@{@"menuName" : NSLocalizedString(@"새로 고침",nil),
  51. @"iconName": @"tp_01_img_bg_morepopup_icon_refresh",
  52. @"target": weakSelf,
  53. @"selector": [NSValue valueWithPointer:@selector(requestDeviceDetail)]}];
  54. if ([JDFacade facade].loginUser.level > 10) {
  55. [_popooverOptionArray addObject:@{@"menuName" : NSLocalizedString(@"이름 변경", @"이름 변경"),
  56. @"iconName": @"tp_01_img_bg_morepopup_icon_edit",
  57. @"target": weakSelf,
  58. @"selector": [NSValue valueWithPointer:@selector(modifyDeviceName)]}];
  59. }
  60. }
  61. - (void)prepareViewDidLoad {
  62. [self requestDeviceDetail];
  63. }
  64. - (void)modifyDeviceName {
  65. _mpopup = [[ModifyDeviceNamePopupView alloc] initFromNib];
  66. _mpopup.txtDeviceName.text = _deviceDetail.deviceName;
  67. [_mpopup showWithCompletion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
  68. if (buttonIndex == 0) {//OK
  69. [self requestDeviceUpdate];
  70. }
  71. }];
  72. }
  73. - (void)updateTitle {
  74. if (![JDFacade facade].loginUser.isHomehubOnline) {
  75. _lblTitle.text = @"홈허브 오프라인";
  76. [_lblTitle setColor:kUITextColor02 text:_lblTitle.text];
  77. return;
  78. }
  79. if (_deviceDetail && !_deviceDetail.isOnline) {
  80. _lblTitle.text = @"장치 상태를 확인하세요";
  81. [_lblTitle setColor:kUITextColor02 text:_lblTitle.text];
  82. return;
  83. }
  84. }
  85. #pragma mark - Main Logic
  86. - (void)requestDeviceDetail {
  87. NSString *path = [NSString stringWithFormat:API_GET_DEVICE_DETAIL, _refDevice.deviceId];
  88. [[RequestHandler handler] sendAsyncGetRequestAPIPath:path parameters:nil modelClass:[DeviceDetailModel class] completion:^(id responseObject) {
  89. if (!responseObject) {//응답결과가 잘못되었거나 없을 경우,
  90. return;
  91. }
  92. _deviceDetail = (DeviceDetailModel *)responseObject;
  93. if (_deviceDetail) {//API 성공 ,
  94. [self setDeviceContents];
  95. }
  96. } failure:^(id errorObject) {
  97. JDErrorModel *error = (JDErrorModel *)errorObject;
  98. [[JDFacade facade] alert:error.errorMessage];
  99. }];
  100. }
  101. - (void)setDeviceContents {
  102. [self updateTitle];
  103. [_tableView reloadData];
  104. }
  105. - (void)requestDeviceUpdate {
  106. //parameters
  107. NSDictionary *parameter = @{@"device_name": _mpopup.txtDeviceName.text};
  108. NSString *path = [NSString stringWithFormat:API_GET_DEVICE_UPDATE, _deviceDetail.deviceId];
  109. [[RequestHandler handler] sendAsyncPostRequestAPIPath:path parameters:parameter modelClass:[JDJSONModel class] completion:^(id responseObject) {
  110. _refDevice.deviceName = _deviceDetail.deviceName = _mpopup.txtDeviceName.text;
  111. [_tableView reloadData];
  112. //리스트를 다시 로드함.
  113. ThingsViewController *vc = [[JDFacade facade] viewControllerOnNaviationController:[ThingsViewController class]];
  114. [vc.collectionView reloadData];
  115. } failure:^(id errorObject) {
  116. JDErrorModel *error = (JDErrorModel *)errorObject;
  117. [[JDFacade facade] alert:error.errorMessage];
  118. }];
  119. }
  120. - (void)requestDeleteDevice {
  121. //parameters
  122. NSString *path = [NSString stringWithFormat:API_DELETE_DEVICE, _deviceDetail.deviceId];
  123. [[RequestHandler handler] sendAsyncRequestAPIPath:path method:ksHTTPRequestDELETE parameters:nil modelClass:[JDJSONModel class] showLoadingView:YES completion:^(id responseObject) {
  124. [[JDFacade facade] toast:NSLocalizedString(@"장치가 삭제되었습니다", @"장치가 삭제되었습니다")];
  125. // if ([JDFacade facade].loginHomeGroup.deviceId && ![[JDFacade facade].loginHomeGroup.deviceId isEmptyString]) {
  126. // if ([[JDFacade facade].loginHomeGroup.deviceId isEqualToString:_deviceDetail.deviceId]) {//현재 홈의 스마튼폰일 경우,
  127. // [JDFacade facade].loginHomeGroup.deviceId = nil;
  128. // [JDFacade facade].loginHomeGroup.deviceAuthorization = nil;
  129. // [JDFacade facade].loginHomeGroup.deviceKey = nil;
  130. // }
  131. // }
  132. //리스트를 다시 로드함.
  133. ThingsViewController *vc = [[JDFacade facade] viewControllerOnNaviationController:[ThingsViewController class]];
  134. [vc prepareViewDidLoad];
  135. [self.navigationController popToRootViewControllerAnimated:YES];
  136. } failure:^(id errorObject) {
  137. JDErrorModel *error = (JDErrorModel *)errorObject;
  138. [[JDFacade facade] alert:error.errorMessage];
  139. }];
  140. }
  141. #pragma mark - UITableView DataSource & Delegate
  142. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  143. return 3;
  144. }
  145. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  146. NSInteger count = 1;
  147. if (section == 2) {//optional nodes
  148. count = _deviceDetail.nodes.count - 1; //두번째 이후부터 옵셔널 노드임.
  149. }
  150. return count;
  151. }
  152. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  153. CGFloat height = 80;
  154. NSInteger section = indexPath.section;
  155. if (section == 1) {
  156. height = 375.0f;
  157. } else if (section == 2) {
  158. height = 110.0f;
  159. }
  160. return height;
  161. }
  162. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  163. UITableViewCell *cell = nil;
  164. NSInteger section = indexPath.section;
  165. if (section == 0) {//title
  166. ThingsDetailTitleTableViewCell *tcell = (ThingsDetailTitleTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"TitleCellIdentifier"];
  167. tcell.lblDeviceName.text = _deviceDetail.deviceName;
  168. [tcell.imgvDevice sd_setImageWithURL:[NSURL URLWithString:_deviceDetail.imageFileName] placeholderImage:nil options:SDWebImageRefreshCached];
  169. cell = tcell;
  170. } else if (section == 1) {//device control
  171. ThingsDetailControlTableViewCell *tcell = (ThingsDetailControlTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"ControlCellIdentifier"];
  172. //뷰를 초기화함.
  173. [[tcell.controlContainer subviews] enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
  174. UIView *subview = (UIView *)obj;
  175. [subview removeFromSuperview];
  176. }];
  177. NodeModel *node = _deviceDetail.nodes.firstObject;
  178. CommandClassControlView *controlView = [CommandClassControlDetailView viewForCommandClass:node.cmdclsType];
  179. node.deviceId = _deviceDetail.deviceId;
  180. controlView.node = node;
  181. tcell.controlContainer.hidden = !controlView;
  182. if (!tcell.controlContainer.hidden) {
  183. UIView *superview = tcell.controlContainer;
  184. [superview addSubview:controlView];
  185. [controlView mas_makeConstraints:^(MASConstraintMaker *make) {
  186. make.size.mas_equalTo(controlView.frame.size);
  187. make.center.equalTo(superview);
  188. }];
  189. }
  190. cell = tcell;
  191. } else if (section == 2) {//option control
  192. ThingsDetailOptionTableViewCell *tcell = (ThingsDetailOptionTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"OptionCellIdentifier"];
  193. NodeModel *node = _deviceDetail.nodes[indexPath.row + 1];
  194. tcell.lblNodeName.text = node.nodeName;
  195. tcell.lblNodeValue.text = node.cmdclsValueMsg;
  196. cell = tcell;
  197. }
  198. return cell;
  199. }
  200. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  201. [super tableView:tableView didSelectRowAtIndexPath:indexPath];
  202. }
  203. #pragma mark - UI Events
  204. - (IBAction)btnOptionTouched:(id)sender {
  205. [self toggleOptions:sender];
  206. }
  207. - (IBAction)btnCloseTouched:(id)sender {
  208. [[JDFacade facade] dismissModalStack:YES completion:^{
  209. //TODO: - list refresh
  210. }];
  211. }
  212. #pragma mark - MemoryWarning
  213. - (void)didReceiveMemoryWarning
  214. {
  215. [super didReceiveMemoryWarning];
  216. // Dispose of any resources that can be recreated.
  217. }
  218. @end