How to design Complications for watchOS 6 independent app?

Viewed 343

I have an independent application on watchOS 6 and in my app I am using the Firestore REST API to show the data to the user using URLSession.

Since the Cloud Firestore REST API returns a JSON string, in order to process the data, I have created nested structs. Example: To access the 'title' in a particular response, I do something like this: response.mapValue.fields.title.stringValue. The app works fine for now. In the long run I plan on creating the URLSessions as background tasks. Right now, I am calling URLSession every time the view is rendered by using .onAppear(functionToUseURLSession())) on my data's List view.

Now the next thing I want to implement is the complication for this particular app. I am new to Swift and SwiftUI and am having the hardest time just getting started. All the tutorials I've been through online use WKExtensionDelegate to get the data models for the complication. But in my case, I don't even have a data model! I just have structs calling other structs in a nested fashion in order to process the JSON response I get from the API call.

Any help allowing me to understand this is highly appreciated!

Here is some code to better understand the structure of the App:

struct Firebase: Codable {
    var name: String?
    var createTime, updateTime: String?
    var fields: FirebaseFields
}

Now, FirebaseFields is also another struct:

struct FirebaseFields: Codable {
    var emailID, lastName, firstName: String?
    var goalsRoutines: GoalsRoutines

    enum CodingKeys: String, CodingKey {
        case emailID = "email_id"
        case lastName = "last_name"
        case firstName = "first_name"
    }
}

Similarly, GoalsRoutines is also a struct...

As mentioned above, I have made these structs to follow the exact structure of the JSON object is get in response from Firebase API. So I access fields like: Firebase.FirebaseFields.GoalsAndRoutines.whatever.is.my.firebase.schema

My GoalsView.swift is:

var body: some View {
    List{
        ...
        ...
    }.onAppear{
        FirebaseServices.getFirebaseData() {
         (data) in self.data = data   
        }
    }
}

And finally, in FirebaseServices.swift, func getFirebaseData is:

func getFirebaseData(completion: @escaping ([Value]) -> ()) {
    guard let url = URL(string: "FIRESTORE URL HERE") else { return }
    URLSession.shared.dataTask(with: url) { (data, _, _) in
        let data = try! JSONDecoder().decode(Firebase.self, from: data!)
        DispatchQueue.main.async {
            completion(data.fields.goalsRoutines.arrayValue.values)
        }
    }.resume()
}

So this is how I currently get the data and show it on the app.

I am really lost on how to do the same on my Complication.

Any help is really appreciated.

0 Answers
Related