// // SCCaptureFaceDetectorTrigger.m // Snapchat // // Created by Jiyang Zhu on 3/22/18. // Copyright © 2018 Snapchat, Inc. All rights reserved. // #import "SCCaptureFaceDetectorTrigger.h" #import "SCCaptureFaceDetector.h" #import #import #import #import #import @interface SCCaptureFaceDetectorTrigger () { id __weak _detector; } @end @implementation SCCaptureFaceDetectorTrigger - (instancetype)initWithDetector:(id)detector { self = [super init]; if (self) { _detector = detector; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_applicationDidBecomeActive) name:kSCPostponedUIApplicationDidBecomeActiveNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_applicationWillResignActive) name:UIApplicationWillResignActiveNotification object:nil]; } return self; } #pragma mark - Internal Methods - (void)_applicationWillResignActive { SCTraceODPCompatibleStart(2); [self _stopDetection]; } - (void)_applicationDidBecomeActive { SCTraceODPCompatibleStart(2); [self _waitUntilAppStartCompleteToStartDetection]; } - (void)_waitUntilAppStartCompleteToStartDetection { SCTraceODPCompatibleStart(2); @weakify(self); if (SCExperimentWithWaitUntilIdleReplacement()) { [[SCTaskManager sharedManager] addTaskToRunWhenAppIdle:"SCCaptureFaceDetectorTrigger.startDetection" performer:[_detector detectionPerformer] block:^{ @strongify(self); SC_GUARD_ELSE_RETURN(self); [self _startDetection]; }]; } else { [[SCIdleMonitor sharedInstance] waitUntilIdleForTag:"SCCaptureFaceDetectorTrigger.startDetection" callbackQueue:[_detector detectionPerformer].queue block:^{ @strongify(self); SC_GUARD_ELSE_RETURN(self); [self _startDetection]; }]; } } - (void)_startDetection { SCTraceODPCompatibleStart(2); [[_detector detectionPerformer] performImmediatelyIfCurrentPerformer:^{ [_detector startDetection]; }]; } - (void)_stopDetection { SCTraceODPCompatibleStart(2); [[_detector detectionPerformer] performImmediatelyIfCurrentPerformer:^{ [_detector stopDetection]; }]; } @end