call: add connecting animation

This commit adds an animated circular progress view when the state is in
Initialization or Connected states.
We also use LRC's HumanStateName instead of hardcoding call state in the client

Refs #75634

Change-Id: I574838d624ba9705011c463ccabc5c78857193b4
diff --git a/src/CurrentCallVC.mm b/src/CurrentCallVC.mm
index 078e617..7971a6f 100644
--- a/src/CurrentCallVC.mm
+++ b/src/CurrentCallVC.mm
@@ -42,6 +42,7 @@
 #import <video/renderer.h>
 #import <media/text.h>
 
+#import "views/ITProgressIndicator.h"
 #import "views/CallView.h"
 
 @interface RendererConnectionsHolder : NSObject
@@ -67,6 +68,7 @@
 @property (unsafe_unretained) IBOutlet NSButton *muteAudioButton;
 @property (unsafe_unretained) IBOutlet NSButton *muteVideoButton;
 
+@property (unsafe_unretained) IBOutlet ITProgressIndicator *loadingIndicator;
 
 @property (unsafe_unretained) IBOutlet NSTextField *timeSpentLabel;
 @property (unsafe_unretained) IBOutlet NSView *controlsPanel;
@@ -91,7 +93,7 @@
 @synthesize personLabel, actionHash, stateLabel, holdOnOffButton, hangUpButton,
             recordOnOffButton, pickUpButton, chatButton, timeSpentLabel,
             muteVideoButton, muteAudioButton, controlsPanel, videoView,
-            videoLayer, previewLayer, previewView, splitView;
+            videoLayer, previewLayer, previewView, splitView, loadingIndicator;
 
 @synthesize previewHolder;
 @synthesize videoHolder;
@@ -130,53 +132,42 @@
     [timeSpentLabel setStringValue:callIdx.data((int)Call::Role::Length).toString().toNSString()];
 
     Call::State state = callIdx.data((int)Call::Role::State).value<Call::State>();
-
+    [loadingIndicator setHidden:YES];
+    [stateLabel setStringValue:callIdx.data((int)Call::Role::HumanStateName).toString().toNSString()];
     switch (state) {
         case Call::State::DIALING:
-            [stateLabel setStringValue:@"Dialing"];
+            [loadingIndicator setHidden:NO];
             break;
         case Call::State::NEW:
-            [stateLabel setStringValue:@"New"];
             break;
         case Call::State::INITIALIZATION:
-            [stateLabel setStringValue:@"Initializing"];
             [videoView setShouldAcceptInteractions:NO];
+            [loadingIndicator setHidden:NO];
+            break;
+        case Call::State::CONNECTED:
+            [videoView setShouldAcceptInteractions:NO];
+            [loadingIndicator setHidden:NO];
             break;
         case Call::State::RINGING:
-            [stateLabel setStringValue:@"Ringing"];
             [videoView setShouldAcceptInteractions:NO];
             break;
         case Call::State::CURRENT:
-            [stateLabel setStringValue:@"Current"];
             [videoView setShouldAcceptInteractions:YES];
             break;
         case Call::State::HOLD:
-            [stateLabel setStringValue:@"On Hold"];
             [videoView setShouldAcceptInteractions:NO];
             break;
         case Call::State::BUSY:
-            [stateLabel setStringValue:@"Busy"];
             [videoView setShouldAcceptInteractions:NO];
             break;
         case Call::State::OVER:
-            [stateLabel setStringValue:@"Finished"];
             [videoView setShouldAcceptInteractions:NO];
             if(videoView.isInFullScreenMode)
                 [videoView exitFullScreenModeWithOptions:nil];
             break;
-        case Call::State::ABORTED:
-            [stateLabel setStringValue:@"Aborted"];
-            break;
         case Call::State::FAILURE:
-            [stateLabel setStringValue:@"Failure"];
             [videoView setShouldAcceptInteractions:NO];
             break;
-        case Call::State::INCOMING:
-            [stateLabel setStringValue:@"Incoming"];
-            break;
-        default:
-            [stateLabel setStringValue:@""];
-            break;
     }
 
 }
