Skip to content

Commit

Permalink
👌 Improve readability in target element trait logic
Browse files Browse the repository at this point in the history
  • Loading branch information
iujames authored and mmaatttt committed Apr 11, 2023
1 parent a9ecf7c commit 183107b
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ internal class UIKitElementSelector: AppcuesElementSelector {

super.init()

// note: accessibilityLabel is inhertied from base type NSObject and used here
// note: accessibilityLabel is inherited from base type NSObject and used here
self.accessibilityLabel = accessibilityLabel

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public protocol AppcuesElementTargeting {
internal extension AppcuesElementTargeting {
// Default implementation that looks at the current view capture and finds any matches.
// Used during targeted element experiences to find the position in the UI for the experience.
func findMatches(for selector: AppcuesElementSelector) -> [(AppcuesViewElement, Int)]? {
func findMatches(for selector: AppcuesElementSelector) -> [(view: AppcuesViewElement, weight: Int)]? {
return captureLayout()?.viewsMatchingSelector(selector)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ internal extension AppcuesViewElement {
// Used internally by the SDK to find all views, recursively, that have at least a
// partial match with the given selector. Used to find the position of an element
// targeted experience.
func viewsMatchingSelector(_ target: AppcuesElementSelector) -> [(AppcuesViewElement, Int)] {
func viewsMatchingSelector(_ target: AppcuesElementSelector) -> [(view: AppcuesViewElement, weight: Int)] {
var views: [(AppcuesViewElement, Int)] = []

if let current = selector {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ internal class AppcuesTargetElementTrait: BackdropDecoratingTrait {

// if only a single match of anything, use it
if weightedViews.count == 1 {
return weightedViews[0].0
return weightedViews[0].view
}

// iterating through the array, storing the highest weight value and the list of views
Expand All @@ -104,8 +104,8 @@ internal class AppcuesTargetElementTrait: BackdropDecoratingTrait {
var maxWeightViews: [AppcuesViewElement] = []

weightedViews.forEach {
let view = $0.0
let weight = $0.1
let view = $0.view
let weight = $0.weight
if weight > maxWeight {
// new max weight, reset list
maxWeight = weight
Expand All @@ -116,12 +116,12 @@ internal class AppcuesTargetElementTrait: BackdropDecoratingTrait {
}
}

// if this has produced a single most distinct result, use it
if maxWeightViews.count == 1 {
return maxWeightViews[0]
guard maxWeightViews.count == 1 else {
// this selector was not able to find a distinct match in this view
throw TraitError(description: "multiple non-distinct views (\(weightedViews.count)) matched selector \(config.selector)")
}

// otherwise, this selector was not able to find a distinct match in this view
throw TraitError(description: "multiple non-distinct views (\(weightedViews.count)) matched selector \(config.selector)")
// if this has produced a single most distinct result, use it
return maxWeightViews[0]
}
}

0 comments on commit 183107b

Please sign in to comment.