HomeHubSearchViewController.m 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. //
  2. // HomeHubSearchViewController.m
  3. // OneCable
  4. //
  5. // Created by nComz on 2017. 5. 11..
  6. // Copyright © 2017년 ntels. All rights reserved.
  7. //
  8. #import "HomeHubSearchViewController.h"
  9. @interface HomeHubSearchViewController () {
  10. NSTimer *_timer;
  11. NSInteger _elapsedSeconds;
  12. }
  13. @end
  14. @implementation HomeHubSearchViewController
  15. - (void)viewDidLoad {
  16. [super viewDidLoad];
  17. [self initUI];
  18. [self prepareViewDidLoad];
  19. }
  20. - (void)didReceiveMemoryWarning {
  21. [super didReceiveMemoryWarning];
  22. }
  23. - (void)initUI {
  24. [self startLoading:YES];
  25. }
  26. - (void)prepareViewDidLoad {
  27. //ble
  28. BLEServiceHandler *bleService = [BLEServiceHandler sharedManager];
  29. bleService.delegate = self;
  30. [bleService startScan];
  31. }
  32. -(void)startLoading:(BOOL)isStart {
  33. _lblTimer.hidden = !isStart;
  34. _imgvLoading.hidden = !isStart;
  35. if (isStart) {
  36. _elapsedSeconds = kMaxTimeOut;
  37. _lblTimer.text = [NSString stringWithFormat:@"%zd", _elapsedSeconds];
  38. if (!_timer) {
  39. _timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(updateInclusionStatus) userInfo:nil repeats:YES];
  40. }
  41. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  42. if ([_imgvLoading.layer animationForKey:@"SpinAnimation"] == nil) {
  43. CABasicAnimation* animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
  44. animation.fromValue = [NSNumber numberWithFloat:0.0f];
  45. animation.toValue = [NSNumber numberWithFloat: 2*M_PI];
  46. animation.duration = 2.0f;
  47. animation.repeatCount = INFINITY;
  48. [_imgvLoading.layer addAnimation:animation forKey:@"SpinAnimation"];
  49. }
  50. });
  51. }
  52. else {
  53. _elapsedSeconds = kMaxTimeOut;
  54. if (_timer) {
  55. [_timer invalidate];
  56. _timer = nil;
  57. }
  58. if ([_imgvLoading.layer animationForKey:@"SpinAnimation"] != nil) {
  59. [_imgvLoading.layer removeAnimationForKey:@"SpinAnimation"];
  60. }
  61. }
  62. }
  63. - (void)updateInclusionStatus {
  64. dispatch_async(dispatch_get_main_queue(), ^(void) {
  65. _elapsedSeconds--;
  66. _lblTimer.text = [NSString stringWithFormat:@"%zd", _elapsedSeconds];
  67. // if (_elapsedSeconds == 0) {
  68. // [self finishDeviceDel:NO];
  69. // }
  70. });
  71. }
  72. #pragma mark - ble service delegate
  73. - (void)BLEEndScan:(NSMutableArray *)devices {
  74. UIViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"HomeHubSearchSuccessViewController" storyboardName:@"HomeHub"];
  75. if (devices.count == 0) {
  76. vc = [CommonUtil instantiateViewControllerWithIdentifier:@"HomeHubSearchFailViaewController" storyboardName:@"HomeHub"];
  77. }
  78. [self presentViewController:vc animated:YES completion:nil];
  79. }
  80. #pragma mark - User Event
  81. - (IBAction)btnCloseTouched:(id)sender {
  82. [self dismissViewControllerAnimated:YES completion:nil];
  83. }
  84. @end