[MediaWiki-commits] [Gerrit] Save scroll position across rotation better - change (apps...wikipedia)

2014-08-01 Thread Mhurd (Code Review)
Mhurd has submitted this change and it was merged.

Change subject: Save scroll position across rotation better
..


Save scroll position across rotation better

When rotating, we now check for the element that's at the top of the
screen, estimate how far into it we were (by percentage of height),
and return us to the recalculated position after rotating.

Bug: 68683
Change-Id: Ibdd0f59bde682da46040fa3b0a5b8daab4822721
---
M wikipedia/View Controllers/WebView/WebViewController.m
1 file changed, 39 insertions(+), 20 deletions(-)

Approvals:
  Mhurd: Verified; Looks good to me, approved



diff --git a/wikipedia/View Controllers/WebView/WebViewController.m 
b/wikipedia/View Controllers/WebView/WebViewController.m
index 1ab10f4..bd48251 100644
--- a/wikipedia/View Controllers/WebView/WebViewController.m
+++ b/wikipedia/View Controllers/WebView/WebViewController.m
@@ -72,7 +72,7 @@
 
 @property (nonatomic) BOOL unsafeToScroll;
 
-@property (nonatomic) NSInteger indexOfFirstOnscreenSectionBeforeRotate;
+@property (nonatomic) float relativeScrollOffsetBeforeRotate;
 @property (nonatomic) NSUInteger sectionToEditId;
 
 @property (strong, nonatomic) NSDictionary *adjacentHistoryIDs;
@@ -142,8 +142,6 @@
 self.zeroStatusLabel.text = @"";
 
 self.sectionToEditId = 0;
