@ -54,6 +54,9 @@ final class ConversationVC : BaseVC, ConversationViewModelDelegate, OWSConversat
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				        return  messagesTableView . contentSize . height  -  tableViewUnobscuredHeight 
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				    } 
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				    
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				    var  lastContentOffset :  CGFloat ?  =  nil 
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				    var  initialKeyboardHeight :  CGFloat  =  0 
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				    
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				    lazy  var  viewModel  =  ConversationViewModel ( thread :  thread ,  focusMessageIdOnOpen :  focusedMessageID ,  delegate :  self ) 
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				    
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				    lazy  var  mediaCache :  NSCache < NSString ,  AnyObject >  =  { 
 
			
		 
		
	
	
		
			
				
					
						
							
								 
							 
						
						
							
								 
							 
						
						
					 
				
			
			 
			 
			
				@ -217,6 +220,13 @@ final class ConversationVC : BaseVC, ConversationViewModelDelegate, OWSConversat
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				        } 
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				    } 
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				    
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				    override  func  viewWillAppear ( _  animated :  Bool )  { 
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				        super . viewWillAppear ( animated ) 
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				        if  let  y  =  lastContentOffset  { 
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				            messagesTableView . setContentOffset ( CGPoint ( x :  0 ,  y :  y ) ,  animated :  false ) 
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				        } 
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				    } 
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				    
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				    override  func  viewDidAppear ( _  animated :  Bool )  { 
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				        super . viewDidAppear ( animated ) 
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				        didFinishInitialLayout  =  true 
 
			
		 
		
	
	
		
			
				
					
						
						
						
							
								 
							 
						
					 
				
			
			 
			 
			
				@ -225,6 +235,10 @@ final class ConversationVC : BaseVC, ConversationViewModelDelegate, OWSConversat
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				    
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				    override  func  viewWillDisappear ( _  animated :  Bool )  { 
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				        super . viewWillDisappear ( animated ) 
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				        lastContentOffset  =  messagesTableView . contentOffset . y 
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				        if  ( messagesTableView . keyboardHeight  >  initialKeyboardHeight )  { 
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				            lastContentOffset !  -=  messagesTableView . keyboardHeight  -  initialKeyboardHeight 
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				        } 
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				        let  text  =  snInputView . text 
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				        if  ! text . isEmpty  { 
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				            Storage . write  {  transaction  in 
 
			
		 
		
	
	
		
			
				
					
						
							
								 
							 
						
						
							
								 
							 
						
						
					 
				
			
			 
			 
			
				@ -284,24 +298,32 @@ final class ConversationVC : BaseVC, ConversationViewModelDelegate, OWSConversat
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				    
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				    @objc  func  handleKeyboardWillChangeFrameNotification ( _  notification :  Notification )  { 
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				        guard  let  newHeight  =  ( notification . userInfo ? [ UIResponder . keyboardFrameEndUserInfoKey ]  as ?  NSValue ) ? . cgRectValue . size . height  else  {  return  } 
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				        if  ( newHeight  >  initialKeyboardHeight  &&  initialKeyboardHeight  = =  0 )  { 
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				            initialKeyboardHeight  =  newHeight 
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				            self . messagesTableView . keyboardHeight  =  newHeight 
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				        } 
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				        if  ! didConstrainScrollButton  { 
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				            //   H A C K :   P a r t   o f   a   w o r k a r o u n d   t o   g e t   t h e   s c r o l l   b u t t o n   t o   s h o w   u p   i n   t h e   r i g h t   p l a c e 
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				            scrollButton . pin ( . bottom ,  to :  . bottom ,  of :  view ,  withInset :  - ( newHeight  +  16 ) )  //   +   1 6   t o   m a t c h   t h e   b o t t o m   i n s e t   o f   t h e   t a b l e   v i e w 
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				            didConstrainScrollButton  =  true 
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				        } 
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				        let  shouldScroll  =  ( newHeight  >  200 )  //   A r b i t r a r y   v a l u e   t h a t ' s   h i g h e r   t h a n   t h e   c o l l a p s e d   s i z e   a n d   l o w e r   t h a n   t h e   e x p a n d e d   s i z e 
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				        print ( " Ryan: keyboardWillChangeFrame, new height:  \( newHeight ) , old height:  \( self . messagesTableView . keyboardHeight ) , contentOffsetY:  \( self . messagesTableView . contentOffset . y ) " ) 
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				        UIView . animate ( withDuration :  0.25 )  { 
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				            if  shouldScroll  { 
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				                self . messagesTableView . contentOffset . y  +=  ( newHeight  -  self . messagesTableView . keyboardHeight ) 
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				                self . messagesTableView . keyboardHeight  =  newHeight 
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				            } 
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				            self . messagesTableView . keyboardHeight  =  newHeight 
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				            self . scrollButton . alpha  =  0 
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				        } 
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				        print ( " Ryan: keyboardWillChangeFrame, contentOffsetY:  \( self . messagesTableView . contentOffset . y ) " ) 
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				    } 
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				    
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				    @objc  func  handleKeyboardWillHideNotification ( _  notification :  Notification )  { 
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				        print ( " Ryan: handleKeyboardWillHide " ) 
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				        UIView . animate ( withDuration :  0.25 )  { 
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				            self . messagesTableView . keyboardHeight  =  0 
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				            self . messagesTableView . contentOffset . y  -=  ( self . messagesTableView . keyboardHeight  -  self . initialKeyboardHeight ) 
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				            self . messagesTableView . keyboardHeight  =  self . initialKeyboardHeight 
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				            self . scrollButton . alpha  =  self . getScrollButtonOpacity ( ) 
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				            self . unreadCountView . alpha  =  self . scrollButton . alpha 
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				        } 
 
			
		 
		
	
	
		
			
				
					
						
							
								 
							 
						
						
							
								 
							 
						
						
					 
				
			
			 
			 
			
				@ -436,7 +458,9 @@ final class ConversationVC : BaseVC, ConversationViewModelDelegate, OWSConversat
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				        view . layoutIfNeeded ( ) 
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				        let  firstContentPageTop :  CGFloat  =  0 
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				        let  contentOffsetY  =  max ( firstContentPageTop ,  lastPageTop ) 
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				        lastContentOffset  =  contentOffsetY 
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				        messagesTableView . setContentOffset ( CGPoint ( x :  0 ,  y :  contentOffsetY ) ,  animated :  isAnimated ) 
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				        print ( " Ryan: Scroll to bottom, contentOffSetY:  \( self . messagesTableView . contentOffset . y ) " ) 
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				    } 
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				    
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				    func  scrollViewWillBeginDragging ( _  scrollView :  UIScrollView )  {