mirror of
https://github.com/ivanvorobei/SwiftUI.git
synced 2026-04-19 15:21:31 +02:00
Combine examples
This commit is contained in:
@@ -10,27 +10,51 @@ import SwiftUI
|
||||
|
||||
struct RepositoryListView : View {
|
||||
|
||||
@EnvironmentObject private var viewModel: RepositoryListViewModel
|
||||
@State private var text: String = ""
|
||||
@ObjectBinding
|
||||
private(set) var viewModel: RepositoryListViewModel
|
||||
|
||||
var body: some View {
|
||||
|
||||
NavigationView {
|
||||
|
||||
TextField($text,
|
||||
placeholder: Text("Search reposipories..."),
|
||||
onCommit: { self.viewModel.search(query: self.text) })
|
||||
VStack {
|
||||
HStack {
|
||||
|
||||
TextField($viewModel.text,
|
||||
placeholder: Text("Search reposipories..."),
|
||||
onCommit: { self.viewModel.search() })
|
||||
.frame(height: 40)
|
||||
.padding(EdgeInsets(top: 0, leading: 8, bottom: 0, trailing: 8))
|
||||
.border(Color.gray, cornerRadius: 8)
|
||||
.padding(EdgeInsets(top: 0, leading: 16, bottom: 0, trailing: 16))
|
||||
|
||||
Button(action: { self.viewModel.search() }) {
|
||||
Text("Search")
|
||||
}
|
||||
.frame(height: 40)
|
||||
.padding(EdgeInsets(top: 0, leading: 8, bottom: 0, trailing: 8))
|
||||
.border(Color.gray, cornerRadius: 8)
|
||||
.padding(EdgeInsets(top: 0, leading: 16, bottom: 0, trailing: 16))
|
||||
.border(Color.blue, cornerRadius: 8)
|
||||
.padding(EdgeInsets(top: 0, leading: 0, bottom: 0, trailing: 16))
|
||||
}
|
||||
|
||||
List {
|
||||
|
||||
viewModel.errorMessage.map(Text.init)?
|
||||
.lineLimit(nil)
|
||||
.multilineTextAlignment(.center)
|
||||
|
||||
ForEach(viewModel.repositories.identified(by: \.id)) { repository in
|
||||
RepositoryView(repository: repository)
|
||||
|
||||
NavigationLink(destination:
|
||||
WebView(url: repository.htmlUrl)
|
||||
.navigationBarTitle(Text(repository.fullName))
|
||||
) {
|
||||
|
||||
RepositoryView(repository: repository)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
.navigationBarTitle(Text("Search🔍"))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,39 +11,60 @@ import Foundation
|
||||
import SwiftUI
|
||||
|
||||
final class RepositoryListViewModel: BindableObject {
|
||||
typealias SearchRepositories = (String) -> AnyPublisher<Result<[Repository], ErrorResponse>, Never>
|
||||
|
||||
let didChange: AnyPublisher<RepositoryListViewModel, Never>
|
||||
private let _didChange = PassthroughSubject<RepositoryListViewModel, Never>()
|
||||
|
||||
private let _searchWithQuery = PassthroughSubject<String, Never>()
|
||||
private lazy var repositoriesAssign = Subscribers.Assign(object: self, keyPath: \.repositories)
|
||||
private var cancellables: [AnyCancellable] = []
|
||||
|
||||
private(set) var repositories: [Repository] = [] {
|
||||
didSet {
|
||||
// TODO: Do not want to use DispatchQueue.main here
|
||||
DispatchQueue.main.async {
|
||||
self._didChange.send(self)
|
||||
}
|
||||
_didChange.send(self)
|
||||
}
|
||||
}
|
||||
|
||||
deinit {
|
||||
repositoriesAssign.cancel()
|
||||
private(set) var errorMessage: String? {
|
||||
didSet {
|
||||
_didChange.send(self)
|
||||
}
|
||||
}
|
||||
var text: String = ""
|
||||
|
||||
init<S: Scheduler>(searchRepositories: @escaping SearchRepositories = RepositoryAPI.search,
|
||||
mainScheduler: S) {
|
||||
|
||||
init() {
|
||||
self.didChange = _didChange.eraseToAnyPublisher()
|
||||
|
||||
_searchWithQuery
|
||||
.flatMap { query -> AnyPublisher<[Repository], Never> in
|
||||
RepositoryAPI.search(query: query)
|
||||
.replaceError(with: [])
|
||||
let response = _searchWithQuery
|
||||
.filter { !$0.isEmpty }
|
||||
.debounce(for: .milliseconds(300), scheduler: mainScheduler)
|
||||
.flatMapLatest { query -> AnyPublisher<([Repository], String?), Never> in
|
||||
searchRepositories(query)
|
||||
.map { result -> ([Repository], String?) in
|
||||
switch result {
|
||||
case let .success(repositories):
|
||||
return (repositories, nil)
|
||||
case let .failure(response):
|
||||
return ([], response.message)
|
||||
}
|
||||
}
|
||||
.eraseToAnyPublisher()
|
||||
}
|
||||
.receive(subscriber: repositoriesAssign)
|
||||
.receive(on: mainScheduler)
|
||||
.share()
|
||||
|
||||
cancellables += [
|
||||
response
|
||||
.map { $0.0 }
|
||||
.assign(to: \.repositories, on: self),
|
||||
response
|
||||
.map { $0.1 }
|
||||
.assign(to: \.errorMessage, on: self)
|
||||
]
|
||||
}
|
||||
|
||||
func search(query: String) {
|
||||
_searchWithQuery.send(query)
|
||||
func search() {
|
||||
_searchWithQuery.send(text)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
//
|
||||
// WebView.swift
|
||||
// GitHubSearchWithSwiftUI
|
||||
//
|
||||
// Created by marty-suzuki on 2019/06/11.
|
||||
// Copyright © 2019 jp.marty-suzuki. All rights reserved.
|
||||
//
|
||||
|
||||
import SafariServices
|
||||
import SwiftUI
|
||||
|
||||
struct WebView: UIViewControllerRepresentable {
|
||||
|
||||
let url: URL
|
||||
|
||||
func makeUIViewController(context: UIViewControllerRepresentableContext<WebView>) -> SFSafariViewController {
|
||||
return SFSafariViewController(url: url)
|
||||
}
|
||||
|
||||
func updateUIViewController(_ uiViewController: SFSafariViewController, context: UIViewControllerRepresentableContext<WebView>) {}
|
||||
}
|
||||
Reference in New Issue
Block a user