ios – Why the counter could not activate within the product cancellation card after clicking on including to cart?

0
17
ios – Why the counter could not activate within the product cancellation card after clicking on including to cart?


After the consumer cancels the order, a card of the canceled product seems in entrance of him, which exhibits this product and the add to cart button. After clicking on the cart, the counter ought to activate, and the product must be added to the cart, however this doesn’t occur. It’s added, however the counter doesn’t seem



import SwiftUI
import Shimmer

struct CustomButton: View {
  //    @EnvironmentObject var cartVM: CartVM
  @EnvironmentObject var home windows: Home windows
  
  var textual content: String
  var peak: Double
  var productId: Int? = nil
  var colorChanging: Bool = false
  var motion: () -> Void
  
  var physique: some View {
    Button(motion: {
      if textual content == "В корзину" && productId != nil{
        motion()
      } else {
        motion()
      }
    }) {
      ZStack  textual content == "Повторить звонок" ? Colour("blackEA") : AppColors.lightGray)
        
        Textual content(textual content)
          .mediumSFText(fontSize: 13)
          .foregroundColor(!colorChanging ? .white : textual content == "Получить новый код" 
    }
    .body(peak: peak)
  }
}

struct GrayCustomButton: View {
  var textual content: String
  var peak: Double
  var motion: () -> Void
  
  var physique: some View {
    Button(motion: motion) {
      ZStack {
        RoundedRectangle(cornerRadius: 4)
          .fill(AppColors.blue)
          .body(peak: peak)
        
        Textual content(textual content)
          .mediumSFText(fontSize: 13)
          .foregroundColor(Colour.black)
      }
    }
  }
}

struct ActiveCheckCircle: View {
  var physique: some View {
    Circle()
      .strokeBorder(AppColors.blue, lineWidth: 5)
      .background(Circle().foregroundColor(Colour.white))
      .body(width: 20, peak: 20)
      .padding()
  }
}

struct UnActiveCheckCircle: View {
  var physique: some View {
    Circle()
      .strokeBorder(AppColors.darkGray, lineWidth: 1)
      .background(Circle().foregroundColor(Colour.white))
      .body(width: 20, peak: 20)
      .padding()
  }
}

struct MyButtonStyle: ButtonStyle {
  public func makeBody(configuration: MyButtonStyle.Configuration) -> some View {
    configuration.label
      .opacity(configuration.isPressed ? 1 : 1)
  }
}


struct IconButton: View {
  var icon: Picture
  var width: Double
  var peak: Double
  var motion: () -> Void
  
  var physique: some View {
    Button(motion: motion) {
      ZStack {
        RoundedRectangle(cornerRadius: 4)
          .fill(AppColors.lightGray)
          .body(width: width, peak: peak)
        
        icon
      }
    }
  }
}

enum AddToCartButtonTypes {
  case easy
  case withQuantity
}

struct AddToCartButton: View {
  @EnvironmentObject personal var appVM: AppViewModel
  @EnvironmentObject personal var home windows: Home windows
  
  var productId: Int
  var display: ECommerceScreenCustom
  personal var kind: AddToCartButtonTypes
  personal var width: CGFloat
  personal var peak: CGFloat
  personal var plusMinusButtonsWidth: CGFloat = 32
  personal var plusMinusButtonsHeight: CGFloat = 32
  personal var label: String
  personal var completion: (()->())?
  @State personal var addedToCart = false
  
  // 108 - 32
  // 95 - 32 (деталка товара над таббаром)
  
  init(productId: String, display: ECommerceScreenCustom = DefaultECommerceScreen(), kind: AddToCartButtonTypes = .easy, label: String = "В корзину", width: CGFloat = 108, peak: CGFloat = 32, completion: @escaping() -> () = {}) {
    self.productId = Int(productId) ?? 0
    self.kind = kind
    self.width = width
    self.peak = peak
    self.label = label
    self.completion = completion
    self.display = display
  }
  
  init(productId: Int, display: ECommerceScreenCustom = DefaultECommerceScreen(), kind: AddToCartButtonTypes = .withQuantity, label: String = "В корзину", width: CGFloat = 108, peak: CGFloat = 32, plusMinusButtonsWidth: CGFloat = 32, plusMinusButtonsHeight: CGFloat = 32, completion: @escaping() -> () = {}) {
    self.productId = productId
    self.kind = kind
    self.width = width
    self.peak = peak
    self.label = label
    self.plusMinusButtonsWidth = plusMinusButtonsWidth
    self.plusMinusButtonsHeight = plusMinusButtonsHeight
    self.completion = completion
    self.display = display
  }
  
  var physique: some View {
    Group {
      swap kind {
      case .easy:
        simpleButton
      case .withQuantity:
        buttonWithQuantity
      }
    }
    .onAppear {
      addedToCart = inCart
    }
  }
  
