Advertisement
ahti123

Untitled

Nov 1st, 2023
1,202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 2.40 KB | None | 0 0
  1. import SwiftUI
  2.  
  3. struct Root: Codable {
  4.     let extended: Extended
  5. }
  6.  
  7. struct Extended: Codable {
  8.     let schemaId: String
  9.     let fieldMap: FieldMap
  10. }
  11.  
  12. struct FieldMap: Codable {
  13.     let vehicles: Vehicles
  14. }
  15.  
  16. struct Vehicles: Codable {
  17.     let fieldList: [FieldList]
  18. }
  19.  
  20. struct FieldList: Codable {
  21.     let code: String
  22.     let inactive: Bool
  23.     let fieldMap: VehicleDetails
  24. }
  25.  
  26. struct VehicleDetails: Codable {
  27.     let make: Detail
  28.     let color: Detail
  29.     let country: Detail
  30.     let stateRegistered: Detail
  31.     let model: Detail
  32.     let vehicleId: Detail
  33.     let plateNumber: Detail
  34. }
  35.  
  36. struct Detail: Codable {
  37.     let code: String
  38.     let inactive: Bool
  39.     let value: String
  40. }
  41.  
  42. struct ContentView: View {
  43.     // Sample data for the preview
  44.     let sampleData: Root = {
  45.         let data = """
  46.        {
  47.            "extended": {
  48.                "schemaId": "0b25a741-a660-432b-ae87-88fcfbf2a12d",
  49.                "fieldMap": {
  50.                    "vehicles": {
  51.                        "fieldList": [
  52.                            {
  53.                                "code": "vehicle",
  54.                                "inactive": false,
  55.                                "fieldMap": {
  56.                                    "make": {
  57.                                        "code": "make",
  58.                                        "inactive": false,
  59.                                        "value": "ASD"
  60.                                    },
  61.                                    // ... [rest of the fields]
  62.                                }
  63.                            },
  64.                            // ... [two more entries]
  65.                        ]
  66.                    }
  67.                }
  68.            }
  69.        }
  70.        """.data(using: .utf8)!
  71.        
  72.         return try! JSONDecoder().decode(Root.self, from: data)
  73.     }()
  74.    
  75.     var body: some View {
  76.         List(sampleData.extended.fieldMap.vehicles.fieldList, id: \.code) { vehicle in
  77.             VStack(alignment: .leading) {
  78.                 Text(vehicle.fieldMap.make.value)
  79.                 Text(vehicle.fieldMap.color.value)
  80.                 Text(vehicle.fieldMap.model.value)
  81.                 Text(vehicle.fieldMap.plateNumber.value)
  82.             }
  83.         }
  84.     }
  85. }
  86.  
  87. @available(iOS 15.0, *)
  88. struct ContentView_Previews: PreviewProvider {
  89.     static var previews: some View {
  90.         ContentView()
  91.     }
  92. }
  93.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement