Could you please update your guidebook : https://developer.apple.com/tutorials/swiftui/handling-user-input here as it's mentioned about observable macro but doesn't say we need to import anything , ive spent over 1h to figure out were my programme was not following. your guidance until I came across this topic and it solves my issues
Post
Replies
Boosts
Views
Activity
Sorry guys first trime trying to post my own thread
here is the error from compiler
"Closure containing control flow statement cannot be used with result builder 'ViewBuilder"
appear on line 21 " For item in items {"
//
// ContentView.swift
// Project1
//
// Created by Sebastien BENAVIDES on 2/2/24.
//
import SwiftUI
struct ContentView: View {
var pictures = [String]()
var body: some View {
let fm = FileManager.default
let path = Bundle.main.resourcePath!
let items = try! fm.contentsOfDirectory(atPath: path)
for item in items {
if let item = item.hasPrefix("nssl") {
pictures.append(item)
}
}
print(pictures)
}
}
#Preview {
ContentView()
}
I'm trying to follow STORMVIEWS tutorial which was made with an old version of swift were viewed load was still available...
Sorry I don’t know why the photo posted is rotated 180 deg @appledev team maybe can answer the photo on my phone looks ok ….
Hi Everyone, below is the TimeKeyinRow, I trying to find some way to make it shorter, but this is the start of my project above, I know there are something to do with loop Button but I want to implement some code behind and looping a button will create same iteration which I want to avoid in order to collect the time of different project in the same day to make it 8h and remove option when user spend more than 8h as explain in my drawing ```
import SwiftUI
struct TimeKeyInRow: View {
@Binding var isSet: Bool
var project: Project
var body: some View {
HStack {
Text(project.shortname)
Spacer(minLength:20)
Button {
isSet.toggle()
print(".5h")
} label: {
Label("Toggle Favorite", systemImage: isSet ? "circle.fill" : "circle")
.labelStyle(.iconOnly)
.foregroundStyle(isSet ? .blue : .blue)
.padding(10)
.tag(".5h")
}
Button {
isSet.toggle()
print("1h")
} label: {
Label("Toggle Favorite", systemImage: isSet ? "circle.fill" : "circle")
.labelStyle(.iconOnly)
.foregroundStyle(isSet ? .blue : .blue)
.padding(10)
.tag("1h")
}
Button {
isSet.toggle()
print("2h")
} label: {
Label("Toggle Favorite", systemImage: isSet ? "circle.fill" : "circle")
.labelStyle(.iconOnly)
.foregroundStyle(isSet ? .blue : .blue)
.padding(10)
.tag("2h")
}
Button {
isSet.toggle()
print("3h")
} label: {
Label("Toggle Favorite", systemImage: isSet ? "circle.fill" : "circle")
.labelStyle(.iconOnly)
.foregroundStyle(isSet ? .blue : .blue)
.padding(10)
.tag("3h")
}
Button {
isSet.toggle()
print("4h")
} label: {
Label("Toggle Favorite", systemImage: isSet ? "circle.fill" : "circle")
.labelStyle(.iconOnly)
.foregroundStyle(isSet ? .blue : .blue)
.padding(10)
.tag("4")
}
Spacer(minLength: 20)
Text("\(project.leftPMtime)")
}
}
}
#Preview {
let projects = ModelData().projects
return Group {
TimeKeyInRow(isSet: .constant(false), project: projects[2])
}
}
here is my code so far :
//
// TimeKeyInRow.swift
// MyPmV1
//
// Created by Sebastien BENAVIDES on 9/2/24.
//
import SwiftUI
struct TimeKeyInRow: View {
@Binding var isSet: Bool
var projectshortname: String
var timeleft: Int
var body: some View {
HStack {
Text(projectshortname)
Spacer(minLength:50)
Button {
isSet.toggle()
print(".5h")
} label: {
Label("Toggle .5h", systemImage: isSet ? "circle.fill" : "circle")
.labelStyle(.iconOnly)
.foregroundStyle(isSet ? .blue : .blue)
.padding(4)
.tag(".5h")
}
Button {
isSet.toggle()
print("1h")
} label: {
Label("Toggle 1h", systemImage: isSet ? "circle.fill" : "circle")
.labelStyle(.iconOnly)
.foregroundStyle(isSet ? .blue : .blue)
.padding(4)
.tag("1h")
}
Button {
isSet.toggle()
print("2h")
} label: {
Label("Toggle 2h", systemImage: isSet ? "circle.fill" : "circle")
.labelStyle(.iconOnly)
.foregroundStyle(isSet ? .blue : .blue)
.padding(4)
.tag("2h")
}
Button {
isSet.toggle()
print("3h")
} label: {
Label("Toggle 3h", systemImage: isSet ? "circle.fill" : "circle")
.labelStyle(.iconOnly)
.foregroundStyle(isSet ? .blue : .blue)
.padding(4)
.tag("3h")
}
Button {
isSet.toggle()
print("4h")
} label: {
Label("Toggle 4h", systemImage: isSet ? "circle.fill" : "circle")
.labelStyle(.iconOnly)
.foregroundStyle(isSet ? .blue : .blue)
.padding(4)
.tag("4")
}
Text("\(timeleft)").padding()
}
}
}
#Preview {
let projects = ModelData().projects
return Group {
TimeKeyInRow(isSet: .constant(false), projectshortname: projects[2].shortname, timeleft: projects[2].leftPMtime)
}
}
and this is the row combine swift file
//
// TimeKeyinList.swift
// MyPmV1
//
// Created by Sebastien BENAVIDES on 9/2/24.
//
import SwiftUI
struct TimeKeyinList: View {
@Environment(ModelData.self) var modelData
@State private var hideTeco = false
var filteredProjects: [Project] {
// If TECO and favorite are active then I would only see favourite and not TECO projects
modelData.projects.filter { project in
!project.isTeco
}}
var body: some View {
VStack {
HStack {
Text("TIME KEY_IN")
.font(.title)
.multilineTextAlignment(.leading)
.bold()
Spacer()
Text("8h")
.font(.title)
.multilineTextAlignment(.leading)
.bold()
}.padding()
List {
ForEach (filteredProjects) { project in
TimeKeyInRow(isSet: .constant(true), projectshortname: project.shortname, timeleft: project.leftPMtime)
.listStyle(.inset)
}
Divider()
TimeKeyInRow(isSet: .constant(true), projectshortname: "TRAINING", timeleft: 0)
TimeKeyInRow(isSet: .constant(true), projectshortname: "MEETING", timeleft: 0)
TimeKeyInRow(isSet: .constant(true), projectshortname: "NON ALLOCATED", timeleft: 0)
TimeKeyInRow(isSet: .constant(true), projectshortname: "HOLIDAY", timeleft: 0)
}
.listStyle(.inset)
}
}
}
#Preview {
TimeKeyinList()
.environment(ModelData())
}
result is far here because here my few issues :
I cannot click my button ( point 1) therefore cannot count the time , and the timing is not aligned
hi @Claude31 , I have created the following 3 files :```
//
// HourButton.swift
// MyPmV1
//
// Created by Sebastien BENAVIDES on 9/2/24.
//
import SwiftUI
struct HourButton: View {
var isSet: Bool
var taghour: String
var body: some View {
Button {
print(taghour)
} label: {
Label("Toggle \(taghour)", systemImage: isSet ? "circle.fill" : "circle")
.labelStyle(.iconOnly)
.foregroundStyle(isSet ? .blue : .gray)
}
}
}
#Preview {
HourButton(isSet: false, taghour: "1h")
}
this is my second file
//
// TimeKeyInRow.swift
// MyPmV1
//
// Created by Sebastien BENAVIDES on 9/2/24.
//
import SwiftUI
struct TimeKeyInRow: View {
@Binding var isSet: Bool
var projectshortname: String
var timeleft: Int
var body: some View {
HStack {
Text(projectshortname)
Spacer(minLength:30)
HourButton(isSet: isSet, taghour: "0.5h").tag("0.5h")
HourButton(isSet: isSet, taghour: "1h").tag("1h")
HourButton(isSet: isSet, taghour: "2h").tag("2h")
HourButton(isSet: isSet, taghour: "3h").tag("3h")
HourButton(isSet: isSet, taghour: "4h").tag("4h")
Text("\(timeleft)").padding()
}
}
}
#Preview {
let projects = ModelData().projects
return
TimeKeyInRow(isSet: .constant(true),projectshortname: projects[2].shortname, timeleft: projects[2].leftPMtime)
}
this is my 3rd file which is crashing
//
// TimeKeyinList.swift
// MyPmV1
//
// Created by Sebastien BENAVIDES on 9/2/24.
//
import SwiftUI
struct TimeKeyinList: View {
@Environment(ModelData.self) var modelData
let projects = ModelData().projects
@Binding var isSet: Bool
@State private var hideTeco = false
var filteredProjects: [Project] {
// If TECO and favorite are active then I would only see favourite and not TECO projects
modelData.projects.filter { project in
!project.isTeco
}}
var body: some View {
VStack {
HStack {
Text("TIME KEY_IN")
.font(.title)
.multilineTextAlignment(.leading)
.bold()
Spacer()
Text("8h")
.font(.title)
.multilineTextAlignment(.leading)
.bold()
}.padding()
List {
HStack(alignment: .top) {
Text("Project Name")
.frame(width: 80, height: 25)
.font(.caption)
.multilineTextAlignment(.center)
.lineLimit(8)
.allowsTightening(/*@START_MENU_TOKEN@*/true/*@END_MENU_TOKEN@*/)
.multilineTextAlignment(.center)
.bold()
Spacer(minLength:30)
Text("0.5h")
.font(.footnote)
Text("1h")
.font(.footnote)
.padding(/*@START_MENU_TOKEN@*/EdgeInsets()/*@END_MENU_TOKEN@*/)
Text("2h")
.font(.footnote)
.padding(/*@START_MENU_TOKEN@*/EdgeInsets()/*@END_MENU_TOKEN@*/)
Text("3h")
.font(.footnote)
.padding(/*@START_MENU_TOKEN@*/EdgeInsets()/*@END_MENU_TOKEN@*/)
Text("4h")
.font(.footnote)
.padding(/*@START_MENU_TOKEN@*/EdgeInsets()/*@END_MENU_TOKEN@*/)
Text("Time Left")
.frame(width: 60, height: 25)
.font(.caption)
.multilineTextAlignment(.center)
.lineLimit(8)
.allowsTightening(/*@START_MENU_TOKEN@*/true/*@END_MENU_TOKEN@*/)
.multilineTextAlignment(.center)
.bold()
}
ForEach (filteredProjects) { project in
TimeKeyInRow( isSet: $isSet, projectshortname: project.shortname, timeleft: project.leftPMtime)
}
Divider()
TimeKeyInRow(isSet: $isSet, projectshortname: "TRAINING", timeleft: 0)
TimeKeyInRow(isSet: $isSet, projectshortname: "MEETING", timeleft: 0)
TimeKeyInRow(isSet: $isSet, projectshortname: "NON ALLOCATED", timeleft:0)
TimeKeyInRow(isSet: $isSet, projectshortname: "HOLIDAY", timeleft:0)
}
.listStyle(.inset)
DatePicker(selection: /*@START_MENU_TOKEN@*/.constant(Date())/*@END_MENU_TOKEN@*/, label: { /*@START_MENU_TOKEN@*/Text("Date")/*@END_MENU_TOKEN@*/ })
}
}
}
#Preview {
TimeKeyinList( isSet: .constant(false))
}
hi team,
i actually try a simple project and it’s the same things the hello world’proje t doesn’t even lunch on my phone while no issues on simulator or canvas.
issues is similar as above just dofferent random number but similar text and localtion of missing data
Hi Team,
actually it has nothing to do with my app it's more a general issues as I try to make a simple new project and the issues appear again with similar code :
dyld[885]: Symbol not found: _$s21DeveloperToolsSupport17UVPreviewRegistryCMn
Referenced from: <BED326A9-79E3-3602-8514-8197FB91ACE5> /private/var/containers/Bundle/Application/DEB6506D-CB33-4B47-B9B7-93D3267FC912/testingmydevice.app/testingmydevice
Expected in: <D12C944E-D705-3DDF-90C6-5619BCD328E3> /System/Library/Frameworks/DeveloperToolsSupport.framework/DeveloperToolsSupport
dyld`:
0x1b89c7e88 <+0>: mov x16, #0x209
0x1b89c7e8c <+4>: svc #0x80
-> 0x1b89c7e90 <+8>: b.lo 0x1b89c7eb0 ; <+40>
0x1b89c7e94 <+12>: pacibsp
0x1b89c7e98 <+16>: stp x29, x30, [sp, #-0x10]!
0x1b89c7e9c <+20>: mov x29, sp
0x1b89c7ea0 <+24>: bl 0x1b8974730 ; cerror_nocancel
0x1b89c7ea4 <+28>: mov sp, x29
0x1b89c7ea8 <+32>: ldp x29, x30, [sp], #0x10
0x1b89c7eac <+36>: retab
0x1b89c7eb0 <+40>: ret
Fix my own issues thanks to some website in Bing.com, Issues is that #PREVIEW is not supported.
//#Preview {
// ContentView()
//}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
Hi team, issues is on the preview just revert back to the hold preview type
Struct Nameofyourswift_Previews: PreviewProvider {
static var previews: some View {
//yourcode
}
}
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])
}
}
Hi @Claude31 once again I really appreciate the fact that you are supporting and helping me, I believe you bring me to the right direction as now my ad doesn't't crash anymore but somehting is strange I do not see the list of project in TimekeyinList, it's white full page. here is my updated code with as you explain only one projects [ list] creation under project,
I also have to create another project list under the previous otherwise preview cannot read the Project file....
I'm deeply lost in the UUID documentation as the UUID documentation is very very poor and swift is my first language programming therefore I do not really understand what I'm doing by init and by giving UUID, just try and get ride of the error message till it works lol.
Once again thanks for trying to { understand me, understand my problem and trying to fix my bugs.....) I wish I can return the favour one day I will make sure I can after all this learning session ^^
import Foundation
import SwiftUI
extension String { // To align text
func paddedToWidth(_ width: Int) -> String {
let length = self.count
guard length < width else {
return self
}
let spaces = Array<Character>.init(repeating: " ", count: width - length)
return self + spaces
}
}
struct TimeKeyInRow: View,Identifiable {
var id: String
var project: Project
@Binding var isSet: Bool
let listOfPossibleHours: [Double] = [0.5,1,2,3,4,8]
var body: some View {
HStack {
Spacer() // ADDED
Text(project.shortname.paddedToWidth(5)) // Text(projects[1].shortname)
.font(.custom("Menlo", size: 16)) // <<-- To get proper alignment
Spacer()
ForEach(listOfPossibleHours, id: \.self) { hour in
HourButton(id: "\(project.shortname)", isSet: isSet, value: hour)
}
Text("\(project.leftPMtime)")
.font(.custom("Menlo", size: 16)) // <<-- To get proper alignment
Spacer() // <<-- ADDED
}
}
}
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 = "" // to ease test
var projects = [Project]()
@State var isSet: [Bool] = [true, false, false, true]
@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, project: project, isSet: $isSet[row])
}
}
.labelsHidden()
.onAppear() { // <<-- Added
for (row, project) in filteredProjects.enumerated() {
isSet[row] = !project.isTeco
}
}
}
}
struct Project: Identifiable {
var id: UUID = UUID()
var name: String
var shortname: String
var leftPMtime: Int
var isTeco: Bool
var projects = [
(id: UUID.init(), name: "Project1, isTeco = false, shortname = MORN, leftPMTime = 111",shortname : "MORN", leftPMtime : 111, isTeco: false),
(id: UUID.init(), name: "Project2, isTeco = true, shortname = IFF, leftPMTime = 222",shortname : "IFF", leftPMtime : 222, isTeco: true),
(id: UUID.init(), name: "Project3, isTeco = true, shortname = FFI, leftPMTime = 333",shortname : "FFI", leftPMtime : 333, isTeco: true),
(id: UUID.init(), name: "Project4, isTeco = false, shortname = IFFCO, leftPMTime = 444",shortname : "IFFCO", leftPMtime : 444, isTeco: false)
]
init(id: UUID, name: String, shortname: String, leftPMtime: Int, isTeco: Bool) {
self.id = id
self.name = "project1"
self.shortname = "MORN"
self.leftPMtime = 131
self.isTeco = true
}
}
struct TimeKeyInRow_Previews: PreviewProvider {
static var previews: some View {
//#Preview {
var project: Project = Project(id: UUID.init(), name: "Project1", shortname: "MORN", leftPMtime: 131, isTeco: true)
TimeKeyInRow(id: "test", project: project, 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 {
var isSet: [Bool] = [true, false, false, true]
var projects = [
Project(id: UUID.init(), name: "Project1, isTeco = false, shortname = MORN, leftPMTime = 111",shortname : "MORN", leftPMtime : 111, isTeco: false),
Project(id: UUID.init(), name: "Project2, isTeco = true, shortname = IFF, leftPMTime = 222",shortname : "IFF", leftPMtime : 222, isTeco: true),
Project(id: UUID.init(), name: "Project3, isTeco = true, shortname = FFI, leftPMTime = 333",shortname : "FFI", leftPMtime : 333, isTeco: true),
Project(id: UUID.init(), name: "Project4, isTeco = false, shortname = IFFCO, leftPMTime = 444",shortname : "IFFCO", leftPMtime : 444, isTeco: false)
]
TimeKeyinList(id: "test", projects: projects, isSet: isSet)
}
}
@Claude31 , It's working my app is working now I mean what I was trying to do is working thanks for all your TIPS
now actually the only things that was not working is the previous for timekeyinList.
Do you think you can explain to me why ? is it because of this var filter project ? I have try to add it into the preview but still the issues is coming that only if I change all the project to isTECO= true and keep only one project I see the TIMEkeyinList showing one line....
import Foundation
import SwiftUI
extension String { // To align text
func paddedToWidth(_ width: Int) -> String {
let length = self.count
guard length < width else {
return self
}
let spaces = Array<Character>.init(repeating: " ", count: width - length)
return self + spaces
}
}
struct TimeKeyInRow: View,Identifiable {
var id: String
var project: Project
@Binding var isSet: Bool
let listOfPossibleHours: [Double] = [0.5,1,2,3,4,8]
var body: some View {
HStack {
Spacer() // ADDED
Text(project.shortname.paddedToWidth(5)) // Text(projects[1].shortname)
.font(.custom("Menlo", size: 16)) // <<-- To get proper alignment
Spacer()
ForEach(listOfPossibleHours, id: \.self) { hour in
HourButton(id: "\(project.shortname)", isSet: isSet, value: hour)
}
Text("\(project.leftPMtime)")
.font(.custom("Menlo", size: 16)) // <<-- To get proper alignment
Spacer() // <<-- ADDED
}
}
}
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 = "" // to ease test
var listofprojects = [
Project(name: "Project1, isTeco = true, shortname = MORN, leftPMTime = 111",shortname : "MORN", leftPMtime : 111, isTeco: true),
Project(name: "Project2, isTeco = true, shortname = IFF, leftPMTime = 222",shortname : "IFF", leftPMtime : 222, isTeco: true),
Project(name: "Project3, isTeco = true, shortname = FFI, leftPMTime = 333",shortname : "FFI", leftPMtime : 333, isTeco: true),
Project(name: "Project4, isTeco = true, shortname = IFFCO, leftPMTime = 444",shortname : "IFFCO", leftPMtime : 444, isTeco: true)
]
@State var isSet: [Bool] = [true, true, true, true]
@State private var hideTeco = false
var filteredProjects: [Project] {
// removeTECO project
listofprojects.filter { project in
!project.isTeco
}}
var body: some View {
VStack {
ForEach(Array(filteredProjects.enumerated()), id: \.offset) { (row, project) in
TimeKeyInRow(id: project.shortname, project: project, isSet: $isSet[row])
}
}
.labelsHidden()
.onAppear() {
// <<-- Added
for (row, project) in filteredProjects.enumerated() {
isSet[row] = !project.isTeco
}
}
}
}
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 project: Project = Project(id: UUID.init(), name: "Project1", shortname: "MORN", leftPMtime: 131, isTeco: true)
TimeKeyInRow(id: "test", project: project, 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 {
let projects = [
Project(name: "Project1, isTeco = true, shortname = MORN, leftPMTime = 111",shortname : "MORN", leftPMtime : 111, isTeco: true),
Project(name: "Project2, isTeco = true, shortname = IFF, leftPMTime = 222",shortname : "IFF", leftPMtime : 222, isTeco: true),
Project(name: "Project3, isTeco = true, shortname = FFI, leftPMTime = 333",shortname : "FFI", leftPMtime : 333, isTeco: true),
Project(name: "Project4, isTeco = false, shortname = IFFCO, leftPMTime = 444",shortname : "IFFCO", leftPMtime : 444, isTeco: false)
]
TimeKeyinList(listofprojects: projects, isSet: [true])
}
}
@Claude31 preview Is now working but the status here below is almost a copy past of the. above one which is kind of newby way,
do you think is there a way to reduce this code into a much simpler code ?
struct TimeKeyinList_Previews: PreviewProvider {
static var previews: some View {
//#Preview {
let listofprojects = [
Project(name: "Project1, isTeco = true, shortname = MORN, leftPMTime = 111",shortname : "MORN", leftPMtime : 111, isTeco: false),
Project(name: "Project2, isTeco = true, shortname = IFF, leftPMTime = 222",shortname : "IFF", leftPMtime : 222, isTeco: false),
Project(name: "Project3, isTeco = true, shortname = FFI, leftPMTime = 333",shortname : "FFI", leftPMtime : 333, isTeco: false),
Project(name: "Project4, isTeco = false, shortname = IFFCO, leftPMTime = 444",shortname : "IFFCO", leftPMtime : 444, isTeco: false)
]
@State var isSet: [Bool] = [true, true, true, true]
var filteredProjects: [Project] {
// removeTECO project
listofprojects.filter { project in
!project.isTeco
}}
VStack {
ForEach(Array(filteredProjects.enumerated()), id: \.offset) { (row, project) in
TimeKeyInRow(id: project.shortname, project: project, isSet: $isSet[row])
}
}
.labelsHidden()
.onAppear() {
// <<-- Added
for (row, project) in filteredProjects.enumerated() {
isSet[row] = !project.isTeco
}
}
}
}
@Claude31 now trying to put this back in my main project I believe I messsing aroung with this UUID things
I cannot parse my project anymore since I use this UUID as id instead of Int :
Before:
struct Project: Hashable, Codable,Identifiable {
var id: Int
After
struct Project: Hashable, Codable,Identifiable {
var id: UUID = UUID()
I have try to change my JSON for each project from
Before:
[
{
"name": "BM1 IFFCO JEDDAH 23",
"shortname": "IFFCO",
"leftPMtime": 123,
"id": 1001,
"isFeatured": false,
After
[
{
"name": "BM1 IFFCO JEDDAH 23",
"shortname": "IFFCO",
"leftPMtime": 123,
"id": "Project1,1001"
"isFeatured": false,
or
[
{
"name": "BM1 IFFCO JEDDAH 23",
"shortname": "IFFCO",
"leftPMtime": 123,
"id":"Project1, isTeco = false, shortname = MORN, leftPMTime = 111"
"isFeatured": false,
without any success as it seems on google UUID is a 4 bit ( apple doc too) and I aslo see this in your previous line of code here :
Project(name: "Project1, isTeco = false, shortname = MORN, leftPMTime = 131",shortname : "MORN", leftPMtime : 131, isTeco: false),
which seems new to me but maybe I'm making the like now , anyway all my attempts are failing with the following code.
MyPmV1/ModelData.swift:67: Fatal error: Couldn't parse ProjectData.json as Array<Project>:
dataCorrupted(Swift.DecodingError.Context(codingPath: [_JSONKey(stringValue: "Index 0", intValue: 0), CodingKeys(stringValue: "id", intValue: nil)], debugDescription: "Attempted to decode UUID from invalid UUID string.", underlyingError: nil))
]
sure this is due to the way I'm JSONning my data but I a link to a good website where I can learn or maybe sharing a solution would be awesome,
BTW I'm still trying to understand the difference between preview and sending to phone since we have to almost copy paste the code sometime.... In TimekeyinList for exemple