@@ -221,6 +212,12 @@
     previewHolder = [[RendererConnectionsHolder alloc] init];
     videoHolder = [[RendererConnectionsHolder alloc] init];
 
+    [loadingIndicator setColor:[NSColor whiteColor]];
+    [loadingIndicator setNumberOfLines:100];
+    [loadingIndicator setWidthOfLine:2];
+    [loadingIndicator setLengthOfLine:2];
+    [loadingIndicator setInnerMargin:30];
+
     [self.videoView setFullScreenDelegate:self];
 
     [self connect];
diff --git a/src/views/ITProgressIndicator.h b/src/views/ITProgressIndicator.h
new file mode 100644
index 0000000..59ee8ea
--- /dev/null
+++ b/src/views/ITProgressIndicator.h
@@ -0,0 +1,88 @@
+//Copyright 2013-2015 Ilija Tovilo
+//
+//Licensed under the Apache License, Version 2.0 (the "License");
+//you may not use this file except in compliance with the License.
+//You may obtain a copy of the License at
+//
+//http://www.apache.org/licenses/LICENSE-2.0
+//
+//Unless required by applicable law or agreed to in writing, software
+//distributed under the License is distributed on an "AS IS" BASIS,
+//WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+//See the License for the specific language governing permissions and
+//limitations under the License.
+
+
+#import <Cocoa/Cocoa.h>
+#import <QuartzCore/QuartzCore.h>
+
+//
+// !!!IMPORTANT!!! - Embedd ITProgressIndicator in a layer-backed view to avoid side-effects!
+//
+
+/**
+ *  @class ITProgressIndicator
+ *
+ *  A replacement for `NSProgressIndicator`.
+ *  It's a highly customizable control, driven by Core Animation, which makes it much more performant.
+ *
+ *  So basically, it's awesome.
+ *
+ */
+@interface ITProgressIndicator : NSView
+
+#pragma mark - Methods
+
+/**
+ *  Override this method to achieve a custom animation
+ *
+ *  @return CAKeyframeAnimation - animation which will be put on the progress indicator layer
+ */
+- (CAKeyframeAnimation *)keyFrameAnimationForCurrentPreferences;
+
+#pragma mark - Properties
+
+
+/// @property isIndeterminate - Indicates if the view will show the progress, or just spin
+@property (nonatomic, setter = setIndeterminate:) BOOL isIndeterminate;
+
+
+/// @property progress - The amount that should be shown when `isIndeterminate` is set to `YES`
+@property (nonatomic) CGFloat progress;
+
+
+/// @property animates - Indicates if the view is animating
+@property (nonatomic) BOOL animates;
+
+
+/// @property hideWhenStopped - Indicates if the view will be hidden if it's stopped
+@property (nonatomic) BOOL hideWhenStopped;
+
+
+/// @property lengthOfLine - The length of a single line
+@property (nonatomic) CGFloat lengthOfLine;
+
+
+/// @property widthOfLine - The width of a single line
+@property (nonatomic) CGFloat widthOfLine;
+
+
+/// @property numberOfLines - The number of lines of the indicator
+@property (nonatomic) NSUInteger numberOfLines;
+
+
+/// @property innerMargin - The distance of the lines from the middle
+@property (nonatomic) CGFloat innerMargin;
+
+
+/// @property animationDuration - Duration of a single rotation
+@property (nonatomic) CGFloat animationDuration;
+
+
+/// @property gradualAnimation - Defines if the animation is smooth or gradual
+@property (nonatomic) BOOL steppedAnimation;
+
+/// @property color - The color of the progress indicator
+@property (nonatomic, strong) NSColor *color;
+
+@end
diff --git a/src/views/ITProgressIndicator.mm b/src/views/ITProgressIndicator.mm
new file mode 100644
index 0000000..189ea79
--- /dev/null
+++ b/src/views/ITProgressIndicator.mm
@@ -0,0 +1,349 @@
+//Copyright 2013-2015 Ilija Tovilo
+//
+//Licensed under the Apache License, Version 2.0 (the "License");
+//you may not use this file except in compliance with the License.
+//You may obtain a copy of the License at
+//
+//http://www.apache.org/licenses/LICENSE-2.0
+//
+//Unless required by applicable law or agreed to in writing, software
+//distributed under the License is distributed on an "AS IS" BASIS,
+//WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+//See the License for the specific language governing permissions and
+//limitations under the License.
+
+#if !__has_feature(objc_arc)
+#error ARC needs to be enabled!
+#endif
+
+
+#import "ITProgressIndicator.h"
+
+
+#pragma mark - Consts
+#define kITSpinAnimationKey @"spinAnimation"
+#define kITProgressPropertyKey @"progress"
+
+
+// ----------------------------------------------------------------------------------------
+#pragma mark - NSBezierPath+IT_Geometry
+// ----------------------------------------------------------------------------------------
+
+@interface NSBezierPath (IT_Geometry)
+
+- (NSBezierPath*)it_rotatedBezierPath:(float) angle;
+- (NSBezierPath*)it_rotatedBezierPath:(float) angle aboutPoint:(NSPoint)point;
+
+@end
+
+@implementation NSBezierPath (IT_Geometry)
+
+- (NSBezierPath *)it_rotatedBezierPath:(float)angle {
+	return [self it_rotatedBezierPath:angle aboutPoint:NSMakePoint(NSMidX(self.bounds), NSMidY(self.bounds))];
+}
+
+- (NSBezierPath*)it_rotatedBezierPath:(float)angle aboutPoint:(NSPoint)point {
+	if(angle == 0.0) return self;
+	else
+	{
+		NSBezierPath* copy = [self copy];
+		NSAffineTransform *xfm = [self it_rotationTransformWithAngle:angle aboutPoint:point];
+		[copy transformUsingAffineTransform:xfm];
+
+		return copy;
+	}
+}
+
+- (NSAffineTransform *)it_rotationTransformWithAngle:(const float)angle aboutPoint:(const NSPoint)aboutPoint {
+	NSAffineTransform *xfm = [NSAffineTransform transform];
+	[xfm translateXBy:aboutPoint.x yBy:aboutPoint.y];
+	[xfm rotateByRadians:angle];
+	[xfm translateXBy:-aboutPoint.x yBy:-aboutPoint.y];
+
+	return xfm;
+}
+
+@end
+
+
+
+
+// ----------------------------------------------------------------------------------------
+#pragma mark - ITProgressIndicator
+// ----------------------------------------------------------------------------------------
+
+#pragma mark - Private Interface
+
+@interface ITProgressIndicator ()
+@property (nonatomic, strong, readonly) CALayer *rootLayer;
+@property (nonatomic, strong, readonly) CALayer *progressIndicatorLayer;
+@end
+
+
+#pragma mark - Implementation
+
+@implementation ITProgressIndicator
+@synthesize progressIndicatorLayer = _progressIndicatorLayer;
+
+
+#pragma mark - Init
+
+- (id)initWithCoder:(NSCoder *)coder
+{
+    self = [super initWithCoder:coder];
+    if (self) {
+        [self initLayers];
+    }
+    return self;
+}
+
+- (id)initWithFrame:(NSRect)frame
+{
+    self = [super initWithFrame:frame];
+    if (self) {
+        [self initLayers];
+    }
+    return self;
+}
+
+- (void)initLayers {
+    // Setting initial values
+    self.color = [NSColor blackColor];
+    self.innerMargin = 4;
+	self.widthOfLine = 3;
+    self.lengthOfLine = 6;
+    self.numberOfLines = 8;
+    self.animationDuration = 0.6;
+    self.isIndeterminate = YES;
+    self.steppedAnimation = YES;
+    self.hideWhenStopped = YES;
+    self.animates = YES;
+
+    // Init layers
+    _rootLayer = [CALayer layer];
+    self.layer = _rootLayer;
+    [self setWantsLayer:YES];
+    self.progressIndicatorLayer.frame = _rootLayer.bounds;
+    [_rootLayer addSublayer:self.progressIndicatorLayer];
+
+    [self reloadIndicatorContent];
+    [self reloadAnimation];
+}
+
+- (void)awakeFromNib {
+    [self reloadAnimation];
+}
+
+- (void)reloadIndicatorContent {
+    self.progressIndicatorLayer.contents = [self progressImage];
+}
+
+- (void)reloadAnimation {
+    [self.progressIndicatorLayer removeAnimationForKey:kITSpinAnimationKey];
+
+    if (self.animates) {
+        [self.progressIndicatorLayer addAnimation:[self keyFrameAnimationForCurrentPreferences] forKey:kITSpinAnimationKey];
+    }
+}
+
+
+#pragma mark - Drawing
+
+- (NSImage *)progressImage {
+    NSImage *progressImage = [[NSImage alloc] initWithSize:self.bounds.size];
+    [progressImage lockFocus];
+    {
+        [NSGraphicsContext saveGraphicsState];
+        {
+            [self.color set];
+
+            NSRect r = self.bounds;
+            NSBezierPath *line = [NSBezierPath bezierPathWithRoundedRect:
+                                  NSMakeRect((NSWidth(r) / 2) - (self.widthOfLine / 2),
+                                             (NSHeight(r) / 2) - self.innerMargin - self.lengthOfLine,
+                                             self.widthOfLine, self.lengthOfLine)
+                                                                 xRadius:self.widthOfLine / 2
+                                                                 yRadius:self.widthOfLine / 2];
+
+            void (^lineDrawingBlock)(NSUInteger line) =
+            ^(NSUInteger lineNumber) {
+                NSBezierPath *lineInstance = [line copy];
+                lineInstance = [lineInstance it_rotatedBezierPath:((2 * M_PI) / self.numberOfLines * lineNumber) + M_PI
+                                                       aboutPoint:NSMakePoint(NSWidth(r) / 2, NSHeight(r) / 2)];
+
+                if (_isIndeterminate) [[self.color colorWithAlphaComponent:1.0 - (1.0 / self.numberOfLines * lineNumber)] set];
+
+                [lineInstance fill];
+            };
+
+            if (!self.isIndeterminate) {
+                for (NSUInteger i = self.numberOfLines;
+                     i > round(self.numberOfLines - (self.numberOfLines * self.progress));
+                     i--)
+                {
+                    lineDrawingBlock(i);
+                }
+            } else {
+                for (NSUInteger i = 0; i < self.numberOfLines; i++) {
+                    lineDrawingBlock(i);
+                }
+            }
+        }
+        [NSGraphicsContext restoreGraphicsState];
+    }
+    [progressImage unlockFocus];
+
+    return progressImage;
+}
+
+
+#pragma mark - Helpers
+
+- (CAKeyframeAnimation *)keyFrameAnimationForCurrentPreferences {
+    NSMutableArray* keyFrameValues = [NSMutableArray array];
+    NSMutableArray* keyTimeValues;
+
+    if (self.steppedAnimation) {
+        {
+            [keyFrameValues addObject:[NSNumber numberWithFloat:0.0]];
+            for (NSUInteger i = 0; i < self.numberOfLines; i++) {
+                [keyFrameValues addObject:[NSNumber numberWithFloat:-M_PI * (2.0 / self.numberOfLines * i)]];
+                [keyFrameValues addObject:[NSNumber numberWithFloat:-M_PI * (2.0 / self.numberOfLines * i)]];
+            }
+            [keyFrameValues addObject:[NSNumber numberWithFloat:-M_PI*2.0]];
+        }
+
+        keyTimeValues = [NSMutableArray array];
+        {
+            [keyTimeValues addObject:[NSNumber numberWithFloat:0.0]];
+            for (NSUInteger i = 0; i < (self.numberOfLines - 1); i++) {
+                [keyTimeValues addObject:[NSNumber numberWithFloat:1.0 / self.numberOfLines * i]];
+                [keyTimeValues addObject:[NSNumber numberWithFloat:1.0 / self.numberOfLines * (i + 1)]];
+            }
+            [keyTimeValues addObject:[NSNumber numberWithFloat:1.0 / self.numberOfLines * (self.numberOfLines - 1)]];
+        }
+    } else {
+        {
+            [keyFrameValues addObject:[NSNumber numberWithFloat:-M_PI*0.0]];
+            [keyFrameValues addObject:[NSNumber numberWithFloat:-M_PI*0.5]];
+            [keyFrameValues addObject:[NSNumber numberWithFloat:-M_PI*1.0]];
+            [keyFrameValues addObject:[NSNumber numberWithFloat:-M_PI*1.5]];
+            [keyFrameValues addObject:[NSNumber numberWithFloat:-M_PI*2.0]];
+        }
+    }
+
+
+    CAKeyframeAnimation* animation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
+
+    [animation setRepeatCount:HUGE_VALF];
+    [animation setValues:keyFrameValues];
+    [animation setKeyTimes:keyTimeValues];
+    [animation setValueFunction:[CAValueFunction functionWithName: kCAValueFunctionRotateZ]];
+    [animation setDuration:self.animationDuration];
+
+    return animation;
+}
+
+- (void)reloadVisibility {
+    if (_hideWhenStopped && !_animates && _isIndeterminate) {
+        [self setHidden:YES];
+    } else {
+        [self setHidden:NO];
+    }
+}
+
+
+#pragma mark - NSView methods
+
+// Animatible proxy
++ (id)defaultAnimationForKey:(NSString *)key
+{
+    if ([key isEqualToString:kITProgressPropertyKey]) {
+        return [CABasicAnimation animation];
+    } else {
+        return [super defaultAnimationForKey:key];
+    }
+}
+
+
+#pragma mark - Setters & Getters
+
+- (void)setIndeterminate:(BOOL)isIndeterminate {
+    _isIndeterminate = isIndeterminate;
+
+    if (!_isIndeterminate) {
+        self.animates = NO;
+    }
+}
+
+- (void)setProgress:(CGFloat)progress {
+    if (progress < 0 || progress > 1) {
+        @throw [NSException exceptionWithName:@"Invalid `progress` property value"
+                                       reason:@"`progress` property needs to be between 0 and 1"
+                                     userInfo:nil];
+    }
+
+    _progress = progress;
+
+    if (!self.isIndeterminate) {
+        [self reloadIndicatorContent];
+    }
+}
+
+- (void)setAnimates:(BOOL)animates {
+    _animates = animates;
+    [self reloadIndicatorContent];
+    [self reloadAnimation];
+    [self reloadVisibility];
+}
+
+- (void)setHideWhenStopped:(BOOL)hideWhenStopped {
+    _hideWhenStopped = hideWhenStopped;
+    [self reloadVisibility];
+}
+
+- (CALayer *)progressIndicatorLayer {
+    if (!_progressIndicatorLayer) {
+        _progressIndicatorLayer = [CALayer layer];
+    }
+
+    return _progressIndicatorLayer;
+}
+
+- (void)setLengthOfLine:(CGFloat)lengthOfLine {
+    _lengthOfLine = lengthOfLine;
+    [self reloadIndicatorContent];
+}
+
+- (void)setWidthOfLine:(CGFloat)widthOfLine {
+    _widthOfLine = widthOfLine;
+    [self reloadIndicatorContent];
+}
+
+- (void)setInnerMargin:(CGFloat)innerMargin {
+    _innerMargin = innerMargin;
+    [self reloadIndicatorContent];
+}
+
+- (void)setAnimationDuration:(CGFloat)animationDuration {
+    _animationDuration = animationDuration;
+    [self reloadAnimation];
+}
+
+- (void)setNumberOfLines:(NSUInteger)numberOfLines {
+    _numberOfLines = numberOfLines;
+    [self reloadIndicatorContent];
+    [self reloadAnimation];
+}
+
+- (void)setSteppedAnimation:(BOOL)steppedAnimation {
+    _steppedAnimation = steppedAnimation;
+    [self reloadAnimation];
+}
+
+- (void)setColor:(NSColor *)color {
+    _color = color;
+    [self reloadIndicatorContent];
+}
+
+@end