App design and set up

Dear all, please be patient with me as I'm quite new to SwiftUI. I'm designing and setting up my first SwiftUI app. I'd like to release it for Mac and iPad.

I'm leveraging the NavigationSplit View, storing all the data in SwiftData. The whole application and dataset is driven by the selection of a value at the beginning. In fact I have a table like the one below (and all the other tables have a relationship one to many with this one):

import SwiftData

@Model
class Stagione {
    var initStagione: Bool
    var idStagione: idStagione
    
    init(initStagione: Bool, idStagione: idStagione) {
        self.initStagione = initStagione
        self.idStagione = idStagione
    }
}

enum idStagione: Int, Codable, Identifiable, CaseIterable {
    case PrimoAnno, SecondoAnno, TerzoAnno, QuartoAnno, QuintoAnno, SestoAnno, SettimoAnno
    var id: Self {
        self
    }
    var descr: String {
        switch self {
        case .PrimoAnno:
            "2023/2024"
        case .SecondoAnno:
            "2024/2025"
        case .TerzoAnno:
            "2025/2026"
        case .QuartoAnno:
            "2026/2027"
        case .QuintoAnno:
            "2027/2028"
        case .SestoAnno:
            "2028/2029"
        case .SettimoAnno:
            "2029/2030"
        }
    }
}

I'd like to understand, based on your suggestion, how I can place the selection of the idStagione when the app is launched so that all the data displayed in the application are filtered by that parameter. On the other hand, if the parameter is changed I'd like the application to change the data shown.

I was thinking of two possible options:

  1. Having a form with a picker to select the idStagione
  2. Place the picker in the sidebar

Do you have any suggestion? Is there any example of something with the same logic that you can share with me?

Thanks in advance for your support, A.

I hope I understand exactly what you want to achieve.

You don't show all code, but you should have a first View (often named ContentView).

It looks like the following:

struct ContentView: View {
    
    // var declarations, Sate var…
    @State var showPicker = true // at start only
    @State var selectedStagione = ""  // You could also set the initial value 2023/2024 here without need for onAppear
    
    var body: some View {
        
        VStack(alignment: .leading) {
            // the view content, with the picker for idStagione selection
            if showPicker {
                // display the Picker
                Picker("Please select", selection: $selectedStagione) {
                    // build Picker items
                }
                .onChange(of: selectedStagione) { _, _ in  // The new format for onChange
                    showPicker = false
                    print("Selection done")
                }
            }
        }
        .onAppear {
            // set idStagione initial value here
            selectedStagione = "2023/2024"  // adapt as needed
        }
    }
}

Please tell if you need more explanation ; if so, show your ContentView. If OK, don't forget to close the thread by marking the correct answer.

This will get out of date quickly. You should create your year strings ("2023/2024"... etc.) with some code that crafts the Strings. You wouldn't then need the enum at all because you could just say, "if user picks primo (first year), then use last year and last year +1", "If user picks secondo, use current year and current year + 1" etc.

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.

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.

App design and set up
 
 
Q