Skip to content

Instantly share code, notes, and snippets.

@LukaszDziwosz
Created August 8, 2022 16:15
Show Gist options
  • Save LukaszDziwosz/5ac65bf90f76914d97aa00251989794a to your computer and use it in GitHub Desktop.
Save LukaszDziwosz/5ac65bf90f76914d97aa00251989794a to your computer and use it in GitHub Desktop.
GitHubSearch with Alamofire
import SwiftUI
struct SearchRepoView: View {
@ObservedObject var viewModel: SearchRepoViewModel
var body: some View {
NavigationView {
VStack {
switch viewModel.state {
case .idle:
IdleView()
.onAppear {
viewModel.listenToSearch()
}
case .loading:
ProgressView()
case .failed(let error):
ErrorView(error: error)
case .loaded(let repos):
List {
// go back button
if viewModel.currentPage > 1 {
Button {
viewModel.currentPage -= 1
viewModel.fetchRepos()
} label: {
Text("Go back")
}
}
// main list
ForEach(repos) { repo in
NavigationLink(destination: WebView(url: URL(string: repo.htmlURL)!)) {
RepoListRow(repo: repo)
}
.isDetailLink(false)
}
// load more button
if !viewModel.listFull {
HStack {
Button {
viewModel.currentPage += 1
viewModel.fetchRepos()
} label: {
Text("Load more")
}
}
}
} // List
} // switch State
} // Main Vstack
.navigationTitle("Repos Search")
} // NavigationView
.searchable(text: $viewModel.searchQuery)
.onSubmit(of: .search, viewModel.fetchRepos)
}
}
struct SearchRepoView_Previews: PreviewProvider {
static var previews: some View {
SearchRepoViewDI().searchRepoView
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment