Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
14 kB
3
Indexable
Never
//
//  ComplaintsView.swift
//  Garageworks-iOS-Revamp
//
//  Created by mac on 14/12/22.
//

import SwiftUI

struct ComplaintsView: View {
    @StateObject var commonComplaint = CommonComplaintObserver(model_id: UserDefaultsManager.fetchSelectedModel()?.model_id ?? "")
    @State var selectedComplaints: [String]
    @State var selectedComplaintsId: [Int]
    @State var isHidden = false
    @State private var isSearchBarShowing: Bool = false
    @State var serviceId =  UserDefaultsManager.fetchSelectedServicePackage()?.service_id ?? ""
    
    @State private var searchComplaints: String = ""
    @Binding var manuallyEnteredComplaints : String
    
    var body: some View {
        //        var width = CGFloat.zero
        //        var height = CGFloat.zero
        GeometryReader{ geometry in
            ScrollView{
                VStack {
                    ZStack{
                        VStack(){
                            Text("Please select any vehicle complaints you have")
                                .font(Font.custom(Fonts.product_regular, size: 15))
                                .foregroundColor(Color.init(hex: Colors.color_gr2))
                                .multilineTextAlignment(.leading)
                                .frame(width:geometry.size.width*0.94,alignment: .topLeading)
                                .padding([.top], 20)
                                .padding([.leading],20)
                            
                            //Search TextField Horizontal stack
                            VStack{
                                HStack(alignment: .center, spacing: 15){
                                    Image("search-normal 1")
                                        .resizable()
                                        .frame(width: 25, height: 25)
                                        .foregroundColor(Color.init(hex: Colors.color_4a))
                                        .padding(.leading,20)
                                    CustomTextFieldPlaceHolder(placeholder: Text(Strings.label_search_complaints)
                                        .foregroundColor(Color.init(hex: Colors.color_b9))
                                        .font(Font.custom(Fonts.product_medium, size: 15)), text: $searchComplaints)
                                    .cornerRadius(10)
                                    //                                .padding(.leading,5)
                                    .lineLimit(1)
                                }
                                .frame(width:geometry.size.width*0.9, height:60, alignment: .center)
                                .background(.white)
                                .cornerRadius(10)
                                .onTapGesture {
                                    isSearchBarShowing.toggle()
                                }
                            }
                            .padding(.top,5)
                            
                            Text(Strings.label_common_complaints + "\(UserDefaultsManager.fetchSelectedModel()?.model_name ?? "")")
                                .foregroundColor(Color.init(hex: Colors.color_3f))
                                .font(Font.custom(Fonts.product_regular, size: 15))
                                .frame(width:geometry.size.width*0.94,alignment: .topLeading)
                                .padding([.leading], 10)
                                .padding([.top], 15)
                            
                            //Common Complaints Rounded Buttons
                            
                            VStack{
                                WrappingHStack(models: commonComplaint.commonComplaintsObserver ?? []) { tag in
                                    Button("\(tag.complaint_name ?? "")", action: {
                                        debugPrint("\(tag.complaint_name ?? "") pressed")
                                        
                                        if selectedComplaints.contains(tag.complaint_name ?? ""){
                                            print("Element already present")
                                        }else{
                                            ProgressView()
                                            isHidden = true
                                            selectedComplaints.append(tag.complaint_name ?? "")
                                            selectedComplaintsId.append(Int(tag.complaint_id ?? "") ?? 0)
                                            print("Appended array \(selectedComplaints)")
                                            UserDefaultsManager.storeSelectedComplaints(value: selectedComplaints)
                                            UserDefaultsManager.storeSelectedComplaintsId(value: selectedComplaintsId)
                                        }
                                    })
                                    .padding()
                                    .foregroundColor(Color.init(hex: Colors.color_wh))
                                    .font(Font.custom(Fonts.product_medium, size: 13))
                                    .frame(height: 44)
                                    .background(Color.init(hex: Colors.color_bl))
                                    .cornerRadius(20)
                                    .padding(.all,4)
                                    
                                }  .padding()
                                    .background(.white)
                                    .frame(width:geometry.size.width*0.9)
                                    .cornerRadius(20)
                            }
                            .padding(.top,5)
                            
                            
                            
                            Text(Strings.label_you_selected_following_complaints)
//                                 + "\(UserDefaultsManager.fetchSelectedModel()?.model_name ?? "")")
                                .foregroundColor(Color.init(hex: Colors.color_3f))
                                .font(Font.custom(Fonts.product_regular, size: 15))
                                .frame(width:geometry.size.width*0.94,alignment: .topLeading)
                                .padding([.leading], 10)
                                .padding([.top], 15)
                            
                            
                            //Rounded button frame for selected elements
                            
                            VStack{
                                if isHidden == false{
                                    //debugPrint("Selected list is empty")
                                    // isHidden = true
                                    Text("No Complaints selected")
                                        .font(Font.custom(Fonts.product_regular, size: 13))
                                        .foregroundColor(Color.init(hex: Colors.color_b7))
                                        .frame(width: geometry.size.width - 50, height: 50, alignment: .center)
                                        .foregroundColor(.gray)
                                        .cornerRadius(20)
                                }
                                WrappingHStack(models: selectedComplaints) { selectedTag in
                                    HStack{
                                        Text("\(selectedTag)")
                                        Button(action: {
                                            debugPrint("Items deleted \(selectedTag)")
                                            selectedComplaints = selectedComplaints.filter{$0 != "\(selectedTag)"}
                                            if(selectedComplaints.isEmpty)
                                            {
                                                isHidden = false
                                            }
                                        }, label: {
                                            Image("cancelButton")
                                        })
                                        
                                    }.font(.system(size: 13))
                                        .padding()
                                        .foregroundColor(Color.init(hex: Colors.color_wh))
                                        .font(Font.custom(Fonts.product_bold, size: 15))
                                        .frame(height: 44)
                                        .background(Color.init(hex: Colors.color_bl))
                                        .cornerRadius(20)
                                        .padding(.all,4)
                                }
                            }.padding()
                                .background(.white)
                                .frame(width:geometry.size.width*0.9)
                                .cornerRadius(20)
                                .padding(.top,5)
                            
                            if serviceId == "11" || serviceId == "2" {
                                Text("If you can’t find your complaints, please type here")
                                    .foregroundColor(Color.init(hex: Colors.color_gr2))
                                    .font(Font.custom(Fonts.product_medium, size: 15))
                                    .frame(width:geometry.size.width*0.94,alignment: .topLeading)
                                    .padding([.leading], 10)
                                    .padding([.top], 15)
                                
                                TextField("Manually Entered Complaints", text: $manuallyEnteredComplaints)
                                    .padding()
                                    .font(Font.custom(Fonts.product_regular, size: 13))
                                    .frame(width:geometry.size.width*0.9, height:60, alignment: .center)
                                    .background(.white)
                                    .cornerRadius(10)
                                    .padding([.horizontal], 10)
                                    .padding([.top], 15)
                            }
                            
                            Spacer()
                        }
                        .frame(width:geometry.size.width, height:geometry.size.height,alignment: .top)
                        .background(Color.init(hex: Colors.color_gr))
                        .cornerRadius(30, corners: [.topLeft, .topRight])
                    }
                    ComplaintsSearchAlertView(isSearchBarShowing: $isSearchBarShowing, selectedComplaintArray: $selectedComplaints)
                    //                       .frame(width: geometry.size.width - 20, height: geometry.size.height * 0.2)
                    //                       .cornerRadius(10)
                    
                }//.background()
            }
            
        }
    }
}
struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ComplaintsView(selectedComplaints: [], selectedComplaintsId: [],manuallyEnteredComplaints: .constant(""))
    }
}


