Skip to content

Commit

Permalink
Merge pull request #4825 from wikimedia/image-rec/fix-explore-toggle
Browse files Browse the repository at this point in the history
Image Recommendations - Fix explore card toggle bug
  • Loading branch information
mazevedofs committed Apr 29, 2024
2 parents 7f7f158 + 4579862 commit 7628078
Showing 1 changed file with 33 additions and 23 deletions.
56 changes: 33 additions & 23 deletions Wikipedia/Code/MWKDataStore.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
NSString *const WMFViewContextDidResetNotification = @"WMFViewContextDidResetNotification";

NSString *const WMFLibraryVersionKey = @"WMFLibraryVersion";
static const NSInteger WMFCurrentLibraryVersion = 17;
static const NSInteger WMFCurrentLibraryVersion = 18;

NSString *const WMFCoreDataSynchronizerInfoFileName = @"Wikipedia.info";

Expand Down Expand Up @@ -106,21 +106,22 @@ - (instancetype)initWithContainerURL:(NSURL *)containerURL {
}

- (void)finishSetup:(nullable dispatch_block_t)completion {
[self setupCoreDataStackWithContainerURL:self.containerURL completion:^{
[self setupCoreDataSynchronizersWithContainerURL:self.containerURL];
[self startSynchronizingLibraryContexts];
[self setupHistoryAndSavedPageLists];
self.languageLinkController = [[MWKLanguageLinkController alloc] initWithManagedObjectContext:self.viewContext];
self.feedContentController = [[WMFExploreFeedContentController alloc] initWithDataStore:self];
[self.feedContentController updateContentSources];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didReceiveMemoryWarningWithNotification:) name:UIApplicationDidReceiveMemoryWarningNotification object:nil];
self.remoteNotificationsController = [[RemoteNotificationsController alloc] initWithSession:self.session configuration:self.configuration languageLinkController:self.languageLinkController authManager:self.authenticationManager];
self.notificationsController = [[WMFNotificationsController alloc] initWithDataStore:self languageLinkController:self.languageLinkController];
self.articleSummaryController = [[WMFArticleSummaryController alloc] initWithSession:self.session configuration:self.configuration dataStore:self];
if (completion) {
completion();
}
}];
[self setupCoreDataStackWithContainerURL:self.containerURL
completion:^{
[self setupCoreDataSynchronizersWithContainerURL:self.containerURL];
[self startSynchronizingLibraryContexts];
[self setupHistoryAndSavedPageLists];
self.languageLinkController = [[MWKLanguageLinkController alloc] initWithManagedObjectContext:self.viewContext];
self.feedContentController = [[WMFExploreFeedContentController alloc] initWithDataStore:self];
[self.feedContentController updateContentSources];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didReceiveMemoryWarningWithNotification:) name:UIApplicationDidReceiveMemoryWarningNotification object:nil];
self.remoteNotificationsController = [[RemoteNotificationsController alloc] initWithSession:self.session configuration:self.configuration languageLinkController:self.languageLinkController authManager:self.authenticationManager];
self.notificationsController = [[WMFNotificationsController alloc] initWithDataStore:self languageLinkController:self.languageLinkController];
self.articleSummaryController = [[WMFArticleSummaryController alloc] initWithSession:self.session configuration:self.configuration dataStore:self];
if (completion) {
completion();
}
}];
}

- (void)teardown:(nullable dispatch_block_t)completion {
Expand Down Expand Up @@ -212,8 +213,8 @@ - (void)setupCoreDataStackWithContainerURL:(NSURL *)containerURL completion:(nul
[description setOption:@YES forKey:NSInferMappingModelAutomaticallyOption];
description.shouldAddStoreAsynchronously = YES;
container.persistentStoreDescriptions = @[description];
[container loadPersistentStoresWithCompletionHandler:^(NSPersistentStoreDescription * _Nonnull description, NSError * _Nullable error) {

[container loadPersistentStoresWithCompletionHandler:^(NSPersistentStoreDescription *_Nonnull description, NSError *_Nullable error) {
if (error) {
// TODO: Metrics
DDLogError(@"Error adding persistent store: %@", error);
Expand All @@ -222,15 +223,15 @@ - (void)setupCoreDataStackWithContainerURL:(NSURL *)containerURL completion:(nul
}
return;
}

dispatch_async(dispatch_get_main_queue(), ^(void) {
self.persistentContainer = container;
self.viewContext = container.viewContext;
self.viewContext.mergePolicy = NSMergeByPropertyStoreTrumpMergePolicy;
self.viewContext.automaticallyMergesChangesFromParent = YES;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(managedObjectContextDidSave:) name:NSManagedObjectContextDidSaveNotification object:self.viewContext];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(viewContextDidChange:) name:NSManagedObjectContextObjectsDidChangeNotification object:self.viewContext];

if (completion) {
completion();
}
Expand All @@ -245,7 +246,7 @@ - (void)viewContextDidChange:(NSNotification *)note {
// NSInvalidatedAllObjectsKey is present when NSManagedObjectContext.reset() is called
if (userInfo[NSInvalidatedAllObjectsKey]) {
[self clearMemoryCache];
[nc postNotificationName:WMFViewContextDidResetNotification object: nil];
[nc postNotificationName:WMFViewContextDidResetNotification object:nil];
}
for (NSString *key in keys) {
NSSet<NSManagedObject *> *changedObjects = userInfo[key];
Expand Down Expand Up @@ -473,7 +474,7 @@ - (void)performUpdatesFromLibraryVersion:(NSUInteger)currentLibraryVersion inMan
return;
}
}

if (currentLibraryVersion < 15) {
[self markAllNeedingConversionFromMobileviewArticlesAsNotDownloaded:moc];
[moc wmf_setValue:@(15) forKey:WMFLibraryVersionKey];
Expand All @@ -491,7 +492,7 @@ - (void)performUpdatesFromLibraryVersion:(NSUInteger)currentLibraryVersion inMan
return;
}
}

if (currentLibraryVersion < 17) {
[self migrateAKToTWInManagedObjectContext:moc];
[moc wmf_setValue:@(17) forKey:WMFLibraryVersionKey];
Expand All @@ -501,6 +502,15 @@ - (void)performUpdatesFromLibraryVersion:(NSUInteger)currentLibraryVersion inMan
}
}

if (currentLibraryVersion < 18) {
[self.feedContentController toggleContentGroupOfKind:WMFContentGroupKindSuggestedEdits isOn:YES updateFeed:NO];
[moc wmf_setValue:@(18) forKey:WMFLibraryVersionKey];
if ([moc hasChanges] && ![moc save:&migrationError]) {
DDLogError(@"Error saving during migration: %@", migrationError);
return;
}
}

// IMPORTANT: When adding a new library version and migration, update WMFCurrentLibraryVersion to the latest version number
}

Expand Down

0 comments on commit 7628078

Please sign in to comment.