Dynamic form from JSON input swift

Asked by At

i have a json file from which i am trying to create a dynamic for in swift with 3 pages I have passed the json data into a model using Codable and I can print it. This JSON has 3 pages which I suppose would be 3 UIViewController. How do I create this dynamic form because I seem confused. This is what I have tried so far

class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()

        guard let data = stubbedResponse("forms") else {return}
        do {
            let decoded = try JSONDecoder().decode(BaseModel.self, from: data)
            print(decoded)
        } catch {

        }
    }

    func stubbedResponse(_ filename: String) -> Data! {
        @objc class VC: NSObject {}

        let bundle = Bundle(for: VC.self)
        let path = bundle.path(forResource: filename, ofType: "json")
        return (try? Data(contentsOf: URL(fileURLWithPath: path!)))
    }
}

JSON:

{
  "id": "form_1",
  "name": "Pet Adoption Application Form",
  "pages": [
    {
      "label": "Page 1",
      "sections": [
        {
          "label": "Welcome to Pets Rescue",
          "elements": [
            {
              "type": "embeddedphoto",
              "file": "https://images.pexels.com/photos/8700/wall-animal-dog-pet.jpg?cs=srgb&dl=animal-collar-dog-8700.jpg&fm=jpg",
              "unique_id": "embeddedphoto_1",
              "rules": []
            }
          ]
        },
        {
          "label": "Basic Info",
          "elements": [
            {
              "type": "text",
              "label": "Your fullname",
              "isMandatory": true,
              "unique_id": "text_1",
              "rules": []
            },
            {
              "type": "text",
              "label": "Email address",
              "isMandatory": true,
              "unique_id": "text_2",
              "rules": []
            },
            {
              "type": "formattednumeric",
              "label": "Phone Number",
              "keyboard": "numeric",
              "formattedNumeric": "####-###-####",
              "isMandatory": true,
              "unique_id": "formattednumeric_1",
              "rules": []
            },
            {
              "type": "datetime",
              "mode": "date",
              "label": "Date of Birth",
              "isMandatory": true,
              "unique_id": "datetime_1",
              "rules": []
            }
          ]
        }
      ]
    },
    {
      "label": "Page 2",
      "sections": [
        {
          "label": "About your home",
          "elements": [
            {
              "type": "yesno",
              "label": "Do you have a yard?",
              "isMandatory": true,
              "unique_id": "yesno_1",
              "rules": [
                {
                  "condition": "equals",
                  "value": "Yes",
                  "action": "show",
                  "otherwise": "hide",
                  "targets": [
                    "text_3"
                  ]
                }
              ]
            },
            {
              "type": "text",
              "label": "Is it fenced? Also indicate the type",
              "isMandatory": false,
              "unique_id": "text_3",
              "rules": []
            }
          ]
        }
      ]
    },
    {
      "label": "Page 3",
      "sections": [
        {
          "label": "Additional Information",
          "elements": [
            {
              "type": "text",
              "label": "Please provide your veterinarian's name",
              "isMandatory": false,
              "unique_id": "text_4",
              "rules": []
            },
            {
              "type": "text",
              "label": "Please provide the name of a personal reference",
              "isMandatory": true,
              "unique_id": "text_5",
              "rules": []
            }
          ]
        }
      ]
    }
  ]
}

I understand a tableView could also be used in this instance with a button that signifies next page

1 Answers

0
Sh_Khan On

You have this structure

struct BaseModel {  
 let pages: [Model] 
}

You will have a vc that you should display recursively like

class SomeVC:UIViewController {
  var currentIndex = 0
  var pages = [Model]()
}

if you have say a tableView structured there then inside a next page action

let next = currentIndex + 1
if next < pages.count {
  let vc = SomeVC()
  vc.currentIndex = next
  vc.pages = pages
 // push the vc
}
else {
 // no more
}

Also you have an option to make this array inside a singleton