Skip to content

Commit

Permalink
Merge pull request #5074 from wikimedia/yir-data-population-cleanup
Browse files Browse the repository at this point in the history
[YiR] Clear out edit data upon logout, populate data upon login and settings toggle
  • Loading branch information
l-olson1214 authored Nov 15, 2024
2 parents 0519898 + f0b132d commit e1e6356
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import CoreData
private let developerSettingsDataController: WMFDeveloperSettingsDataControlling

public let targetConfigYearID = "2024.1"
@objc public static let targetYear = 2024
public static let appShareLink = "https://apps.apple.com/app/apple-store/id324715238?pt=208305&ct=yir_2024_share&mt=8"

private let service = WMFDataEnvironment.current.mediaWikiService

Expand Down Expand Up @@ -153,7 +155,7 @@ import CoreData
}

// Check persisted year in review report. Year in Review entry point should display if one or more personalized slides are set to display and slide is not disabled in remote config
guard let yirReport = try? fetchYearInReviewReport(forYear: 2024) else {
guard let yirReport = try? fetchYearInReviewReport(forYear: Self.targetYear) else {
return false
}

Expand Down Expand Up @@ -696,6 +698,45 @@ import CoreData
}
}

public func deleteAllPersonalizedEditingData() async throws {
let backgroundContext = try coreDataStore.newBackgroundContext

try await backgroundContext.perform { [weak self] in
guard let self else { return }

let cdReports = try self.coreDataStore.fetch(
entityType: CDYearInReviewReport.self,
predicate: nil,
fetchLimit: nil,
in: backgroundContext
)

guard let cdReports else {
return
}

for report in cdReports {

guard let slides = report.slides as? Set<CDYearInReviewSlide> else {
continue
}

for slide in slides {

guard slide.id == WMFYearInReviewPersonalizedSlideID.editCount.rawValue else {
continue
}

slide.data = nil
slide.display = false
slide.evaluated = false
}
}

try self.coreDataStore.saveIfNeeded(moc: backgroundContext)
}
}

public func fetchUserContributionsCount(username: String, project: WMFProject?, startDate: String, endDate: String) async throws -> (Int, Bool) {
return try await withCheckedThrowingContinuation { continuation in
fetchUserContributionsCount(username: username, project: project, startDate: startDate, endDate: endDate) { result in
Expand Down
11 changes: 11 additions & 0 deletions Wikipedia/Code/WMFAppViewController+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,17 @@ extension WMFAppViewController {
}
}
}

@objc func deleteYearInReviewPersonalizedEditingData() {
Task {
do {
let yirDataController = try WMFYearInReviewDataController()
try await yirDataController.deleteAllPersonalizedEditingData()
} catch {
DDLogError("Failure deleting personalized editing data from year in review: \(error)")
}
}
}
}

// MARK: WMFComponents App Environment Helpers
Expand Down
8 changes: 7 additions & 1 deletion Wikipedia/Code/WMFAppViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
#import "Wikipedia-Swift.h"
#import "EXTScope.h"

@import WMFData;

/**
* Enums for each tab in the main tab bar.
*/
Expand Down Expand Up @@ -398,7 +400,7 @@ - (void)performTasksThatShouldOccurAfterBecomeActiveAndResume {
[self checkRemoteAppConfigIfNecessary];
[self.periodicWorkerController start];
[self.savedArticlesFetcher start];
[self populateYearInReviewReportFor:2024];
[self populateYearInReviewReportFor:WMFYearInReviewDataController.targetYear];
}

- (void)performTasksThatShouldOccurAfterAnnouncementsUpdated {
Expand Down Expand Up @@ -2210,6 +2212,8 @@ - (void)userWasLoggedOut:(NSNotification *)note {
force:YES
completion:nil];
});

[self deleteYearInReviewPersonalizedEditingData];
}

- (void)userWasLoggedIn:(NSNotification *)note {
Expand All @@ -2228,6 +2232,8 @@ - (void)userWasLoggedIn:(NSNotification *)note {
force:YES
completion:nil];
});

[self populateYearInReviewReportFor:WMFYearInReviewDataController.targetYear];
}

