Skip to content

Commit 8cdd9bb

Browse files
committed
add focusing, move option parsing logic to helper function.
1 parent a0dc624 commit 8cdd9bb

File tree

1 file changed

+34
-17
lines changed

1 file changed

+34
-17
lines changed

mobile/examples/model_tester/ios/ModelTester/ModelTester/ContentView.swift

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,42 @@ enum ExecutionProviderType: String, CaseIterable, Identifiable {
1212
}
1313

1414
struct ContentView: View {
15+
enum Field: Hashable {
16+
case numIterations
17+
case executionProviderType
18+
case executionProviderOptionsText
19+
}
20+
1521
@State private var runResultMessage: String = ""
1622
@State private var isRunning: Bool = false
1723
@State private var numIterations: UInt = 10
1824
@State private var executionProviderType: ExecutionProviderType = .cpu
1925
@State private var executionProviderOptionsText: String = ""
26+
@FocusState private var focusedField: Field?
27+
28+
private func parseExecutionProviderOptionsText() throws -> [String: String] {
29+
let executionProviderOptions =
30+
try executionProviderOptionsText
31+
.components(separatedBy: "\n")
32+
.reduce(
33+
into: [String: String](),
34+
{ options, line in
35+
guard !line.isEmpty else {
36+
return
37+
}
38+
let nameAndValue = line.split(separator: ":", maxSplits: 2)
39+
guard nameAndValue.count == 2 else {
40+
throw ModelTesterError.runtimeError(msg: "Failed to parse provider option: '\(line)'")
41+
}
42+
options[String(nameAndValue[0])] = String(nameAndValue[1])
43+
})
44+
45+
return executionProviderOptions
46+
}
2047

21-
private func Run() {
48+
private func run() {
2249
isRunning = true
50+
focusedField = nil
2351

2452
DispatchQueue.global().async {
2553
var output: String
@@ -33,23 +61,10 @@ struct ContentView: View {
3361
config.setNumIterations(numIterations)
3462

3563
if executionProviderType != .cpu {
36-
let executionProviderOptions =
37-
try executionProviderOptionsText
38-
.components(separatedBy: "\n")
39-
.reduce(
40-
into: [String: String](),
41-
{ options, line in
42-
let nameAndValue = line.split(separator: ":", maxSplits: 2)
43-
guard nameAndValue.count == 2 else {
44-
throw ModelTesterError.runtimeError(msg: "Failed to parse provider option: '\(line)'")
45-
}
46-
47-
options[String(nameAndValue[0])] = String(nameAndValue[1])
48-
})
64+
let executionProviderOptions = try parseExecutionProviderOptionsText()
4965

5066
print("Execution provider type: \(executionProviderType)")
5167
print("Execution provider options: \(executionProviderOptions)")
52-
5368
config.setExecutionProvider(executionProviderType.rawValue, options: executionProviderOptions)
5469
}
5570

@@ -73,21 +88,23 @@ struct ContentView: View {
7388
"", value: $numIterations,
7489
format: IntegerFormatStyle<UInt>.number
7590
).keyboardType(.numberPad)
91+
.focused($focusedField, equals: .numIterations)
7692

7793
Picker("Execution provider type", selection: $executionProviderType) {
7894
ForEach(ExecutionProviderType.allCases) { epType in
7995
Text(epType.rawValue).tag(epType)
8096
}
81-
}
97+
}.focused($focusedField, equals: .executionProviderType)
8298

8399
if executionProviderType != .cpu {
84100
Text("Execution provider options")
85101
Text("The expected format is 'name:value', one per line")
86102
.font(.caption)
87103
TextEditor(text: $executionProviderOptionsText)
104+
.focused($focusedField, equals: .executionProviderOptionsText)
88105
}
89106

90-
Button(action: Run) { Text("Run") }
107+
Button(action: run) { Text("Run") }
91108
.disabled(isRunning)
92109

93110
Text(runResultMessage)

0 commit comments

Comments
 (0)