Wednesday, November 4, 2026
HomeiOS Developmentios - Swiftui filtered record will not present button if worth doesn't...

ios – Swiftui filtered record will not present button if worth doesn’t exist

[ad_1]

Query: so, making an attempt to provide the consumer to submit a username if it does not exist within the firebase database. Presently in the event that they do have a username within the present database the choice to alter it does present up on my display screen. However, the choice so as to add a username doesn’t present up if the consumer has by no means given a username. Been looking for a means round this, however I am unable to determine it out. My present code:

struct ProfileInfo: Identifiable, Codable {
var id: String = UUID().uuidString
var userID: String
var userName: String
var exists: Bool
}


struct Settings: View {
let exampleColor : Coloration = Coloration(purple: 150/255, inexperienced: 185/255, blue: 208/255)
@AppStorage("log_Status") var log_Status = true
@State var userID = ""
@State var userName = ""
@State var exists = false
@ObservedObject var mannequin = ListViewModel()
var temp = false
let uidx = Auth.auth().currentUser?.uid
let x = ""
var physique: some View {
    if log_Status{
        ZStack{
            Picture("Wallpaper10").resizable().edgesIgnoringSafeArea(.all)
            
            VStack{
                Part{
                    Type{
                        Group{
                            Textual content("Your present profile identify: " )
                            Checklist( mannequin.listTwo.filter({  $0.userID.incorporates(String(describing: uidx)
                            )
                            })) {
                                ebook in
                                Textual content(ebook.userName).font(.headline).body(maxWidth: .infinity, alignment: .heart).font(Font.headline.weight(.semibold))
                                
                                if ebook.exists == false {
                                    TextField("Add your profile identify", textual content: $userName).textFieldStyle(RoundedBorderTextFieldStyle()).multilineTextAlignment(.heart)
                                    Button(motion: {
                                        mannequin.addProfileData(userID:  String(describing: uidx), userName: userName, exists: true)
                                        userID = ""
                                        userName = ""
                                        exists = true
                                    }, label: {
                                        Textual content("Submit change").body(maxWidth: .infinity, alignment: .heart).font(Font.headline.weight(.semibold))
                                    })
                                }
                                else if ebook.exists == true {
                                    TextField("Replace your consumer identify", textual content: $userName).textFieldStyle(RoundedBorderTextFieldStyle()).multilineTextAlignment(.heart)
                                    Button(motion: {
                                        mannequin.updateClub(userID:  String(describing: uidx), userName: userName)
                                        
                                    }, label: {
                                        Textual content("Submit change").body(maxWidth: .infinity, alignment: .heart).font(Font.headline.weight(.semibold))
                                    })
                                } else if userID.isEmpty {
                                    Textual content("hi")
                                }
                            }
                        }
                    }}
                if mannequin.listTwo.isEmpty {
                    Textual content("Empty")
                }
                Button("Logout")
                {
                    GIDSignIn.sharedInstance.signOut()
                    strive? Auth.auth().signOut()
                    withAnimation{
                        log_Status = false
                    }
                }
            }
        }
        
    }
}


    func getDataTwo() {
    let db = Firestore.firestore()
    db.assortment("profileInfo").getDocuments { snapshot, error in
        if error == nil {
            if let snapshot = snapshot {
                DispatchQueue.fundamental.async{
                    
                    self.listTwo = snapshot.paperwork.map { d in
                        return ProfileInfo(id: d.documentID,
                                           userID: d["userID"] as? String ?? "",
                                           userName: d["userName"] as? String ?? "",
                                           exists: d["exists"] as? Bool ?? true
                        )
                    }}
            }
        }
        else{
        }
    }}


func addProfileData(userID: String, userName: String, exists: Bool){
    let db = Firestore.firestore()
    db.assortment("profileInfo").addDocument(information: ["userID": userID, "userName": userName, "exists": exists]){
        error in
        if error == nil {
            
        }
        else{
            self.getDataTwo()
            
        }
    }
}


 func checkUser(userId: String) async throws -> Bool {
    let db = Firestore.firestore()
    let querySnapshot = strive await db.assortment("profileInfo")
        .whereField("userId", isEqualTo: userId).getDocuments()
    if querySnapshot.isEmpty {
        return false
    } else {
        for doc in (querySnapshot.paperwork) {
            if doc.information()["userId"] != nil {
                return true
            }
        }
        return false
    }
}

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments