-
Notifications
You must be signed in to change notification settings - Fork 133
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1954 from DataDog/ganeshnj/chore/dist-trace-manua…
…l-testing
- Loading branch information
Showing
4 changed files
with
162 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0. | ||
* This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
* Copyright 2019-Present Datadog, Inc. | ||
*/ | ||
|
||
import SwiftUI | ||
|
||
@available(iOS 14.0, *) | ||
struct MultiSelector<LabelView: View, Selectable: Identifiable & Hashable>: View { | ||
let label: LabelView | ||
let options: [Selectable] | ||
let optionToString: (Selectable) -> String | ||
|
||
var selected: Binding<Set<Selectable>> | ||
|
||
private var formattedSelectedListString: String { | ||
ListFormatter.localizedString( | ||
byJoining: selected.wrappedValue.map { | ||
optionToString($0) | ||
} | ||
) | ||
} | ||
|
||
var body: some View { | ||
NavigationLink(destination: multiSelectionView()) { | ||
HStack { | ||
label | ||
Spacer() | ||
Text(formattedSelectedListString) | ||
.foregroundColor(.gray) | ||
.multilineTextAlignment(.trailing) | ||
} | ||
} | ||
} | ||
|
||
private func multiSelectionView() -> some View { | ||
MultiSelectionView( | ||
options: options, | ||
optionToString: optionToString, | ||
selected: selected | ||
) | ||
} | ||
} | ||
|
||
|
||
@available(iOS 13.0, *) | ||
struct MultiSelectionView<Selectable: Identifiable & Hashable>: View { | ||
let options: [Selectable] | ||
let optionToString: (Selectable) -> String | ||
|
||
@Binding var selected: Set<Selectable> | ||
|
||
var body: some View { | ||
List { | ||
ForEach(options) { selectable in | ||
Button(action: { | ||
toggleSelection(selectable: selectable) | ||
}) { | ||
HStack { | ||
Text(optionToString(selectable)) | ||
.foregroundColor(.black) | ||
Spacer() | ||
if selected.contains(where: { | ||
$0.id == selectable.id | ||
}) { | ||
Image(systemName: "checkmark") | ||
.foregroundColor(.accentColor) | ||
} | ||
} | ||
} | ||
.tag(selectable.id) | ||
} | ||
} | ||
.listStyle(GroupedListStyle()) | ||
} | ||
|
||
private func toggleSelection(selectable: Selectable) { | ||
if let existingIndex = selected.firstIndex(where: { $0.id == selectable.id }) { | ||
selected.remove(at: existingIndex) | ||
} else { | ||
selected.insert(selectable) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters