// // SyncPanelController.m // Perforce // // Created by Adam Czubernat on 15.07.2013. // Copyright (c) 2013 Perforce Software, Inc. All rights reserved. // #import "SyncPanelController.h" #import "P4Workspace.h" #import "ErrorPanelController.h" @interface SyncPanelController () { BOOL indeterminate; __weak IBOutlet NSView *indeterminateView; __weak IBOutlet NSView *progressView; __weak IBOutlet NSView *successView; __weak IBOutlet NSProgressIndicator *indeterminateIndicator; __weak IBOutlet NSProgressIndicator *progressIndicator; __weak IBOutlet NSTextField *progressLabel; __weak IBOutlet NSImageView *progressSyncImage; __weak IBOutlet NSTextField *successLabel; } - (void)synchronize; - (void)showIndeterminate; - (void)showProgress; - (void)updateProgress; - (void)syncStartedNotification:(NSNotification *)notification; - (void)syncFinishedNotification:(NSNotification *)notification; - (void)syncProgressNotification:(NSNotification *)notification; - (IBAction)cancelButtonPressed:(id)sender; - (IBAction)hideButtonPressed:(id)sender; @end @implementation SyncPanelController - (void)windowDidLoad { [super windowDidLoad]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(syncStartedNotification:) name:P4SyncStartedNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(syncFinishedNotification:) name:P4SyncFinishedNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(syncProgressNotification:) name:P4SyncProgressNotification object:nil]; // Check if not already synchronizing if (![[P4Workspace sharedInstance] isSynchronizing]) [self synchronize]; // Check if progress is determined if ([P4Workspace sharedInstance].syncOperation.total) [self showProgress]; else [self showIndeterminate]; } - (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self]; } #pragma mark - Private - (void)synchronize { PSLog(@"Manual sync"); [[P4Workspace sharedInstance] syncWorkspace:nil response:nil]; } - (void)showIndeterminate { indeterminate = YES; [indeterminateIndicator startAnimation:nil]; [self setPresentedView:indeterminateView]; } - (void)showProgress { indeterminate = NO; [indeterminateIndicator stopAnimation:nil]; [self setPresentedView:progressView animated:YES]; [self updateProgress]; // Animate image progressSyncImage.wantsLayer = YES; CAKeyframeAnimation *anim = [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation.z"]; CAMediaTimingFunction *timing = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; anim.duration = 1.5f; anim.repeatCount = HUGE_VALF; anim.values = @[ @(0.0f), @(-M_PI) ]; anim.keyTimes = @[ @(0.0f), @(0.9f) ]; anim.timingFunctions = @[ timing, timing ]; anim.removedOnCompletion = NO; [progressSyncImage.layer addAnimation:anim forKey:@"rotation"]; } - (void)updateProgress { P4Operation *operation = [[P4Workspace sharedInstance] syncOperation]; if (!operation.total) { progressLabel.stringValue = @"Loading..."; return; } CGFloat progress = operation.progress; progressIndicator.doubleValue = progress * 100.0f; NSTimeInterval time = PSTimeInterval(operation.timestamp); NSTimeInterval eta = time / progress - time; progressLabel.stringValue = [NSString stringWithFormat: @"%.1f%% (%.2f of %.2f MB) – %@ remaining", progress * 100.0f, operation.completed/1024/1024.0f, operation.total/1024/1024.0f, [NSString stringWithTimeInterval:eta]]; } - (void)syncStartedNotification:(NSNotification *)notification { } - (void)syncFinishedNotification:(NSNotification *)notification { [progressSyncImage.layer removeAllAnimations]; [indeterminateIndicator stopAnimation:nil]; P4Operation *operation = notification.object; NSArray *upToDate = [operation errorsWithCode:P4ErrorFileUpToDate]; [operation ignoreErrors:upToDate]; if (operation.errors) { [self dismiss]; if (![operation errorsWithCode:P4ErrorSessionExpired]) { ErrorPanelController *alert = [[ErrorPanelController alloc] init]; [alert setTitle:@"\nSynchronization failed"]; [alert setMessage:operation.error.localizedDescription]; [alert presentWithMainWindow]; } } else { if (upToDate) successLabel.stringValue = @"Files are up-to-date"; [self setPresentedView:successView animated:YES]; [self dismissAfter:2.0f]; } } - (void)syncProgressNotification:(NSNotification *)notification { if (indeterminate) [self showProgress]; // Switch to progress [self updateProgress]; } #pragma mark - Actions - (IBAction)cancelButtonPressed:(id)sender { PSLog(@"Sync Cancel pressed"); [[P4Workspace sharedInstance].syncOperation cancel]; } - (IBAction)hideButtonPressed:(id)sender { [self dismiss]; } @end