Hi @Claude31, first of all thank you very much for trying to help, this is very appreciated, I have tried both solution but issues is still the same ( either the app crash without any warning or advice on where is the issuess...... only giving me crazy line of code...
when I click one of the row created by the for each project filtered all the buttons change from true to false ( regardless of the id... I really don;t get what I'm doing wrong)
I started to understand your logic when I read your prompt mentioned above but either I do it wrongly or either I beelieve is coming from my List creation....
anyway I did follow you advice and copy only the interesting part for you here is my code which fit only into one file
with his code you will be able to see what I'm struggling with.
what I need is to have a ROW which have 5 button each button represent a timing , 0.5h 1h,2h,3,4,8h , then user will be able to clic any of those button , the catch is that it's working in a rowtimekeyin as you ca see on the code below but when I create a list of row , like you set my asset seems to be look to the rowitelf and not each button as I need to.
Because the end target it to let the user click at the end of the day the number of hours for each project he word on but if he key-in more than 5 hours all the remaining hours available (not click) which are more than 3h (because 8h per days max- 5hours already choose) those will not be clickable anymore with a warning saying that he already chose 5h therefore only 3h and lower will be clickable. but that will be a data logic file, for now I'm building the UI and as you can see I'm diving into forum.....
//
// TimeKeyInRow.swift
// MyPmV1
//
// Created by Sebastien BENAVIDES on 9/2/24.
//
import Foundation
import SwiftUI
struct TimeKeyInRow: View,Identifiable {
var id: String
var projects = [
Project(name: "Project1, isTeco = false, shortname = MORN, leftPMTime = 131",shortname : "MORN", leftPMtime : 131, isTeco: false),
Project(name: "Project2, isTeco = false, shortname = MORN, leftPMTime = 122",shortname : "IFF", leftPMtime : 122, isTeco: true),
Project(name: "Project3, isTeco = false, shortname = MORN, leftPMTime = 133",shortname : "FFI", leftPMtime : 133, isTeco: true),
Project(name: "Project4, isTeco = false, shortname = MORN, leftPMTime = 444",shortname : "IFFCO", leftPMtime : 444, isTeco: false)
]
@Binding var isSet: Bool
let listOfPossibleHours: [Double] = [0.5,1,2,3,4,8]
var body: some View {
HStack {
Text(projects[1].shortname)
Spacer()
HourButton(id: "\(projects[1].shortname)\(0)", isSet: true, value: listOfPossibleHours[0])
HourButton(id: "\(projects[1].shortname)\(1)", isSet: true, value: listOfPossibleHours[1])
Text("\(projects[1].leftPMtime)")
}
}
}
struct HourButton: View,Identifiable {
var id: String = "hour"
@State var isSet: Bool
var value: Double = 1
var body: some View {
HStack {
Button {
isSet.toggle()
print("clic already \(isSet) \(id)h & \(value) has Value")
} label: {
Label("8h", systemImage: isSet ? "circle.fill" : "circle")
.labelStyle(.iconOnly)
.foregroundStyle(isSet ? .blue : .gray)
}.id(id)
}
}
}
struct TimeKeyinList: View, Identifiable{
var id: String
var projects = [
Project(name: "Project1, isTeco = false, shortname = MORN, leftPMTime = 131",shortname : "MORN", leftPMtime : 131, isTeco: false),
Project(name: "Project2, isTeco = false, shortname = MORN, leftPMTime = 122",shortname : "IFF", leftPMtime : 122, isTeco: true),
Project(name: "Project3, isTeco = false, shortname = MORN, leftPMTime = 133",shortname : "FFI", leftPMtime : 133, isTeco: true),
Project(name: "Project4, isTeco = false, shortname = MORN, leftPMTime = 444",shortname : "IFFCO", leftPMtime : 444, isTeco: false)
]
@State var isSet: [Bool]
@State private var hideTeco = false
var filteredProjects: [Project] {
// removeTECO project
projects.filter { project in
!project.isTeco
}}
var body: some View {
VStack {
ForEach(Array(filteredProjects.enumerated()), id: \.offset) { (row, project) in
TimeKeyInRow(id: project.shortname, projects: projects, isSet: $isSet[row])
}
}.labelsHidden()
}
}
struct Project: Identifiable {
var id: UUID = UUID()
var name: String
var shortname: String
var leftPMtime: Int
var isTeco: Bool
}
struct TimeKeyInRow_Previews: PreviewProvider {
static var previews: some View {
//#Preview {
let projects = [
Project(name: "Project1, isTeco = false, shortname = MORN, leftPMTime = 131",shortname : "MORN", leftPMtime : 131, isTeco: false),
Project(name: "Project2, isTeco = false, shortname = MORN, leftPMTime = 122",shortname : "IFF", leftPMtime : 122, isTeco: true),
Project(name: "Project3, isTeco = false, shortname = MORN, leftPMTime = 133",shortname : "FFI", leftPMtime : 133, isTeco: true),
Project(name: "Project4, isTeco = false, shortname = MORN, leftPMTime = 444",shortname : "IFFCO", leftPMtime : 444, isTeco: false)
]
TimeKeyInRow(id: "test", projects: projects, isSet: .constant(true))
}
}
struct HourButton_Previews: PreviewProvider {
static var previews: some View {
//#Preview {
HourButton(id: "MORN", isSet: true, value: 3)
}
}
struct TimeKeyinList_Previews: PreviewProvider {
static var previews: some View {
//#Preview {
TimeKeyinList(id: "test",isSet: [true])
}
}