Add examples project

This commit is contained in:
Ivan Vorobei
2019-06-06 11:16:28 +03:00
parent 86fe2d7323
commit b866885a44
962 changed files with 45026 additions and 5 deletions

View File

@@ -0,0 +1,15 @@
import Combine
final class AnySubscription: Subscription {
private let cancellable: Cancellable
init(_ cancel: @escaping () -> Void) {
cancellable = AnyCancellable(cancel)
}
func request(_ demand: Subscribers.Demand) {}
func cancel() {
cancellable.cancel()
}
}

View File

@@ -0,0 +1,4 @@
import UIKit
@UIApplicationMain
final class AppDelegate: UIResponder, UIApplicationDelegate {}

View File

@@ -0,0 +1,98 @@
{
"images" : [
{
"idiom" : "iphone",
"size" : "20x20",
"scale" : "2x"
},
{
"idiom" : "iphone",
"size" : "20x20",
"scale" : "3x"
},
{
"idiom" : "iphone",
"size" : "29x29",
"scale" : "2x"
},
{
"idiom" : "iphone",
"size" : "29x29",
"scale" : "3x"
},
{
"idiom" : "iphone",
"size" : "40x40",
"scale" : "2x"
},
{
"idiom" : "iphone",
"size" : "40x40",
"scale" : "3x"
},
{
"idiom" : "iphone",
"size" : "60x60",
"scale" : "2x"
},
{
"idiom" : "iphone",
"size" : "60x60",
"scale" : "3x"
},
{
"idiom" : "ipad",
"size" : "20x20",
"scale" : "1x"
},
{
"idiom" : "ipad",
"size" : "20x20",
"scale" : "2x"
},
{
"idiom" : "ipad",
"size" : "29x29",
"scale" : "1x"
},
{
"idiom" : "ipad",
"size" : "29x29",
"scale" : "2x"
},
{
"idiom" : "ipad",
"size" : "40x40",
"scale" : "1x"
},
{
"idiom" : "ipad",
"size" : "40x40",
"scale" : "2x"
},
{
"idiom" : "ipad",
"size" : "76x76",
"scale" : "1x"
},
{
"idiom" : "ipad",
"size" : "76x76",
"scale" : "2x"
},
{
"idiom" : "ipad",
"size" : "83.5x83.5",
"scale" : "2x"
},
{
"idiom" : "ios-marketing",
"size" : "1024x1024",
"scale" : "1x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}

View File

@@ -0,0 +1,6 @@
{
"info" : {
"version" : 1,
"author" : "xcode"
}
}

View File

@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="13122.16" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" launchScreen="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="01J-lp-oVM">
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="13104.12"/>
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<scenes>
<!--View Controller-->
<scene sceneID="EHf-IW-A2E">
<objects>
<viewController id="01J-lp-oVM" sceneMemberID="viewController">
<view key="view" contentMode="scaleToFill" id="Ze5-6b-2t3">
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<color key="backgroundColor" xcode11CocoaTouchSystemColor="systemBackgroundColor" cocoaTouchSystemColor="whiteColor"/>
<viewLayoutGuide key="safeArea" id="6Tk-OE-BBY"/>
</view>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="iYj-Kq-Ea1" userLabel="First Responder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="53" y="375"/>
</scene>
</scenes>
</document>

View File

@@ -0,0 +1,34 @@
import SwiftUI
import Combine
enum RequestError: Error {
case request(code: Int, error: Error?)
case unknown
}
extension URLSession {
func send(request: URLRequest) -> AnyPublisher<(data: Data, response: HTTPURLResponse), RequestError> {
AnyPublisher<(data: Data, response: HTTPURLResponse), RequestError> { subscriber in
let task = self.dataTask(with: request) { data, response, error in
DispatchQueue.main.async {
let httpReponse = response as? HTTPURLResponse
if let data = data, let httpReponse = httpReponse, 200..<300 ~= httpReponse.statusCode {
_ = subscriber.receive((data, httpReponse))
subscriber.receive(completion: .finished)
}
else if let httpReponse = httpReponse {
subscriber.receive(completion: .failure(.request(code: httpReponse.statusCode, error: error)))
}
else {
subscriber.receive(completion: .failure(.unknown))
}
}
}
subscriber.receive(subscription: AnySubscription(task.cancel))
task.resume()
}
}
}
extension JSONDecoder: TopLevelDecoder {}

View File

@@ -0,0 +1,60 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>$(DEVELOPMENT_LANGUAGE)</string>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>$(PRODUCT_NAME)</string>
<key>CFBundlePackageType</key>
<string>$(PRODUCT_BUNDLE_PACKAGE_TYPE)</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleVersion</key>
<string>1</string>
<key>LSRequiresIPhoneOS</key>
<true/>
<key>UIApplicationSceneManifest</key>
<dict>
<key>UIApplicationSupportsMultipleScenes</key>
<false/>
<key>UISceneConfigurations</key>
<dict>
<key>UIWindowSceneSessionRoleApplication</key>
<array>
<dict>
<key>UILaunchStoryboardName</key>
<string>LaunchScreen</string>
<key>UISceneConfigurationName</key>
<string>Default Configuration</string>
<key>UISceneDelegateClassName</key>
<string>$(PRODUCT_MODULE_NAME).SceneDelegate</string>
</dict>
</array>
</dict>
</dict>
<key>UILaunchStoryboardName</key>
<string>LaunchScreen</string>
<key>UIRequiredDeviceCapabilities</key>
<array>
<string>armv7</string>
</array>
<key>UISupportedInterfaceOrientations</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
</array>
<key>UISupportedInterfaceOrientations~ipad</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationPortraitUpsideDown</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
</dict>
</plist>

View File

@@ -0,0 +1,6 @@
{
"info" : {
"version" : 1,
"author" : "xcode"
}
}

View File

@@ -0,0 +1,15 @@
import UIKit
import SwiftUI
final class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
let window = UIWindow(frame: UIScreen.main.bounds)
window.rootViewController = UIHostingController(
rootView: SearchUserView().environmentObject(SearchUserViewModel())
)
self.window = window
window.makeKeyAndVisible()
}
}

View File

@@ -0,0 +1,31 @@
import SwiftUI
struct SearchUserBar: View {
@Binding var text: String
@State var action: () -> Void
var body: some View {
ZStack {
HStack {
TextField(
$text,
placeholder: Text("Search User")
.color(Color.gray)
)
.padding([.leading, .trailing], 8)
.frame(height: 32)
.background(Color.white.opacity(0.4))
.cornerRadius(8)
Button(
action: action,
label: { Text("Search") }
)
.foregroundColor(Color.white)
}
.padding([.leading, .trailing], 16)
}
.frame(height: 64)
.background(Color.yellow)
}
}

View File

@@ -0,0 +1,3 @@
struct SearchUserResponse: Decodable {
var items: [User]
}

View File

@@ -0,0 +1,26 @@
import SwiftUI
struct SearchUserRow: View {
@EnvironmentObject var viewModel: SearchUserViewModel
@State var user: User
var body: some View {
HStack {
self.viewModel.userImages[user].map { image in
Image(uiImage: image)
.frame(width: 44, height: 44)
.aspectRatio(contentMode: .fit)
.clipShape(Circle())
.overlay(Circle().stroke(Color.gray, lineWidth: 1))
}
Text(user.login)
.font(Font.system(size: 18).bold())
Spacer()
}
.padding(EdgeInsets(top: 8, leading: 16, bottom: 8, trailing: 16))
.frame(height: 60)
.onAppear { self.viewModel.getImage(for: self.user) }
}
}

View File

@@ -0,0 +1,22 @@
import SwiftUI
struct SearchUserView: View {
@EnvironmentObject var viewModel: SearchUserViewModel
@State var text = "ra1028"
var body: some View {
NavigationView {
VStack {
SearchUserBar(text: $text) {
self.viewModel.search(name: self.text)
}
List(viewModel.users) { user in
SearchUserRow(user: user)
.tapAction { print(user) }
}
}
.navigationBarTitle(Text("Users"))
}
}
}

View File

@@ -0,0 +1,60 @@
import SwiftUI
import Combine
final class SearchUserViewModel: BindableObject {
var didChange = PassthroughSubject<SearchUserViewModel, Never>()
private(set) var users = [User]() {
didSet {
didChange.send(self)
}
}
private(set) var userImages = [User: UIImage]() {
didSet {
didChange.send(self)
}
}
private var cancellable: Cancellable? {
didSet { oldValue?.cancel() }
}
func search(name: String) {
guard !name.isEmpty else {
return users = []
}
var urlComponents = URLComponents(string: "https://api.github.com/search/users")!
urlComponents.queryItems = [
URLQueryItem(name: "q", value: name)
]
var request = URLRequest(url: urlComponents.url!)
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
let assign = Subscribers.Assign(object: self, keyPath: \.users)
cancellable = assign
URLSession.shared.send(request: request)
.map { $0.data }
.decode(type: SearchUserResponse.self, decoder: JSONDecoder())
.map { $0.items }
.replaceError(with: [])
.receive(subscriber: assign)
}
func getImage(for user: User) {
guard case .none = userImages[user] else {
return
}
let request = URLRequest(url: user.avatar_url)
URLSession.shared.send(request: request)
.map { UIImage(data: $0.data) }
.replaceError(with: nil)
.eraseToAnyPublisher()
.receive(subscriber: Subscribers.Sink<AnyPublisher<UIImage?, Never>> { [weak self] image in
self?.userImages[user] = image
})
}
}

View File

@@ -0,0 +1,8 @@
import Foundation
import SwiftUI
struct User: Hashable, Identifiable, Decodable {
var id: Int64
var login: String
var avatar_url: URL
}