- (void)authManagerDidHandlePrimaryLanguageChange:(NSNotification *)note {
Expand Down
2 changes: 1 addition & 1 deletion Wikipedia/Code/WMFSettingsViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,7 @@ - (void)showNotifications {
#pragma mark - Year in Review

- (void)showYearInReview {
WMFYearInReviewSettingsViewController *yearInReviewSettingsVC = [[WMFYearInReviewSettingsViewController alloc] initWithTheme:self.theme];
WMFYearInReviewSettingsViewController *yearInReviewSettingsVC = [[WMFYearInReviewSettingsViewController alloc] initWithDataStore:self.dataStore theme:self.theme];
[self.navigationController pushViewController:yearInReviewSettingsVC animated:YES];
}

Expand Down
4 changes: 2 additions & 2 deletions Wikipedia/Code/YearInReviewCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ final class YearInReviewCoordinator: NSObject, Coordinator {
private func getPersonalizedSlides() -> PersonalizedSlides {

guard let dataController = try? WMFYearInReviewDataController(),
let report = try? dataController.fetchYearInReviewReport(forYear: 2024) else {
let report = try? dataController.fetchYearInReviewReport(forYear: WMFYearInReviewDataController.targetYear) else {
return PersonalizedSlides(readCount: nil, editCount: nil, donateCount: nil)
}

Expand Down Expand Up @@ -390,7 +390,7 @@ final class YearInReviewCoordinator: NSObject, Coordinator {
usernameTitle: CommonStrings.userTitle
)

let appShareLink = "https://apps.apple.com/app/apple-store/id324715238?pt=208305&ct=yir_2024_share&mt=8"
let appShareLink = WMFYearInReviewDataController.appShareLink
let hashtag = "#WikipediaYearInReview"

let viewModel = WMFYearInReviewViewModel(localizedStrings: localizedStrings, slides: slides, username: dataStore.authenticationManager.authStatePermanentUsername, shareLink: appShareLink, hashtag: hashtag, hasPersonalizedDonateSlide: hasPersonalizedDonateSlide, coordinatorDelegate: self, loggingDelegate: self, badgeDelegate: badgeDelegate)
Expand Down
34 changes: 33 additions & 1 deletion Wikipedia/Code/YearInReviewSettingsViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,22 @@ final class YearInReviewSettingsViewController: SubSettingsViewController {

// MARK: - Properties

private let dataStore: MWKDataStore
private var sections: [YearInReviewSettingsSection] = []
private let dataController = try? WMFYearInReviewDataController()

fileprivate let headerText = WMFLocalizedString("settings-year-in-review-header", value: "Turning off Year in Review will clear all stored personalized insights and hide the Year in Review.", comment: "Text informing user of benefits of hiding the year in review feature.") + "\n"

// MARK: - Lifecycle

@objc init(dataStore: MWKDataStore, theme: Theme) {
self.dataStore = dataStore
super.init(theme: theme)
}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

override func viewDidLoad() {
super.viewDidLoad()
Expand Down Expand Up @@ -75,7 +85,8 @@ final class YearInReviewSettingsViewController: SubSettingsViewController {
DDLogError("Error deleting year in review reports: \(error)")
}
}

} else {
self?.populateYearInReviewReportData()
}
})

Expand All @@ -84,6 +95,27 @@ final class YearInReviewSettingsViewController: SubSettingsViewController {

self.tableView.reloadData()
}

private func populateYearInReviewReportData() {
guard let language = dataStore.languageLinkController.appLanguage?.languageCode,
let countryCode = Locale.current.region?.identifier
else { return }
let wmfLanguage = WMFLanguage(languageCode: language, languageVariantCode: nil)
let project = WMFProject.wikipedia(wmfLanguage)

Task {
do {
let yirDataController = try WMFYearInReviewDataController()
try await yirDataController.populateYearInReviewReportData(
for: WMFYearInReviewDataController.targetYear,
countryCode: countryCode,
primaryAppLanguageProject: project,
username: dataStore.authenticationManager.authStatePermanentUsername)
} catch {
DDLogError("Failure populating year in review report: \(error)")
}
}
}

// MARK: - UITableView

Expand Down

0 comments on commit e1e6356

Please sign in to comment.