Last active
October 18, 2020 05:56
-
-
Save afterxleep/71ac7a0072cd615db3716316bb9d0024 to your computer and use it in GitHub Desktop.
[danielbernal.co] Protocols and Combine: Using @published in your Protocol declaration - https://danielbernal.co/combine-and-protocols-in-swift/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class AnimalGenerator: Generator, ObservableObject { | |
@Published private(set) var name: String = "" | |
var namePublished: Published<String> { _name } | |
var namePublisher: Published<String>.Publisher { $name } | |
let animals = ["Cat", "Dog", "Crow", "Horse", "Iguana", "Cow", "Racoon"] | |
init() { | |
generate() | |
} | |
func generate() { | |
self.name = animals.randomElement() ?? "" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class AnimalGenerator: Generator, ObservableObject { | |
@Published private(set) var name: String = "" | |
let animals = ["Cat", "Dog", "Crow", "Horse", "Iguana", "Cow", "Racoon"] | |
init() { | |
generate() | |
} | |
func generate() { | |
self.name = animals.randomElement() ?? "" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
protocol Generator { | |
// Wrapped value | |
var name: String { get } | |
// (Published property wrapper) | |
var namePublished: Published<String> { get } | |
// Publisher | |
var namePublisher: Published<String>.Publisher { get } | |
func generate() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
protocol Generator { | |
@Published var name: String { get } | |
func generate() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import PlaygroundSupport | |
import SwiftUI | |
import Combine | |
class AnimalGenerator: ObservableObject { | |
@Published private(set) var name: String = "" | |
let animals = ["Cat", "Dog", "Crow", "Horse", "Iguana", "Cow", "Racoon"] | |
init() { | |
generate() | |
} | |
func generate() { | |
self.name = animals.randomElement() ?? "" | |
} | |
} | |
class TheViewModel: ObservableObject { | |
@Published var displayData: String = "No data" | |
var generator: AnimalGenerator = AnimalGenerator() | |
var cancellable: AnyCancellable? | |
init() { | |
cancellable = | |
generator | |
.$name | |
.receive(on: RunLoop.main) | |
.sink { [weak self] data in | |
self?.displayData = data | |
} | |
} | |
func generate() { | |
generator.generate() | |
} | |
} | |
struct TheView: View { | |
@ObservedObject var viewModel: TheViewModel = TheViewModel() | |
var body: some View { | |
VStack { | |
Text(viewModel.displayData).font(.system(.largeTitle)).padding() | |
Button("Tap me", action: { self.viewModel.generate() }) | |
} | |
} | |
} | |
PlaygroundPage.current.setLiveView(TheView()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PersonGenerator: Generator, ObservableObject { | |
var namePublished: Published<String> { _name } | |
var namePublisher: Published<String>.Publisher { $name } | |
@Published private(set) var name: String = "" | |
let persons = ["John", "Jane", "Carlos", "Daniel", "Helen", "David", "Bill"] | |
init() { | |
generate() | |
} | |
func generate() { | |
self.name = persons.randomElement() ?? "" | |
} | |
} | |
let viewModel = TheViewModel(generator: PersonGenerator()) | |
PlaygroundPage.current.setLiveView( | |
TheView(viewModel: viewModel) | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct TheView: View { | |
@ObservedObject var viewModel: TheViewModel | |
var body: some View { | |
VStack { | |
Text(viewModel.textToShow).font(.system(.largeTitle)).padding() | |
Button("Tap me", action: { self.viewModel.generate() }) | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct TheView: View { | |
@ObservedObject var viewModel: TheViewModel = TheViewModel(generator: AnimalGenerator()) | |
var body: some View { | |
VStack { | |
Text(viewModel.textToShow).font(.system(.largeTitle)).padding() | |
Button("Tap me", action: { self.viewModel.generate() }) | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class TheViewModel: ObservableObject { | |
@Published var textToShow: String = "No data" | |
var generator: Generator | |
var cancellable: AnyCancellable? | |
init(generator: Generator) { | |
self.generator = generator | |
cancellable = | |
generator | |
.namePublisher | |
.receive(on: RunLoop.main) | |
.sink { [weak self] data in | |
self?.textToShow = data | |
} | |
} | |
func generate() { | |
generator.generate() | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let viewModel: TheViewModel = TheViewModel(generator: AnimalGenerator()) | |
PlaygroundPage.current.setLiveView( | |
TheView(viewModel: viewModel) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment