Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mark podcasts unsynced when user changes #2198

Draft
wants to merge 2 commits into
base: trunk
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ public enum FeatureFlag: String, CaseIterable {
/// When `true`, we only mark podcasts as unsynced if the user never signed in before
case onlyMarkPodcastsUnsyncedForNewUsers

/// When a user sign in, we always marked ALL podcasts as unsynced
/// The `onlyMarkPodcastsUnsyncedForNewUsers` flag added a check that a user was new before marking unsynced
/// This _also_ caused issues with users who signed out and signed in to a new account, causing podcasts to remain unsynced
/// When `true`, we mark podcasts as unsynced if the user accounts changed
case onlyMarkPodcastsUnsyncedForChangedUsers

/// Only update an episode if it fails playing
/// If set to `false`, it will use the previous mechanism that always update
/// but can lead to a bigger time between tapping play and actually playing it
Expand Down Expand Up @@ -193,6 +199,8 @@ public enum FeatureFlag: String, CaseIterable {
true
case .useMimetypePackage:
true
case .onlyMarkPodcastsUnsyncedForChangedUsers:
true
}
}

Expand Down
8 changes: 7 additions & 1 deletion podcasts/AuthenticationHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ class AuthenticationHelper {
// MARK: Common

private static func handleSuccessfulSignIn(_ response: AuthenticationResponse) {

let isNewUser = ServerSettings.lastSyncTime == nil
let userHasChanged = ServerSettings.userId != response.uuid

SyncManager.clearTokensFromKeyChain()

ServerSettings.userId = response.uuid
Expand All @@ -66,7 +70,9 @@ class AuthenticationHelper {

// we've signed in, set all our existing podcasts to
// be non synced if the user never logged in before
if (FeatureFlag.onlyMarkPodcastsUnsyncedForNewUsers.enabled && ServerSettings.lastSyncTime == nil)
if (FeatureFlag.onlyMarkPodcastsUnsyncedForNewUsers.enabled && isNewUser)
// Instead of checking that the user has never logged in before, it may make sense to check that the account has changed
|| (FeatureFlag.onlyMarkPodcastsUnsyncedForChangedUsers.enabled && userHasChanged)
|| !FeatureFlag.onlyMarkPodcastsUnsyncedForNewUsers.enabled {
DataManager.sharedManager.markAllPodcastsUnsynced()
}
Expand Down
40 changes: 23 additions & 17 deletions podcasts/SyncSigninViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -271,10 +271,29 @@ class SyncSigninViewController: PCViewController, UITextFieldDelegate {
activityIndicatorView.isHidden = false

mainButton.setTitle("", for: .normal)
ApiServerHandler.shared.validateLogin(username: username, password: password) { success, userId, error in
DispatchQueue.main.async {
if !success {
Analytics.track(.userSignInFailed, properties: ["source": "password", "error_code": (error ?? .UNKNOWN).rawValue])
Task {
do {
let response = try await AuthenticationHelper.validateLogin(username: username, password: password, scope: .mobile)

let userId = response.uuid
DispatchQueue.main.async {
self.progressAlert = ShiftyLoadingAlert(title: L10n.syncAccountLogin)
self.progressAlert?.showAlert(self, hasProgress: false, completion: {
// clear any previously stored tokens as we're signing in again and we might have one in Keychain already
SyncManager.clearTokensFromKeyChain()

self.handleSuccessfulSignIn(username, password: password, userId: userId)
RefreshManager.shared.refreshPodcasts(forceEvenIfRefreshedRecently: true)
Settings.setPromotionFinishedAcknowledged(true)
Settings.setLoginDetailsUpdated()

NotificationCenter.postOnMainThread(notification: .userSignedIn)
})
}
} catch let error {
DispatchQueue.main.async {
let error = error as? APIError
Analytics.track(.userSignInFailed, properties: ["source": "password", "error_code": (error ?? APIError.UNKNOWN).rawValue])

if error != .UNKNOWN, let message = error?.localizedDescription, !message.isEmpty {
self.showErrorMessage(message)
Expand All @@ -289,19 +308,6 @@ class SyncSigninViewController: PCViewController, UITextFieldDelegate {
self.progressAlert = nil
return
}

self.progressAlert = ShiftyLoadingAlert(title: L10n.syncAccountLogin)
self.progressAlert?.showAlert(self, hasProgress: false, completion: {
// clear any previously stored tokens as we're signing in again and we might have one in Keychain already
SyncManager.clearTokensFromKeyChain()

self.handleSuccessfulSignIn(username, password: password, userId: userId)
RefreshManager.shared.refreshPodcasts(forceEvenIfRefreshedRecently: true)
Settings.setPromotionFinishedAcknowledged(true)
Settings.setLoginDetailsUpdated()

NotificationCenter.postOnMainThread(notification: .userSignedIn)
})
}
}
}
Expand Down