Skip to content

Instantly share code, notes, and snippets.

@NunciosChums
Last active March 30, 2022 06:30
Show Gist options
  • Save NunciosChums/f027aade018de3c1e3b2610aca500ec1 to your computer and use it in GitHub Desktop.
Save NunciosChums/f027aade018de3c1e3b2610aca500ec1 to your computer and use it in GitHub Desktop.
[SwiftUI] Subscribe PassthroughSubject in UIViewRepresentable
import Combine
import MapKit
import SwiftUI
struct AppleMapView_Previews: PreviewProvider {
static var previews: some View {
AppleMapView()
}
}
struct AppleMapView: UIViewRepresentable {
@EnvironmentObject var mapViewEnvironment: MapViewEnvironment
func makeUIView(context: Context) -> MKMapView {
let view = MKMapView()
view.mapType = .standard
willAppear(context)
return view
}
func updateUIView(_ uiView: MKMapView, context: UIViewRepresentableContext<AppleMapView>) {}
static func dismantleUIView(_ uiView: MKMapView, coordinator: AppleMapView.Coordinator) {
print("apple dismantleUIView")
coordinator.cancellable.removeAll()
}
func makeCoordinator() -> AppleMapView.Coordinator {
return Coordinator()
}
final class Coordinator {
var cancellable = Set<AnyCancellable>()
}
}
extension AppleMapView: MapViewProtocol {
func willAppear(_ context: Context) {
mapViewEnvironment.$value1
.filter { $0 == "aaa" }
.sink {
print("=============== apple 1111 \($0)")
self.aaa()
}.store(in: &context.coordinator.cancellable)
mapViewEnvironment.$value1
.filter { $0 == "bbb" }
.sink {
print("=============== apple 1111 \($0)")
self.bbb()
}.store(in: &context.coordinator.cancellable)
mapViewEnvironment.value2
.filter { $0 == "aaa" }
.sink {
print("--------------- apple 2222 \($0)")
self.aaa()
}.store(in: &context.coordinator.cancellable)
mapViewEnvironment.value2
.filter { $0 == "bbb" }
.sink {
print("--------------- apple 2222 \($0)")
self.bbb()
}.store(in: &context.coordinator.cancellable)
}
func aaa() {
print("AppleMapView - aaa")
}
func bbb() {
print("AppleMapView - bbb")
}
}
import Combine
import GoogleMaps
import SwiftUI
struct GoogleMapView_Previews: PreviewProvider {
static var previews: some View {
GoogleMapView()
}
}
struct GoogleMapView: UIViewRepresentable {
@EnvironmentObject var mapViewEnvironment: MapViewEnvironment
func makeUIView(context: Context) -> GMSMapView {
let view = GMSMapView()
view.mapType = .hybrid
willAppear(context)
return view
}
func updateUIView(_ uiView: GMSMapView, context: UIViewRepresentableContext<GoogleMapView>) {}
static func dismantleUIView(_ uiView: GMSMapView, coordinator: GoogleMapView.Coordinator) {
print("google dismantleUIView")
coordinator.cancellable.removeAll()
}
func makeCoordinator() -> GoogleMapView.Coordinator {
return Coordinator()
}
final class Coordinator {
var cancellable = Set<AnyCancellable>()
}
}
extension GoogleMapView: MapViewProtocol {
func willAppear(_ context: Context) {
mapViewEnvironment.$value1
.filter { $0 == "aaa" }
.sink {
print("============ google 1111 \($0)")
self.aaa()
}
.store(in: &context.coordinator.cancellable)
mapViewEnvironment.$value1
.filter { $0 == "bbb" }
.sink {
print("============ google 1111 \($0)")
self.bbb()
}
.store(in: &context.coordinator.cancellable)
mapViewEnvironment.value2
.filter { $0 == "aaa" }
.sink {
print("------------ google 2222 \($0)")
self.aaa()
}
.store(in: &context.coordinator.cancellable)
mapViewEnvironment.value2
.filter { $0 == "bbb" }
.sink {
print("------------ google 2222 \($0)")
self.bbb()
}
.store(in: &context.coordinator.cancellable)
}
func aaa() {
print("GoogleMapView - aaa")
}
func bbb() {
print("GoogleMapView - bbb")
}
}
import Combine
import SwiftUI
class MapViewEnvironment: ObservableObject {
@Published var value1 = "aaa"
var value2 = PassthroughSubject<String, Never>()
@Published var currentMapCompany = "apple"
}
import SwiftUI
protocol MapViewProtocol {
func aaa()
func bbb()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment