ThingsAddIncludeViewController.m 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 = 64;
  38. [UIView animateWithDuration:kfAnimationDur animations:^{
  39. [self.view layoutIfNeeded];
  40. }];
  41. }];
  42. }
  43. - (void)prepareViewDidLoad {
  44. [self requestIncludeDevice];
  45. }
  46. #pragma mark - Main Logic
  47. - (void)requestIncludeDevice {
  48. NSString *path = [NSString stringWithFormat:API_POST_DEVICE_INCLUSION, [JDFacade facade].loginUser.homehubDeviceId];
  49. [[RequestHandler handler] sendAsyncPostRequestAPIPath:path parameters:nil modelClass:[DeviceModel class] completion:^(id responseObject) {
  50. if (!responseObject) {//응답결과가 잘못되었거나 없을 경우,
  51. return;
  52. }
  53. DeviceModel *device = (DeviceModel *)responseObject;
  54. if (device) {//API 성공 ,
  55. [self requestCheckInclustion:device.commandId];
  56. }
  57. } failure:^(id errorObject) {
  58. JDErrorModel *error = (JDErrorModel *)errorObject;
  59. [[JDFacade facade] alert:error.errorMessage];
  60. }];
  61. }
  62. - (void)requestCheckInclustion:(NSString *)commandId {
  63. if (!_timer) {
  64. _timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(updateInclusionStatus) userInfo:nil repeats:YES];
  65. // _timer = [NSTimer timerWithTimeInterval:1.0f
  66. // target:self
  67. // selector:@selector(updateInclusionStatus)
  68. // userInfo:nil repeats:YES];
  69. // [[NSRunLoop mainRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
  70. }
  71. //parameters
  72. NSDictionary *parameter = @{@"command_id": commandId};
  73. NSString *path = [NSString stringWithFormat:API_GET_DEVICE_COMMAND, [JDFacade facade].loginUser.homehubDeviceId];
  74. [[RequestHandler handler] sendAsyncRequestAPIPath:path method:ksHTTPRequestGET parameters:parameter
  75. modelClass:[DeviceModel class] showLoadingView:NO completion:^(id responseObject) {
  76. if (!responseObject) {//응답결과가 잘못되었거나 없을 경우,
  77. return;
  78. }
  79. DeviceModel *device = (DeviceModel *)responseObject;
  80. if (device) {//API 성공 ,
  81. if ([device.status isEqualToString:KNEET_DEVICE_WAITING]) {
  82. if (_elapsedSeconds > 2) {
  83. [self performSelector:@selector(requestCheckInclustion:) withObject:commandId afterDelay:3.0f];
  84. }
  85. //retry
  86. return;
  87. } else if ([device.status isEqualToString:KNEET_DEVICE_INCLUSION]) {
  88. //success
  89. if (_timer) {
  90. [_timer invalidate];
  91. _timer = nil;
  92. }
  93. ThingsAddCompleteViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"ThingsAddCompleteViewController" storyboardName:@"Things"];
  94. vc.providesPresentationContextTransitionStyle = YES;
  95. vc.definesPresentationContext = YES;
  96. [vc setModalPresentationStyle:UIModalPresentationOverCurrentContext];
  97. [self presentViewController:vc animated:YES completion:nil];
  98. }
  99. }
  100. } failure:^(id errorObject) {
  101. JDErrorModel *error = (JDErrorModel *)errorObject;
  102. [[JDFacade facade] alert:error.errorMessage completionHander:^{
  103. [self didFailInclusion];
  104. }];
  105. }];
  106. }
  107. - (void)didFailInclusion {
  108. if (_timer) {
  109. [_timer invalidate];
  110. _timer = nil;
  111. }
  112. ThingsAddFailViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"ThingsAddFailViewController" storyboardName:@"Things"];
  113. vc.addableDevice = _addableDevice;
  114. vc.providesPresentationContextTransitionStyle = YES;
  115. vc.definesPresentationContext = YES;
  116. [vc setModalPresentationStyle:UIModalPresentationOverCurrentContext];
  117. UIViewController *pvc = self.presentingViewController;
  118. [self dismissViewControllerAnimated:NO completion:^{//TODO : UI Confirm with Mr.Mo
  119. [pvc presentViewController:vc animated:YES completion:nil];
  120. }];
  121. }
  122. - (void)updateInclusionStatus {
  123. dispatch_async(dispatch_get_main_queue(), ^(void) {
  124. _lblElapsedTime.text = [NSString stringWithFormat:@"%zd", _elapsedSeconds];
  125. _elapsedSeconds--;
  126. if (_elapsedSeconds == 0) {
  127. [self didFailInclusion];
  128. }
  129. });
  130. }
  131. #pragma mark - UI Events
  132. - (IBAction)btnCancelTouched:(id)sender {
  133. if (_timer) {
  134. [_timer invalidate];
  135. _timer = nil;
  136. }
  137. [UIView animateWithDuration:kfAnimationDur animations:^{
  138. _maskView.alpha = 0.0;
  139. } completion:^(BOOL finished) {
  140. [[JDFacade facade] dismissModalStack:NO completion:^{
  141. [[JDFacade facade] toast:@"취소되었습니다"];
  142. }];
  143. }];
  144. }
  145. #pragma mark - MemoryWarning
  146. - (void)didReceiveMemoryWarning
  147. {
  148. [super didReceiveMemoryWarning];
  149. // Dispose of any resources that can be recreated.
  150. }
  151. @end