Open janhendry opened 3 years ago
Thank you, this is great. I'd be happy to consider a pull request that adds your demo example to the examples folder in the repo.
pull request it out :)
I could really use this example app, so please add it in a pull request!
I want to implement this view
I found a good way to integrate ORSSerialPort into a SwiftUI view. I would like to show them here. Maybe it will help someone, or maybe someone has an even better idea.
I have a View struct, a ViewModel Class and then SerialPortCombine class. The SerialPortCombine wrapper the ORSSerialPort.
Start with a simple View
We have the
PortSettings
they have all values of the ORSSerialPort.I add the SerialPortCombine the
var portSettings: CurrentValueSubject<PortSettings,Never>
Publisher. They will trigger a event when one of the values from the ORSSerialPort should change.And finally we have the ViewModel.
The model will subscribes the
portSettings
publisher and loads the data into thesetting
variable. This is an@observerbal
, so the view can@binding
this struct.Now it is possible to display all variables in the view. Now we want to control the variables via the view Control like this.
For this we need
@Published
properties. We add an@Published
variable toSerialPortCombine
for every variable that we can change onORSSerialPort
. And I add theObservableObject
protocol, that we can observe the@Published
values in the View.That is all what we need. Now comes the big question about how we are implementing SerialPortCombine.
Let's start :)
At first we track the KVO from
ORSSerialPort
, If someone change we trigger the portSettings publisher. And update the Values inSerialPortCombine
.The next step is, we need update the values in
ORSSerialPort
if the@Published
values inSerialPortCombine
will change.By the values
kts
anddtr
Its Important that we check, that we cancel the Cycle. That notORSSerialPort update
SerialPortCombineand that
SerialPortCombineupdate
ORSSerialPort` and so on. I always check whether the value has really changed.So now we are finish with the connection between ORSSerialPort and SerialPortCombine.
What we are now missing is the
isOpen
andisConnection
value. For that we need add theORSSerialPortDelegate
protocol and add this variable to SerialPortCombine.That's all. I will upload the full code in example. If anyone has better ideas, please feel free to comment.