UI: parametrize animating in/out VCs and disable

- corrects UI bugs produced by animating VC presentation
  without a properly coordinated view stack

Change-Id: I8b397f0ecc9ebcb6957b23d39c45a71b7e66162e
Reviewed-by: Kateryna Kostiuk <kateryna.kostiuk@savoirfairelinux.com>
diff --git a/src/ConversationVC.h b/src/ConversationVC.h
index c658882..ecc86cc 100644
--- a/src/ConversationVC.h
+++ b/src/ConversationVC.h
@@ -26,8 +26,8 @@
 @interface ConversationVC : NSViewController
 
 -(void) initFrame;
--(void) animateIn;
--(void) animateOut;
+-(void) showWithAnimation:(BOOL)animate;
+-(void) hideWithAnimation:(BOOL)animate;
 
 /**
  * Message contained in messageField TextField.
diff --git a/src/ConversationVC.mm b/src/ConversationVC.mm
index ec1fe40..c42559d 100644
--- a/src/ConversationVC.mm
+++ b/src/ConversationVC.mm
@@ -235,13 +235,18 @@
 
 - (IBAction)backPressed:(id)sender {
     [delegate rightPanelClosed];
-    [self animateOut];
+    [self hideWithAnimation:false];
 }
 
 # pragma mark private IN/OUT animations
 
--(void) animateIn
+-(void) showWithAnimation:(BOOL)animate
 {
+    if (!animate) {
+        [self.view setHidden:NO];
+        return;
+    }
+
     CGRect frame = CGRectOffset(self.view.superview.bounds, -self.view.superview.bounds.size.width, 0);
     [self.view setHidden:NO];
 
@@ -256,12 +261,17 @@
     [CATransaction commit];
 }
 
--(void) animateOut
+-(void) hideWithAnimation:(BOOL)animate
 {
     if(self.view.frame.origin.x < 0) {
         return;
     }
 
+    if (!animate) {
+        [self.view setHidden:YES];
+        return;
+    }
+
     CGRect frame = CGRectOffset(self.view.frame, -self.view.frame.size.width, 0);
     [CATransaction begin];
     CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
diff --git a/src/CurrentCallVC.h b/src/CurrentCallVC.h
index 8caf624..d59c77f 100644
--- a/src/CurrentCallVC.h
+++ b/src/CurrentCallVC.h
@@ -33,8 +33,8 @@
 }
 
 -(void) initFrame;
--(void) animateIn;
--(void) animateOut;
+-(void) showWithAnimation:(BOOL)animate;
+-(void) hideWithAnimation:(BOOL)animate;
 -(void) setCurrentCall:(const std::string&)callUid
           conversation:(const std::string&)convUid
                account:(const lrc::api::account::Info*)account;
diff --git a/src/CurrentCallVC.mm b/src/CurrentCallVC.mm
index 568019a..0993f93 100644
--- a/src/CurrentCallVC.mm
+++ b/src/CurrentCallVC.mm
@@ -271,7 +271,7 @@
         case Status::INVALID:
             [controlsPanel setHidden:YES];
             [outgoingPanel setHidden:NO];
-            [self animateOut];
+            [self hideWithAnimation:false];
             break;
     }
 
@@ -504,55 +504,6 @@
     [self.chatVC takeFocus];
 }
 
--(void) animateIn
-{
-    CGRect frame = CGRectOffset(self.view.superview.bounds, -self.view.superview.bounds.size.width, 0);
-    [self.view setHidden:NO];
-
-    [CATransaction begin];
-    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
-    [animation setFromValue:[NSValue valueWithPoint:frame.origin]];
-    [animation setToValue:[NSValue valueWithPoint:self.view.superview.bounds.origin]];
-    [animation setDuration:0.2f];
-    [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]];
-    [CATransaction setCompletionBlock:^{
-        if (accountInfo_ == nil)
-            return;
-
-        auto* callModel = accountInfo_->callModel.get();
-        auto* convModel = accountInfo_->conversationModel.get();
-
-        // when call comes in we want to show the controls/header
-        [self mouseIsMoving:YES];
-
-        [self connectVideoSignals];
-        /* check if text media is already present */
-        if(not callModel->hasCall(callUid_))
-            return;
-
-        [loadingIndicator setAnimates:YES];
-        [self updateCall:YES];
-
-        /* monitor media for messaging text messaging */
-        QObject::disconnect(self.messageConnection);
-        self.messageConnection = QObject::connect(convModel,
-                                                  &lrc::api::ConversationModel::interactionStatusUpdated,
-                                                  [self] (std::string convUid,
-                                                          uint64_t msgId,
-                                                          lrc::api::interaction::Info msg) {
-                                                      if (msg.type == lrc::api::interaction::Type::TEXT) {
-                                                          if(not [[self splitView] isSubviewCollapsed:[[[self splitView] subviews] objectAtIndex: 1]]){
-                                                              return;
-                                                          }
-                                                          [self uncollapseRightView];
-                                                      }
-                                                  });
-    }];
-
-    [self.view.layer addAnimation:animation forKey:animation.keyPath];
-    [CATransaction commit];
-}
-
 -(void) cleanUp
 {
     if(self.splitView.isInFullScreenMode)
@@ -586,12 +537,77 @@
     [advancedPanel setHidden:YES];
 }
 
