Thank you so much @DTS Engineer
Post
Replies
Boosts
Views
Activity
Thanks @MobileTen.
So I am selecting the seasoned in the sidebar view as follows:
import SwiftData
struct SidebarView: View {
@EnvironmentObject var navigationManager: NavigationStateManager
@State private var isShowingSeasonSelection = false
var body: some View {
VStack {
List(selection: $navigationManager.SelectionState) {
Label("Home", systemImage: "house.fill")
.tag(SelectionState.home)
Section("Squadra") {
Label("Rosa", systemImage: "person.2.fill")
.tag(SelectionState.rosa)
Label("Calendario", systemImage: "calendar")
.tag(SelectionState.calendarioSquadra)
}
Section("Campionato") {
Label("Calendario", systemImage: "calendar")
.tag(SelectionState.calendarioCampionato)
Label("Classifica", systemImage: "list.bullet.rectangle.fill")
.tag(SelectionState.classifica)
}
Section("Allenatore") {
Label("Esercizi", systemImage: "pencil.and.list.clipboard")
.tag(SelectionState.esercizi)
}
Section("Impostazioni") {
Label("Impostazioni", systemImage: "gear")
.tag(SelectionState.settings)
}
}
.listStyle(.sidebar)
Spacer()
Button(action: {
isShowingSeasonSelection.toggle()
}) {
Text("Seleziona Stagione")
}
.padding()
.buttonStyle(.borderedProminent)
.popover(isPresented: $isShowingSeasonSelection) {
SeasonSelectionPopover(isShowingPopover: $isShowingSeasonSelection) // Passa la variabile di stato al popover
}
.navigationTitle("Stagione")
}
.frame(minWidth: 0, maxWidth: .infinity, alignment: .leading) // Imposta la larghezza per adattarsi al testo
}
}
struct SeasonSelectionPopover: View {
@EnvironmentObject var navigationManager: NavigationStateManager
@Binding var isShowingPopover: Bool // Binding per gestire la visualizzazione del popover
@State private var selectedSeasonId: String = ""
@Query(sort: \Stagione.idStagione, order: .reverse) private var seasons: [Stagione] // Assume che le stagioni siano disponibili
var body: some View {
VStack {
Text("Seleziona la stagione:")
.font(.headline)
.padding()
List(seasons, id: \.idStagione) { season in
Button(action: {
// Imposta la stagione selezionata come quella corrente
navigationManager.selectedSeasonId = season.idStagione
// Chiudi il popover dopo la selezione
isShowingPopover = false
}) {
Text(season.idStagione)
}
}
.frame(width: 200, height: 200)
}
.onAppear {
if let largestSeason = seasons.max(by: { $0.idStagione < $1.idStagione }) {
selectedSeasonId = largestSeason.idStagione
}
}
}
}
and I'd like to use it to filter data in the detail view "CalendarioCampionatoView":
import SwiftData
class CalendarioPartiteViewModel: ObservableObject {
@Published var partite: [CalendarioPartite] = [] // Partite filtrate per stagione
func fetchPartiteForStagione(stagioneId: String) {
// Qui dovresti implementare la logica per recuperare le partite dal datastore filtrate per stagioneId
@Query(filter: #Predicate<CalendarioPartite> {
$0.stagione == selectedSeasonId}) private var partite: [CalendarioPartite]
// Popola l'array partite con le partite corrispondenti
}
}
struct CalendarioCampionatoView: View {
@EnvironmentObject var navigationManager: NavigationStateManager
@ObservedObject var calendarioPartiteViewModel = CalendarioPartiteViewModel()
var body: some View {
VStack {
if calendarioPartiteViewModel.partite.isEmpty {
Text("Non ci sono partite attualmente salvate per la stagione \(navigationManager.selectedSeasonId)")
} else {
List(calendarioPartiteViewModel.partite, id: \.idGiornata) { partita in
// Visualizza le informazioni della partita
}
}
}
.onAppear {
// Recupera le partite filtrate per la stagione selezionata
calendarioPartiteViewModel.fetchPartiteForStagione(stagioneId: navigationManager.selectedSeasonId)
print("\(navigationManager.selectedSeasonId)")
}
}
}
I have here two errors and one warning:
Error: Attribute 'private' can only be used in a non-local scope
Error: Cannot find 'selectedSeasonId' in scope
Warning: Variable 'partite' was never used; consider replacing with '_' or removing it
As I quite new to SwiftUI, I'd really appreciate your experience to help me. I'm getting crazy since two days to solve this.
Thanks,
A.
Thanks @MobileTen, but I'm not looking for a searcheable field. I'd like to have an autocomplete functionality which also filters just some elements out of the all available in the class.
Sorry for this - solved.
It was the formatter in the second textfield.
Sorry,
A.
Dear @darkpaw,
I'm perfectly aware that this is not the optimal way to indicate the years. Now I'm focusing more on a smart way to perform the selection of the year and retain it in all my views.
I will come back for sure to this - in the meanwhile I'd appreciate if you have any suggestion on how to realize it. Everything that can improve my knowledge is more than welcome!
Thanks,
A.
Dear @Claude31,
thanks a lot for your kind reply.
I think you understood perfectly my question.
Here the code I have for my ContentView:
struct ContentView: View {
@StateObject var navigationStateManager = NavigationStateManager()
var body: some View {
NavigationSplitView {
SidebarView()
} detail: {
DetailView()
}
.environmentObject(navigationStateManager)
}
}
#Preview {
ContentView()
.environmentObject(NavigationStateManager())
}
As you can see, I set up a NavigationSplit view, but indeed I would like to have here the picker or a button which allows me to make the selection and keep the selection as filter for all the views (e.g. imagine I select "2024/2025", I'd like all the views to retrieve data which have that id as parameter).
I'm looking for a smart method to achieve this. Sorry for this, but as said I'm quite new to SwiftUI. More than this, I looked over internet for any example but have not been able to find any.
Thanks if you'd like to suggest me a way,
A.
Solved in the following way:
private let adaptiveColumn = [
GridItem(.adaptive(minimum: 1000)),
GridItem(.adaptive(minimum: 1000)),
GridItem(.adaptive(minimum: 1000)),
]
Thanks @darkpaw for your support!
Dear @darkpaw,
I've done exactly what you suggested and got pretty good results from a data retrieval point of view (I'm displaying correctly the four football league days with the proper teams and results).
Here below the code modification I've done:
import SwiftUI
struct CalendarioView: View {
@State var Calendario: [CalendarioPartite] = CalendarioPartite.testCalendario()
@State var stagione: String = "2023/2024"
@State var totalePartite: Int = 4
private var giornate = Array(1...4)
private let adaptiveColumn = [GridItem(.adaptive(minimum: 1500))]
var partiteCampionato: [CalendarioPartite] {
CalendarioPartite.testCalendario().filter{
$0.stagione == stagione
}
}
var body: some View {
ScrollView(.vertical) {
LazyVGrid(columns: adaptiveColumn, spacing: 20) {
ForEach(giornate, id:\.self) { giornata in
// Inizio schermata Giornata
VStack {
var partiteGiornata: [CalendarioPartite] {
partiteCampionato.filter {
$0.giornata == giornata
}
}
Text("Giornata \(giornata)")
.fontWeight(/*@START_MENU_TOKEN@*/.bold/*@END_MENU_TOKEN@*/)
.font(.headline)
.foregroundColor(/*@START_MENU_TOKEN@*/.blue/*@END_MENU_TOKEN@*/)
.padding(.vertical)
Grid {
ForEach (partiteGiornata) { partita in
GridRow {
Text(partita.squadracasa)
.gridCellAnchor(UnitPoint(x: 0, y: 0.5))
Text("-")
Text(partita.squadratrasferta)
.gridCellAnchor(UnitPoint(x: 0, y: 0.5))
Text("=")
Text(partita.golsquadracasa, format: .number)
Text("-")
Text(partita.golsquadratrasferta, format: .number)
}
}
}
}
.padding()
.background(Color.gray.opacity(0.2))
.clipShape(RoundedRectangle(cornerRadius: 15, style: .circular))
// Fine schermata Giornata
}
}
}
}
}
Now, I have basically a problem: all the "giornata" are displayed vertically one-below-the other, while I would like to display them aligned on the left with a certain number on a row and then below (e.g. Day 1-2-3 on the same row and then the Day 4 below the Day 1) depending on the size of the screen. Is this possible? I think it depends on the size of the screen. Am I right?
Thanks in advance for your support,
A.
Thanks a lot Claude31!
Above all thanks for your patience in explaining me - I admit to have started just few weeks ago to develop in SwiftUI so I'm conscious of my limits.
Here the code for the Rosa structure:
import Foundation
struct Rosa: Identifiable, Codable, Hashable, Equatable {
var id = UUID()
let stagione: String
let nomeGiocatore: String
let cognomeGiocatore: String
let nascitaGiocatore: String
let etàGiocatore: Int
let ruoloGiocatore: String
init(stagione: String, nomeGiocatore: String, cognomeGiocatore: String, nascitaGiocatore: String, etàGiocatore: Int, ruoloGiocatore: String) {
self.stagione = stagione
self.nomeGiocatore = nomeGiocatore
self.cognomeGiocatore = cognomeGiocatore
self.nascitaGiocatore = nascitaGiocatore
self.etàGiocatore = etàGiocatore
self.ruoloGiocatore = ruoloGiocatore
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd-MM-yyyy"
guard let birthDate = dateFormatter.date(from: nascitaGiocatore) else { return}
let currentYear = Calendar.current.component(.year, from: Date())
let birthYear = Calendar.current.component(.year, from: birthDate)
_ = currentYear - birthYear
}
static func testRosa() -> [Rosa] {
return [Rosa(stagione: "2023/2024", nomeGiocatore: "Matt", cognomeGiocatore: "Bar", nascitaGiocatore: "31-03-2000", etàGiocatore: 24, ruoloGiocatore: "Portiere"),
Rosa(stagione: "2023/2024", nomeGiocatore: "Fabio", cognomeGiocatore: "Bel", nascitaGiocatore: "30-11-1982", etàGiocatore: 41, ruoloGiocatore: "Difensore"),
Rosa(stagione: "2023/2024", nomeGiocatore: "Ale", cognomeGiocatore: "Nev", nascitaGiocatore: "23-04-2001", etàGiocatore: 23, ruoloGiocatore: "Attaccante"),
Rosa(stagione: "2022/2023", nomeGiocatore: "Matte", cognomeGiocatore: "Repe", nascitaGiocatore: "28-02-1999", etàGiocatore: 25, ruoloGiocatore: "Centrocampista")]
}
}
I have then indeed applied all your suggestions, even if the second one "Using the structure name (Rosa) as the argument of the closure is incorrect." is not clear to me. Would you be so kind to help me in understanding it?
Thanks,
A.
Thanks Calude31,
that works!
I should have also closed this thread and the others - please let me know if any other action is required on my side.
Thanks,
A.
Thanks a lot Claude31 - I'll try.
Sorry I have not been so precise in writing my question.
My idea is:
to have a decreasing order (from the max of punti to the min)
show the first half in one List, the second half in a second List
Any changes to your kindly suggested code?
Thanks,
A.
Hi Claude31,
that works perfectly!
Thank you so much!
A.
Hi,
I'm writing here as I found a solution for the problem and hope this will be useful for anybody else.
Here below the code I've added to the chart:
This way the chart is behaving as expected.
A.
Thanks Timo2303,
it worked perfectly.
Please tell me how can I close the thread.
Andrea