this below is the view which doesn't remove the columns after I click the delete button and it shows the data even after it is removed from the firestore unless relaunched the app.
import SwiftUI
import Firebase
struct user:Identifiable{
@State var id:String=""
@State var productnm:String=""
@State var quan:String=""
}
class mainviewmodel:ObservableObject{
@Published var use = [user]()
init(){
fetchCurrentUser()
}
func fetchCurrentUser(){
guard let uid = Firebase.Auth.auth().currentUser?.uid else {
return
}
FirebaseManager.shared.firestore.collection("users").document(uid).collection("product").getDocuments{snapshot,error in
if error==nil{
//no errors
if let snapshot = snapshot{
//update the user property in the main thread
DispatchQueue.main.async {
// //get all the documents and create user
self.use=snapshot.documents.map{ d in //map will iterate over array and perform code on each item
//create an new user for each document returned
return user(id: d.documentID,productnm: d["product name"] as? String ?? "",
quan: d["quantity"] as? String ?? "")
}
}
}
print("no data found")
}
else{
print("no data found")
}
}
}
}
struct cart: View {
@ObservedObject var model=mainviewmodel()
@ObservedObject var vm = mainviewmodel()
@State var productnm:String=""
@State var quantity:String=""
@State var address:String=""
@State var payment:String=""
var body: some View{
VStack{
HStack{
Text("Cart")
navcustom(content:
Image(systemName: "cart"),col: .white)
Button(action: {vm.fetchCurrentUser()
}, label: {navcustom(content:
Image(systemName: "repeat.circle.fill"),col: .blue)})
}
List(model.use) {item in
HStack(spacing: 16) {
Image(systemName: "person.fill")
.font(.system(size: 32))
Text("\(item.productnm)")
Text("\(item.quan)")
}
Button(action: {deleteuser(productnm: item.productnm, quan: item.quan, id: item.id)}, label: {Text("Delete")})
Button(action: {if(address==""){Text("Enter the delivery address!").foregroundColor(.red)}else{buying(productnm: item.productnm, quantity: item.quan, address: address)}
deleteuser(productnm: item.productnm, quan: item.quan, id: item.id)
}, label: {Text("BUY")})
}
Text("Delivery Address:")
TextField("", text:$address)
.foregroundColor(.white)
.frame(width: 295)
.textFieldStyle(.roundedBorder)
}
}
func deleteuser(productnm:String,quan:String,id:String){
guard let uid = Firebase.Auth.auth().currentUser?.uid else {
return
}
FirebaseManager.shared.firestore.collection("users").document(uid).collection("product").document(id).delete(){ err in
if let err = err {
print("Error removing document: \(err)")
}
else {
print("Document successfully removed!")
}
}
}
func buying(productnm:String,quantity:String,address:String){
guard let uid = FirebaseManager.shared.auth.currentUser?.uid else { return }
let userData = ["user":uid,"product name":productnm,"quantity":quantity,"address":address]
FirebaseManager.shared.firestore.collection("purchase").document(uid).collection("product").addDocument(data: userData)
{ err in
if let err = err{
print(err)
return
}
}
}
}
struct cart_Previews: PreviewProvider {
static var previews: some View {
Group {
cart()
.environment(\.colorScheme, .dark)
}
}
}
Post
Replies
Boosts
Views
Activity
This is the content view which moves down on the screen as I open it from different view.
when I click top left button to open menu.
The menu
When I click the Home option from menu whole view moves down on screen.
The whole view gets moved down on screen each time I open it.
//the content view
import SwiftUI
struct ContentView: View {
var body: some View {
NavigationView{
ScrollView{
Text("Men")
.font(.system(size: 30, weight:.bold))
.padding(.trailing, 320.0)
ScrollView(.horizontal){
HStack{
NavigationLink(destination:mentshirt()){
custom(content:Image("mentshirt"),over:"T-shirt",col: .black)
}
NavigationLink(destination:menjeans()){
custom(content:Image("menjeans"),over:"Jeans",col: .black)
}
NavigationLink(destination:menjacket()){
custom(content:Image("menjacket"),over: "Jacket",col: .black)
}
}
}
Text("Women")
.font(.system(size: 30,weight: .bold))
.padding(.trailing,280.0)
ScrollView(.horizontal){
HStack{
NavigationLink(destination:womentshirt()){
custom(content:Image("womentshirt"),over:"T-shirt",col: .black)
}
NavigationLink(destination:womenjeans()){
custom(content:Image("womenjeans"),over:"Jeans",col: .black)
}
NavigationLink(destination:womenjacket()){
custom(content:Image("womenjacket"),over:"Jacket",col: .black)
}
}
}
Text("Kids")
.font(.system(size: 30,weight: .bold))
.padding(.trailing,320.0)
ScrollView(.horizontal){
HStack{
NavigationLink(destination:kidtshirt()){
custom(content:Image("kidtshirt"),over:"T-shirt",col:.black)
}
NavigationLink(destination:kidjeans()){
custom(content:Image("kidjeans"),over:"Jeans",col: .black)
}
NavigationLink(destination:kidjacket()){
custom(content:Image("kidjacket"),over:"Jacket",col:.black)
}
}
}
}
.navigationBarBackButtonHidden(true)
.navigationBarItems(leading:
NavigationLink(
destination: clothlist()){
navcustom(content:
Image(systemName: "line.horizontal.3"),col: .white)
},
trailing:
HStack{
Image("logo")
.resizable()
.clipShape(Circle())
.shadow(color:.white,radius: 10)
.frame(width: 30, height: 30, alignment: .center)
Spacer(minLength: 80)
NavigationLink(destination: cart()){
navcustom(content:
Image(systemName: "cart"),col: .white)
}
Spacer(minLength: 15)
NavigationLink(destination: login()){
navcustom(content:Image(systemName: "person.crop.circle.fill"), col: .white)
}
}
)
}.navigationBarBackButtonHidden(true)
.ignoresSafeArea()
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
Group {
ContentView()
.environment(\.colorScheme, .dark)
}
}
}
func custom(content:Image,over:String,col:Color) -> some View {
content
.resizable()
.overlay(Text(over).font(.system(size: 20,weight: .bold)).foregroundColor(col),alignment: .bottomLeading)
.frame(width: 400, height: 500, alignment: .center)
.padding(5)
}
func navcustom(content:Image,col:Color)-> some View {
content
.resizable()
.frame(width: 30, height: 30, alignment: .center)
.foregroundColor(col)
}
//the menu view from which I open content view
import SwiftUI
struct clothlist: View {
var body: some View {
List
{
NavigationLink(destination:ContentView())
{
Text(Image(systemName: "house"))+Text(" Home")
}
Text(Image(systemName: "person"))+Text(" Men")
NavigationLink(destination:menjeans())
{
Text("Jeans")
}
NavigationLink(destination:menjacket())
{
Text("Jacket")
}
NavigationLink(destination:mentshirt())
{
Text("Tshirt")
}
Text(Image(systemName: "person"))+Text(" Women")
NavigationLink(destination:womenjeans())
{
Text("Jeans")
}
NavigationLink(destination:womenjacket())
{
Text("Jacket")
}
NavigationLink(destination:womentshirt())
{
Text("Tshirt")
}
Group{
Text(Image(systemName: "person"))+Text(" Kids")
NavigationLink(destination:kidjeans())
{
Text("Jeans")
}
NavigationLink(destination:kidjacket())
{
Text("Jacket")
}
NavigationLink(destination:kidtshirt())
{
Text("Tshirt")
}
}
}
.navigationBarBackButtonHidden(true)
}
}
struct clothlist_Previews: PreviewProvider {
static var previews: some View {
clothlist()
}
}
how to stop the view from moving down on screen on every open?
I want the details entered in textfield and password should stay even after changing the view or closing the app and the person should be logged in even after he closes the app, Can anyone help me out?
import SwiftUI
import Firebase
struct login: View {
@State var chec:String=""
@State var email:String=""
@State var navigated = false
@State var loggedin:Bool=false
@State var pass:String=""
@State private var secured:Bool=true
var body: some View
{
VStack
{
if(loggedin==true)
{
Image("logo")
.resizable()
.clipShape(Circle())
.shadow(color:.green,radius: 10)
.frame(width: 200, height: 200, alignment: .center)
}
else
{
Image("logo")
.resizable()
.clipShape(Circle())
.shadow(color:.red,radius: 10)
.frame(width: 200, height: 200, alignment: .center)
}
Spacer(minLength: 50.0)
Text(chec)
.foregroundColor(.red)
HStack
{
Text("E-mail:")
TextField("", text:$email)
.foregroundColor(.white)
.frame(width: 275)
.textFieldStyle(.roundedBorder)
.autocapitalization(.none)
.disableAutocorrection(true)
}
HStack
{
Text("Password:")
if secured
{
SecureField("",text:$pass)
.background(Color.black)
.foregroundColor(.white)
.textFieldStyle(.roundedBorder)
.autocapitalization(.none)
.disableAutocorrection(true)
}
else
{
TextField("",text: $pass)
.background(Color.black)
.foregroundColor(.white)
.textFieldStyle(.roundedBorder)
.autocapitalization(.none)
.disableAutocorrection(true)
}
Button(action: {self.secured.toggle()})
{
if secured
{
Image(systemName:"eye.slash")
}
else
{
Image(systemName:"eye")
}
}.buttonStyle(BorderlessButtonStyle())
}
VStack{
Button(action: {
if(email==""&&pass=="")
{
chec="Enter E-mail and Password"
}
else if(pass=="")
{
chec="Enter Password"
}
else if(email=="")
{
chec="Enter E-mail"
}
guard !email.isEmpty,!pass.isEmpty else
{
return
}
loginfunc()
}){
Text("Login")
}
NavigationLink(destination: signup(),label:{Text("Not Yet signed? Create new account")})
.padding(.top, 3.0)
}
.padding()
Spacer(minLength: 400)
}
}
func loginfunc(){
Auth.auth().signIn(withEmail: email, password: pass) { result, error in
if error != nil{
print(error?.localizedDescription ?? "")
loggedin=false
chec="E-mail or Password wrong!"
}else {
print("success")
loggedin=true
}
if(loggedin==true)
{
chec=""
print("wow so nice to get work done")
}
}
}
}
//}
struct ButtonView: View {
var buttext:String
var body: some View {
Text(buttext)
.frame(width: 200, height: 100, alignment: .center)
.background(Color.black)
.foregroundColor(Color.white)
}
}
struct login_Previews: PreviewProvider {
static var previews: some View {
login()
.environment(\.colorScheme, .dark)
}
}
ForEach with 2 arguments
If I try more than 2 arguments I get error that there are extra arguments.
And even tried using two for each together.
Help me how to use foreach together as I want to do it together.
import SwiftUI
struct lastview: View {
var img:UIImage
var texty:String
var body: some View {
ScrollView(.vertical){
Image(uiImage: img)
.resizable()
.frame(width: 500, height: 500, alignment: .center)
Text(texty)
.multilineTextAlignment(.leading)
}
}
}
I want the image I clicked in the first view to the last view as main image.
First View
Clicked on image
Last View
Code for it is below:
//This is the First View
import SwiftUI
struct kidtshirt: View {
var clothimages:[UIImage] =
[UIImage(named:"IMG_9981")!,
UIImage(named:"IMG_9985")!,
UIImage(named:"IMG_9987")!]
var tshirt:[String]=["PLAIN T-SHIRT\n₹590.00","PRINTED T-SHIRT\n₹790.00","JACQUARD DAISY PRINT T-SHIRT\n₹1,090.00"]
let layout=[GridItem(.flexible()),GridItem(.flexible())]
var body: some View
{
// NavigationView
// {
ScrollView(.vertical)
{
LazyVGrid(columns: layout,spacing: 2)
{
ForEach(Array(zip(clothimages,tshirt)), id: .0)
{
(imager,texter) in
NavigationLink(destination:lastview()){maincustom(content: Image(uiImage:imager), text: texter)
}
}
.navigationBarTitle("Kid Tshirt")
}
}
.navigationBarBackButtonHidden(true)
.navigationBarItems(leading:
NavigationLink(
destination: clothlist()){
navcustom(content:
Image(systemName: "line.horizontal.3"),col: .white)
})
}
//}
}
struct kidtshirt_Previews: PreviewProvider {
static var previews: some View {
kidtshirt()
}
}
//This is the Last View
import SwiftUI
struct lastview: View {
var body: some View {
Image("IMG_9981")
.resizable()
.frame(width: 500, height: 500, alignment: .center)
}
}
struct lastview_Previews: PreviewProvider {
static var previews: some View {
lastview()
}
}
import SwiftUI
struct menjeans: View {
var menje:[UIImage] =
[UIImage(named:"IMG_9917")!,
UIImage(named:"IMG_9920")!,
UIImage(named:"IMG_9923")!]
let layout=[GridItem(.flexible()),GridItem(.flexible())]
var body: some View
{
NavigationView
{
ScrollView(.vertical)
{
LazyVGrid(columns: layout,spacing: 2)
{
ForEach(menje, id: .self)
{
imager in
maincustom(content: Image(uiImage:imager), text: "df")
}
.navigationBarTitle("Men")
}
}
}
}
}
struct menjeans_Previews: PreviewProvider {
static var previews: some View {
menjeans()
}
}
I want to create a view in which if I scroll images the text below stays and the images also come over another how to make it?
iam seeing some of the images but not in every row or column any answers.
And any ideas on how to print text below every image like a description, I tried in one of them writing denim but it didn't work.
Like this:-
I get this type of error when I write any iteration statements.
the NavigationView and scrollview braces are closed below.
//
// ContentView.swift
// shop
//
// Created by Cloud Patel on 26/12/21.
//
import SwiftUI
func trying()
{
for el in clothimages
{
print(el)
}
}
struct ContentView: View {
var clothimages:[UIImage] =
[UIImage(named:"menjacket")!,
UIImage(named:"menjeans")!,
UIImage(named:"mentshirt")!]
var body: some View {
NavigationView{
ScrollView{
Text("Men")
.font(.system(size: 30, weight:.bold))
.padding(.trailing, 320.0)
ScrollView(.horizontal){
HStack{ //this is where I get error //Type '()' cannot conform to 'View'
trying()
// custom(content:Image("mentshirt"),over:"T-shirt",col: .black)
// custom(content:Image("menjeans"),over:"Jeans",col: .black)
// custom(content:Image("menjacket"),over: "Jacket",col: .black)
}
}
Text("Women")
.font(.system(size: 30,weight: .bold))
.padding(.trailing,280.0)
ScrollView(.horizontal){
HStack{
custom(content:Image("womentshirt"),over:"T-shirt",col: .black)
custom(content:Image("womenjeans"),over:"Jeans",col: .black)
custom(content:Image("womenjacket"),over:"Jacket",col: .black)
}
}
Text("Kids")
.font(.system(size: 30,weight: .bold))
.padding(.trailing,320.0)
ScrollView(.horizontal){
HStack{
NavigationLink(destination:login()){
custom(content:Image("kidtshirt"),over:"T-shirt",col:.black)
}
custom(content:Image("kidjeans"),over:"Jeans",col: .black)
custom(content:Image("kidjacket"),over:"Jacket",col:.black)
}
}
}
.navigationBarItems(leading:
NavigationLink(
destination: edit()){
navcustom(content:
Image(systemName: "line.horizontal.3"),col: .white)
},
trailing:
HStack{
navcustom(content:
Image(systemName: "indianrupeesign.circle.fill"),col: .white)
Spacer(minLength: 80)
NavigationLink(destination: cart()){
navcustom(content:
Image(systemName: "cart"),col: .white)
}
Spacer(minLength: 15)
NavigationLink(destination: logger()){
navcustom(content:Image(systemName: "person.crop.circle.fill"), col: .white)
}
}
)
}
}
}
func logger() -> some View{
var userlogg=false
if userlogg{
return AnyView(settings())
}
else{
return AnyView(login())
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
Group {
ContentView()
// .environment(.colorScheme, .dark)
}
}
}
func custom(content:Image,over:String,col:Color) -> some View {
content
.resizable()
.overlay(Text(over).font(.system(size: 20,weight: .bold)).foregroundColor(col),alignment: .bottomLeading)
.frame(width: 400, height: 500, alignment: .center)
.padding(5)
}
func navcustom(content:Image,col:Color)-> some View {
content
.resizable()
.frame(width: 30, height: 30, alignment: /@START_MENU_TOKEN@/.center/@END_MENU_TOKEN@/)
.foregroundColor(col)
}
//class watchlog:ObservableObject{
// @Published var userlogg=false
//}
//struct logging{
// func logger() -> some View{
// var userlogg=true
// if userlogg{
// return AnyView(settings())
// }
// else{
// return AnyView(login())
// }
// }
//}
// even if I use the function a structure I get this error
struct nonsense:View{
var body: some View
{
func trying(). //Closure containing a declaration cannot be used with result builder 'ViewBuilder'
{
for el in clothimages
{
print(el)
}
}
}
}
Write a Swift program to test whether a value presents sequentially three times in an array of integers or not Without using in-built function.