Untitled

mail@pastecode.io avatar
unknown
swift
a year ago
10 kB
1
Indexable
Never
//
//  Date&TimeSelectionView.swift
//  Garageworks-iOS-Revamp
//
//  Created by mac on 06/12/22.
//

import SwiftUI

struct Date_TimeSelectionView: View {
    @StateObject var dateAndTimeObserver:DateAndTimeObserver = DateAndTimeObserver()
    private var gridItemLayout = [GridItem(.fixed(115)), GridItem(.fixed(115)), GridItem(.fixed(115))]
    @State private var didTap:Bool = false
    @State var selectedBtnBackg: String = ""
    @State var selectedBtnForg: String = ""
    @State var dateSelectedBtnBack: String = ""
    @State var dateSelectedBtnForg: String = ""
    @State var defaultValue: String = ""
    @State var selectTimeSlot = "Please Select Slot"
    @State var showText: String = ""
    @State var isProceed :Bool = false
    @State var callDate_TimeSelectionView: Int? = nil

    var body: some View {
        GeometryReader { geometry in
            VStack(spacing:0){
                //                           VStack{
                ScrollView(.horizontal, showsIndicators: false){
                    HStack(spacing: 10){
                        ForEach(dateAndTimeObserver.availabledates?.available_dates ?? [], id: \.self) { apiDates in
                            let convertedDate = dateConverter(dateString: apiDates)
                            Button(){
                                dateAndTimeObserver.getAvailableTime(date: UserDefaultsManager.fetchSelectedDate() ?? "", city: UserDefaultsManager.fetchSelectedArea()?.city ?? "")
                                UserDefaultsManager.storeSelectedDate(value: apiDates)
                                print( UserDefaultsManager.storeSelectedDate(value: apiDates))
                                debugPrint("API response \(apiDates)")
                                self.dateSelectedBtnBack = apiDates
                                self.dateSelectedBtnForg = apiDates
                            }label: {
                                Text(convertedDate)    //date
                                    .font(Font.custom(Fonts.product_medium, size: 14))
                                    .frame(width: 50,height: geometry.size.height * 0.12)
                                    .foregroundColor(self.dateSelectedBtnForg == apiDates ? Color.white : Color.black)
                                    .background(self.dateSelectedBtnBack == apiDates ? Color.orange : Color.white)
                                    .clipShape(Capsule())
                                
                                    .onAppear{
                                        if(dateAndTimeObserver.availabledates?.available_dates[0] == apiDates){
                                            self.dateSelectedBtnBack = apiDates
                                            self.dateSelectedBtnForg = apiDates
                                            ProgressView()
                                            dateAndTimeObserver.getAvailableTime(date: UserDefaultsManager.fetchSelectedDate() ?? "", city: UserDefaultsManager.fetchSelectedArea()?.city ?? "")
                                            print("API response \(apiDates)")
                                        }
                                    }
                            }
                            if dateAndTimeObserver.showProgress {
                                ProgressView()
                                    .padding()
                            }
                            //                            .onTapGesture{
                            //                                UserDefaultsManager.storeSelectedDate(value: dateConverterInDDMM(dateString: apiDates))
                            //                                print(dateConverterInDDMM(dateString: apiDates),"Datessssssssss")
                            //                            }
                        }
                    }
                }
                .padding()
                //                           }
                .frame(width:geometry.size.width, height:geometry.size.height * 0.16,alignment: .top)
                .background(Color.init(hex: Colors.color_wh))
                .background(.green)
                
                VStack{
                    VStack{
                        ScrollView {
                            LazyVGrid(columns: gridItemLayout, spacing: 19) {
                                ForEach(dateAndTimeObserver.timeSlot?.time_slots ?? [], id: \.self) { time in
                                    Text(time.text ?? "")
                                        .font(Font.custom(Fonts.product_medium, size: 16))
                                        .frame(width:geometry.size.width * 0.27, height: geometry.size.height * 0.08)
                                        .foregroundColor(self.selectedBtnBackg == time.id ? Color.white : Color.orange)
                                        .background(self.selectedBtnBackg == time.id ? Color.orange : Color.white)
                                        .cornerRadius(10)
                                        .overlay(
                                            RoundedRectangle(cornerRadius: 10)
                                                .stroke(Color.init(hex: Colors.color_e7), lineWidth: 0.8))
                                        .onTapGesture {
                                            UserDefaultsManager.storeSelectedTime(value: time)
                                            print(UserDefaultsManager.fetchSelectedTime()!,"SELECTED TIME")
                                            if selectedBtnBackg == "" {
                                                self.didTap = true
//                                                self.isProceed = true
                                            }
                                            self.selectedBtnBackg = time.id!
                                            self.selectedBtnForg = time.id!
                                        }
                                }
                            }.padding([.top], 25)
                        }
                    }
                }.frame(width:geometry.size.width, height:geometry.size.height * 0.65,alignment: .bottom)
                    .background(Color.init(hex: Colors.color_gr))
                    .cornerRadius(30, corners: [.topLeft, .topRight])

                HStack{
                    VStack(alignment:.leading,spacing: 5){
                        Text("Price")
                            .font(Font.custom(Fonts.product_medium, size: 12))
                            .foregroundColor(Color.init(hex: Colors.color_light_gr))
                        Text("\u{20B9}"+"\(UserDefaultsManager.fetchSelectedService()?.rates ?? "")")
                            .font(.system(size: 20.0))
                            .foregroundColor(Color.init(hex: Colors.color_4a))
                    }
                    Spacer()
//                    id didTap {
//                        Button(action: {
//
//                            //                            serviceFlowObserver.serviceFlow(service_type: UserDefaultsManager.fetchService()?.service_type ?? "")
//                        }) {
                            //                            NavigationLink(destination: ComplaintsContainerView(servicePackages: servicePackages!)
                            //                                                    .navigationBarHidden(true)){
//                    VStack{
//                        NavigationLink(destination: CartView(paymentButton: false, instructionNumber: "") .navigationBarHidden(true), tag:1 ,selection: $callDate_TimeSelectionView){
//                            Text(Strings.label_next)
//                                .frame(width:geometry.size.width*0.32,height: 45)
//                                .foregroundColor(Color.init(hex: Colors.color_wh))
//                                .font(Font.custom(Fonts.product_medium, size: 15))
//                                .background(Color.init(hex: Colors.color_or))
//                                .font(Font.custom(Fonts.product_medium, size: 15))
//                                .cornerRadius(15)
//                                .onTapGesture {
//                                    if selectedBtnBackg == "" {
////                                        self.isProceed = true
////                                        self.didTap = true
//                                        self.showText = "Please Select Slot"
//                                    }
//                                    else{
//                                        callDate_TimeSelectionView = 1
//
//                                    }
//                                }
//                        }
//                    }
                    
                
            }
            .navigationBarTitle("")
            .navigationBarBackButtonHidden(true)
            .navigationBarHidden(true)
            .onAppear{
                dateAndTimeObserver.getAvailableDates(area: UserDefaultsManager.fetchSelectedArea()?.area ?? "", city: UserDefaultsManager.fetchSelectedArea()?.city ?? "")
}
            .showToast(text: $showText)
        }
        
    }
    
    func dateConverter(dateString: String) -> String {
        var dayOfTheWeekString = ""
        // Create String
        let string = dateString
        // Create Date Formatter
        let dateFormatter = DateFormatter()
        // Set Date Format
        dateFormatter.dateFormat = "yy/MM/dd"
        // Convert String to Date
        var convertedDate = dateFormatter.date(from: string)
        let dateFormatter1 = DateFormatter()
        dateFormatter1.dateFormat = "EEE\n\ndd "
        dayOfTheWeekString = dateFormatter1.string(from: convertedDate!)
        print(dayOfTheWeekString)
        return dayOfTheWeekString
    }
}

struct Date_TimeSelectionView_Previews: PreviewProvider {
    static var previews: some View {
        Date_TimeSelectionView()
    }
}