From 4c69628a94d7948ab93301ddb23f8944f6c0a7d7 Mon Sep 17 00:00:00 2001 From: BilalKhanWDI Date: Wed, 31 Jul 2024 19:42:51 +0530 Subject: [PATCH] - Updated get user data response for avatar image - updated cart icon in whole shop module , myoders - Finalsied the payment gateway response --- .../Address/Controller/PaymentWebViewVC.swift | 2 + WOKA/Authentication/Model/UserDataDM.swift | 2 + WOKA/Authentication/ViewModel/LoginVM.swift | 2 +- WOKA/Cart/CartDataCache.swift | 54 ++--- WOKA/Constants K/OnBoardVM.swift | 2 +- WOKA/Network Adapter/APIEndPoints.swift | 1 + WOKA/Network Adapter/NetworkManager.swift | 2 +- .../ViewModel/SplashVM.swift | 2 +- WOKA/Shop/Controller/ProductDetailsVC.swift | 18 +- WOKA/Shop/Controller/ShopCategoryVC.swift | 45 +---- WOKA/Shop/Controller/ShopListingVC.swift | 190 ++++++------------ WOKA/Shop/Controller/ShopProductsVC.swift | 49 +---- WOKA/Shop/Model/ShopProductsListingDM.swift | 139 +++++++------ WOKA/Shop/Shop.storyboard | 36 +++- WOKA/Shop/ViewModel/ProductDetailsVM.swift | 134 +++++++++++- WOKA/Shop/ViewModel/ShopCategoryVM.swift | 16 +- WOKA/Shop/ViewModel/ShopListingVM.swift | 33 +-- WOKA/Shop/ViewModel/ShopProductsVM.swift | 16 +- WOKA/SideBarNav/Controller/MyOrdersVC.swift | 5 + WOKA/SideBarNav/ViewModel/MyOrdersVM.swift | 29 ++- .../ViewModel/SideMenuVM.swift | 6 +- WOKA/Theme/Controller/RadioVC.swift | 2 +- WOKA/Theme/ViewModel/ThemeOneVM.swift | 6 +- WOKA/Theme/ViewModel/ThemeTwoVM.swift | 6 +- 24 files changed, 446 insertions(+), 351 deletions(-) diff --git a/WOKA/Address/Controller/PaymentWebViewVC.swift b/WOKA/Address/Controller/PaymentWebViewVC.swift index bd19990..b9eee13 100644 --- a/WOKA/Address/Controller/PaymentWebViewVC.swift +++ b/WOKA/Address/Controller/PaymentWebViewVC.swift @@ -20,6 +20,8 @@ class PaymentWebViewVC: UIViewController { let request = URLRequest(url: link) webView.load(request) } + + //Observer to check the url changes webView.addObserver(self, forKeyPath: "URL", options: .new, context: nil) } diff --git a/WOKA/Authentication/Model/UserDataDM.swift b/WOKA/Authentication/Model/UserDataDM.swift index 8abab5b..64d4d26 100644 --- a/WOKA/Authentication/Model/UserDataDM.swift +++ b/WOKA/Authentication/Model/UserDataDM.swift @@ -18,6 +18,7 @@ struct UserDataDM: Codable { // let gender: Gender? let birthdate, email: String? let avtar: String? + let avtarURL : String? var userType: String? let languageMasterID: Int? let lastLogin, rememberToken: String? @@ -27,6 +28,7 @@ struct UserDataDM: Codable { let isDeactive : Bool? enum CodingKeys: String, CodingKey { case id, username, fullname, birthdate, email, avtar + case avtarURL = "avtar_url" // case gender case userType = "user_type" case languageMasterID = "language_master_id" diff --git a/WOKA/Authentication/ViewModel/LoginVM.swift b/WOKA/Authentication/ViewModel/LoginVM.swift index 70da478..d2650b2 100644 --- a/WOKA/Authentication/ViewModel/LoginVM.swift +++ b/WOKA/Authentication/ViewModel/LoginVM.swift @@ -194,7 +194,7 @@ class LoginVM{ Utilities.dismissProgressHUD() guard let dataUser = data.data else{return} self.vc.toast(msg: data.message ?? "Unrecognised error" , time: 2) { - let userDataConverted = UserDataDM.ResultData(id: nil, username: dataUser.username, fullname: dataUser.fullname, birthdate: nil, email: nil, avtar: nil, userType: "3", languageMasterID: nil, lastLogin: nil, rememberToken: nil, childDetail: nil, language: nil, alreadyLoggedIn: nil, isDeactive: nil) + let userDataConverted = UserDataDM.ResultData(id: nil, username: dataUser.username, fullname: dataUser.fullname, birthdate: nil, email: nil, avtar: nil, avtarURL: nil, userType: "3", languageMasterID: nil, lastLogin: nil, rememberToken: nil, childDetail: nil, language: nil, alreadyLoggedIn: nil, isDeactive: nil) AuthFunc.shareInstance.loginDefaults(data: userDataConverted) } default: diff --git a/WOKA/Cart/CartDataCache.swift b/WOKA/Cart/CartDataCache.swift index 968b22c..fc82bfd 100644 --- a/WOKA/Cart/CartDataCache.swift +++ b/WOKA/Cart/CartDataCache.swift @@ -12,15 +12,11 @@ class CartDataCache{ static var cartListData = [CartListingDM.ResultData](){ didSet{ - if cartListData.count == 0{ - cartBadgeLabel.text = "0" - cartBadgeLabel.isHidden = true - }else{ cartBadgeLabel.text = cartListData.count.toString() cartBadgeLabel.isHidden = false - } } } + static var isFetched = false // static var cartCount = 0 { // didSet{ @@ -33,11 +29,12 @@ class CartDataCache{ // } // } // } + static var addressData = [AddressListDM]() static let cartBadgeLabel = UILabel() static let shareInstance = CartDataCache() - func getCartList(vc : UIViewController){ + func getCartList(vc : UIViewController, onCompletion : @escaping (Bool) -> Void){ let headers : HTTPHeaders = ["access-token" : AuthFunc.shareInstance.getAccessToken()] NetworkManager.shareInstance.apiRequest(url: APIEndPoints.Cart.cart_listing, method: .get,headers: headers, queue : QueueHelper.background) { (result : Result, NetworkManager.APIError>) in @@ -49,68 +46,77 @@ class CartDataCache{ Error */ Utilities.dismissProgressHUD() + onCompletion(false) case 1: Utilities.dismissProgressHUD() guard let data = data.data?.result else{return} CartDataCache.cartListData = data CartDataCache.isFetched = true + CartDataCache.cartBadgeLabel.text = data.count.toString() + onCompletion(true) default: Utilities.dismissProgressHUD() + onCompletion(false) break } case .failure(let error): Utilities.dismissProgressHUD() Utilities.alert(title: "Error", message: error.localizedDescription, viewController: vc) + onCompletion(false) } } } } -class BadgeBarBtn{ - +class BadgeBarBtn { static let shared = BadgeBarBtn() - - private init() {} // Prevents others from using the default '()' initializer - - public var tapAction: (() -> Void)? - + func setupBadge(for button: UIButton, with text: String) { - let badgeLabel = CartDataCache.cartBadgeLabel + var badgeLabel = CartDataCache.cartBadgeLabel + + if let existingLabel = button.viewWithTag(999) as? UILabel { + badgeLabel = existingLabel + } else { + badgeLabel = UILabel() + badgeLabel.tag = 999 + button.addSubview(badgeLabel) + } badgeLabel.frame = CGRect(x: 30, y: 0, width: 18, height: 18) badgeLabel.backgroundColor = UIColor.red - badgeLabel.isHidden = false // Ensure it is not hidden +// badgeLabel.isHidden = false // Ensure it is not hidden badgeLabel.clipsToBounds = true badgeLabel.layer.cornerRadius = 9 badgeLabel.textColor = UIColor.white badgeLabel.font = FontCustom.shareInstance.customFont(fontName: .Exo2_Regular, size: 10) badgeLabel.textAlignment = .center badgeLabel.text = text - + // Add the badge label to the button if not already added if badgeLabel.superview != button { button.addSubview(badgeLabel) } - + // Ensure the badge label is brought to the front button.bringSubviewToFront(badgeLabel) } - func setupCartButton() -> UIBarButtonItem { + func setupCartButton(target: Any?, action: Selector) -> UIBarButtonItem { let filterBtn = UIButton(frame: CGRect(x: 0, y: 0, width: 45, height: 45)) filterBtn.setImage(UIImage(named: "CartIcon"), for: .normal) - filterBtn.addTarget(self, action: #selector(buttonPressed), for: .touchUpInside) - + filterBtn.addTarget(target, action: action, for: .touchUpInside) + // Configure the badge label setupBadge(for: filterBtn, with: CartDataCache.cartListData.count.toString()) - + return UIBarButtonItem(customView: filterBtn) } - @objc private func buttonPressed() { - if let action = tapAction { - action() + func updateBadge(for button: UIButton, with text: String) { + if let badgeLabel = button.viewWithTag(999) as? UILabel { + badgeLabel.text = text + badgeLabel.isHidden = text.isEmpty } } } diff --git a/WOKA/Constants K/OnBoardVM.swift b/WOKA/Constants K/OnBoardVM.swift index f971d58..74620d6 100644 --- a/WOKA/Constants K/OnBoardVM.swift +++ b/WOKA/Constants K/OnBoardVM.swift @@ -134,7 +134,7 @@ class OnBoardVM{ guard let dataUser = data.data else{return} self.vc.toast(msg: data.message ?? "Unrecognised error" , time: 2) { - let userDataConverted = UserDataDM.ResultData(id: nil, username: dataUser.username, fullname: dataUser.fullname, birthdate: nil, email: nil, avtar: nil, userType: "3", languageMasterID: nil, lastLogin: nil, rememberToken: nil, childDetail: nil, language: nil, alreadyLoggedIn: nil, isDeactive: nil) + let userDataConverted = UserDataDM.ResultData(id: nil, username: dataUser.username, fullname: dataUser.fullname, birthdate: nil, email: nil, avtar: nil, avtarURL: nil, userType: "3", languageMasterID: nil, lastLogin: nil, rememberToken: nil, childDetail: nil, language: nil, alreadyLoggedIn: nil, isDeactive: nil) AuthFunc.shareInstance.loginDefaults(data: userDataConverted) } default: diff --git a/WOKA/Network Adapter/APIEndPoints.swift b/WOKA/Network Adapter/APIEndPoints.swift index 886ac33..8ddd7e5 100644 --- a/WOKA/Network Adapter/APIEndPoints.swift +++ b/WOKA/Network Adapter/APIEndPoints.swift @@ -114,6 +114,7 @@ struct APIEndPoints { static let sub_category_listing = makeURL(path: "sub_category_listing") static let shop_product_listing = makeURL(path: "shop_product_listing") static let shop_product_listing_v2 = makeURL(path: "v2/shop_product_listing") + static let shop_product_view = makeURL(path: "shop_product_view") } struct Cart{ diff --git a/WOKA/Network Adapter/NetworkManager.swift b/WOKA/Network Adapter/NetworkManager.swift index de37363..cfc518d 100644 --- a/WOKA/Network Adapter/NetworkManager.swift +++ b/WOKA/Network Adapter/NetworkManager.swift @@ -50,7 +50,7 @@ class NetworkManager{ let loginCred = getLoginIDPass() // Execute the request on the specified queue queue.async { - AF.request(url, method: method, parameters: parameters, encoding: encoding, headers: headers, requestModifier: { $0.timeoutInterval = 30 }) + AF.request(url, method: method, parameters: parameters, encoding: encoding, headers: headers, requestModifier: { $0.timeoutInterval = 20 }) .authenticate(username: loginCred.0, password: loginCred.1) .validate(statusCode: 200..<300) .responseDecodable(of: T.self) { response in diff --git a/WOKA/OnBoarding Module/ViewModel/SplashVM.swift b/WOKA/OnBoarding Module/ViewModel/SplashVM.swift index a2840e0..5ff218f 100644 --- a/WOKA/OnBoarding Module/ViewModel/SplashVM.swift +++ b/WOKA/OnBoarding Module/ViewModel/SplashVM.swift @@ -65,7 +65,7 @@ class SplashVM{ func getUserData(){ if AuthFunc.shareInstance.getUserType() == 3{ //setusertype - AuthFunc.shareInstance.userData = UserDataDM.ResultData(id: nil, birthdate: nil, email: nil, avtar: nil, userType: "3", languageMasterID: nil, lastLogin: nil, rememberToken: nil, childDetail: nil, language: nil, alreadyLoggedIn: nil, isDeactive: nil) + AuthFunc.shareInstance.userData = UserDataDM.ResultData(id: nil, birthdate: nil, email: nil, avtar: nil, avtarURL: nil, userType: "3", languageMasterID: nil, lastLogin: nil, rememberToken: nil, childDetail: nil, language: nil, alreadyLoggedIn: nil, isDeactive: nil) UIApplication.setRootView(SideMenuController.instantiate(from: .Home)) return } diff --git a/WOKA/Shop/Controller/ProductDetailsVC.swift b/WOKA/Shop/Controller/ProductDetailsVC.swift index 61e392e..dad2d70 100644 --- a/WOKA/Shop/Controller/ProductDetailsVC.swift +++ b/WOKA/Shop/Controller/ProductDetailsVC.swift @@ -18,6 +18,9 @@ class ProductDetailsVC: UIViewController { @IBOutlet weak var pageControl: UIPageControl! @IBOutlet weak var productImageCV: UICollectionView! @IBOutlet weak var shadowView: UIView! + @IBOutlet weak var noDataStack: UIStackView! + @IBOutlet weak var retryBtn: UIButton! + @IBOutlet weak var scrollView: UIScrollView! var vm = ProductDetailsVM() @@ -32,6 +35,11 @@ class ProductDetailsVC: UIViewController { override func viewWillAppear(_ animated: Bool) { self.navigationController?.setNavigationBarHidden(false, animated: animated) + + // Update the badge label with the current cart data + if let button = vm.cartButton.customView as? UIButton { + BadgeBarBtn.shared.updateBadge(for: button, with: CartDataCache.cartListData.count.toString()) + } } override func viewDidAppear(_ animated: Bool) { @@ -60,10 +68,18 @@ class ProductDetailsVC: UIViewController { vm.addToCart(shopMasterID: shopMasterID) } }else{ - print("View") + if let shopMasterID = vm.productDetails?.id{ + vm.removeItemFromCart(shopMasterID: shopMasterID) + } } } + + @IBAction func retryBtnTapped(_ sender: UIButton) { + if let productID = vm.productID{ + vm.getProductDetails(productID: productID) + } + } } // MARK: - Collection delegate and datasource diff --git a/WOKA/Shop/Controller/ShopCategoryVC.swift b/WOKA/Shop/Controller/ShopCategoryVC.swift index 4d87ec5..c574a6c 100644 --- a/WOKA/Shop/Controller/ShopCategoryVC.swift +++ b/WOKA/Shop/Controller/ShopCategoryVC.swift @@ -19,50 +19,15 @@ class ShopCategoryVC: UIViewController { vm.initView() navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default) navigationController?.navigationBar.shadowImage = UIImage() - -// // Create the button -// let backbutton = UIButton(type: .custom) -// backbutton.setImage(UIImage(named: "CartIcon"), for: .normal) // Image can be downloaded from the provided link -// backbutton.setTitleColor(.black, for: .normal) // You can change the TitleColor -// backbutton.addTarget(self, action: #selector(cartBtnTapped), for: .touchUpInside) -// -// // Set button height and width -// let buttonHeight: CGFloat = 50 -// let buttonWidth: CGFloat = 50 -// backbutton.translatesAutoresizingMaskIntoConstraints = false -// backbutton.widthAnchor.constraint(equalToConstant: buttonWidth).isActive = true -// backbutton.heightAnchor.constraint(equalToConstant: buttonHeight).isActive = true -// -// // Create a container view for the button -// let containerView = UIView(frame: CGRect(x: 0, y: 0, width: buttonWidth, height: buttonHeight)) -// containerView.addSubview(backbutton) -// -// // Set constraints for the button inside the container view -// NSLayoutConstraint.activate([ -// backbutton.centerYAnchor.constraint(equalTo: containerView.centerYAnchor), -// backbutton.centerXAnchor.constraint(equalTo: containerView.centerXAnchor) -// ]) -// -// // Create a UIBarButtonItem with the custom view -// let customBarButton = UIBarButtonItem(customView: containerView) -// -// // Create a flexible space item to push the custom view to the left -// let flexibleSpace = UIBarButtonItem(barButtonSystemItem: .fixedSpace, target: nil, action: nil) -// flexibleSpace.width = 10 -// // Create a negative spacer to fine-tune the position -// let negativeSpacer = UIBarButtonItem(barButtonSystemItem: .fixedSpace, target: nil, action: nil) -// negativeSpacer.width = 10 // Adjust this value to move the button to the left -// -// // Add the custom bar button and the spacer to the navigation bar -// self.navigationItem.rightBarButtonItems = [flexibleSpace,negativeSpacer, customBarButton] - } - - @objc func cartBtnTapped(){ - print("cart tapped") } override func viewWillAppear(_ animated: Bool) { self.navigationController?.setNavigationBarHidden(false, animated: animated) + + // Update the badge label with the current cart data + if let button = vm.cartButton.customView as? UIButton { + BadgeBarBtn.shared.updateBadge(for: button, with: CartDataCache.cartListData.count.toString()) + } } override func viewDidAppear(_ animated: Bool) { diff --git a/WOKA/Shop/Controller/ShopListingVC.swift b/WOKA/Shop/Controller/ShopListingVC.swift index a3edd1e..a6f12a8 100644 --- a/WOKA/Shop/Controller/ShopListingVC.swift +++ b/WOKA/Shop/Controller/ShopListingVC.swift @@ -20,73 +20,17 @@ class ShopListingVC: UIViewController { navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default) navigationController?.navigationBar.shadowImage = UIImage() - - - -// // Create the button -// let backbutton = UIButton(type: .custom) -// backbutton.setImage(UIImage(named: "CartIcon"), for: .normal) // Image can be downloaded from the provided link -// backbutton.setTitleColor(.black, for: .normal) // You can change the TitleColor -// backbutton.addTarget(self, action: #selector(vm.cartBtnTapped), for: .touchUpInside) -// -// // Set button height and width -// let buttonHeight: CGFloat = 50 -// let buttonWidth: CGFloat = 50 -// backbutton.translatesAutoresizingMaskIntoConstraints = false -// backbutton.widthAnchor.constraint(equalToConstant: buttonWidth).isActive = true -// backbutton.heightAnchor.constraint(equalToConstant: buttonHeight).isActive = true -// -// // Create the badge label -// CartDataCache.cartBadgeLabel.text = CartDataCache.cartCount.toString() // Set your badge number here -// CartDataCache.cartBadgeLabel.textColor = .white -// CartDataCache.cartBadgeLabel.backgroundColor = .red -// CartDataCache.cartBadgeLabel.font = UIFont.systemFont(ofSize: 12) -// CartDataCache.cartBadgeLabel.textAlignment = .center -// CartDataCache.cartBadgeLabel.layer.cornerRadius = 10 -// CartDataCache.cartBadgeLabel.clipsToBounds = true -// -// // Set badge label constraints -// CartDataCache.cartBadgeLabel.translatesAutoresizingMaskIntoConstraints = false -// CartDataCache.cartBadgeLabel.widthAnchor.constraint(equalToConstant: 20).isActive = true -// CartDataCache.cartBadgeLabel.heightAnchor.constraint(equalToConstant: 20).isActive = true -// -// // Create a container view for the button -// let containerView = UIView(frame: CGRect(x: 0, y: 0, width: buttonWidth, height: buttonHeight)) -// containerView.backgroundColor = .blue -// containerView.addSubview(backbutton) -// -// containerView.addSubview(CartDataCache.cartBadgeLabel) -// -// // Set constraints for the button inside the container view -// NSLayoutConstraint.activate([ -// backbutton.centerYAnchor.constraint(equalTo: containerView.centerYAnchor), -// backbutton.centerXAnchor.constraint(equalTo: containerView.centerXAnchor), -// -// // Position the badge at the top-right corner of the button -// CartDataCache.cartBadgeLabel.topAnchor.constraint(equalTo: backbutton.topAnchor, constant: -3), -// CartDataCache.cartBadgeLabel.rightAnchor.constraint(equalTo: backbutton.rightAnchor, constant: -2) -// ]) -// -// // Create a UIBarButtonItem with the custom view -// let customBarButton = UIBarButtonItem(customView: containerView) -// -// // Create a flexible space item to push the custom view to the left -// let flexibleSpace = UIBarButtonItem(barButtonSystemItem: .fixedSpace, target: nil, action: nil) -// flexibleSpace.width = 10 -// -// // Create a negative spacer to fine-tune the position -// let negativeSpacer = UIBarButtonItem(barButtonSystemItem: .fixedSpace, target: nil, action: nil) -// negativeSpacer.width = 10 // Adjust this value to move the button to the left -// -// // Add the custom bar button and the spacer to the navigation bar -// self.navigationItem.rightBarButtonItems = [flexibleSpace, negativeSpacer, customBarButton] - } override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) navigationController?.setNavigationBarHidden(false, animated: animated) self.navigationController?.setColor(color: .white) + + // Update the badge label with the current cart data + if let button = vm.cartButton.customView as? UIButton { + BadgeBarBtn.shared.updateBadge(for: button, with: CartDataCache.cartListData.count.toString()) + } } override func viewWillDisappear(_ animated: Bool) { @@ -134,65 +78,65 @@ extension ShopListingVC : TableViewSRC{ -extension UIViewController { - - func createCartButton(imageName: String, - badgeNumber: Int, - buttonSize: CGSize = CGSize(width: 50, height: 50), - badgeColor: UIColor = .red, - badgeTextColor: UIColor = .white, - buttonColor: UIColor = .blue, - target: Any, - action: Selector) -> UIBarButtonItem { - - // Create the button - let button = UIButton(type: .custom) - button.setImage(UIImage(named: imageName), for: .normal) - button.setTitleColor(.black, for: .normal) - button.addTarget(target, action: action, for: .touchUpInside) - button.isEnabled = true - button.isUserInteractionEnabled = true - // Set button height and width - button.translatesAutoresizingMaskIntoConstraints = false - button.widthAnchor.constraint(equalToConstant: buttonSize.width).isActive = true - button.heightAnchor.constraint(equalToConstant: buttonSize.height).isActive = true - - // Create the badge label -// let badgeLabel = UILabel() - CartDataCache.cartBadgeLabel.text = "\(badgeNumber)" - CartDataCache.cartBadgeLabel.textColor = badgeTextColor - CartDataCache.cartBadgeLabel.backgroundColor = badgeColor - CartDataCache.cartBadgeLabel.font = UIFont.systemFont(ofSize: 12) - CartDataCache.cartBadgeLabel.textAlignment = .center - CartDataCache.cartBadgeLabel.layer.cornerRadius = 10 - CartDataCache.cartBadgeLabel.clipsToBounds = true - - badgeNumber == 0 ? (CartDataCache.cartBadgeLabel.isHidden = true) : (CartDataCache.cartBadgeLabel.isHidden = false) - // Set badge label constraints - CartDataCache.cartBadgeLabel.translatesAutoresizingMaskIntoConstraints = false - CartDataCache.cartBadgeLabel.widthAnchor.constraint(equalToConstant: 20).isActive = true - CartDataCache.cartBadgeLabel.heightAnchor.constraint(equalToConstant: 20).isActive = true - - // Create a container view for the button - let containerView = UIView(frame: CGRect(x: 0, y: 0, width: buttonSize.width, height: buttonSize.height)) - - containerView.backgroundColor = buttonColor - containerView.addSubview(button) - containerView.addSubview(CartDataCache.cartBadgeLabel) - containerView.isUserInteractionEnabled = true - // Set constraints for the button inside the container view - NSLayoutConstraint.activate([ - button.centerYAnchor.constraint(equalTo: containerView.centerYAnchor), - button.centerXAnchor.constraint(equalTo: containerView.centerXAnchor), - - // Position the badge at the top-right corner of the button - CartDataCache.cartBadgeLabel.topAnchor.constraint(equalTo: button.topAnchor, constant: -3), - CartDataCache.cartBadgeLabel.rightAnchor.constraint(equalTo: button.rightAnchor, constant: -2) - ]) - - // Create a UIBarButtonItem with the custom view - let customBarButton = UIBarButtonItem(customView: containerView) - // Return the custom bar button - return customBarButton - } -} +//extension UIViewController { +// +// func createCartButton(imageName: String, +// badgeNumber: Int, +// buttonSize: CGSize = CGSize(width: 50, height: 50), +// badgeColor: UIColor = .red, +// badgeTextColor: UIColor = .white, +// buttonColor: UIColor = .blue, +// target: Any, +// action: Selector) -> UIBarButtonItem { +// +// // Create the button +// let button = UIButton(type: .custom) +// button.setImage(UIImage(named: imageName), for: .normal) +// button.setTitleColor(.black, for: .normal) +// button.addTarget(target, action: action, for: .touchUpInside) +// button.isEnabled = true +// button.isUserInteractionEnabled = true +// // Set button height and width +// button.translatesAutoresizingMaskIntoConstraints = false +// button.widthAnchor.constraint(equalToConstant: buttonSize.width).isActive = true +// button.heightAnchor.constraint(equalToConstant: buttonSize.height).isActive = true +// +// // Create the badge label +//// let badgeLabel = UILabel() +// CartDataCache.cartBadgeLabel.text = "\(badgeNumber)" +// CartDataCache.cartBadgeLabel.textColor = badgeTextColor +// CartDataCache.cartBadgeLabel.backgroundColor = badgeColor +// CartDataCache.cartBadgeLabel.font = UIFont.systemFont(ofSize: 12) +// CartDataCache.cartBadgeLabel.textAlignment = .center +// CartDataCache.cartBadgeLabel.layer.cornerRadius = 10 +// CartDataCache.cartBadgeLabel.clipsToBounds = true +// +// badgeNumber == 0 ? (CartDataCache.cartBadgeLabel.isHidden = true) : (CartDataCache.cartBadgeLabel.isHidden = false) +// // Set badge label constraints +// CartDataCache.cartBadgeLabel.translatesAutoresizingMaskIntoConstraints = false +// CartDataCache.cartBadgeLabel.widthAnchor.constraint(equalToConstant: 20).isActive = true +// CartDataCache.cartBadgeLabel.heightAnchor.constraint(equalToConstant: 20).isActive = true +// +// // Create a container view for the button +// let containerView = UIView(frame: CGRect(x: 0, y: 0, width: buttonSize.width, height: buttonSize.height)) +// +// containerView.backgroundColor = buttonColor +// containerView.addSubview(button) +// containerView.addSubview(CartDataCache.cartBadgeLabel) +// containerView.isUserInteractionEnabled = true +// // Set constraints for the button inside the container view +// NSLayoutConstraint.activate([ +// button.centerYAnchor.constraint(equalTo: containerView.centerYAnchor), +// button.centerXAnchor.constraint(equalTo: containerView.centerXAnchor), +// +// // Position the badge at the top-right corner of the button +// CartDataCache.cartBadgeLabel.topAnchor.constraint(equalTo: button.topAnchor, constant: -3), +// CartDataCache.cartBadgeLabel.rightAnchor.constraint(equalTo: button.rightAnchor, constant: -2) +// ]) +// +// // Create a UIBarButtonItem with the custom view +// let customBarButton = UIBarButtonItem(customView: containerView) +// // Return the custom bar button +// return customBarButton +// } +//} diff --git a/WOKA/Shop/Controller/ShopProductsVC.swift b/WOKA/Shop/Controller/ShopProductsVC.swift index a77c2dc..0e90f3c 100644 --- a/WOKA/Shop/Controller/ShopProductsVC.swift +++ b/WOKA/Shop/Controller/ShopProductsVC.swift @@ -22,49 +22,15 @@ class ShopProductsVC: UIViewController { navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default) navigationController?.navigationBar.shadowImage = UIImage() - // Create the button - let backbutton = UIButton(type: .custom) - backbutton.setImage(UIImage(named: "CartIcon"), for: .normal) // Image can be downloaded from the provided link - backbutton.setTitleColor(.black, for: .normal) // You can change the TitleColor - backbutton.addTarget(self, action: #selector(cartBtnTapped), for: .touchUpInside) - - // Set button height and width - let buttonHeight: CGFloat = 50 - let buttonWidth: CGFloat = 50 - backbutton.translatesAutoresizingMaskIntoConstraints = false - backbutton.widthAnchor.constraint(equalToConstant: buttonWidth).isActive = true - backbutton.heightAnchor.constraint(equalToConstant: buttonHeight).isActive = true - - // Create a container view for the button - let containerView = UIView(frame: CGRect(x: 0, y: 0, width: buttonWidth, height: buttonHeight)) - containerView.addSubview(backbutton) - - // Set constraints for the button inside the container view - NSLayoutConstraint.activate([ - backbutton.centerYAnchor.constraint(equalTo: containerView.centerYAnchor), - backbutton.centerXAnchor.constraint(equalTo: containerView.centerXAnchor) - ]) - - // Create a UIBarButtonItem with the custom view - let customBarButton = UIBarButtonItem(customView: containerView) - - // Create a flexible space item to push the custom view to the left - let flexibleSpace = UIBarButtonItem(barButtonSystemItem: .fixedSpace, target: nil, action: nil) - flexibleSpace.width = 10 - // Create a negative spacer to fine-tune the position - let negativeSpacer = UIBarButtonItem(barButtonSystemItem: .fixedSpace, target: nil, action: nil) - negativeSpacer.width = 10 // Adjust this value to move the button to the left - - // Add the custom bar button and the spacer to the navigation bar - self.navigationItem.rightBarButtonItems = [flexibleSpace,negativeSpacer, customBarButton] } - - @objc func cartBtnTapped(){ - print("cart tapped") - } - + override func viewWillAppear(_ animated: Bool) { self.navigationController?.setNavigationBarHidden(false, animated: animated) + + // Update the badge label with the current cart data + if let button = vm.cartButton.customView as? UIButton { + BadgeBarBtn.shared.updateBadge(for: button, with: CartDataCache.cartListData.count.toString()) + } } override func viewDidAppear(_ animated: Bool) { @@ -128,8 +94,7 @@ extension ShopProductsVC : CollectionViewSRC{ let data = vm.shopProductsData[indexPath.row] let sb = UIStoryboard(name: K.StoryBoard.shop, bundle: nil) let vcPush = sb.instantiateViewController(withIdentifier: K.StoryBoardID.Shop.productDetailsVC) as! ProductDetailsVC - vcPush.vm.categoryName = vm.pageTitle ?? "Shop" - vcPush.vm.productDetails = data + vcPush.vm.productID = data.id self.navigationController?.pushViewController(vcPush, animated: true) case subCategoryCV: if let selectedSubCategory = vm.subCategoryData[indexPath.row].id{ diff --git a/WOKA/Shop/Model/ShopProductsListingDM.swift b/WOKA/Shop/Model/ShopProductsListingDM.swift index ccf54ae..e2f482f 100644 --- a/WOKA/Shop/Model/ShopProductsListingDM.swift +++ b/WOKA/Shop/Model/ShopProductsListingDM.swift @@ -29,11 +29,11 @@ struct ShopProductsListingDM: Codable { let taxValue: String? let shopMasterDetail: CartListingDM.ShopMasterDetail? let shopImage: [String]? - let addedToCart: Bool? + var addedToCart: Bool? let productFinalPrice : Double? - + let category : Category? enum CodingKeys: String, CodingKey { - case id + case id, category case skuID = "sku_id" case productName = "product_name" case productThumbnail = "product_thumbnail" @@ -50,6 +50,18 @@ struct ShopProductsListingDM: Codable { case productFinalPrice = "product_final_price" } } + + // MARK: - Category + struct Category: Codable { + let id: Int? + let categoryName: String? + + enum CodingKeys: String, CodingKey { + case id + case categoryName = "category_name" + } + } + // MARK: - ShopMasterDetail struct ShopMasterDetail: Codable { @@ -67,65 +79,64 @@ struct ShopProductsListingDM: Codable { } } +/* +// MARK: - AddressListDMElement +struct AddressListDMElement: Codable { + let id: Int? + let skuID, productName: String? + let categoryMasterID, subCategoryMasterID: Int? + let productPrice: String? + let remainStockQuantity: Int? + let stockStatus: String? + let taxCategory: JSONNull? + let taxValue: String? + let shopMasterDetail: ShopMasterDetail? + let shopImage: [String]? + let category: Category? + let addedToCart: Bool? -//struct ShopProductDM: Codable { -// let result: ResultData? -// -// // MARK: - ResultData -// struct ResultData: Codable { -// let sabakDegiNaniSeries: [SabakDegiNaniSery]? -// -// enum CodingKeys: String, CodingKey { -// case sabakDegiNaniSeries = "Sabak Degi Nani Series" -// } -// } -// -// // MARK: - SabakDegiNaniSery -// struct SabakDegiNaniSery: Codable { -// let id: Int? -// let skuID, productName, productThumbnail: String? -// let categoryMasterID, subCategoryMasterID: Int? -// let productPrice: String? -// let remainStockQuantity: Int? -// let stockStatus: String? -//// let taxCategory: JSONNull? -// let taxValue: String? -// let shopMasterDetail: ShopMasterDetail? -// let shopImage: [String]? -// let addedToCart: Bool? -// -// enum CodingKeys: String, CodingKey { -// case id -// case skuID = "sku_id" -// case productName = "product_name" -// case productThumbnail = "product_thumbnail" -// case categoryMasterID = "category_master_id" -// case subCategoryMasterID = "sub_category_master_id" -// case productPrice = "product_price" -// case remainStockQuantity = "remain_stock_quantity" -// case stockStatus = "stock_status" -//// case taxCategory = "tax_category" -// case taxValue = "tax_value" -// case shopMasterDetail = "shop_master_detail" -// case shopImage = "shop_image" -// case addedToCart = "added_to_cart" -// } -// } -// -// // MARK: - ShopMasterDetail -// struct ShopMasterDetail: Codable { -// let id, productID: Int? -// let productNameEnglish, productNameHindi, descriptionEnglish, descriptionHindi: String? -// -// enum CodingKeys: String, CodingKey { -// case id -// case productID = "product_id" -// case productNameEnglish = "product_name_english" -// case productNameHindi = "product_name_hindi" -// case descriptionEnglish = "description_english" -// case descriptionHindi = "description_hindi" -// } -// } -// -//} -// + enum CodingKeys: String, CodingKey { + case id + case skuID = "sku_id" + case productName = "product_name" + case categoryMasterID = "category_master_id" + case subCategoryMasterID = "sub_category_master_id" + case productPrice = "product_price" + case remainStockQuantity = "remain_stock_quantity" + case stockStatus = "stock_status" + case taxCategory = "tax_category" + case taxValue = "tax_value" + case shopMasterDetail = "shop_master_detail" + case shopImage = "shop_image" + case category + case addedToCart = "added_to_cart" + } +} + +// MARK: - Category +struct Category: Codable { + let id: Int? + let categoryName: String? + + enum CodingKeys: String, CodingKey { + case id + case categoryName = "category_name" + } +} + +// MARK: - ShopMasterDetail +struct ShopMasterDetail: Codable { + let id, productID: Int? + let productNameEnglish, productNameHindi, descriptionEnglish, descriptionHindi: String? + + enum CodingKeys: String, CodingKey { + case id + case productID = "product_id" + case productNameEnglish = "product_name_english" + case productNameHindi = "product_name_hindi" + case descriptionEnglish = "description_english" + case descriptionHindi = "description_hindi" + } +} + +*/ diff --git a/WOKA/Shop/Shop.storyboard b/WOKA/Shop/Shop.storyboard index c22442c..8868efa 100644 --- a/WOKA/Shop/Shop.storyboard +++ b/WOKA/Shop/Shop.storyboard @@ -186,10 +186,35 @@ + - +