skip to Main Content

I have the following code someone presses on the Table and its displays the current time which is the arrival time of a customer.

I want to display the time they must leave by, next to the current time this will always be 1.5 hours ahead I can not work out how to do this. everything I have tried comes back with an error.

Still new to Xcode

any help would be great

    import SwiftUI

struct TimeListView: View {
    
    @State var tableOne = false
    @State var tableTwo = false
    @State var tableThree = false
    
    
    var body: some View {
        // Title
        
        VStack {
            Text("Arrival Times")
                .font(.title)
                .fontWeight(.bold)
            
            // List View
            List {
                
                // Table 1
                HStack {
                    
                    Button(action: {
                        self.tableOne.toggle()
                    }, label: {
                        Text("Table 1 -")
                    })
                    if tableOne {
                        Text(getCurrentTime())
                    }
                }
                
                // Table 2
                HStack {
                    
                    Button(action: {
                        self.tableTwo.toggle()
                    }, label: {
                        Text("Table 2 -")
                    })
                    if tableTwo {
                        Text(getCurrentTime())
                    }
                }
                // Table 3
                HStack {
                    
                    Button(action: {
                        self.tableThree.toggle()
                    }, label: {
                        Text("Table 3 -")
                    })
                    if tableThree {
                        Text(getCurrentTime())
                    }
                }
                
            }
            
        }
        
    }
}

struct TimeListView_Previews: PreviewProvider {
    static var previews: some View {
        TimeListView()
    }
}

// Get Current Time Function

func getCurrentTime() -> String {
    let dateFormatter = DateFormatter()
    dateFormatter.locale = Locale(identifier: "en_UK_POSIX")
    dateFormatter.dateFormat = "HH:mm"
    
    return dateFormatter.string(from: Date())

2

Answers


  1. you need make a date and add 1.5 hour to it, also you forgot create 3 deferent State for them.


    import SwiftUI
    
    struct ContentView: View {
        var body: some View {
            
            TimeListView()
        }
    }
    

    struct TimeListView: View {
        
        @State private var table1: Bool = false
        @State private var table2: Bool = false
        @State private var table3: Bool = false
    
        @State private var timeForShow1: String?
        @State private var timeForShow2: String?
        @State private var timeForShow3: String?
        
        var body: some View {
            
            
            VStack {
                
                Text("Arrival Times")
                    .font(.title)
                    .fontWeight(.bold)
                
                List {
                    
                    HStack {
                        
                        Text("Table 1 -")
                            .onTapGesture {
                                
                                if table1 { table1.toggle() }
                                else { timeForShow1 = getCurrentTime; table1.toggle() }
                                
                            }
                        
                        if table1 { Text(timeForShow1 ?? "not available!") }
                        
                    }
                    
                    
                    HStack {
                        
                        Text("Table 2 -")
                            .onTapGesture {
                                
                                if table2 { table2.toggle() }
                                else { timeForShow2 = getCurrentTime; table2.toggle() }
                                
                            }
                        
                        if table2 { Text(timeForShow2 ?? "not available!") }
                        
                    }
                    
                    
                    
                    HStack {
                        
                        Text("Table 3 -")
                            .onTapGesture {
                                
                                if table3 { table3.toggle() }
                                else { timeForShow3 = getCurrentTime; table3.toggle() }
                                
                            }
                        
                        if table3 { Text(timeForShow3 ?? "not available!") }
                        
                    }
                    
                    
                    
                    
                }
                
            }
            
            
        }
    }
    

    var getCurrentTime: String? {
        
        if let date = Calendar.current.date(byAdding: .minute, value: 90, to: Date()) {
            
            let dateFormatter = DateFormatter()
            dateFormatter.locale = Locale(identifier: "en_UK_POSIX")
            dateFormatter.dateFormat = "HH:mm"
            return dateFormatter.string(from: date)
            
        }
        else {
            
            return nil
        }
        
    }
    
    Login or Signup to reply.
  2. You’ll probably get a half dozen different ways of doing this but this version allows for adaptive/reusable code. You can easily add a Table by adding one to the var tables: [Table] everything would adjust automatically.

    import SwiftUI
    class Table: ObservableObject {
        let id: UUID = UUID()
        @Published var name: String
        @Published var status: Bool
        @Published var entryTime: Date
        var exitTime: Date{
            return entryTime.addingTimeInterval(60*60*1.5)
        }
        init(name: String, status: Bool = false, entryTime: Date = Date.init(timeIntervalSince1970: 0)) {
            self.name = name
            self.status = status
            self.entryTime = entryTime
        }
    }
    struct TimeListView: View {
        
        @State var tables: [Table] = [Table(name: "Table 1 -"), Table(name: "Table 2 -"), Table(name: "Table 3 -")]
        var body: some View {
            VStack{
                Text("Arrival Times")
                    .font(.title)
                    .fontWeight(.bold)
                List {
                    ForEach(tables, id: .id, content: { table in
                        TableView(table: table)
                    })
                }
            }
        }
    }
    struct TableView: View {
        @ObservedObject var table: Table
        var body: some View {
            HStack {
                Button(action: {
                    table.status.toggle()
                    table.entryTime = Date()
                }, label: {
                    Text(table.name)
                })
                if table.status{
                    Text(table.entryTime, formatter: dateFormatter)
                    Text(table.exitTime, formatter: dateFormatter)
                }
            }
        }
    }
    struct TimeListView_Previews: PreviewProvider {
        static var previews: some View {
            TimeListView()
        }
    }
    var dateFormatter: DateFormatter {
        let dateFormatter = DateFormatter()
        dateFormatter.locale = Locale(identifier: "en_UK_POSIX")
        dateFormatter.dateFormat = "HH:mm"
        
        return dateFormatter
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search