Reputation: 3
I have several horizontal scrollviews inside a VStack that display cards for games played today, coming or previous. Initially I only get 12 from the database and have added a button after the forEach-loop that the user can press to 'get more items'. When the user does that the items are put at the end of the list of games but since the focus was on the 'get more items' button the user is put at the back of the list (not very user friendly imo).
if (!viewModel.previousGames.isEmpty) {
VStack (alignment: .leading, spacing: 8) {
Text(languageManager.localizedString(for: "Past"))
.font(.title2)
.foregroundColor(Color.white)
ScrollView(.horizontal, showsIndicators: false) {
HStack (alignment: .top, spacing: 48) {
ForEach(viewModel.previousGames) { game in
NavigationLink(destination: MagazineGameView(viewModel: FollowGameAPIController(), gameID: game.gameID)) {
GameCardView(game: game, showGameResults: showGameResults)
}
.buttonStyle(.borderless)
}
Button(action: {
getMoreItems()
}) {
MoreItemsView()
}
.buttonStyle(.borderless)
}
}
}
}
private func getMoreItems() {
viewModel.getMemberCalendarGames(isPrevious: true, lastGameId: viewModel.previousGames.last?.gameID) { result in
switch result {
case .success(let newGames):
print("Successfully fetched more games: \(newGames)")
case .failure(let error):
print("Failed to fetch more games: \(error)")
}
loading = false
}
}
I was wondering if I should add another button and array so that if the user loads more items i "hide" that 'get more items' button and show the new array instead and then add a new button at the end of that forEach-loop.
Correct me if I'm wrong but using @focusState won't work as they seem to break NavigationLinks
Upvotes: 0
Views: 42