  personal var simpleButton: some View {
    ZStack {
      RoundedRectangle(cornerRadius: 4)
        .fill(AppColors.blue)
        .body(width: width, peak: peak)
      
      Textual content(label)
        .medium(fontSize: 13)
        .foregroundColor(Colour.white)
    }
    .contentShape(Rectangle())
    .onTapGesture {
      addToCart()
    }
  }
  
  personal func addToCart() {
    appVM.showLoadingScreen = true
    
    appVM.cartVM.basketAddProduct(productId: productId) { response in
      appVM.showLoadingScreen = false
      swap response {
      case .success(_):
        addedToCart = true
        
        completion?()
        
        let button = Button(motion: {
          appVM.controllers.setTabBar()
          home windows.switchTo(4)
        }, label: {
          Textual content("Показать")
            .medium(fontSize: 13)
            .foregroundColor(AppColors.blue)
        })
        
        // Тост с переходом в корзину
        appVM.showToast(withMessage: "Товар добавлен в корзину", toastButton: AnyView(button))
        
        appVM.cartVM.getProducts(withLoadingScreen: false, fiasId: appVM.selectedCity.fiasID) {_ in
          sendAnalytics()
        }
      case .failure(_):
        appVM.showToast(withMessage: "Не удалось добавить товар в корзину")
      }
    }
  }
  
  personal func sendAnalytics() {
    guard let product = (appVM.cartVM.merchandise + appVM.cartVM.missingProducts).first(the place: { $0.id == productId }) else { return }
    
    var parameters = [String: Any]()
    parameters["AnalyticsParameterValue"] = (product.costs?.additionalPrice ?? 0.0) * (product.basket?.amount ?? 0.0)
    parameters["AnalyticsParameterPrice"] = product.costs?.value
    parameters["AnalyticsParameterCurrency"] = "₽"
    parameters["AnalyticsParameterItemID"] = productId
    parameters["AnalyticsParameterItemName"] = product.title
    parameters["AnalyticsParameterQuantity"] = amount
    AnalyticsManager.sharedInstance.sendAddingProductToBasketEvent(with: parameters)
    
    AppMetricaECommerceCartChagesHelper().addToCartEvent(display: DefaultECommerceScreen(), product: product, rely: 1)
  }
  
  // BUTTON WITH QUANTITY
  personal var inCart: Bool {
    (appVM.cartVM.merchandise + appVM.cartVM.missingProducts).map({ $0.id }).comprises(productId)
    //        || addedToCart
  }
  
  personal var amount: String {
    guard let product = (appVM.cartVM.merchandise + appVM.cartVM.missingProducts).first(the place: { $0.id == productId }) else {
      return ""
    }
    return product.basket?.format?.amount ?? ""
  }
  
  personal var availableQuantity: Int {
    guard let product = (appVM.cartVM.merchandise + appVM.cartVM.missingProducts).first(the place: { $0.id == productId }) else {
      return 999
    }
    return 999//product.availableQuantity ?? 999
  }
  
  personal var buttonWithQuantity: some View {
    ZStack {
      if appVM.cartVM.dataIsLoading {
        shimmering
      } else if inCart {
        quantityView
      } else {
        simpleButton
      }
    }
  }
  
  personal var shimmering: some View {
    RoundedRectangle(cornerRadius: 4)
      .fill(AppColors.grayText)
      .shimmering()
      .body(width: width, peak: peak)
  }
  
  personal var quantityView: some View {
    HStack {
      minusButton
      
      Textual content("(amount)")
        .medium(fontSize: 13)
        .foregroundColor(AppColors.black)
        .body(minWidth: 28)
        .withIdentifier("amount.(amount)", in: "CartButtonView")
        .onTapGesture {
          appVM.cartVM.quantityChangingProductId = productId
        }
      
      plusButton
    }
    .body(minWidth: width)
  }
  
  personal var plusButton: some View {
    ZStack {
      RoundedRectangle(cornerRadius: 4)
        .fill(Colour("primary200"))
      
      Textual content("+")
        .daring(fontSize: 16)
        .foregroundColor(AppColors.blue)
    }
    .body(width: plusMinusButtonsWidth, peak: plusMinusButtonsHeight)
    .withIdentifier("plusButton", in: "CartButtonView")
    .contentShape(Rectangle())
    .onTapGesture {
      appVM.cartVM.changedProductId = productId
      for index in 0.. 0 {
            appVM.cartVM.merchandise[index].basket?.amount -= 1.0
            appVM.cartVM.changedQuantity = -1//Int(appVM.cartVM.merchandise[index].basket?.amount ?? 0.0)
            if appVM.cartVM.merchandise[index].basket?.amount == 0 {
              addedToCart = false
            }
          }
          break
        }
      }
    }
  }
}

I seemed via all the things rigorously, however I nonetheless don’t perceive what the issue might be

LEAVE A REPLY

Please enter your comment!
Please enter your name here