MultiPart Form uploads and Automation

Hello Devs and Support,

I am a new developer with a challenge to build a MacOS app that captures preliminary data from clients (text, signatures, jpgs and pdfs) and then pulls that data into a database where a secondary app automatically extracts the data into a word document template.

I have been researching to find documentation and support on how to implement this using SwiftUI and have only managed to build a basic content view:


//  ContentView.swift

//  Client Form

//

//  Created by Andrew Duncan Poole on 2022/05/10.

//



import SwiftUI



struct ContentView: View {



    @State var firstName = ""

    @State var lastName = ""

    @State var companyName = ""

    @State var isExchange: Bool = true

    @State var description = "This book is about .."

    @State var price = ""

    @State private var scrollViewContentSize: CGSize = .zero



    @State private var categoryIndex = 0

    var categorySelection = ["Action", "classic","Comic Book","Fantasy","Historical","Literary Fiction","Biographies","Essays"]

    

    

    var body: some View {

        ScrollView(.horizontal, showsIndicators: true) {

            HStack (spacing: 1) {

        

                Form {

            

                Section(header: Text("CE Technical File Preliminary Data")) {

                    TextField("First Name", text: $firstName)

                    TextField("Last Name", text: $lastName)

                    TextField("Company Name", text: $companyName)

                    Toggle(isOn: $isExchange) {

                        Text("I'm interested in an exhange")

                    }

                    .frame(width: 650)

                }

            

            Section() {

                Picker(selection: $categoryIndex, label: Text("Categorie")) {

                    ForEach(0 ..< categorySelection.count) {

                        Text(self.categorySelection[$0])

                    }

                }

            }

            Section(header: Text("Description")) {

                TextEditor(text: $description)

            }

            

            Section {

                Button(action: {

                    print("submitted ..")

                }) {

                    Text("Publish now")

                }

            }

        }

            

                

        }.navigationTitle("Simplimedica Client Form")

            }

        }

    

struct ContentView_Previews: PreviewProvider {

    static var previews: some View {

        ContentView()

        }

    }

}

Is there anyone who can PLEASE advise where I can find further assistance be it tutorials or sample code for this...

When testing your code, there were several errors:

  • nothing displays in simulator. I removed scrollView to get something
  • Previews was inside ContentView. You have to move it out.

What is it you don't succeed to do ?

A first step would be to test whether all TextFields have been populated and propose to publish only when OK.

To do so, you could add a var to check if a testField is filled and test for each TextField:

    @State private var firstNameOk = false
                            TextField("First Name", text: $firstName)
                                .onChange(of: firstName, perform: { _ in firstNameOk = !firstName.isEmpty ; /* test all fields OK */ })

I've updated code with the test for 3 textFields ; you have to do it for all information that user has to enter.

struct ContentView: View {
    
    @State var firstName = ""
    @State var lastName = ""
    @State var companyName = ""
    @State var isExchange: Bool = true
    @State var description = "This book is about .."
    @State var price = ""
    @State private var allOk = false                      // Are all fields populated ? Use it to enable publish
    @State private var firstNameOk = false        // Is firstname populated ?
    @State private var lastNameOk = false
    @State private var companyOk = false
    @State private var scrollViewContentSize: CGSize = .zero
    
    @State private var categoryIndex = 0
    
    var categorySelection = ["Action", "classic","Comic Book","Fantasy","Historical","Literary Fiction","Biographies","Essays"]
    
    var body: some View {
        
        NavigationView {
//            ScrollView(.horizontal, showsIndicators: true) {  // I had to remove this
                HStack (spacing: 1) {
                    Form {
                        Section(header: Text("CE Technical File Preliminary Data")) {
                            TextField("First Name", text: $firstName)
                                .onChange(of: firstName, perform: { _ in firstNameOk = !firstName.isEmpty ; allOk = firstNameOk && lastNameOk && companyOk })
                            TextField("Last Name", text: $lastName)
                                .onChange(of: lastName, perform: { _ in lastNameOk = !lastName.isEmpty ; allOk = firstNameOk && lastNameOk && companyOk })
                            TextField("Company Name", text: $companyName)
                                .onChange(of: companyName, perform: { _ in companyOk = !companyName.isEmpty ; allOk = firstNameOk && lastNameOk && companyOk })
                            Toggle(isOn: $isExchange) {
                                Text("I'm interested in an exchange")
                            }
                           //  .frame(width: 650) // Remove, unneeded
                            
                        }
                        
                        Section() {
                            Picker(selection: $categoryIndex, label: Text("Categorie")) {
                                ForEach(0 ..< categorySelection.count) {
                                    Text(self.categorySelection[$0])
                                }
                            }
                        }
                        
                        Section(header: Text("Description")) {
                            TextEditor(text: $description)
                        }
                        
                        Section {
                            Button(action: {
                                print("submitted ..")
                            }) {
                                Text("Publish now")
                            }.disabled(!allOk)
                        }
                    }
                }.navigationTitle("Simplimedica Client Form")
            }
//        }
    }
}

struct ContentView_Previews: PreviewProvider {
    
    static var previews: some View {
        ContentView()
    }
    
}

Second step will be to write to your database (publish in your code).

You may have a look at this tutorial for some insight.

https://www.hackingwithswift.com/books/ios-swiftui/how-to-combine-core-data-and-swiftui

MultiPart Form uploads and Automation
 
 
Q