4

I'm creating a widget for my application and successfully added intent configuration with dynamic data that I got from API.

This snippet gets the list and returns it to Intent:

func provideLeagueOptionsCollection(for intent: LeagueConfigurationIntent,
                                  with completion: @escaping (INObjectCollection<LeagueType>?, Error?) -> Void) {
    
    request(for: Leagues.self, completion: { leagueList in
        
        let leagues = leagueList as! Leagues
        
        let activeLeagues = leagues.leagueList.map { (league: League) -> LeagueType in
            LeagueType(identifier: league.id,
                       display: league.name)
        }
        
        let collection = INObjectCollection(items: activeLeagues)
        
        completion(collection, nil)
    })
}

What I want to add is, when the user selects anything from this list, I want to show another option to select, it goes something like that:

  1. User selects League.
  2. Intent fetches Teams data from API with League's id provided on selection before and team list selection gets visible.
  3. User selects a team from the second list.
  4. Intent returns this data to IntentTimelineProvider -> getTimeline()
  5. The widget shows the selected team's stats.

I tried to debug step-by-step, but after the first selection, none of these ConfigurationIntentHandling functions gets called.

func provideTeamOptionsCollection(for intent: TeamConfigurationIntent, with completion: @escaping (INObjectCollection<TeamType>?, Error?) -> Void) { ... } 

func provideLeagueOptionsCollection(for intent: LeagueConfigurationIntent,
                                      with completion: @escaping (INObjectCollection<LeagueType>?, Error?) -> Void) { ... }
// This method only gets called when interacted with League opiton in configuration view (not after selection from data list).

override func handler(for intent: INIntent) -> Any { return self } 
// This method only gets called when the configuration menu shows up.

My goal is to filter teams from a selected league, so the user won't have to scroll through a long list of teams.

Also open for another suggestions beside this approach.

oğuz
  • 145
  • 1
  • 13
  • If I understood you correctly, you already have the function that the user can select a League from the list? If so, you can follow along this guide https://developer.apple.com/documentation/widgetkit/making-a-configurable-widget Just add your api request in the 'getTimeline' function before entry. There you can fetch your data with your custom values. In this example: configuration.character.name gives you the selected value. See also https://medium.com/swlh/build-your-first-ios-widget-part-3-36ba53033e33 – DoTryCatch Feb 21 '22 at 12:22
  • @DoTryCatch I'm sorry, but I think you misunderstood the question. Indeed, I can fetch the League list, put it in intent selection, and find out which one is selected in the intent, and show said league's data in the widget. My question is not "how to show data based on selection within intent on the widget". I'm trying to show another intent selection based on the first one's result, and I can achieve while using Enums and preset categories (leagues) but not with fetched data. – oğuz Feb 21 '22 at 13:01
  • Please read the numerated list in my question. In the end, I think there is no way I can get the data from selected intent after step 1 before it goes to timeline, so I can use it to fetch the second list for team selection. – oğuz Feb 21 '22 at 13:09
  • You could do something like this: 1. fetch the data from the API (all leagues and teams) 2. create League list and provide a default value for the teams - or make it optional 3. create the second list with the teams dynamically sorted by the League id I'm thinking of apples remainder widget where you can search the list, the "searching"/sorting could be done programatically when selecting the League. – DoTryCatch Feb 21 '22 at 15:22
  • https://medium.com/swlh/advanced-widgets-network-calls-deeplinking-intent-configuration-30c697e09789 - last section. If you could manage to struct the teams in a way that you can see to which league they belong you could dynamically sort the teams array and use it like in the example here with: "availableUsers". – DoTryCatch Feb 21 '22 at 15:30
  • @DoTryCatch There is no way I can fetch all the League and Team list at once and sort it because there is like 10k of items in those list collectively. Also, the API doesn't have an endpoint like `server.com/teams`. I need to fetch teams of the league data with ID of the league, like `server.com/league-id/teams`, so I need the value of the first selection, then request teams from server. – oğuz Feb 22 '22 at 07:23
  • @oğuz have figured it out? is it possible? – Adam Bardon Feb 23 '23 at 11:27

1 Answers1

1

Think I've found the solution(at least it works for me) - set a parent parameter for your second param:

enter image description here

And then when providing items for the list for the second param, the provideTeamOptionsCollection function's param intent has all the other params, including the selected league id.

Adam Bardon
  • 3,829
  • 7
  • 38
  • 73