0

I have a menu in which the user should select exercises from a list of which they want to include in a routine. These exercises are presented in a LazyVGrid which pops up with a sheet on interaction. This sheet gives the option to add that exercise via a button.

What I would like to have happen is on the button press that grid item would disappear from the LazyVGrid and the exercise, which is of a custom type ExerciseModel, would be appended to an array of selected exercises.

The type ExerciseModel follows this model:

import Foundation
import SwiftUI

struct ExerciseModel: Identifiable, Hashable {
    
    var id = UUID()
    var exerciseID: String // ID for the exercise in DB
    var userID: String // ID for the user in DB
    var username: String // username of user in DB
    var exerciseTitle: String
    var dateCreate: Date
    var exerciseImage: String // where is the image located
    var repsInfo: String // How many repetitions for the exercise
    var setsInfo: String // How many sets for the exercise
    var sharedUserID: String? // ID for the shared user in DB
    var sharedUserUsername: String? // Username for the shared user in DB
    
    func hash(into hasher: inout Hasher) {
        hasher.combine(id)
    }
    
}

So far I have done this, starting with the menu where the user is to select which exercises they want to include in their routine (ExerciseView is simply a ZStack containing information about the exercise):

import SwiftUI

struct ExerciseSelectView: View {
    @ObservedObject var exercises: ExerciseArrayObject
    @State var selectedExercise: ExerciseModel?
    @State var selectionArray: [ExerciseModel] = [
        
    ]
    @State var showSheet: Bool = false
    @State var searchText = ""
    @Environment(\.presentationMode) var presentationMode
    var columns = Array(repeating: GridItem(.flexible()), count: 2)
    var body: some View {
        NavigationView {
            VStack {
                
                SearchBar(text: $searchText)
                    .padding(.all)
                
                Divider()
                
                ScrollView(.vertical, showsIndicators: false){
                    
                    LazyVGrid(columns: columns, spacing: 10){
                        
                        ForEach(exercises.dataArray.filter({"\($0)".contains(searchText) || searchText.isEmpty}), id: \.self) { i in
                            Button {
                                selectedExercise = i
                                showSheet.toggle()
                            } label: {
                                ExerciseView(exercise: i)
                            }
                            .sheet(item: $selectedExercise) { exercise in
                                ExerciseDetailAddView(exercise: exercise)
                            }
                        }
                        .listStyle(SidebarListStyle())
                        .navigationTitle("")
                        .navigationBarHidden(true)
                        
                    }
                }.padding()
            }
        }
    }
}

For reference, this is the sheet view which is presented when the grid items are pressed:

import SwiftUI

struct ExerciseDetailAddView: View {
    @State var exercise: ExerciseModel
    @State var selection: ExerciseModel?
    @State var addToggle: Bool = false
    var body: some View {
        VStack {
            ExerciseDetailView(exercise: exercise)
            
            Button(action: {
                if addToggle {
                    
                } else {
                    selection = exercise
                    addToggle.toggle()
                    print("--> selection: \(selection!)")
                }
            }, label: {
                if addToggle {
                    Text("Exercise Added".uppercased())
                        .font(.headline)
                        .fontWeight(.bold)
                        .foregroundColor(.white)
                        .transition(.opacity)
                } else {
                    Text("Add to Routine".uppercased())
                        .font(.headline)
                        .fontWeight(.bold)
                        .foregroundColor(.white)
                }
            })
            .padding()
            .frame(width: 300)
            .background(addToggle ? Color.green : Color.blue)
            .cornerRadius(25)
        }
    }    
}
fesari
  • 33
  • 7
  • What you have won't compile for anyone else because of missing types. It would increase the likelihood of getting an answer if you include a [mre]. – jnpdx Jan 05 '22 at 05:09
  • Thank you for the feedback, I have added in the ExerciseModel type. Hopefully this helps. – fesari Jan 05 '22 at 05:35

0 Answers0