-
-self.indexOfFirstOnscreenSectionBeforeRotate = -1;
 
 [[NSNotificationCenter defaultCenter] addObserver: self
  selector: 
@selector(webViewFinishedLoading)
@@ -710,6 +708,15 @@
 
 point.y += 2;
 
+[self tocScrollWebViewToPoint:point
+ duration:duration
+  thenHideTOC:hideTOC];
+}
+
+-(void)tocScrollWebViewToPoint: (CGPoint)point
+  duration: (CGFloat)duration
+   thenHideTOC: (BOOL)hideTOC
+{
 [UIView animateWithDuration: duration
   delay: 0.0f
 options: UIViewAnimationOptionBeginFromCurrentState
@@ -1763,14 +1770,17 @@
 
 
-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
 duration:(NSTimeInterval)duration
 {
-NSManagedObjectID *articleID = [articleDataContext_.mainContext 
getArticleIDForTitle: [SessionSingleton sharedInstance].currentArticleTitle
-   
   domain: [SessionSingleton sharedInstance].currentArticleDomain];
-if (articleID) {
-Article *article = (Article *)[articleDataContext_.mainContext 
objectWithID:articleID];
-
-self.indexOfFirstOnscreenSectionBeforeRotate = [self.webView 
getIndexOfTopOnScreenElementWithPrefix:@"section_heading_and_content_block_" 
count:article.section.count];
-}
-//self.view.alpha = 0.0f;
+NSString *js = @"(function() {"
+   @"_topElement = document.elementFromPoint( 
window.innerWidth / 2, 0 );"
+   @"if (_topElement) {"
+   @"var rect = _topElement.getBoundingClientRect();"
+   @"return rect.top / rect.height;"
+   @"} else {"
+   @"return 0;"
+   @"}"
+   @"})()";
+float relativeScrollOffset = [[self.webView 
stringByEvaluatingJavaScriptFromString:js] floatValue];
+self.relativeScrollOffsetBeforeRotate = relativeScrollOffset;
 
 [self tocHideWithDuration:@0.0f];
 
@@ -1781,20 +1791,29 @@
 {
 [super didRotateFromInterfaceOrientation:fromInterfaceOrientation];
 
-[self 
performSelector:@selector(scrollToIndexOfFirstOnscreenSectionBeforeRotate) 
withObject:nil afterDelay:0.1f];
+[self scrollToElementOnScreenBeforeRotate];
 
 [self updateWebViewContentAndScrollInsets];
 }
 
--(void)scrollToIndexOfFirstOnscreenSectionBeforeRotate{
-if(self.indexOfFirstOnscreenSectionBeforeRotate == -1)return;
-NSString *elementId = [NSString 
stringWithFormat:@"section_heading_and_content_block_%ld", 
(long)self.indexOfFirstOnscreenSectionBeforeRotate];
-
-[self tocScrollWebViewToSectionWithElementId: elementId
-duration: 0.2
- thenHideTOC: NO];
+-(void)scrollToElementOnScreenBeforeRotate
+{
+NSString *js = @"(function() {"
+   @"if (_topElement) {"
+   @"var rect = _topElement.getBoundingClientRect();"
+   @"return (window.scrollY + rect.top) - (%f * 
rect.height);"
+   @"} else {"
+   @"return 0;"
+   @"}"
+   @"})()";
+NSString *js2 = [NSString stringWithFormat:js, 
self.relativeScrollOffsetBeforeRotate, self.relativeScrollOffsetBeforeRotate];
+int finalScrollOffset = [[self.webView 
stringByEvaluatingJavaScriptFromString:js2] intValue];
 
-[self.tocVC centerC

[MediaWiki-commits] [Gerrit] Save scroll position across rotation better - change (apps...wikipedia)

2014-07-30 Thread Brion VIBBER (Code Review)
Brion VIBBER has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/150740

Change subject: Save scroll position across rotation better
..

Save scroll position across rotation better

When rotating, we now check for the element that's at the top of the
screen, estimate how far into it we were (by percentage of height),
and return us to the recalculated position after rotating.

Bug: 68683
Change-Id: Ibdd0f59bde682da46040fa3b0a5b8daab4822721
---
M wikipedia/View Controllers/WebView/WebViewController.m
1 file changed, 39 insertions(+), 20 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/apps/ios/wikipedia 
refs/changes/40/150740/1

diff --git a/wikipedia/View Controllers/WebView/WebViewController.m 
b/wikipedia/View Controllers/WebView/WebViewController.m
index 1ab10f4..bd48251 100644
--- a/wikipedia/View Controllers/WebView/WebViewController.m
+++ b/wikipedia/View Controllers/WebView/WebViewController.m
@@ -72,7 +72,7 @@
 
 @property (nonatomic) BOOL unsafeToScroll;
 
-@property (nonatomic) NSInteger indexOfFirstOnscreenSectionBeforeRotate;
+@property (nonatomic) float relativeScrollOffsetBeforeRotate;
 @property (nonatomic) NSUInteger sectionToEditId;
 
 @property (strong, nonatomic) NSDictionary *adjacentHistoryIDs;
@@ -142,8 +142,6 @@
 self.zeroStatusLabel.text = @"";
 
 self.sectionToEditId = 0;
-
-self.indexOfFirstOnscreenSectionBeforeRotate = -1;
 
 [[NSNotificationCenter defaultCenter] addObserver: self
  selector: 
@selector(webViewFinishedLoading)
@@ -710,6 +708,15 @@
 
 point.y += 2;
 
+[self tocScrollWebViewToPoint:point
+ duration:duration
+  thenHideTOC:hideTOC];
+}
+
+-(void)tocScrollWebViewToPoint: (CGPoint)point
+  duration: (CGFloat)duration
+   thenHideTOC: (BOOL)hideTOC
+{
 [UIView animateWithDuration: duration
   delay: 0.0f
 options: UIViewAnimationOptionBeginFromCurrentState
@@ -1763,14 +1770,17 @@
 
 
-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
 duration:(NSTimeInterval)duration
 {
-NSManagedObjectID *articleID = [articleDataContext_.mainContext 
getArticleIDForTitle: [SessionSingleton sharedInstance].currentArticleTitle
-   
   domain: [SessionSingleton sharedInstance].currentArticleDomain];
-if (articleID) {
-Article *article = (Article *)[articleDataContext_.mainContext 
objectWithID:articleID];
-
-self.indexOfFirstOnscreenSectionBeforeRotate = [self.webView 
getIndexOfTopOnScreenElementWithPrefix:@"section_heading_and_content_block_" 
count:article.section.count];
-}
-//self.view.alpha = 0.0f;
+NSString *js = @"(function() {"
+   @"_topElement = document.elementFromPoint( 
window.innerWidth / 2, 0 );"
+   @"if (_topElement) {"
+   @"var rect = _topElement.getBoundingClientRect();"
+   @"return rect.top / rect.height;"
+   @"} else {"
+   @"return 0;"
+   @"}"
+   @"})()";
+float relativeScrollOffset = [[self.webView 
stringByEvaluatingJavaScriptFromString:js] floatValue];
+self.relativeScrollOffsetBeforeRotate = relativeScrollOffset;
 
 [self tocHideWithDuration:@0.0f];
 
@@ -1781,20 +1791,29 @@
 {
 [super didRotateFromInterfaceOrientation:fromInterfaceOrientation];
 
-[self 
performSelector:@selector(scrollToIndexOfFirstOnscreenSectionBeforeRotate) 
withObject:nil afterDelay:0.1f];
+[self scrollToElementOnScreenBeforeRotate];
 
 [self updateWebViewContentAndScrollInsets];
 }
 
--(void)scrollToIndexOfFirstOnscreenSectionBeforeRotate{
-if(self.indexOfFirstOnscreenSectionBeforeRotate == -1)return;
-NSString *elementId = [NSString 
stringWithFormat:@"section_heading_and_content_block_%ld", 
(long)self.indexOfFirstOnscreenSectionBeforeRotate];
-
-[self tocScrollWebViewToSectionWithElementId: elementId
-duration: 0.2
- thenHideTOC: NO];
+-(void)scrollToElementOnScreenBeforeRotate
+{
+NSString *js = @"(function() {"
+   @"if (_topElement) {"
+   @"var rect = _topElement.getBoundingClientRect();"
+   @"return (window.scrollY + rect.top) - (%f * 
rect.height);"
+   @"} else {"
+   @"return 0;"
+   @"}"
+   @"})()";
+NSString *js2 = [NSString stringWithFormat:js, 
self.relativeScrollOffsetBeforeRotate, self.relativeScrollOffsetBeforeRotate];
+int finalScrollOffset = [[self.webView 
stringByEv