mirror of
https://github.com/ivanvorobei/SwiftUI.git
synced 2026-03-29 21:52:10 +02:00
Add examples project
This commit is contained in:
37
Examples/GitHub Search/GitHubSearchWithSwiftUI/View/RepositoryListView.swift
Executable file
37
Examples/GitHub Search/GitHubSearchWithSwiftUI/View/RepositoryListView.swift
Executable file
@@ -0,0 +1,37 @@
|
||||
//
|
||||
// RepositoryListView.swift
|
||||
// GitHubSearchWithSwiftUI
|
||||
//
|
||||
// Created by marty-suzuki on 2019/06/05.
|
||||
// Copyright © 2019 jp.marty-suzuki. All rights reserved.
|
||||
//
|
||||
|
||||
import SwiftUI
|
||||
|
||||
struct RepositoryListView : View {
|
||||
|
||||
@EnvironmentObject private var viewModel: RepositoryListViewModel
|
||||
@State private var text: String = ""
|
||||
|
||||
var body: some View {
|
||||
|
||||
NavigationView {
|
||||
|
||||
TextField($text,
|
||||
placeholder: Text("Search reposipories..."),
|
||||
onCommit: { self.viewModel.search(query: self.text) })
|
||||
.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))
|
||||
|
||||
List {
|
||||
|
||||
ForEach(viewModel.repositories.identified(by: \.id)) { repository in
|
||||
RepositoryView(repository: repository)
|
||||
}
|
||||
}
|
||||
.navigationBarTitle(Text("Search🔍"))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
//
|
||||
// RepositoryListViewModel.swift
|
||||
// GitHubSearchWithSwiftUI
|
||||
//
|
||||
// Created by marty-suzuki on 2019/06/06.
|
||||
// Copyright © 2019 jp.marty-suzuki. All rights reserved.
|
||||
//
|
||||
|
||||
import Combine
|
||||
import Foundation
|
||||
import SwiftUI
|
||||
|
||||
final class RepositoryListViewModel: BindableObject {
|
||||
|
||||
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(set) var repositories: [Repository] = [] {
|
||||
didSet {
|
||||
// TODO: Do not want to use DispatchQueue.main here
|
||||
DispatchQueue.main.async {
|
||||
self._didChange.send(self)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
deinit {
|
||||
repositoriesAssign.cancel()
|
||||
}
|
||||
|
||||
init() {
|
||||
self.didChange = _didChange.eraseToAnyPublisher()
|
||||
|
||||
_searchWithQuery
|
||||
.flatMap { query -> AnyPublisher<[Repository], Never> in
|
||||
RepositoryAPI.search(query: query)
|
||||
.replaceError(with: [])
|
||||
.eraseToAnyPublisher()
|
||||
}
|
||||
.receive(subscriber: repositoriesAssign)
|
||||
}
|
||||
|
||||
func search(query: String) {
|
||||
_searchWithQuery.send(query)
|
||||
}
|
||||
}
|
||||
36
Examples/GitHub Search/GitHubSearchWithSwiftUI/View/RepositoryView.swift
Executable file
36
Examples/GitHub Search/GitHubSearchWithSwiftUI/View/RepositoryView.swift
Executable file
@@ -0,0 +1,36 @@
|
||||
//
|
||||
// RepositoryView.swift
|
||||
// GitHubSearchWithSwiftUI
|
||||
//
|
||||
// Created by marty-suzuki on 2019/06/05.
|
||||
// Copyright © 2019 jp.marty-suzuki. All rights reserved.
|
||||
//
|
||||
|
||||
import SwiftUI
|
||||
|
||||
struct RepositoryView : View {
|
||||
|
||||
let repository: Repository
|
||||
|
||||
var body: some View {
|
||||
|
||||
VStack(alignment: .leading) {
|
||||
|
||||
HStack {
|
||||
Image(systemName: "doc.text")
|
||||
Text(repository.fullName)
|
||||
.bold()
|
||||
}
|
||||
|
||||
// Show text if description exists
|
||||
repository.description
|
||||
.map(Text.init)?
|
||||
.lineLimit(nil)
|
||||
|
||||
HStack {
|
||||
Image(systemName: "star")
|
||||
Text("\(repository.stargazersCount)")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user