Skip to content

Commit

Permalink
Merge pull request #3 from ralfebert/master
Browse files Browse the repository at this point in the history
example updated for Beta 7
  • Loading branch information
ra1028 authored Sep 7, 2019
2 parents 4734d8c + eeb4036 commit 7a20076
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 85 deletions.
8 changes: 0 additions & 8 deletions SwiftUI-Combine-Example.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
6B194F1722A710E6001B3151 /* SearchUserResponse.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6B194F1622A710E6001B3151 /* SearchUserResponse.swift */; };
6B194F1922A710FF001B3151 /* SearchUserViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6B194F1822A710FF001B3151 /* SearchUserViewModel.swift */; };
6B194F1B22A7112D001B3151 /* SearchUserBar.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6B194F1A22A7112D001B3151 /* SearchUserBar.swift */; };
6B194F1D22A71154001B3151 /* FoundationExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6B194F1C22A71154001B3151 /* FoundationExtensions.swift */; };
6B194F1F22A71B0F001B3151 /* AnySubscription.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6B194F1E22A71B0F001B3151 /* AnySubscription.swift */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
Expand All @@ -36,8 +34,6 @@
6B194F1622A710E6001B3151 /* SearchUserResponse.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SearchUserResponse.swift; sourceTree = "<group>"; };
6B194F1822A710FF001B3151 /* SearchUserViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SearchUserViewModel.swift; sourceTree = "<group>"; };
6B194F1A22A7112D001B3151 /* SearchUserBar.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SearchUserBar.swift; sourceTree = "<group>"; };
6B194F1C22A71154001B3151 /* FoundationExtensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FoundationExtensions.swift; sourceTree = "<group>"; };
6B194F1E22A71B0F001B3151 /* AnySubscription.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AnySubscription.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -76,8 +72,6 @@
6B194F1222A7108C001B3151 /* SearchUserRow.swift */,
6B194F1622A710E6001B3151 /* SearchUserResponse.swift */,
6B194F1422A710AB001B3151 /* User.swift */,
6B194F1E22A71B0F001B3151 /* AnySubscription.swift */,
6B194F1C22A71154001B3151 /* FoundationExtensions.swift */,
6B194F0022A6BA07001B3151 /* SceneDelegate.swift */,
6B194EFE22A6BA07001B3151 /* AppDelegate.swift */,
6B194F0422A6BA08001B3151 /* Assets.xcassets */,
Expand Down Expand Up @@ -170,11 +164,9 @@
6B194F1B22A7112D001B3151 /* SearchUserBar.swift in Sources */,
6B194EFF22A6BA07001B3151 /* AppDelegate.swift in Sources */,
6B194F0122A6BA07001B3151 /* SceneDelegate.swift in Sources */,
6B194F1F22A71B0F001B3151 /* AnySubscription.swift in Sources */,
6B194F0322A6BA07001B3151 /* SearchUserView.swift in Sources */,
6B194F1922A710FF001B3151 /* SearchUserViewModel.swift in Sources */,
6B194F1722A710E6001B3151 /* SearchUserResponse.swift in Sources */,
6B194F1D22A71154001B3151 /* FoundationExtensions.swift in Sources */,
6B194F1522A710AB001B3151 /* User.swift in Sources */,
6B194F1322A7108C001B3151 /* SearchUserRow.swift in Sources */,
);
Expand Down
15 changes: 0 additions & 15 deletions SwiftUI-Combine-Example/AnySubscription.swift

This file was deleted.

34 changes: 0 additions & 34 deletions SwiftUI-Combine-Example/FoundationExtensions.swift

This file was deleted.

12 changes: 6 additions & 6 deletions SwiftUI-Combine-Example/SceneDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ 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()
)
self.window = window
window.makeKeyAndVisible()
if let windowScene = scene as? UIWindowScene {
let window = UIWindow(windowScene: windowScene)
window.rootViewController = UIHostingController(rootView: SearchUserView())
self.window = window
window.makeKeyAndVisible()
}
}
}
6 changes: 1 addition & 5 deletions SwiftUI-Combine-Example/SearchUserBar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,7 @@ struct SearchUserBar: View {
ZStack {
Color.yellow
HStack {
TextField(
$text,
placeholder: Text("Search User")
.color(Color.gray)
)
TextField("Search User", text: $text)
.padding([.leading, .trailing], 8)
.frame(height: 32)
.background(Color.white.opacity(0.4))
Expand Down
2 changes: 1 addition & 1 deletion SwiftUI-Combine-Example/SearchUserRow.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import SwiftUI

struct SearchUserRow: View {
@ObjectBinding var viewModel: SearchUserViewModel
@ObservedObject var viewModel: SearchUserViewModel
@State var user: User

var body: some View {
Expand Down
4 changes: 2 additions & 2 deletions SwiftUI-Combine-Example/SearchUserView.swift
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import SwiftUI

struct SearchUserView: View {
@ObjectBinding var viewModel = SearchUserViewModel()
@ObservedObject var viewModel = SearchUserViewModel()

var body: some View {
NavigationView {
VStack {
SearchUserBar(text: viewModel[\.name]) {
SearchUserBar(text: $viewModel.name) {
self.viewModel.search()
}

Expand Down
24 changes: 10 additions & 14 deletions SwiftUI-Combine-Example/SearchUserViewModel.swift
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
import SwiftUI
import Combine

final class SearchUserViewModel: BindableObject {
var didChange = PassthroughSubject<SearchUserViewModel, Never>()
final class SearchUserViewModel: ObservableObject {

var name = "ra1028" {
didSet { didChange.send(self) }
}
@Published var name = "ra1028"

private(set) var users = [User]() {
didSet { didChange.send(self) }
}
@Published private(set) var users = [User]()

private(set) var userImages = [User: UIImage]() {
didSet { didChange.send(self) }
}
@Published private(set) var userImages = [User: UIImage]()

private var searchCancellable: Cancellable? {
didSet { oldValue?.cancel() }
Expand All @@ -37,10 +30,12 @@ final class SearchUserViewModel: BindableObject {
var request = URLRequest(url: urlComponents.url!)
request.addValue("application/json", forHTTPHeaderField: "Content-Type")

searchCancellable = URLSession.shared.send(request: request)
searchCancellable = URLSession.shared.dataTaskPublisher(for: request)
.map { $0.data }
.decode(type: SearchUserResponse.self, decoder: JSONDecoder())
.map { $0.items }
.replaceError(with: [])
.receive(on: RunLoop.main)
.assign(to: \.users, on: self)
}

Expand All @@ -50,9 +45,10 @@ final class SearchUserViewModel: BindableObject {
}

let request = URLRequest(url: user.avatar_url)
_ = URLSession.shared.send(request: request)
.map { UIImage(data: $0) }
_ = URLSession.shared.dataTaskPublisher(for: request)
.map { UIImage(data: $0.data) }
.replaceError(with: nil)
.receive(on: RunLoop.main)
.sink(receiveValue: { [weak self] image in
self?.userImages[user] = image
})
Expand Down

0 comments on commit 7a20076

Please sign in to comment.