Skip to content

Instantly share code, notes, and snippets.

@bannzai
Last active December 20, 2022 12:22
Show Gist options
  • Save bannzai/2b525de11add7e19299e5aa1e67aae64 to your computer and use it in GitHub Desktop.
Save bannzai/2b525de11add7e19299e5aa1e67aae64 to your computer and use it in GitHub Desktop.
struct SearchView: View {
@State var text: String
@FocusState var focused: Bool
@Async<[Item]> var async
var body: some View {
VStack {
TextField("Placeholder", text: $text)
.focused($focused)
.onSubmit {
async(fetch)
}
if text.isEmpty {
Text("Do Input")
} else if focused {
Text("Do submit")
} else {
// 違うViewに切り出しても良いかも
switch async.state {
case .success(let items):
if items.isEmpty {
Text("No Result")
} else {
List {
ForEach(items) { item in
SearchedView(item)
}
}
}
case .failure(let error):
ErrorPage(error: error)
case .loading:
ProgressView()
}
}
}
}
func fetch() async throws -> [Item] {
try await apiClient.call("search", args: ["query": text])
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment