ThingsAddIncludeViewController.m 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. //
  2. // ThingsAddIncludeViewController.m
  3. // kneet2
  4. //
  5. // Created by Jason Lee on 11/13/15.
  6. // Copyright © 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 "ThingsAddIncludeViewController.h"
  14. #import "ThingsAddFailViewController.h"
  15. #import "ThingsAddCompleteViewController.h"
  16. @interface ThingsAddIncludeViewController () {
  17. NSTimer *_timer;
  18. NSInteger _elapsedSeconds;
  19. }
  20. @end
  21. #pragma mark - Class Definition
  22. @implementation ThingsAddIncludeViewController
  23. - (void)viewDidLoad {
  24. [super viewDidLoad];
  25. // Do any additional setup after loading the view.
  26. [self initUI];
  27. [self prepareViewDidLoad];
  28. }
  29. - (void)initUI {
  30. _elapsedSeconds = 60;
  31. }
  32. - (void)viewDidAppear:(BOOL)animated {
  33. [super viewDidAppear:animated];
  34. [UIView animateWithDuration:kfAnimationDur animations:^{
  35. _maskView.alpha = 0.7;
  36. } completion:^(BOOL finished) {
  37. _constraintPopViewTop.constant = (IPHONE_HEIGHT - _popView.height) / 2;
  38. [UIView animateWithDuration:kfAnimationDur animations:^{
  39. [self.view layoutIfNeeded];
  40. }];
  41. }];
  42. }
  43. - (void)prepareViewDidLoad {
  44. if (_addableDevice) {
  45. _lblDesc.text = [NSString stringWithFormat:@"%@의\n00 버튼을\n00 누르세요", _addableDevice[@"deviceName"]];
  46. [self requestIncludeDevice];
  47. } else if (_removableDevice) {
  48. _lblDesc.text = [NSString stringWithFormat:@"%@의\n00 버튼을\n00 누르세요", _removableDevice.deviceName];
  49. [self requestExcludeDevice];
  50. }
  51. }
  52. #pragma mark - Main Logic
  53. - (void)requestIncludeDevice {
  54. NSString *path = [NSString stringWithFormat:API_POST_DEVICE_INCLUSION, [JDFacade facade].loginUser.homehubDeviceId];
  55. [[RequestHandler handler] sendAsyncPostRequestAPIPath:path parameters:nil modelClass:[DeviceModel class] completion:^(id responseObject) {
  56. if (!responseObject) {//응답결과가 잘못되었거나 없을 경우,
  57. return;
  58. }
  59. DeviceModel *device = (DeviceModel *)responseObject;
  60. if (device) {//API 성공 ,
  61. [self requestCheckInclustion:device.commandId];
  62. }
  63. } failure:^(id errorObject) {
  64. JDErrorModel *error = (JDErrorModel *)errorObject;
  65. [[JDFacade facade] alert:error.errorMessage];
  66. }];
  67. }
  68. - (void)requestExcludeDevice {
  69. NSString *path = [NSString stringWithFormat:API_DELETE_DEVICE_EXCLUSION, [JDFacade facade].loginUser.homehubDeviceId];
  70. [[RequestHandler handler] sendAsyncRequestAPIPath:path method:ksHTTPRequestDELETE parameters:nil modelClass:[DeviceModel class] showLoadingView:YES completion:^(id responseObject) {
  71. if (!responseObject) {//응답결과가 잘못되었거나 없을 경우,
  72. return;
  73. }
  74. DeviceModel *device = (DeviceModel *)responseObject;
  75. if (device) {//API 성공 ,
  76. [self requestCheckInclustion:device.commandId];
  77. }
  78. } failure:^(id errorObject) {
  79. JDErrorModel *error = (JDErrorModel *)errorObject;
  80. [[JDFacade facade] alert:error.errorMessage];
  81. }];
  82. }
  83. - (void)requestCheckInclustion:(NSString *)commandId {
  84. if (!_timer) {
  85. _timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(updateInclusionStatus) userInfo:nil repeats:YES];
  86. // _timer = [NSTimer timerWithTimeInterval:1.0f
  87. // target:self
  88. // selector:@selector(updateInclusionStatus)
  89. // userInfo:nil repeats:YES];
  90. // [[NSRunLoop mainRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
  91. }
  92. //parameters
  93. NSDictionary *parameter = @{@"command_id": commandId};
  94. NSString *path = [NSString stringWithFormat:API_GET_DEVICE_COMMAND, [JDFacade facade].loginUser.homehubDeviceId];
  95. [[RequestHandler handler] sendAsyncRequestAPIPath:path method:ksHTTPRequestGET parameters:parameter
  96. modelClass:[DeviceModel class] showLoadingView:NO completion:^(id responseObject) {
  97. if (!responseObject) {//응답결과가 잘못되었거나 없을 경우,
  98. return;
  99. }
  100. DeviceModel *device = (DeviceModel *)responseObject;
  101. if (device) {//API 성공 ,
  102. if ([device.status isEqualToString:KNEET_DEVICE_WAITING]) {
  103. if (_elapsedSeconds > 2) {
  104. [self performSelector:@selector(requestCheckInclustion:) withObject:commandId afterDelay:3.0f];
  105. }
  106. //retry
  107. return;
  108. } else if ([device.status isEqualToString:KNEET_DEVICE_INCLUSION] || [device.status isEqualToString:KNEET_DEVICE_EXCLUSION]) {
  109. //success
  110. if (_timer) {
  111. [_timer invalidate];
  112. _timer = nil;
  113. }
  114. ThingsAddCompleteViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"ThingsAddCompleteViewController" storyboardName:@"Things"];
  115. vc.isDeleteMode = [device.status isEqualToString:KNEET_DEVICE_EXCLUSION];
  116. vc.providesPresentationContextTransitionStyle = YES;
  117. vc.definesPresentationContext = YES;
  118. [vc setModalPresentationStyle:UIModalPresentationOverCurrentContext];
  119. [self presentViewController:vc animated:YES completion:nil];
  120. }
  121. }
  122. } failure:^(id errorObject) {
  123. JDErrorModel *error = (JDErrorModel *)errorObject;
  124. [[JDFacade facade] alert:error.errorMessage completionHander:^{
  125. [self didFailInclusion];
  126. }];
  127. }];
  128. }
  129. - (void)didFailInclusion {
  130. if (_timer) {
  131. [_timer invalidate];
  132. _timer = nil;
  133. }
  134. ThingsAddFailViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"ThingsAddFailViewController" storyboardName:@"Things"];
  135. vc.addableDevice = _addableDevice;
  136. vc.providesPresentationContextTransitionStyle = YES;
  137. vc.definesPresentationContext = YES;
  138. [vc setModalPresentationStyle:UIModalPresentationOverCurrentContext];
  139. UIViewController *pvc = self.presentingViewController;
  140. [self dismissViewControllerAnimated:NO completion:^{//TODO : UI Confirm with Mr.Mo
  141. [pvc presentViewController:vc animated:YES completion:nil];
  142. }];
  143. }
  144. - (void)updateInclusionStatus {
  145. dispatch_async(dispatch_get_main_queue(), ^(void) {
  146. _lblElapsedTime.text = [NSString stringWithFormat:@"%zd", _elapsedSeconds];
  147. _elapsedSeconds--;
  148. if (_elapsedSeconds == 0) {
  149. [self didFailInclusion];
  150. }
  151. });
  152. }
  153. #pragma mark - UI Events
  154. - (IBAction)btnCancelTouched:(id)sender {
  155. if (_timer) {
  156. [_timer invalidate];
  157. _timer = nil;
  158. }
  159. [UIView animateWithDuration:kfAnimationDur animations:^{
  160. _maskView.alpha = 0.0;
  161. } completion:^(BOOL finished) {
  162. [[JDFacade facade] dismissModalStack:NO completion:^{
  163. [[JDFacade facade] toast:@"취소되었습니다"];
  164. }];
  165. }];
  166. }
  167. #pragma mark - MemoryWarning
  168. - (void)didReceiveMemoryWarning
  169. {
  170. [super didReceiveMemoryWarning];
  171. // Dispose of any resources that can be recreated.
  172. }
  173. @end