//MARK: custom model of wrapped button (chips)
struct WrappingHStack<Model, V>: View where Model: Hashable, V: View {
    typealias ViewGenerator = (Model) -> V
    
    var models: [Model]
    var viewGenerator: ViewGenerator
    var horizontalSpacing: CGFloat = 2
    var verticalSpacing: CGFloat = 0
    
    @State private var totalHeight
    = CGFloat.zero       // << variant for ScrollView/List
    //    = CGFloat.infinity   // << variant for VStack
    
    var body: some View {
        VStack {
            GeometryReader { geometry in
                self.generateContent(in: geometry)
            }
        }
        .frame(height: totalHeight)// << variant for ScrollView/List
        //.frame(maxHeight: totalHeight) // << variant for VStack
    }
    
    private func generateContent(in geometry: GeometryProxy) -> some View {
        var width = CGFloat.zero
        var height = CGFloat.zero
        
        return ZStack(alignment: .topLeading) {
            ForEach(self.models, id: \.self) { models in
                viewGenerator(models)
                    .padding(.horizontal, horizontalSpacing)
                    .padding(.vertical, verticalSpacing)
                    .alignmentGuide(.leading, computeValue: { dimension in
                        if (abs(width - dimension.width) > geometry.size.width)
                        {
                            width = 0
                            height -= dimension.height
                        }
                        let result = width
                        if models == self.models.last! {
                            width = 0 //last item
                        } else {
                            width -= dimension.width
                        }
                        return result
                    })
                    .alignmentGuide(.top, computeValue: {dimension in
                        let result = height
                        if models == self.models.last! {
                            height = 0 // last item
                        }
                        return result
                    })
            }
        }.background(viewHeightReader($totalHeight))
    }
    
    private func viewHeightReader(_ binding: Binding<CGFloat>) -> some View {
        return GeometryReader { geometry -> Color in
            let rect = geometry.frame(in: .local)
            DispatchQueue.main.async {
                binding.wrappedValue = rect.size.height
            }
            return .clear
        }
    }
}