mirror of
https://github.com/ivanvorobei/SwiftUI.git
synced 2026-03-25 02:41:16 +01:00
Add examples project
This commit is contained in:
13
Examples/GitHub Search/GitHubSearchWithSwiftUI/Model/ItemResponse.swift
Executable file
13
Examples/GitHub Search/GitHubSearchWithSwiftUI/Model/ItemResponse.swift
Executable file
@@ -0,0 +1,13 @@
|
||||
//
|
||||
// ItemResponse.swift
|
||||
// GitHubSearchWithSwiftUI
|
||||
//
|
||||
// Created by marty-suzuki on 2019/06/06.
|
||||
// Copyright © 2019 jp.marty-suzuki. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
struct ItemResponse<T: Decodable>: Decodable {
|
||||
let items: [T]
|
||||
}
|
||||
17
Examples/GitHub Search/GitHubSearchWithSwiftUI/Model/Repository.swift
Executable file
17
Examples/GitHub Search/GitHubSearchWithSwiftUI/Model/Repository.swift
Executable file
@@ -0,0 +1,17 @@
|
||||
//
|
||||
// Repository.swift
|
||||
// GitHubSearchWithSwiftUI
|
||||
//
|
||||
// Created by marty-suzuki on 2019/06/05.
|
||||
// Copyright © 2019 jp.marty-suzuki. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
struct Repository: Decodable {
|
||||
let id: Int
|
||||
let fullName: String
|
||||
let description: String?
|
||||
let stargazersCount: Int
|
||||
let htmlUrl: URL
|
||||
}
|
||||
36
Examples/GitHub Search/GitHubSearchWithSwiftUI/Model/RepositoryAPI.swift
Executable file
36
Examples/GitHub Search/GitHubSearchWithSwiftUI/Model/RepositoryAPI.swift
Executable file
@@ -0,0 +1,36 @@
|
||||
//
|
||||
// RepositoryModel.swift
|
||||
// GitHubSearchWithSwiftUI
|
||||
//
|
||||
// Created by marty-suzuki on 2019/06/06.
|
||||
// Copyright © 2019 jp.marty-suzuki. All rights reserved.
|
||||
//
|
||||
|
||||
import Combine
|
||||
import Foundation
|
||||
|
||||
enum RepositoryAPI {
|
||||
|
||||
static func search(query: String) -> AnyPublisher<[Repository], Error> {
|
||||
|
||||
guard var components = URLComponents(string: "https://api.github.com/search/repositories") else {
|
||||
return Publishers.Empty<[Repository], Error>().eraseToAnyPublisher()
|
||||
}
|
||||
components.queryItems = [URLQueryItem(name: "q", value: query)]
|
||||
|
||||
guard let url = components.url else {
|
||||
return Publishers.Empty<[Repository], Error>().eraseToAnyPublisher()
|
||||
}
|
||||
|
||||
var request = URLRequest(url: url)
|
||||
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
|
||||
let decoder = JSONDecoder()
|
||||
decoder.keyDecodingStrategy = .convertFromSnakeCase
|
||||
return URLSession.shared.combine.send(request: request)
|
||||
.decode(type: ItemResponse<Repository>.self, decoder: decoder)
|
||||
.map { $0.items }
|
||||
.handleEvents(receiveOutput: { print($0) },
|
||||
receiveCompletion: { print($0)})
|
||||
.eraseToAnyPublisher()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user