Created
August 8, 2022 16:15
-
-
Save LukaszDziwosz/5ac65bf90f76914d97aa00251989794a to your computer and use it in GitHub Desktop.
GitHubSearch with Alamofire
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
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