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

Fix SwiftLint 0.47.0 warning #508

Merged
merged 1 commit into from
Apr 6, 2022
Merged
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
52 changes: 26 additions & 26 deletions DuckDuckGo/Statistics/ATB/VariantManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,27 @@ enum FeatureName: String {

// Used for unit tests
case dummy

}

struct Variant {

struct When {
static let always = { return true }

static let inRequiredCountry = { return ["AU", "AT", "DK", "FI", "FR", "DE", "IT", "IE", "NZ", "NO", "ES", "SE", "GB"]
.contains(where: { Locale.current.regionCode == $0 }) }
.contains(where: { Locale.current.regionCode == $0 }) }

static let inEnglish = { return Locale.current.languageCode == "en" }
}

static let doNotAllocate = 0

// Note: Variants with `doNotAllocate` weight, should always be included so that previous installations are unaffected
static let defaultVariants: [Variant] = [

]

let name: String
let weight: Int
let isIncluded: () -> Bool
Expand All @@ -53,30 +53,30 @@ struct Variant {
}

protocol VariantRNG {

func nextInt(upperBound: Int) -> Int

}

protocol VariantManager {

var currentVariant: Variant? { get }
func assignVariantIfNeeded(_ newInstallCompletion: (VariantManager) -> Void)
func isSupported(feature: FeatureName) -> Bool

}

final class DefaultVariantManager: VariantManager {

var currentVariant: Variant? {
let variantName = ProcessInfo.processInfo.environment["VARIANT", default: storage.variant ?? "" ]
return variants.first(where: { $0.name == variantName })
}

private let variants: [Variant]
private let storage: StatisticsStore
private let rng: VariantRNG

init(variants: [Variant] = Variant.defaultVariants,
storage: StatisticsStore = LocalStatisticsStore(),
rng: VariantRNG = Arc4RandomUniformVariantRNG()) {
Expand All @@ -88,21 +88,21 @@ final class DefaultVariantManager: VariantManager {
func isSupported(feature: FeatureName) -> Bool {
return currentVariant?.features.contains(feature) ?? false
}

func assignVariantIfNeeded(_ newInstallCompletion: (VariantManager) -> Void) {
guard !storage.hasInstallStatistics else {
os_log("ATB: No new variant needed for existing user", type: .debug)
return
}

if let variant = currentVariant {
os_log("ATB: Already assigned variant: %s", type: .debug, String(describing: variant))
return
}

guard let variant = selectVariant() else {
os_log("ATB: Failed to assign variant", type: .debug)

// it's possible this failed because there are none to assign, we should still let new install logic execute
_ = newInstallCompletion(self)
return
Expand All @@ -111,30 +111,30 @@ final class DefaultVariantManager: VariantManager {
storage.variant = variant.name
newInstallCompletion(self)
}

private func selectVariant() -> Variant? {
let totalWeight = variants.reduce(0, { $0 + $1.weight })
let randomPercent = rng.nextInt(upperBound: totalWeight)

var runningTotal = 0
for variant in variants {
runningTotal += variant.weight
if randomPercent < runningTotal {
return variant.isIncluded() ? variant : nil
}
}

return nil
}

}

final class Arc4RandomUniformVariantRNG: VariantRNG {

init() { }

func nextInt(upperBound: Int) -> Int {
return Int(arc4random_uniform(UInt32(upperBound)))
return .random(in: 0..<upperBound)
}

}