Brion VIBBER has submitted this change and it was merged.

Change subject: Fix for browsing after language switching.
......................................................................


Fix for browsing after language switching.

Change-Id: Id7aafe3924517696cc6e54b24893bee92539a5f4
---
M Wikipedia-iOS/Categories/NSManagedObjectContext+SimpleFetch.h
M Wikipedia-iOS/Categories/NSManagedObjectContext+SimpleFetch.m
M Wikipedia-iOS/Data/Operations/DownloadLeadSectionOp.h
M Wikipedia-iOS/Data/Operations/DownloadLeadSectionOp.m
M Wikipedia-iOS/Data/Operations/DownloadNonLeadSectionsOp.h
M Wikipedia-iOS/Data/Operations/DownloadNonLeadSectionsOp.m
M Wikipedia-iOS/Data/Operations/DownloadSectionWikiTextOp.h
M Wikipedia-iOS/Data/Operations/DownloadSectionWikiTextOp.m
M Wikipedia-iOS/Data/Operations/UploadSectionWikiTextOp.h
M Wikipedia-iOS/Data/Operations/UploadSectionWikiTextOp.m
M Wikipedia-iOS/Session/SessionSingleton.h
M Wikipedia-iOS/Session/SessionSingleton.m
M Wikipedia-iOS/View Controllers/History/HistoryViewController.m
M Wikipedia-iOS/View Controllers/MainMenu/MainMenuTableViewController.m
M Wikipedia-iOS/View Controllers/SavedPages/SavedPagesViewController.m
M Wikipedia-iOS/View Controllers/SearchResults/SearchResultsController.m
M Wikipedia-iOS/View Controllers/SectionEditor/SectionEditorViewController.m
M Wikipedia-iOS/View Controllers/TableOfContents/TOCViewController.m
M Wikipedia-iOS/View Controllers/WebView/WebViewController.m
19 files changed, 188 insertions(+), 115 deletions(-)

Approvals:
  Brion VIBBER: Verified; Looks good to me, approved



diff --git a/Wikipedia-iOS/Categories/NSManagedObjectContext+SimpleFetch.h 
b/Wikipedia-iOS/Categories/NSManagedObjectContext+SimpleFetch.h
index cf84dc9..5ecc0a5 100644
--- a/Wikipedia-iOS/Categories/NSManagedObjectContext+SimpleFetch.h
+++ b/Wikipedia-iOS/Categories/NSManagedObjectContext+SimpleFetch.h
@@ -8,6 +8,6 @@
 
 -(NSManagedObject *)getEntityForName:(NSString *)entityName 
withPredicateFormat:(NSString *)predicateFormat, ...;
 
--(NSManagedObjectID *)getArticleIDForTitle:(NSString *)title;
+-(NSManagedObjectID *)getArticleIDForTitle:(NSString *)title domain:(NSString 
*)domain;
 
 @end
diff --git a/Wikipedia-iOS/Categories/NSManagedObjectContext+SimpleFetch.m 
b/Wikipedia-iOS/Categories/NSManagedObjectContext+SimpleFetch.m
index 2d2cc9f..30a4704 100644
--- a/Wikipedia-iOS/Categories/NSManagedObjectContext+SimpleFetch.m
+++ b/Wikipedia-iOS/Categories/NSManagedObjectContext+SimpleFetch.m
@@ -36,7 +36,7 @@
     }
 }
 