--(void) animateOut
+-(void) setupCallView
+{
+    if (accountInfo_ == nil)
+        return;
+
+    auto* callModel = accountInfo_->callModel.get();
+    auto* convModel = accountInfo_->conversationModel.get();
+
+    // when call comes in we want to show the controls/header
+    [self mouseIsMoving:YES];
+
+    [self connectVideoSignals];
+    /* check if text media is already present */
+    if(not callModel->hasCall(callUid_))
+        return;
+
+    [loadingIndicator setAnimates:YES];
+    [self updateCall:YES];
+
+    /* monitor media for messaging text messaging */
+    QObject::disconnect(self.messageConnection);
+    self.messageConnection = QObject::connect(convModel,
+                                              &lrc::api::ConversationModel::interactionStatusUpdated,
+                                              [self] (std::string convUid,
+                                                      uint64_t msgId,
+                                                      lrc::api::interaction::Info msg) {
+                                                  if (msg.type == lrc::api::interaction::Type::TEXT) {
+                                                      if(not [[self splitView] isSubviewCollapsed:[[[self splitView] subviews] objectAtIndex: 1]]){
+                                                          return;
+                                                      }
+                                                      [self uncollapseRightView];
+                                                  }
+                                              });
+}
+
+-(void) showWithAnimation:(BOOL)animate
+{
+    if (!animate) {
+        [self.view setHidden:NO];
+        [self setupCallView];
+        return;
+    }
+
+    CGRect frame = CGRectOffset(self.view.superview.bounds, -self.view.superview.bounds.size.width, 0);
+    [self.view setHidden:NO];
+
+    [CATransaction begin];
+    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
+    [animation setFromValue:[NSValue valueWithPoint:frame.origin]];
+    [animation setToValue:[NSValue valueWithPoint:self.view.superview.bounds.origin]];
+    [animation setDuration:0.2f];
+    [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]];
+    [CATransaction setCompletionBlock:^{
+        [self setupCallView];
+    }];
+
+    [self.view.layer addAnimation:animation forKey:animation.keyPath];
+    [CATransaction commit];
+}
+
+-(void) hideWithAnimation:(BOOL)animate
 {
     if(self.view.frame.origin.x < 0) {
         return;
     }
 
+    if (!animate) {
+        [self.view setHidden:YES];
+        return;
+    }
+
     CGRect frame = CGRectOffset(self.view.frame, -self.view.frame.size.width, 0);
     [CATransaction begin];
     CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
diff --git a/src/RingWindowController.mm b/src/RingWindowController.mm
index a5c4928..f4ebace 100644
--- a/src/RingWindowController.mm
+++ b/src/RingWindowController.mm
@@ -75,7 +75,8 @@
     IBOutlet SmartViewVC* smartViewVC;
 
     CurrentCallVC* currentCallVC;
-    ConversationVC* offlineVC;
+    ConversationVC* conversationVC;
+
     // toolbar menu items
     ChooseAccountVC* chooseAccountVC;
 }
