Skip to content

Commit

Permalink
Filter non-active pipelines from the user-enabled verification pipelines
Browse files Browse the repository at this point in the history
  • Loading branch information
lukbukkit committed Oct 22, 2024
1 parent fd3b44f commit 4fc4518
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ struct AdvancedSettingsView: View {

// Add or remove the pipeline in question
if newVal {
enabledPipelines.append(pipeline.id)
enabledPipelines.insert(pipeline.id)
Self.logger.info("User enabled pipeline \(pipeline.name) with id \(pipeline.id)")
} else {
if let index = enabledPipelines.firstIndex(of: pipeline.id) {
Expand All @@ -87,8 +87,8 @@ struct AdvancedSettingsView: View {
}
}

// Update user defaults with the new array
UserDefaults.standard.setValue(enabledPipelines, forKey: UserDefaultsKeys.activePipelines.rawValue)
// Update user defaults with the new array (convert set to array beforehand)
UserDefaults.standard.setValue(enabledPipelines.sorted(), forKey: UserDefaultsKeys.activePipelines.rawValue)
}))
.disabled(primary)
}
Expand Down
22 changes: 12 additions & 10 deletions CellGuardAppSwift/CellGuard/UserDefaultsKeys.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,29 +76,31 @@ extension UserDefaults {
}
}

func userEnabledVerificationPipelineIds() -> [Int16] {
func userEnabledVerificationPipelineIds() -> Set<Int16> {
let primaryId = primaryVerificationPipeline.id

// If none are set, just enable the primary pipeline
guard let pipelineIds = array(forKey: UserDefaultsKeys.activePipelines.rawValue) as? [Int16] else {
return [primaryId]
guard let pipelineIdsArray = array(forKey: UserDefaultsKeys.activePipelines.rawValue) as? [Int16] else {
return Set([primaryId])
}

// Convert to set
var pipelineIds = Set(pipelineIdsArray)

// Add primary pipeline if missing
var localIds = pipelineIds
if !localIds.contains(primaryId) {
localIds.append(primaryId)
}
pipelineIds.insert(primaryId)

// Sort the pipelines by their id
return localIds.sorted { $0 < $1 }
// Remove non-active pipelines
pipelineIds.formIntersection(activeVerificationPipelines.map { $0.id })

return pipelineIds
}

func userEnabledVerificationPipelines() -> [any VerificationPipeline] {
// Map all pipeline ids to their pipeline
return userEnabledVerificationPipelineIds().compactMap { pipelineId in
activeVerificationPipelines.first { $0.id == pipelineId }
}
}.sorted { $0.id < $1.id }
}

}

0 comments on commit 4fc4518

Please sign in to comment.