Day 13 : 20 June 2022 : Adding Buttons in SwiftUI

My 100 Daze of Code

https://github.com/davidjwalling/100-days-of-code

#13 : Adding Buttons in SwiftUI

We've made some structural changes in our Ava library. Now we need to revisit our iOS app and catch up with those changes.

The Driver class instance in the library is a singleton which is created when the library is loaded. We expose Start and Stop methods on this class to start the Sockets I/O thread and to stop it. Up to now, our iOS app has called Start when the app is launched. But Stop had been called in the Driver destructor. That is no longer the case. So we'll need to call Stop either on-demand or when the app terminates.

One advantage of having Start and Stop methods on the Driver is that we should be able to stop the driver and stop it again, repeatedly as needed, without stopping the App entirely. To debug this on iOS, we can replace our message-of-the-day Text control with two Button controls, one to start the driver and one to stop the driver.

First, though, we need to make sure that our Driver logic is fully reentrant. That is, we need to be able to operate correctly calling Start and Stop several times. Also, we should take care that accidentally calling Start multiple times without stopping operates as expected. The same for Stop. For Start, if _thread is already created, do not recreate and enter Run a subsequent time. Stop already checks for _thread before attempting the join.

The main driver will call TheDriver to return a pointer to the singleton IDriver. Then Start is called. If successful, the main logic loop runs until "exit" is entered. Then Stop is called.


The runtime behavior of the ava executable is the same on Windows, Linux and macOS as before.

On iOS, the Objective-C wrappers are updated to call the Start and Stop C++ methods.

For the app, we've added "Start" and "Stop" buttons which will invoke the Start and Stop methods of IDriver, respectively. Here is the screenshot of the iOS app.

Since wrapperItem is initialized as a pointer to the IDriver when View opens, we can reuse wrapperItem for each button. Note our logging messages report successful startup when the "Start" button is pressed and "Program ending" when the "Stop" button is pressed.

With the iOS having called Start, the TCP listening socket accepts connections and returns its CSS/HTML output in the HTTP reply.




 

Comments

Popular posts from this blog

Day 12 : 19 June 2022 : Adding Windows Service Logic

Day 5 : 12 June 2022 : CMake on Windows

Day 11: 18 June 2022 : Handling Program Arguments