Skip to content

Commit

Permalink
Skip onboarding migration if no migratable profiles (#1002)
Browse files Browse the repository at this point in the history
Annoying!
  • Loading branch information
keeshux authored Dec 11, 2024
1 parent c6b3ad5 commit 81aa3a2
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 2 deletions.
7 changes: 7 additions & 0 deletions Library/Sources/AppUIMain/Views/App/OnboardingModifier.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ import SwiftUI

struct OnboardingModifier: ViewModifier {

@EnvironmentObject
private var migrationManager: MigrationManager

@Environment(\.isUITesting)
private var isUITesting

Expand Down Expand Up @@ -87,6 +90,10 @@ private extension OnboardingModifier {

switch step {
case .migrateV3:
guard migrationManager.hasMigratableProfiles else {
advance()
return
}
modalRoute = .migrateProfiles
case .community:
isPresentingCommunity = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ public final class MigrationManager: ObservableObject {
// MARK: - Public interface

extension MigrationManager {
public var hasMigratableProfiles: Bool {
profileStrategy.hasMigratableProfiles
}

public func fetchMigratableProfiles() async throws -> [MigratableProfile] {
try await profileStrategy.fetchMigratableProfiles()
}
Expand Down Expand Up @@ -173,7 +177,8 @@ private extension MigrationManager {
// MARK: - Dummy

private final class DummyProfileStrategy: ProfileMigrationStrategy {
public init() {
var hasMigratableProfiles: Bool {
false
}

public func fetchMigratableProfiles() async throws -> [MigratableProfile] {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import Foundation
import PassepartoutKit

public protocol ProfileMigrationStrategy {
var hasMigratableProfiles: Bool { get}

func fetchMigratableProfiles() async throws -> [MigratableProfile]

func fetchProfile(withId profileId: UUID) async throws -> Profile?
Expand Down
18 changes: 17 additions & 1 deletion Library/Sources/LegacyV2/Strategy/CDProfileRepositoryV2.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,30 @@ final class CDProfileRepositoryV2: Sendable {
self.context = context
}

var hasMigratableProfiles: Bool {
do {
return try context.performAndWait { [weak self] in
guard let self else {
return false
}
let entities = try CDProfile.fetchRequest().execute()
return !entities.compactMap {
($0.encryptedJSON ?? $0.json) != nil
}.isEmpty
}
} catch {
return false
}
}

func migratableProfiles() async throws -> [MigratableProfile] {
try await fetchProfiles(
prefetch: {
$0.propertiesToFetch = ["uuid", "name", "lastUpdate"]
},
map: {
$0.compactMap {
guard $0.value.encryptedJSON ?? $0.value.json != nil else {
guard ($0.value.encryptedJSON ?? $0.value.json) != nil else {
pp_log(.App.migration, .error, "ProfileV2 \($0.key) is not migratable: missing JSON")
return nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ public final class ProfileV2MigrationStrategy: ProfileMigrationStrategy, Sendabl
// MARK: - ProfileMigrationStrategy

extension ProfileV2MigrationStrategy {
public var hasMigratableProfiles: Bool {
profilesRepository.hasMigratableProfiles
}

public func fetchMigratableProfiles() async throws -> [MigratableProfile] {
try await profilesRepository.migratableProfiles()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ final class MockProfileMigrationStrategy: ProfileMigrationStrategy {

var failedProfiles: Set<UUID> = []

var hasMigratableProfiles: Bool {
!migratedProfiles.isEmpty
}

func fetchMigratableProfiles() async throws -> [MigratableProfile] {
migratableProfiles
}
Expand Down

0 comments on commit 81aa3a2

Please sign in to comment.