@@ -93,18 +94,18 @@
     lrc_ = std::make_unique<lrc::api::Lrc>();
 
     currentCallVC = [[CurrentCallVC alloc] initWithNibName:@"CurrentCall" bundle:nil];
-    offlineVC = [[ConversationVC alloc] initWithNibName:@"Conversation" bundle:nil delegate:self];
+    conversationVC = [[ConversationVC alloc] initWithNibName:@"Conversation" bundle:nil delegate:self];
     // toolbar items
     chooseAccountVC = [[ChooseAccountVC alloc] initWithNibName:@"ChooseAccount" bundle:nil model:&(lrc_->getAccountModel()) delegate:self];
     [callView setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
     [[currentCallVC view] setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
-    [[offlineVC view] setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
+    [[conversationVC view] setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
 
     [callView addSubview:[currentCallVC view] positioned:NSWindowAbove relativeTo:nil];
-    [callView addSubview:[offlineVC view] positioned:NSWindowAbove relativeTo:nil];
+    [callView addSubview:[conversationVC view] positioned:NSWindowAbove relativeTo:nil];
 
     [currentCallVC initFrame];
-    [offlineVC initFrame];
+    [conversationVC initFrame];
     @try {
         [smartViewVC setConversationModel: [chooseAccountVC selectedAccount].conversationModel.get()];
     }
@@ -139,8 +140,8 @@
                                           conversation:convInfo.uid
                                                account:accInfo];
                          [smartViewVC selectConversation: convInfo model:accInfo->conversationModel.get()];
-                         [currentCallVC animateIn];
-                         [offlineVC animateOut];
+                         [currentCallVC showWithAnimation:false];
+                         [conversationVC hideWithAnimation:false];
                      });
 
     QObject::connect(&lrc_->getBehaviorController(),
@@ -157,8 +158,8 @@
                                           conversation:convInfo.uid
                                                account:accInfo];
                          [smartViewVC selectConversation: convInfo model:accInfo->conversationModel.get()];
-                         [currentCallVC animateIn];
-                         [offlineVC animateOut];
+                         [currentCallVC showWithAnimation:false];
+                         [conversationVC hideWithAnimation:false];
                      });
 
     QObject::connect(&lrc_->getBehaviorController(),
@@ -166,10 +167,10 @@
                      [self](const std::string& accountId,
                             const lrc::api::conversation::Info& convInfo){
                          auto& accInfo = lrc_->getAccountModel().getAccountInfo(accountId);
-                         [offlineVC setConversationUid:convInfo.uid model:accInfo.conversationModel.get()];
+                         [conversationVC setConversationUid:convInfo.uid model:accInfo.conversationModel.get()];
                          [smartViewVC selectConversation: convInfo model:accInfo.conversationModel.get()];
-                         [offlineVC animateIn];
-                         [currentCallVC animateOut];
+                         [conversationVC showWithAnimation:false];
+                         [currentCallVC hideWithAnimation:false];
                      });
 }
 
@@ -371,8 +372,8 @@
 {
     // If the selected account has been changed, we close any open panel
     if ([smartViewVC setConversationModel:accInfo.conversationModel.get()]) {
-        [currentCallVC animateOut];
-        [offlineVC animateOut];
+        [currentCallVC hideWithAnimation:false];
+        [conversationVC hideWithAnimation:false];
     }
 
     // Welcome view informations are also updated
@@ -390,8 +391,8 @@
 }
 
 -(void) listTypeChanged {
-    [offlineVC animateOut];
-    [currentCallVC animateOut];
+    [conversationVC hideWithAnimation:false];
+    [currentCallVC hideWithAnimation:false];
 }
 
 #pragma mark - NSToolbarDelegate