I’m trying to put together a simple text selection app for MacOS. The SubjectView will list approx 15 subjects and each subject navigates to a new TabView (2 tabs) displaying multiple lines of text that are copied to the clipboard when selected.
The code below is working how I want it to but I can’t work out how to scale it up without it becoming unmanageable.
In its current form, it’s going to need 15 separate Tab views, 30 Functions etc.. I know that not correct but I can’t work out what changes are needed to use just the one SubjectView and a single TabView (using the data from SubjectItemLists file).
Can someone help me out with that please? (It's my first attempt at Swift!).
SubjectView
import SwiftUI
let pasteBoard = NSPasteboard.general
var subjectTitle: String = "Q Subjects..."
struct SubjectItems: Identifiable, Hashable {
let subjectName: String
let id = NSUUID().uuidString
}
struct SubjectView: View {
var body: some View {
NavigationStack{
List{
Section (subjectTitle){
ForEach (subjects) { subject in
NavigationLink ( value:subject){
Text (subject.subjectName)
}
}
}
} .navigationDestination(for: SubjectItems.self){ brand in
viewForBrand(brand)
}
}
}
func viewForBrand(_ subjectName: SubjectItems) -> AnyView {
switch subjectName.subjectName {
case "Doors...":
return AnyView(DoorView())
case "Windows...":
return AnyView(WindowView())
default:
return AnyView(Color(.systemGray))
}
}
}
struct ContentView_Previews: PreviewProvider {
static
var previews: some View {
SubjectView()
}
}
DoorView
import SwiftUI
struct DoorItems_Obs: Identifiable, Hashable {
let name: String
let id = NSUUID().uuidString
}
struct DoorItems_Rec: Identifiable, Hashable {
let name: String
let id = NSUUID().uuidString
}
struct DoorView: View {
var body: some View {
TabView {
// Observations.....
List{
Section("Door Comments (Observations)..."){
ForEach(lstDoors_Obs) {door in
NavigationLink( value:door){
Text(door.name)
}
}
}
}
.navigationDestination(for: DoorItems_Obs.self){ door in
viewForwindow(door)
}
.tabItem {
Text ("Doors - Observations...")
}
// Recommendations.....
List{
Section("Door Comments (Recommendations)..."){
ForEach(lstDoors_Rec) {door in
NavigationLink( value:door){
Text(door.name)
}
}
}
}
.navigationDestination(for: DoorItems_Rec.self){door in
viewForwindow(door)
}
.tabItem {
Text ("Doors - Recommendations...")
}
}
}
// Must be able to use one function here!!!....
func viewForwindow(_ doorName: DoorItems_Obs) -> AnyView {
if (doorName.name == "Return to subjects..."){
return AnyView(SubjectView())
} else {
// Copy to clipboard and then return to subjects
let _ = print("Copied - \(doorName.name)")
pasteBoard.clearContents()
pasteBoard.writeObjects([doorName.name as NSString])
return AnyView(SubjectView())
}
}
func viewForwindow(_ doorName: DoorItems_Rec) -> AnyView {
if (doorName.name == "Return to subjects..."){
return AnyView(SubjectView())
} else {
// Copy to clipboard and then return to subjects
let _ = print("Copied - \(doorName.name)")
pasteBoard.clearContents()
pasteBoard.writeObjects([doorName.name as NSString])
return AnyView(SubjectView())
}
}
}
#Preview {
DoorView()
}
SubjectItemLists
import Foundation
let subjects: [SubjectItems] = [
.init (subjectName:"Doors..."),
.init (subjectName:"Windows...")
]
// Observations...
var lstDoors_Obs: [DoorItems_Obs] = [
.init (name:"Return to subjects..."),
.init (name:"Door comment 1 "),
.init (name:"Door comment 2 ")
]
// Recommendations...
let lstDoors_Rec: [DoorItems_Rec] = [
.init (name:"Return to subjects..."),
.init (name:"Door comment 3 "),
.init (name:"Door comment 4 ")
]
// Windows x 2, etc etc....