Created
May 17, 2024 15:55
-
-
Save WeZZard/bf420c38b506ee5c72bbe15ddcac9175 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 SwiftUI | |
class Model1: ObservableObject { | |
static let shared = Model1() | |
var value: Int = 0 | |
@Published | |
var date: Date = Date() | |
} | |
struct Model2 { | |
class Heap { | |
static let shared = Heap() | |
var value: Int = 0 | |
var date: Date = Date() | |
} | |
let heap = Heap.shared | |
var value: Int { | |
get { | |
heap.value | |
} | |
set { | |
heap.value = newValue | |
} | |
} | |
var date: Date { | |
get { | |
heap.date | |
} | |
set { | |
heap.date = newValue | |
} | |
} | |
} | |
let timer = Timer.publish(every: 1, on: .main, in: .common).autoconnect() | |
struct ContentView: View { | |
var body: some View { | |
HStack { | |
SwiftUIView1() | |
UIKitView1() | |
} | |
} | |
} | |
struct SwiftUIView2: View { | |
struct Subview: View { | |
@Binding | |
var value: Int | |
init(value: Binding<Int>) { | |
self._value = value | |
} | |
var body: some View { | |
Text("\(value)") | |
} | |
} | |
@State | |
var model = Model2() | |
var body: some View { | |
Subview(value: $model.value) | |
.onReceive(timer) { input in | |
self.model.date = input | |
} | |
} | |
} | |
class MyUIKitView2: UIView { | |
var model = Model2() | |
override func layoutSubviews() { | |
super.layoutSubviews() | |
DispatchQueue.main.asyncAfter(deadline: .now().advanced(by: .seconds(1))) { | |
self.model.value += 1 | |
self.setNeedsLayout() | |
} | |
} | |
} | |
struct UIKitView2: UIViewRepresentable { | |
func makeUIView(context: Context) -> UIView { | |
MyUIKitView2() | |
} | |
func updateUIView(_ uiView: UIView, context: Context) { | |
} | |
} | |
struct SwiftUIView1: View { | |
struct Subview: View { | |
@Binding | |
var value: Int | |
init(value: Binding<Int>) { | |
self._value = value | |
} | |
var body: some View { | |
Text("\(value)") | |
} | |
} | |
@StateObject | |
var model: Model1 = .shared | |
var body: some View { | |
Subview(value: $model.value) | |
.onReceive(timer) { input in | |
self.model.date = input | |
} | |
} | |
} | |
class MyUIKitView1: UIView { | |
var myModel = Model1.shared | |
override func layoutSubviews() { | |
super.layoutSubviews() | |
DispatchQueue.main.asyncAfter(deadline: .now().advanced(by: .seconds(1))) { | |
self.myModel.value += 1 | |
self.setNeedsLayout() | |
} | |
} | |
} | |
struct UIKitView1: UIViewRepresentable { | |
func makeUIView(context: Context) -> UIView { | |
MyUIKitView1() | |
} | |
func updateUIView(_ uiView: UIView, context: Context) { | |
} | |
} | |
#Preview { | |
ContentView() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment