ThingsAddViewController.m 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 "ThingsAddViewController.h"
  13. #import "ThingsAddWallpadSubViewController.h"
  14. @interface ThingsAddViewController () {
  15. NSInteger _rowCount;
  16. }
  17. @end
  18. #pragma mark - Class Definition
  19. @implementation ThingsAddViewController
  20. - (void)viewDidLoad {
  21. [super viewDidLoad];
  22. // Do any additional setup after loading the view.
  23. [self initUI];
  24. [self prepareViewDidLoad];
  25. }
  26. - (void)viewWillAppear:(BOOL)animated {
  27. [super viewWillAppear:animated];
  28. self.title = NSLocalizedString(@"새 장치 추가",nil);
  29. }
  30. - (void)initUI {
  31. //set tableview option
  32. self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
  33. self.tableView.backgroundColor = kUILineColor2;
  34. self.tableView.tableFooterView = [[UIView alloc] init]; //this call table events;
  35. //Localization
  36. _lblCommaxTitle.text = NSLocalizedString(@"COMMAX 월패드", @"COMMAX 월패드");
  37. _lblCommaxInfo.text = NSLocalizedString(@"월패드에서 했던 일들을 앱으로도 할 수 있게 됩니다", @"월패드에서 했던 일들을 앱으로도 할 수 있게 됩니다");
  38. _lblCommaxSubTitle.text = NSLocalizedString(@"COMMAX 추가 연결제품", @"COMMAX 추가 연결제품");
  39. _lblCommaxSubInfo.text = NSLocalizedString(@"월패드에 더 많은 장치를 연결해보세요", @"월패드에 더 많은 장치를 연결해보세요");
  40. _lblDawonPlugTitle.text = NSLocalizedString(@"DAWON 스마트플러그", @"DAWON 스마트플러그");
  41. _lblDawonPlugInfo.text = NSLocalizedString(@"멀리 있어도 켜고 끌 수 있고 전력량까지 알 수 있습니다", @"멀리 있어도 켜고 끌 수 있고 전력량까지 알 수 있습니다");
  42. _lblFoscamTitle.text = NSLocalizedString(@"FOSCAM (MJPEG 지원모델)", @"FOSCAM (MJPEG 지원모델)");
  43. _lblFoscamInfo.text = NSLocalizedString(@"어디서나 홈의 상황을 모니터링 할 수 있게 됩니다", @"어디서나 홈의 상황을 모니터링 할 수 있게 됩니다");
  44. _lblGeofenceTitle.text = NSLocalizedString(@"스마트폰 위치센서", @"스마트폰 위치센서");
  45. _lblGeofenceInfo.text = NSLocalizedString(@"귀가/외출 시 자동실행되는 규칙을 만들 수 있게 됩니다", @"귀가/외출 시 자동실행되는 규칙을 만들 수 있게 됩니다");
  46. }
  47. - (void)prepareViewDidLoad {
  48. _rowCount = [JDFacade facade].loginHomeGroup.level > 10 ? 5 : 1; //사용자 권한 별
  49. }
  50. #pragma mark - Main Logic
  51. #pragma mark - UITableView DataSource & Delegate
  52. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  53. return _rowCount;
  54. }
  55. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  56. UITableViewCell *cell = nil;
  57. if (_rowCount == 1) {//심플 유저인 경우,
  58. NSIndexPath *tmpIndexPath = [NSIndexPath indexPathForRow:4 inSection:0];
  59. cell = [super tableView:tableView cellForRowAtIndexPath:tmpIndexPath];
  60. } else {//마스터, 파워 유저,
  61. cell = [super tableView:tableView cellForRowAtIndexPath:indexPath];
  62. }
  63. return cell;
  64. }
  65. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  66. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  67. __block UIViewController *vc = nil;
  68. if (_rowCount == 1) {//심플 유저인 경우,
  69. // if ([JDFacade facade].loginHomeGroup.deviceId && ![[JDFacade facade].loginHomeGroup.deviceId isEmptyString]) {
  70. // [[JDFacade facade] alert:NSLocalizedString(@"이미 등록하셨습니다", @"이미 등록하셨습니다")];
  71. // return;
  72. // }
  73. vc = [CommonUtil instantiateViewControllerWithIdentifier:@"ThingsAddGeofencingViewController" storyboardName:@"Things"];
  74. } else {//마스터, 파워 유저,
  75. if (indexPath.row == 0) {//wallpad
  76. vc = [CommonUtil instantiateViewControllerWithIdentifier:@"ThingsAddWallpadViewController" storyboardName:@"Things"];
  77. } else if (indexPath.row == 1) {//wallpad append devices.
  78. NSString *path = [NSString stringWithFormat:API_GET_GROUPS_DEVICES];
  79. dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{//RUN to background thread
  80. GroupsListModel *groupList = [[RequestHandler handler] sendSyncGetRequestAPIPath:path parameters:nil
  81. modelClass:[GroupsListModel class] showLoadingView:NO];
  82. NSMutableDictionary *wallpads = nil;
  83. if (groupList && groupList.groups && groupList.groups.count) {
  84. for (GroupsModel *group in groupList.groups) {
  85. for (DeviceModel *device in group.devices) {//월패드가 등록되었는 지 확인
  86. if ([device.deviceProtocolType isEqualToString:@"COMMAX"] && [device.deviceType isEqualToString:@"CONTROLLER_PRIMARY"]) {
  87. if (!wallpads) {
  88. wallpads = [[NSMutableDictionary alloc] init];
  89. }
  90. [wallpads setObject:device.deviceName forKey:device.deviceId];
  91. }
  92. }
  93. }
  94. }
  95. if (wallpads && wallpads.count) {
  96. ThingsAddWallpadSubViewController *tvc = [CommonUtil instantiateViewControllerWithIdentifier:@"ThingsAddWallpadSubViewController" storyboardName:@"Things"];
  97. tvc.wallpads = wallpads;
  98. vc = tvc;
  99. } else {
  100. [[JDFacade facade] toast:NSLocalizedString(@"월패드 등록을 먼저 진행해주세요", @"월패드 등록을 먼저 진행해주세요")];
  101. return;
  102. }
  103. });
  104. } else if (indexPath.row == 2) {//plug
  105. vc = [CommonUtil instantiateViewControllerWithIdentifier:@"ThingsAddPlugViewController" storyboardName:@"Things"];
  106. } else if (indexPath.row == 3) {//camera
  107. vc = [CommonUtil instantiateViewControllerWithIdentifier:@"ThingsAddCameraViewController" storyboardName:@"Things"];
  108. } else if (indexPath.row == 4) {//phone
  109. // if ([JDFacade facade].loginHomeGroup.deviceId && ![[JDFacade facade].loginHomeGroup.deviceId isEmptyString]) {
  110. // [[JDFacade facade] alert:NSLocalizedString(@"이미 등록하셨습니다", @"이미 등록하셨습니다")];
  111. // return;
  112. // }
  113. vc = [CommonUtil instantiateViewControllerWithIdentifier:@"ThingsAddGeofencingViewController" storyboardName:@"Things"];
  114. }
  115. }
  116. if (vc) {
  117. if (self.navigationController) {
  118. [self.navigationController pushViewController:vc animated:YES];
  119. } else {
  120. UINavigationController *nc = [[UINavigationController alloc] init];
  121. nc.viewControllers = @[vc];
  122. [[JDFacade facade] presentViewControllerByPush:nc pvc:self];
  123. }
  124. }
  125. }
  126. #pragma mark - TableView Delegate
  127. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
  128. // Remove seperator inset
  129. if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
  130. [cell setSeparatorInset:UIEdgeInsetsZero];
  131. }
  132. // Prevent the cell from inheriting the Table View's margin settings
  133. if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
  134. [cell setPreservesSuperviewLayoutMargins:NO];
  135. }
  136. // Explictly set your cell's layout margins
  137. if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
  138. [cell setLayoutMargins:UIEdgeInsetsZero];
  139. }
  140. }
  141. #pragma mark - UI Events
  142. #pragma mark - MemoryWarning
  143. - (void)didReceiveMemoryWarning
  144. {
  145. [super didReceiveMemoryWarning];
  146. // Dispose of any resources that can be recreated.
  147. }
  148. @end