--(NSManagedObjectID *)getArticleIDForTitle:(NSString *)title
+-(NSManagedObjectID *)getArticleIDForTitle:(NSString *)title domain:(NSString 
*)domain
 {
     Article *article = (Article *)[self getEntityForName: @"Article" 
withPredicateFormat: @"\
                        title ==[c] %@ \
@@ -46,7 +46,7 @@
                        domain == %@",
                        title,
                        [SessionSingleton sharedInstance].site,
-                       [SessionSingleton sharedInstance].domain
+                       domain
     ];
 
     return (article) ? article.objectID : nil;
diff --git a/Wikipedia-iOS/Data/Operations/DownloadLeadSectionOp.h 
b/Wikipedia-iOS/Data/Operations/DownloadLeadSectionOp.h
index 5230013..43bed13 100644
--- a/Wikipedia-iOS/Data/Operations/DownloadLeadSectionOp.h
+++ b/Wikipedia-iOS/Data/Operations/DownloadLeadSectionOp.h
@@ -5,6 +5,7 @@
 @interface DownloadLeadSectionOp : MWNetworkOp
 
 - (id)initForPageTitle: (NSString *)title
+                domain: (NSString *)domain
        completionBlock: (void (^)(NSArray *))completionBlock
         cancelledBlock: (void (^)(NSError *))cancelledBlock
             errorBlock: (void (^)(NSError *))errorBlock
diff --git a/Wikipedia-iOS/Data/Operations/DownloadLeadSectionOp.m 
b/Wikipedia-iOS/Data/Operations/DownloadLeadSectionOp.m
index 229a7e8..da7d386 100644
--- a/Wikipedia-iOS/Data/Operations/DownloadLeadSectionOp.m
+++ b/Wikipedia-iOS/Data/Operations/DownloadLeadSectionOp.m
@@ -8,13 +8,14 @@
 @implementation DownloadLeadSectionOp
 
 - (id)initForPageTitle: (NSString *)title
+                domain: (NSString *)domain
        completionBlock: (void (^)(NSArray *))completionBlock
         cancelledBlock: (void (^)(NSError *))cancelledBlock
             errorBlock: (void (^)(NSError *))errorBlock
 {
     self = [super init];
     if (self) {
-        self.request = [NSURLRequest postRequestWithURL: [NSURL 
URLWithString:[SessionSingleton sharedInstance].searchApiUrl]
+        self.request = [NSURLRequest postRequestWithURL: [[SessionSingleton 
sharedInstance] urlForDomain:domain]
                                              parameters: @{
                                                            @"action": 
@"mobileview",
                                                            @"prop": 
@"sections|text",
diff --git a/Wikipedia-iOS/Data/Operations/DownloadNonLeadSectionsOp.h 
b/Wikipedia-iOS/Data/Operations/DownloadNonLeadSectionsOp.h
index 3a57df1..39a705d 100644
--- a/Wikipedia-iOS/Data/Operations/DownloadNonLeadSectionsOp.h
+++ b/Wikipedia-iOS/Data/Operations/DownloadNonLeadSectionsOp.h
@@ -5,6 +5,7 @@
 @interface DownloadNonLeadSectionsOp : MWNetworkOp
 
 - (id)initForPageTitle: (NSString *)title
+                domain: (NSString *)domain
        completionBlock: (void (^)(NSArray *))completionBlock
         cancelledBlock: (void (^)(NSError *))cancelledBlock
             errorBlock: (void (^)(NSError *))errorBlock
diff --git a/Wikipedia-iOS/Data/Operations/DownloadNonLeadSectionsOp.m 
b/Wikipedia-iOS/Data/Operations/DownloadNonLeadSectionsOp.m
index 668eefa..606bb98 100644
--- a/Wikipedia-iOS/Data/Operations/DownloadNonLeadSectionsOp.m
+++ b/Wikipedia-iOS/Data/Operations/DownloadNonLeadSectionsOp.m
@@ -8,13 +8,14 @@
 @implementation DownloadNonLeadSectionsOp
 
 - (id)initForPageTitle: (NSString *)title
+                domain: (NSString *)domain
        completionBlock: (void (^)(NSArray *))completionBlock
         cancelledBlock: (void (^)(NSError *))cancelledBlock
             errorBlock: (void (^)(NSError *))errorBlock
 {
     self = [super init];
     if (self) {
-        self.request = [NSURLRequest postRequestWithURL: [NSURL 
URLWithString:[SessionSingleton sharedInstance].searchApiUrl]
+        self.request = [NSURLRequest postRequestWithURL: [[SessionSingleton 
sharedInstance] urlForDomain:domain]
                                              parameters: @{
                                                            @"action": 
@"mobileview",
                                                            @"prop": 
@"sections|text",
diff --git a/Wikipedia-iOS/Data/Operations/DownloadSectionWikiTextOp.h 
b/Wikipedia-iOS/Data/Operations/DownloadSectionWikiTextOp.h
index f5c3077..2f68e2e 100644
--- a/Wikipedia-iOS/Data/Operations/DownloadSectionWikiTextOp.h
+++ b/Wikipedia-iOS/Data/Operations/DownloadSectionWikiTextOp.h
@@ -5,6 +5,7 @@
 @interface DownloadSectionWikiTextOp : MWNetworkOp
 
 - (id)initForPageTitle: (NSString *)title
+                domain: (NSString *)domain
                section: (NSNumber *)section
        completionBlock: (void (^)(NSString *))completionBlock
         cancelledBlock: (void (^)(NSError *))cancelledBlock
diff --git a/Wikipedia-iOS/Data/Operations/DownloadSectionWikiTextOp.m 
b/Wikipedia-iOS/Data/Operations/DownloadSectionWikiTextOp.m
index af88d78..6f6c5a9 100644
--- a/Wikipedia-iOS/Data/Operations/DownloadSectionWikiTextOp.m
+++ b/Wikipedia-iOS/Data/Operations/DownloadSectionWikiTextOp.m
@@ -8,6 +8,7 @@
 @implementation DownloadSectionWikiTextOp
 
 - (id)initForPageTitle: (NSString *)title
+                domain: (NSString *)domain
                section: (NSNumber *)section
        completionBlock: (void (^)(NSString *))completionBlock
         cancelledBlock: (void (^)(NSError *))cancelledBlock
@@ -15,7 +16,7 @@
 {
     self = [super init];
     if (self) {
-        self.request = [NSURLRequest postRequestWithURL: [NSURL 
URLWithString:[SessionSingleton sharedInstance].searchApiUrl]
+        self.request = [NSURLRequest postRequestWithURL: [[SessionSingleton 
sharedInstance] urlForDomain:domain]
                                              parameters: @{
                                                            @"action": @"query",
                                                            @"prop": 
@"revisions",
diff --git a/Wikipedia-iOS/Data/Operations/UploadSectionWikiTextOp.h 
b/Wikipedia-iOS/Data/Operations/UploadSectionWikiTextOp.h
index b63f082..6796a14 100644
--- a/Wikipedia-iOS/Data/Operations/UploadSectionWikiTextOp.h
+++ b/Wikipedia-iOS/Data/Operations/UploadSectionWikiTextOp.h
@@ -5,6 +5,7 @@
 @interface UploadSectionWikiTextOp : MWNetworkOp
 
 - (id)initForPageTitle: (NSString *)title
+                domain: (NSString *)domain
                section: (NSNumber *)section
               wikiText: (NSString *)wikiText
        completionBlock: (void (^)(NSString *))completionBlock
diff --git a/Wikipedia-iOS/Data/Operations/UploadSectionWikiTextOp.m 
b/Wikipedia-iOS/Data/Operations/UploadSectionWikiTextOp.m
index 5445048..1918498 100644
--- a/Wikipedia-iOS/Data/Operations/UploadSectionWikiTextOp.m
+++ b/Wikipedia-iOS/Data/Operations/UploadSectionWikiTextOp.m
@@ -8,6 +8,7 @@
 @implementation UploadSectionWikiTextOp
 
 - (id)initForPageTitle: (NSString *)title
+                domain: (NSString *)domain
                section: (NSNumber *)section
               wikiText: (NSString *)wikiText
        completionBlock: (void (^)(NSString *))completionBlock
@@ -16,7 +17,7 @@
 {
     self = [super init];
     if (self) {
-        self.request = [NSURLRequest postRequestWithURL: [NSURL 
URLWithString:[SessionSingleton sharedInstance].searchApiUrl]
+        self.request = [NSURLRequest postRequestWithURL: [[SessionSingleton 
sharedInstance] urlForDomain:domain]
                                              parameters: @{
                                                            @"action": @"edit",
                                                            @"token": @"+\\",
diff --git a/Wikipedia-iOS/Session/SessionSingleton.h 
b/Wikipedia-iOS/Session/SessionSingleton.h
index 31152f7..28f89bb 100644
--- a/Wikipedia-iOS/Session/SessionSingleton.h
+++ b/Wikipedia-iOS/Session/SessionSingleton.h
@@ -4,13 +4,20 @@
 
 @interface SessionSingleton : NSObject
 
-// These 3 persist across app restarts.
+// These 5 persist across app restarts.
 @property (strong, nonatomic) NSString *site;
 @property (strong, nonatomic) NSString *domain;
 @property (strong, nonatomic) NSString *domainName;
+@property (strong, nonatomic) NSString *currentArticleTitle;
+@property (strong, nonatomic) NSString *currentArticleDomain;
+
+@property (strong, nonatomic, readonly) NSString *currentArticleDomainName;
 
 @property (strong, nonatomic, readonly) NSString *searchApiUrl;
 
+-(NSURL *)urlForDomain:(NSString *)domain;
+-(NSString *)bundledLanguagesPath;
+
 + (SessionSingleton *)sharedInstance;
 
 @end
diff --git a/Wikipedia-iOS/Session/SessionSingleton.m 
b/Wikipedia-iOS/Session/SessionSingleton.m
index 9e11f29..18830ed 100644
--- a/Wikipedia-iOS/Session/SessionSingleton.m
+++ b/Wikipedia-iOS/Session/SessionSingleton.m
@@ -14,6 +14,11 @@
     return sharedInstance;
 }
 
+-(NSURL *)urlForDomain:(NSString *)domain
+{
+    return [NSURL URLWithString:[NSString 
stringWithFormat:@"https://%@.m.%@/w/api.php";, domain, [self site]]];
+}
+
 -(NSString *)searchApiUrl
 {
     return [NSString stringWithFormat:@"https://%@.m.%@/w/api.php";, [self 
domain], [self site]];
@@ -58,5 +63,50 @@
     return [[NSUserDefaults standardUserDefaults] objectForKey:@"Site"];
 }
 
-@end
+-(void)setCurrentArticleTitle:(NSString *)currentArticleTitle
+{
+    [[NSUserDefaults standardUserDefaults] setObject:currentArticleTitle 
forKey:@"CurrentArticleTitle"];
+    [[NSUserDefaults standardUserDefaults] synchronize];
+}
 
+-(NSString *)currentArticleTitle
+{
+    return [[NSUserDefaults standardUserDefaults] 
objectForKey:@"CurrentArticleTitle"];
+}
+
+-(void)setCurrentArticleDomain:(NSString *)currentArticleDomain
+{
+    [[NSUserDefaults standardUserDefaults] setObject:currentArticleDomain 
forKey:@"CurrentArticleDomain"];
+    [[NSUserDefaults standardUserDefaults] synchronize];
+}
+
+-(NSString *)currentArticleDomain
+{
+    return [[NSUserDefaults standardUserDefaults] 
objectForKey:@"CurrentArticleDomain"];
+}
+
+-(NSString *)currentArticleDomainName
+{
+    NSError *error = nil;
+    NSData *fileData = [NSData dataWithContentsOfFile:[self 
bundledLanguagesPath] options:0 error:&error];
+    if (error) return nil;
+    error = nil;
+    NSArray *result = [NSJSONSerialization JSONObjectWithData:fileData 
options:0 error:&error];
+    if (!error) {
+        for (NSDictionary *d in result) {
+            if ([d[@"code"] isEqualToString:self.currentArticleDomain]) {
+                return d[@"name"];
+            }
+        }
+        return nil;
+    }else{
+        return nil;
+    }
+}
+
+- (NSString *)bundledLanguagesPath
+{
+    return [[[NSBundle mainBundle] bundlePath] 
stringByAppendingString:@"/Languages/languages.json"];
+}
+
+@end
diff --git a/Wikipedia-iOS/View Controllers/History/HistoryViewController.m 
b/Wikipedia-iOS/View Controllers/History/HistoryViewController.m
index 08dac91..c594aef 100644
--- a/Wikipedia-iOS/View Controllers/History/HistoryViewController.m
+++ b/Wikipedia-iOS/View Controllers/History/HistoryViewController.m
@@ -9,6 +9,7 @@
 #import "HistoryTableHeadingLabel.h"
 #import "Defines.h"
 #import "Article+Convenience.h"
+#import "SessionSingleton.h"
 
 @interface HistoryViewController ()
 {
@@ -333,8 +334,8 @@
     [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
     
     // Set CurrentArticleTitle so the web view knows what to display for this 
selection.
-    [[NSUserDefaults standardUserDefaults] 
setObject:historyEntry.article.title forKey:@"CurrentArticleTitle"];
-    [[NSUserDefaults standardUserDefaults] synchronize];
+    [SessionSingleton sharedInstance].currentArticleTitle = 
historyEntry.article.title;
+    [SessionSingleton sharedInstance].currentArticleDomain = 
historyEntry.article.domain;
     
     WebViewController *webViewController = [self getWebViewController];
     [self.navigationController popToViewController:webViewController 
animated:YES];
diff --git a/Wikipedia-iOS/View 
Controllers/MainMenu/MainMenuTableViewController.m b/Wikipedia-iOS/View 
Controllers/MainMenu/MainMenuTableViewController.m
index 9550cb4..a9fa36c 100644
--- a/Wikipedia-iOS/View Controllers/MainMenu/MainMenuTableViewController.m
+++ b/Wikipedia-iOS/View Controllers/MainMenu/MainMenuTableViewController.m
@@ -13,6 +13,7 @@
 #import "SessionSingleton.h"
 #import "NSURLRequest+DictionaryRequest.h"
 #import "MWNetworkActivityIndicatorManager.h"
+#import "SessionSingleton.h"
 
 // Section indexes.
 #define SECTION_MENU_OPTIONS 0
@@ -94,7 +95,7 @@
     // Add a "Show Languages" toggle.
     [self addToTableDataLanguagesToggleWithTitle:LANGUAGES_TOGGLE_TEXT_SHOW];
 
-    NSString *currentArticleTitle = [[NSUserDefaults standardUserDefaults] 
objectForKey:@"CurrentArticleTitle"];
+    NSString *currentArticleTitle = [SessionSingleton 
sharedInstance].currentArticleTitle;
     if(!currentArticleTitle || (currentArticleTitle.length == 0)){
         self.hidePagesSection = YES;
         [[self sectionDict:SECTION_ARTICLE_OPTIONS][@"rows"] removeAllObjects];
@@ -119,7 +120,7 @@
 
 -(void)loadTableData
 {
-    NSString *currentArticleTitle = [[NSUserDefaults standardUserDefaults] 
objectForKey:@"CurrentArticleTitle"];
+    NSString *currentArticleTitle = [SessionSingleton 
sharedInstance].currentArticleTitle;
 
     self.tableData = [@[
                             
@@ -508,16 +509,11 @@
 -(NSMutableArray *)getLanguagesFromFile
 {
     NSError *error = nil;
-    NSData *fileData = [NSData dataWithContentsOfFile:[self 
bundledLanguagesPath] options:0 error:&error];
+    NSData *fileData = [NSData dataWithContentsOfFile:[[SessionSingleton 
sharedInstance] bundledLanguagesPath] options:0 error:&error];
     if (error) return [@[] mutableCopy];
     error = nil;
     NSArray *result = [NSJSONSerialization JSONObjectWithData:fileData 
options:0 error:&error];
     return (error) ? [@[] mutableCopy]: [result mutableCopy];
-}
-
-- (NSString *)bundledLanguagesPath
-{
-    return [[[NSBundle mainBundle] bundlePath] 
stringByAppendingString:@"/Languages/languages.json"];
 }
 
 @end
diff --git a/Wikipedia-iOS/View 
Controllers/SavedPages/SavedPagesViewController.m b/Wikipedia-iOS/View 
Controllers/SavedPages/SavedPagesViewController.m
index 17c419c..4729304 100644
--- a/Wikipedia-iOS/View Controllers/SavedPages/SavedPagesViewController.m
+++ b/Wikipedia-iOS/View Controllers/SavedPages/SavedPagesViewController.m
@@ -8,6 +8,7 @@
 #import "SavedPagesTableHeadingLabel.h"
 #import "Defines.h"
 #import "Article+Convenience.h"
+#import "SessionSingleton.h"
 
 @interface SavedPagesViewController ()
 {
@@ -206,8 +207,8 @@
     [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
     
     // Set CurrentArticleTitle so the web view knows what to display for this 
selection.
-    [[NSUserDefaults standardUserDefaults] setObject:savedEntry.article.title 
forKey:@"CurrentArticleTitle"];
-    [[NSUserDefaults standardUserDefaults] synchronize];
+    [SessionSingleton sharedInstance].currentArticleTitle = 
savedEntry.article.title;
+    [SessionSingleton sharedInstance].currentArticleDomain = 
savedEntry.article.domain;
     
     WebViewController *webViewController = [self getWebViewController];
     [self.navigationController popToViewController:webViewController 
animated:YES];
diff --git a/Wikipedia-iOS/View 
Controllers/SearchResults/SearchResultsController.m b/Wikipedia-iOS/View 
Controllers/SearchResults/SearchResultsController.m
index 4c889c1..f97d968 100644
--- a/Wikipedia-iOS/View Controllers/SearchResults/SearchResultsController.m
+++ b/Wikipedia-iOS/View Controllers/SearchResults/SearchResultsController.m
@@ -15,6 +15,7 @@
 #import "ArticleDataContextSingleton.h"
 #import "ArticleCoreDataObjects.h"
 #import "NSString+Extras.h"
+#import "SessionSingleton.h"
 
 @interface SearchResultsController (){
     CGFloat scrollViewDragBeganVerticalOffset_;
@@ -402,8 +403,8 @@
     // Set CurrentArticleTitle so web view knows what to load.
     title = [self cleanTitle:title];
     
-    [[NSUserDefaults standardUserDefaults] setObject:title 
forKey:@"CurrentArticleTitle"];
-    [[NSUserDefaults standardUserDefaults] synchronize];
+    [SessionSingleton sharedInstance].currentArticleTitle = title;
+    [SessionSingleton sharedInstance].currentArticleDomain = [SessionSingleton 
sharedInstance].domain;
 
     [self.searchResultsTable deselectRowAtIndexPath:indexPath animated:YES];
 
diff --git a/Wikipedia-iOS/View 
Controllers/SectionEditor/SectionEditorViewController.m b/Wikipedia-iOS/View 
Controllers/SectionEditor/SectionEditorViewController.m
index 54f67ca..c976255 100644
--- a/Wikipedia-iOS/View Controllers/SectionEditor/SectionEditorViewController.m
+++ b/Wikipedia-iOS/View Controllers/SectionEditor/SectionEditorViewController.m
@@ -80,7 +80,7 @@
     [self showAlert:@"Loading wiki text..."];
     Section *section = (Section *)[articleDataContext_.mainContext 
objectWithID:self.sectionID];
     
-    DownloadSectionWikiTextOp *downloadWikiTextOp = 
[[DownloadSectionWikiTextOp alloc] initForPageTitle:section.article.title 
section:section.index completionBlock:^(NSString *revision){
+    DownloadSectionWikiTextOp *downloadWikiTextOp = 
[[DownloadSectionWikiTextOp alloc] initForPageTitle:section.article.title 
domain:section.article.domain section:section.index completionBlock:^(NSString 
*revision){
         
         [[NSOperationQueue mainQueue] addOperationWithBlock: ^ {
             [self showAlert:@"Wiki text loaded."];
@@ -141,7 +141,7 @@
 
     NSManagedObjectID *articleID = section.article.objectID;
 
-    UploadSectionWikiTextOp *uploadWikiTextOp = [[UploadSectionWikiTextOp 
alloc] initForPageTitle:section.article.title section:section.index 
wikiText:self.editTextView.text completionBlock:^(NSString *result){
+    UploadSectionWikiTextOp *uploadWikiTextOp = [[UploadSectionWikiTextOp 
alloc] initForPageTitle:section.article.title domain:section.article.domain 
section:section.index wikiText:self.editTextView.text 
completionBlock:^(NSString *result){
 
         // Remove all sections so they will be reloaded.
         // (Needs to be done on worker context as worker context changes 
bubble up through
diff --git a/Wikipedia-iOS/View Controllers/TableOfContents/TOCViewController.m 
b/Wikipedia-iOS/View Controllers/TableOfContents/TOCViewController.m
index 2fe41ac..36c8bb0 100644
--- a/Wikipedia-iOS/View Controllers/TableOfContents/TOCViewController.m
+++ b/Wikipedia-iOS/View Controllers/TableOfContents/TOCViewController.m
@@ -9,6 +9,7 @@
 #import "WebViewController.h"
 #import "UIWebView+ElementLocation.h"
 #import "UIView+Debugging.h"
+#import "SessionSingleton.h"
 
 #define TOC_SECTION_MARGIN 1.0f
 #define TOC_SELECTION_OFFSET_Y 48.0f
@@ -100,11 +101,13 @@
 
 -(void)getSectionIds
 {
-    NSString *currentArticleTitle = [[NSUserDefaults standardUserDefaults] 
objectForKey:@"CurrentArticleTitle"];
-    if(currentArticleTitle) {
+    NSString *currentArticleTitle = [SessionSingleton 
sharedInstance].currentArticleTitle;
+    NSString *currentArticleDomain = [SessionSingleton 
sharedInstance].currentArticleDomain;
+    if(currentArticleTitle && currentArticleDomain) {
         ArticleDataContextSingleton *articleDataContext_ = 
[ArticleDataContextSingleton sharedInstance];
         [articleDataContext_.mainContext performBlockAndWait:^{
-            NSManagedObjectID *articleID = [articleDataContext_.mainContext 
getArticleIDForTitle:currentArticleTitle];
+            NSManagedObjectID *articleID = [articleDataContext_.mainContext 
getArticleIDForTitle: currentArticleTitle
+                                                                               
           domain: currentArticleDomain];
             if (articleID) {
                 Article *article = (Article *)[articleDataContext_.mainContext 
objectWithID:articleID];
                 if (article) {
@@ -120,11 +123,13 @@
 
 -(void)getSectionImageIds
 {
-    NSString *currentArticleTitle = [[NSUserDefaults standardUserDefaults] 
objectForKey:@"CurrentArticleTitle"];
-    if(currentArticleTitle) {
+    NSString *currentArticleTitle = [SessionSingleton 
sharedInstance].currentArticleTitle;
+    NSString *currentArticleDomain = [SessionSingleton 
sharedInstance].currentArticleDomain;
+    if(currentArticleTitle && currentArticleDomain) {
         ArticleDataContextSingleton *articleDataContext_ = 
[ArticleDataContextSingleton sharedInstance];
         [articleDataContext_.mainContext performBlockAndWait:^{
-            NSManagedObjectID *articleID = [articleDataContext_.mainContext 
getArticleIDForTitle:currentArticleTitle];
+            NSManagedObjectID *articleID = [articleDataContext_.mainContext 
getArticleIDForTitle: currentArticleTitle
+                                                                               
           domain: currentArticleDomain];
             if (articleID) {
                 Article *article = (Article *)[articleDataContext_.mainContext 
objectWithID:articleID];
                 if (article) {
diff --git a/Wikipedia-iOS/View Controllers/WebView/WebViewController.m 
b/Wikipedia-iOS/View Controllers/WebView/WebViewController.m
index 0290d09..6bf6436 100644
--- a/Wikipedia-iOS/View Controllers/WebView/WebViewController.m
+++ b/Wikipedia-iOS/View Controllers/WebView/WebViewController.m
@@ -52,7 +52,7 @@
 @property (nonatomic) CGPoint scrollOffset;
 @property (nonatomic) BOOL unsafeToScroll;
 @property (nonatomic) NSInteger indexOfFirstOnscreenSectionBeforeRotate;
-@property (strong, nonatomic) NSDictionary *adjacentHistoryArticleTitles;
+@property (strong, nonatomic) NSDictionary *adjacentHistoryIDs;
 @property (nonatomic) BOOL tocVisible;
 @property (nonatomic) BOOL sectionEditingVisible;
 @property (nonatomic) NSUInteger sectionToEditIndex;
@@ -120,7 +120,9 @@
     // Observe chages to the search box search term.
     [[NSNotificationCenter defaultCenter] addObserver:self 
selector:@selector(searchStringChanged) name:@"SearchStringChanged" object:nil];
 
-    [self updateBottomBarButtonsEnabledState];
+    self.forwardButton.enabled = NO;
+    self.backButton.enabled = NO;
+    self.tocButton.enabled = NO;
 }
 
 -(void)viewDidAppear:(BOOL)animated
@@ -129,8 +131,9 @@
 }
 
 -(void)reloadCurrentArticle{
-    NSString *title = [self getCurrentArticleTitle];
-    [self navigateToPage:title discoveryMethod:DISCOVERY_METHOD_SEARCH];
+    NSString *title = [SessionSingleton sharedInstance].currentArticleTitle;
+    NSString *domain = [SessionSingleton sharedInstance].currentArticleDomain;
+    [self navigateToPage:title domain:domain 
discoveryMethod:DISCOVERY_METHOD_SEARCH];
 }
 
 #pragma mark Edit section
@@ -174,7 +177,8 @@
             if (!sectionEditVC) {
                 sectionEditVC = [self.navigationController.storyboard 
instantiateViewControllerWithIdentifier:@"SectionEditorViewController"];
 
-                NSManagedObjectID *articleID = 
[articleDataContext_.mainContext getArticleIDForTitle:[self 
getCurrentArticleTitle]];
+                NSManagedObjectID *articleID = 
[articleDataContext_.mainContext getArticleIDForTitle: [SessionSingleton 
sharedInstance].currentArticleTitle
+                                                                               
               domain: [SessionSingleton sharedInstance].currentArticleDomain];
                 if (articleID) {
                     Article *article = (Article 
*)[articleDataContext_.mainContext objectWithID:articleID];
                     
@@ -378,7 +382,10 @@
         
         if ([href hasPrefix:@"/wiki/"]) {
             NSString *title = [href substringWithRange:NSMakeRange(6, 
href.length - 6)];
-            [weakSelf navigateToPage:title 
discoveryMethod:DISCOVERY_METHOD_LINK];
+
+            [weakSelf navigateToPage: title
+                              domain: [SessionSingleton 
sharedInstance].currentArticleDomain
+                     discoveryMethod: DISCOVERY_METHOD_LINK];
         }else if ([href hasPrefix:@"//"]) {
             href = [@"http:" stringByAppendingString:href];
             
@@ -404,20 +411,6 @@
 
     self.unsafeToScroll = NO;
     self.scrollOffset = CGPointZero;
-}
-
-#pragma mark Loading last-viewed article on app start
-
--(void)setCurrentArticleTitle:(NSString *)currentArticleTitle
-{
-    [[NSUserDefaults standardUserDefaults] setObject:currentArticleTitle 
forKey:@"CurrentArticleTitle"];
-    [[NSUserDefaults standardUserDefaults] synchronize];
-}
-
--(NSString *)getCurrentArticleTitle
-{
-    NSString *currentArticleTitle = [[NSUserDefaults standardUserDefaults] 
objectForKey:@"CurrentArticleTitle"];
-    return currentArticleTitle;
 }
 
 #pragma mark History
@@ -464,7 +457,8 @@
 
 -(void)saveCurrentPage
 {
-    NSManagedObjectID *articleID = [articleDataContext_.mainContext 
getArticleIDForTitle:[self getCurrentArticleTitle]];
+    NSManagedObjectID *articleID = [articleDataContext_.mainContext 
getArticleIDForTitle: [SessionSingleton sharedInstance].currentArticleTitle
+                                                                               
   domain: [SessionSingleton sharedInstance].currentArticleDomain];
     
     if (!articleID) return;
     
@@ -534,7 +528,8 @@
 {
     [articleDataContext_.mainContext performBlockAndWait:^(){
         // Save scroll location
-        NSManagedObjectID *articleID = [articleDataContext_.mainContext 
getArticleIDForTitle:[self getCurrentArticleTitle]];
+        NSManagedObjectID *articleID = [articleDataContext_.mainContext 
getArticleIDForTitle: [SessionSingleton sharedInstance].currentArticleTitle
+                                                                               
       domain: [SessionSingleton sharedInstance].currentArticleDomain];
         if (articleID) {
             Article *article = (Article *)[articleDataContext_.mainContext 
objectWithID:articleID];
             if (article) {
@@ -639,7 +634,7 @@
 
 #pragma mark Article loading ops
 
-- (void)navigateToPage:(NSString *)title discoveryMethod:(NSString 
*)discoveryMethod
+- (void)navigateToPage:(NSString *)title domain:(NSString *)domain 
discoveryMethod:(NSString *)discoveryMethod
 {
     NSString *cleanTitle = [self cleanTitle:title];
     
@@ -653,20 +648,18 @@
     // Show loading message
     [self showAlert:SEARCH_LOADING_MSG_SECTION_ZERO];
     
-    [self retrieveArticleForPageTitle:cleanTitle 
discoveryMethod:discoveryMethod];
-    
-    // Update the back and forward buttons enabled state.
-    [self updateBottomBarButtonsEnabledState];
+    [self retrieveArticleForPageTitle:cleanTitle domain:domain 
discoveryMethod:discoveryMethod];
 }
 
-- (void)retrieveArticleForPageTitle:(NSString *)pageTitle 
discoveryMethod:(NSString *)discoveryMethod
+- (void)retrieveArticleForPageTitle:(NSString *)pageTitle domain:(NSString 
*)domain discoveryMethod:(NSString *)discoveryMethod
 {
     // Cancel any in-progress article retrieval operations
     [[QueuesSingleton sharedInstance].articleRetrievalQ cancelAllOperations];
     [[QueuesSingleton sharedInstance].searchQ cancelAllOperations];
     [[QueuesSingleton sharedInstance].thumbnailQ cancelAllOperations];
 
-   __block NSManagedObjectID *articleID = [articleDataContext_.mainContext 
getArticleIDForTitle:pageTitle];
+    __block NSManagedObjectID *articleID = [articleDataContext_.mainContext 
getArticleIDForTitle: pageTitle
+                                                                               
           domain: domain];
     
     if (articleID) {
         Article *article = (Article *)[articleDataContext_.mainContext 
objectWithID:articleID];
@@ -681,7 +674,7 @@
     }
 
     // Retrieve first section op
-    DownloadLeadSectionOp *firstSectionOp = [[DownloadLeadSectionOp alloc] 
initForPageTitle:pageTitle completionBlock:^(NSArray *sectionsRetrieved){
+    DownloadLeadSectionOp *firstSectionOp = [[DownloadLeadSectionOp alloc] 
initForPageTitle:pageTitle domain:[SessionSingleton 
sharedInstance].currentArticleDomain completionBlock:^(NSArray 
*sectionsRetrieved){
 
         Article *article = nil;
         
@@ -693,8 +686,8 @@
             article.title = pageTitle;
             article.dateCreated = [NSDate date];
             article.site = [SessionSingleton sharedInstance].site;
-            article.domain = [SessionSingleton sharedInstance].domain;
-            article.domainName = [SessionSingleton sharedInstance].domainName;
+            article.domain = [SessionSingleton 
sharedInstance].currentArticleDomain;
+            article.domainName = [SessionSingleton 
sharedInstance].currentArticleDomainName;
             articleID = article.objectID;
         }else{
             article = (Article *)[articleDataContext_.workerContext 
objectWithID:articleID];
@@ -741,22 +734,10 @@
         //history0.dateVisited = [NSDate dateWithDaysBeforeNow:31];
         history0.discoveryMethod = discoveryMethod;
         [article addHistoryObject:history0];
-
-        article.site = [SessionSingleton sharedInstance].site;   
//self.currentSite;
-        article.domain = [SessionSingleton sharedInstance].domain; 
//self.currentDomain;
-        article.domainName = [SessionSingleton sharedInstance].domainName;
-
-        // Add saved for article
-        //Saved *saved0 = [NSEntityDescription 
insertNewObjectForEntityForName:@"Saved" inManagedObjectContext:dataContext_];
-        //saved0.dateSaved = [NSDate date];
-        //[article addSavedObject:saved0];
         
         // Save the article!
         NSError *error = nil;
         [articleDataContext_.workerContext save:&error];
-
-        // Update the back and forward buttons enabled state now that there's 
a new history entry.
-        [self updateBottomBarButtonsEnabledState];
 
         if (error) {
             NSLog(@"error = %@", error);
@@ -787,7 +768,7 @@
     firstSectionOp.delegate = self;
     
     // Retrieve remaining sections op (dependent on first section op)
-    DownloadNonLeadSectionsOp *remainingSectionsOp = 
[[DownloadNonLeadSectionsOp alloc] initForPageTitle:pageTitle 
completionBlock:^(NSArray *sectionsRetrieved){
+    DownloadNonLeadSectionsOp *remainingSectionsOp = 
[[DownloadNonLeadSectionsOp alloc] initForPageTitle:pageTitle 
domain:[SessionSingleton sharedInstance].currentArticleDomain 
completionBlock:^(NSArray *sectionsRetrieved){
         
         // Just in case the article wasn't created during the "parent" 
operation.
         if (!articleID) return;
@@ -887,7 +868,10 @@
     Article *article = (Article *)[articleDataContext_.mainContext 
objectWithID:articleID];
 
     if (!article) return;
-    [self setCurrentArticleTitle:article.title];
+    [SessionSingleton sharedInstance].currentArticleTitle = article.title;
+    [SessionSingleton sharedInstance].currentArticleDomain = article.domain;
+
+    [self updateBottomBarButtonsEnabledState];
 
     NSArray *sortedSections = [article.section 
sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];
     NSMutableArray *sectionTextArray = [@[] mutableCopy];
@@ -938,57 +922,76 @@
 
 - (IBAction)backButtonPushed:(id)sender
 {
-    NSString *title = self.adjacentHistoryArticleTitles[@"before"];
-    if (title.length > 0) [self navigateToPage:title 
discoveryMethod:DISCOVERY_METHOD_SEARCH];
+    NSManagedObjectID *historyId = self.adjacentHistoryIDs[@"before"];
+    if (historyId){
+        History *history = (History *)[articleDataContext_.mainContext 
objectWithID:historyId];
+        [self navigateToPage:history.article.title 
domain:history.article.domain discoveryMethod:history.discoveryMethod];
+    }
 }
 
 - (IBAction)forwardButtonPushed:(id)sender
 {
-    NSString *title = self.adjacentHistoryArticleTitles[@"after"];
-    if (title.length > 0) [self navigateToPage:title 
discoveryMethod:DISCOVERY_METHOD_SEARCH];
+    NSManagedObjectID *historyId = self.adjacentHistoryIDs[@"after"];
+    if (historyId){
+        History *history = (History *)[articleDataContext_.mainContext 
objectWithID:historyId];
+        [self navigateToPage:history.article.title 
domain:history.article.domain discoveryMethod:history.discoveryMethod];
+    }
 }
 
--(NSDictionary *)getTitlesForAdjacentHistoryArticles
+-(NSDictionary *)getAdjacentHistoryIDs
 {
-    NSMutableDictionary *result = [@{} mutableCopy];
-    NSError *error = nil;
-    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
-    NSEntityDescription *entity = [NSEntityDescription entityForName: 
@"History"
-                                              inManagedObjectContext: 
articleDataContext_.mainContext];
-    [fetchRequest setEntity:entity];
+    __block NSManagedObjectID *currentHistoryId = nil;
+    __block NSManagedObjectID *beforeHistoryId = nil;
+    __block NSManagedObjectID *afterHistoryId = nil;
     
-    NSSortDescriptor *dateSort = [[NSSortDescriptor alloc] 
initWithKey:@"dateVisited" ascending:YES selector:nil];
-    
-    [fetchRequest setSortDescriptors:@[dateSort]];
-    
-    error = nil;
-    NSArray *historyEntities = [articleDataContext_.mainContext 
executeFetchRequest:fetchRequest error:&error];
-    
-    NSString *titleOfArticleBeforeCurrentOne = @"";
-    NSString *titleOfArticleAfterCurrentOne = @"";
-    NSString *currentArticleTitle = [self getCurrentArticleTitle];
-    NSString *lastTitle = @"";
-    for (History *history in historyEntities) {
-        NSString *thisTitle = history.article.title;
-        if ([thisTitle isEqualToString:currentArticleTitle]) {
-            titleOfArticleBeforeCurrentOne = lastTitle;
-        }else if ([lastTitle isEqualToString:currentArticleTitle]) {
-            titleOfArticleAfterCurrentOne = thisTitle;
+    [articleDataContext_.workerContext performBlockAndWait:^(){
+        
+        NSError *error = nil;
+        NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
+        NSEntityDescription *entity = [NSEntityDescription entityForName: 
@"History"
+                                                  inManagedObjectContext: 
articleDataContext_.workerContext];
+        [fetchRequest setEntity:entity];
+        
+        NSSortDescriptor *dateSort = [[NSSortDescriptor alloc] 
initWithKey:@"dateVisited" ascending:YES selector:nil];
+        
+        [fetchRequest setSortDescriptors:@[dateSort]];
+        
+        error = nil;
+        NSArray *historyEntities = [articleDataContext_.workerContext 
executeFetchRequest:fetchRequest error:&error];
+        
+        NSManagedObjectID *currentArticleId = 
[articleDataContext_.workerContext getArticleIDForTitle: [SessionSingleton 
sharedInstance].currentArticleTitle
+                                                                               
                domain: [SessionSingleton sharedInstance].currentArticleDomain];
+        for (NSUInteger i = 0; i < historyEntities.count; i++) {
+            History *history = historyEntities[i];
+            if (history.article.objectID == currentArticleId){
+                currentHistoryId = history.objectID;
+                if (i > 0) {
+                    History *beforeHistory = historyEntities[i - 1];
+                    beforeHistoryId = beforeHistory.objectID;
+                }
+                if ((i + 1) <= (historyEntities.count - 1)) {
+                    History *afterHistory = historyEntities[i + 1];
+                    afterHistoryId = afterHistory.objectID;
+                }
+                break;
+            }
         }
-        lastTitle = thisTitle;
-    }
-    if(titleOfArticleBeforeCurrentOne.length > 0) result[@"before"] = 
titleOfArticleBeforeCurrentOne;
-    if(currentArticleTitle.length > 0) result[@"current"] = 
currentArticleTitle;
-    if(titleOfArticleAfterCurrentOne.length > 0) result[@"after"] = 
titleOfArticleAfterCurrentOne;
+    }];
+
+    NSMutableDictionary *result = [@{} mutableCopy];
+    if(beforeHistoryId) result[@"before"] = beforeHistoryId;
+    if(currentHistoryId) result[@"current"] = currentHistoryId;
+    if(afterHistoryId) result[@"after"] = afterHistoryId;
+
     return result;
 }
 
 -(void)updateBottomBarButtonsEnabledState
 {
-    self.adjacentHistoryArticleTitles = [self 
getTitlesForAdjacentHistoryArticles];
-    self.forwardButton.enabled = (self.adjacentHistoryArticleTitles[@"after"]) 
? YES : NO;
-    self.backButton.enabled = (self.adjacentHistoryArticleTitles[@"before"]) ? 
YES : NO;
-    NSString *currentArticleTitle = [self getCurrentArticleTitle];
+    self.adjacentHistoryIDs = [self getAdjacentHistoryIDs];
+    self.forwardButton.enabled = (self.adjacentHistoryIDs[@"after"]) ? YES : 
NO;
+    self.backButton.enabled = (self.adjacentHistoryIDs[@"before"]) ? YES : NO;
+    NSString *currentArticleTitle = [SessionSingleton 
sharedInstance].currentArticleTitle;
     self.tocButton.enabled = (currentArticleTitle && 
(currentArticleTitle.length > 0)) ? YES : NO;
 }
 
@@ -1023,7 +1026,8 @@
 
 
-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
 duration:(NSTimeInterval)duration
 {
-    NSManagedObjectID *articleID = [articleDataContext_.mainContext 
getArticleIDForTitle:[self getCurrentArticleTitle]];
+    NSManagedObjectID *articleID = [articleDataContext_.mainContext 
getArticleIDForTitle: [SessionSingleton sharedInstance].currentArticleTitle
+                                                                               
   domain: [SessionSingleton sharedInstance].currentArticleDomain];
     if (articleID) {
         Article *article = (Article *)[articleDataContext_.mainContext 
objectWithID:articleID];
 

-- 
To view, visit https://gerrit.wikimedia.org/r/108965
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: Id7aafe3924517696cc6e54b24893bee92539a5f4
Gerrit-PatchSet: 1
Gerrit-Project: apps/ios/wikipedia
Gerrit-Branch: master
Gerrit-Owner: Mhurd <mh...@wikimedia.org>
Gerrit-Reviewer: Brion VIBBER <br...@wikimedia.org>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to