ThingsAddViewController.m 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. //
  2. // ThingsAddViewController.m
  3. // kneet
  4. //
  5. // Created by Jason Lee on 5/8/15.
  6. // Copyright (c) 2015 ntels. All rights reserved.
  7. //
  8. #import "JDObject.h"
  9. #import "RequestHandler.h"
  10. #import "DeviceModel.h"
  11. #import "CustomLabel.h"
  12. #import "CustomButton.h"
  13. #import "CustomImageView.h"
  14. #import "ThingsAddStartViewController.h"
  15. #import "ThingsAddViewController.h"
  16. #import "HomeHubSelectPopupView.h"
  17. @implementation ThingsAddTableViewCell
  18. // 가스 밸브 이미지명 : img_things_product_01_smartgasvalve
  19. // 도어센서 이미지 명 : img_things_product_02_mutisensor_door
  20. // 스마트플러그 이미지 명 : img_things_product_03_smartplug
  21. @end
  22. @interface ThingsAddViewController () <UITableViewDataSource, UITableViewDelegate> {
  23. NSArray *_addableDeviceList;
  24. DataSelectListModel *_homeHubList;
  25. }
  26. @end
  27. #pragma mark - Class Definition
  28. @implementation ThingsAddViewController
  29. - (void)viewDidLoad {
  30. [super viewDidLoad];
  31. // Do any additional setup after loading the view.
  32. [self initUI];
  33. [self prepareViewDidLoad];
  34. }
  35. - (void)viewWillAppear:(BOOL)animated {
  36. [super viewWillAppear:animated];
  37. }
  38. - (void)initUI {
  39. [self initTableViewAsDefaultStyle:_tableView];
  40. }
  41. - (void)prepareViewDidLoad {
  42. _addableDeviceList = @[@{@"deviceName" : @"가스 밸브",
  43. @"manufacturer" : @"타임밸브",
  44. @"nodeName" : @"스마트 플러그",
  45. @"deviceKey": @"36002"},
  46. @{@"deviceName" : @"도어 센서",
  47. @"manufacturer" : @"타임밸브",
  48. @"nodeName" : @"스마트 플러그",
  49. @"deviceKey": @"11011"},
  50. @{@"deviceName" : @"스마트 플러그",
  51. @"manufacturer" : @"타임밸브",
  52. @"nodeName" : @"스마트 플러그",
  53. @"deviceKey": @"17002"},
  54. @{@"deviceName" : @"다우앤텍 멀티 센서",
  55. @"manufacturer" : @"다우엔텍",
  56. @"nodeName" : @"멀티센서",
  57. @"deviceKey": @"11011"}];
  58. _homeHubList = [[DataSelectListModel alloc] init];
  59. _homeHubList.title = @"홈허브 선택";
  60. if ([[JDFacade facade].loginUser isMultiHomeHub]) {
  61. for (DeviceModel *info in [JDFacade facade].loginUser.deviceList) {
  62. DataSelectModel *data = [[DataSelectModel alloc] init];
  63. data.title = info.deviceName;
  64. data.value = info;
  65. [_homeHubList.list addObject:data];
  66. }
  67. }
  68. }
  69. #pragma mark - Main Logic
  70. #pragma mark - UITableView DataSource & Delegate
  71. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  72. return _addableDeviceList.count;
  73. }
  74. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  75. CGFloat height = 154.0f;
  76. return height;
  77. }
  78. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  79. ThingsAddTableViewCell *cell = (ThingsAddTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"AddCellIdentifier"];
  80. NSDictionary *addableDevice = _addableDeviceList[indexPath.row];
  81. cell.lblDeviceName.text = addableDevice[@"deviceName"];
  82. cell.lblVendor.text = addableDevice[@"manufacturer"];
  83. // if (![cell.btnAdd actionsForTarget:self forControlEvent:UIControlEventTouchUpInside]) {
  84. // [cell.btnAdd addTarget:self action:@selector(btnAddDeviceTouched:) forControlEvents:UIControlEventTouchUpInside];
  85. // }
  86. cell.btnAdd.tag = indexPath.row;
  87. return cell;
  88. }
  89. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  90. [tableView deselectRowAtIndexPath:indexPath animated:NO];
  91. // NSDictionary *addableDevice = _addableDeviceList[indexPath.row];
  92. [self addDevice:indexPath];
  93. }
  94. #pragma mark - TableView Delegate
  95. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
  96. // Remove seperator inset
  97. if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
  98. [cell setSeparatorInset:UIEdgeInsetsZero];
  99. }
  100. // Prevent the cell from inheriting the Table View's margin settings
  101. if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
  102. [cell setPreservesSuperviewLayoutMargins:NO];
  103. }
  104. // Explictly set your cell's layout margins
  105. if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
  106. [cell setLayoutMargins:UIEdgeInsetsZero];
  107. }
  108. }
  109. #pragma mark - UI Events
  110. - (IBAction)btnAddDeviceTouched:(id)sender {
  111. UIButton *senderButton = (UIButton *)sender;
  112. NSIndexPath *indexPath = [NSIndexPath indexPathForRow:senderButton.tag inSection:0];
  113. [self addDevice:indexPath];
  114. }
  115. -(void)showHomeHubSelect:(NSIndexPath *)indexPath {
  116. HomeHubSelectPopupView *hpopup = [[HomeHubSelectPopupView alloc] initFromNib];
  117. // hpopup.refTriggers = _triggers;
  118. // hpopup.refDevices = _triggerDevices;
  119. // hpopup.refConditions = _conditions;
  120. [hpopup showWithCompletion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
  121. if (buttonIndex == 0) {
  122. _selectHub = hpopup.selectedDeviceModel;
  123. [self addDevice:indexPath];
  124. }
  125. }];
  126. }
  127. -(void)addDevice:(NSIndexPath *)indexPath
  128. {
  129. if ([[JDFacade facade].loginUser isMultiHomeHub] && _selectHub == nil) {
  130. [self showHomeHubSelect:indexPath];
  131. return;
  132. }
  133. NSDictionary *info = _addableDeviceList[indexPath.row];
  134. NSLog(@"info : %@", info);
  135. DeviceModel *addDevice = [[DeviceModel alloc] init];
  136. addDevice.prdName = info[@"deviceName"];
  137. addDevice.cmdclsTypeId = info[@"deviceKey"];
  138. ThingsAddStartViewController *vc = (ThingsAddStartViewController *)[CommonUtil instantiateViewControllerWithIdentifier:@"ThingsAddStartViewController" storyboardName:@"Things"];
  139. vc.addDevice = addDevice;
  140. vc.selectHub = _selectHub;
  141. vc.providesPresentationContextTransitionStyle = YES;
  142. vc.definesPresentationContext = YES;
  143. [vc setModalPresentationStyle:UIModalPresentationOverCurrentContext];
  144. [self presentViewController:vc animated:NO completion:nil];
  145. }
  146. - (IBAction)btnCloseTouched:(id)sender {
  147. [self dismissViewControllerAnimated:YES completion:nil];
  148. }
  149. #pragma mark - MemoryWarning
  150. - (void)didReceiveMemoryWarning
  151. {
  152. [super didReceiveMemoryWarning];
  153. // Dispose of any resources that can be recreated.
  154. }
  155. @end