diff --git a/WOKA/Cart/CartDataCache.swift b/WOKA/Cart/CartDataCache.swift index 2fb45e8..968b22c 100644 --- a/WOKA/Cart/CartDataCache.swift +++ b/WOKA/Cart/CartDataCache.swift @@ -10,23 +10,33 @@ import Alamofire class CartDataCache{ - static var cartListData = [CartListingDM.ResultData]() - static var isFetched = false - static var cartCount = 0 { + static var cartListData = [CartListingDM.ResultData](){ didSet{ - if cartCount == 0{ + if cartListData.count == 0{ cartBadgeLabel.text = "0" cartBadgeLabel.isHidden = true }else{ - cartBadgeLabel.text = cartCount.toString() + cartBadgeLabel.text = cartListData.count.toString() cartBadgeLabel.isHidden = false } } } + static var isFetched = false +// static var cartCount = 0 { +// didSet{ +// if cartCount == 0{ +// cartBadgeLabel.text = "0" +// cartBadgeLabel.isHidden = true +// }else{ +// cartBadgeLabel.text = cartCount.toString() +// cartBadgeLabel.isHidden = false +// } +// } +// } static var addressData = [AddressListDM]() static let cartBadgeLabel = UILabel() static let shareInstance = CartDataCache() - + func getCartList(vc : UIViewController){ let headers : HTTPHeaders = ["access-token" : AuthFunc.shareInstance.getAccessToken()] @@ -44,7 +54,6 @@ class CartDataCache{ guard let data = data.data?.result else{return} CartDataCache.cartListData = data - CartDataCache.cartCount = data.count CartDataCache.isFetched = true default: Utilities.dismissProgressHUD() @@ -57,3 +66,51 @@ class CartDataCache{ } } } + +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 + + badgeLabel.frame = CGRect(x: 30, y: 0, width: 18, height: 18) + badgeLabel.backgroundColor = UIColor.red + 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 { + 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) + + // 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() + } + } +} diff --git a/WOKA/Main/Delegate/AppDelegate.swift b/WOKA/Main/Delegate/AppDelegate.swift index 449c8a6..385cc63 100644 --- a/WOKA/Main/Delegate/AppDelegate.swift +++ b/WOKA/Main/Delegate/AppDelegate.swift @@ -40,6 +40,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate { // IQKeyboardManager.shared.enableAutoToolbar = false // IQKeyboardManager.shared.layoutIfNeededOnUpdate = true registerCustomFonts() + // Set the authentication ID Pass after app starts AuthFunc.shareInstance.setAuthIDPass() @@ -52,6 +53,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate { //Configure and modify the side bar configureSideBar() + //Configure JWPlayer Liscense JWPlayerKitLicense.setLicenseKey("Lgok1t7H4PKY+M8FZqmCx54ibUF+NeCTn+xgd+/LVTaRdc+L") return true } diff --git a/WOKA/Network Adapter/NetworkManager.swift b/WOKA/Network Adapter/NetworkManager.swift index d52b8cd..de37363 100644 --- a/WOKA/Network Adapter/NetworkManager.swift +++ b/WOKA/Network Adapter/NetworkManager.swift @@ -58,14 +58,27 @@ class NetworkManager{ switch response.result { case .success(let value): /* - if Sucess == 4 menas user has logined in other device, logout that user by saying session timeout + if Sucess == 4 menas user has logined in some other device, logout that user by saying session timeout */ if value.success == 4{ if let topController = UIApplication.topViewController() { - Utilities.alertWithBtnCompletion(title: "Session Expired", msgBody: "Please Login Again", okBtnStr: "OK", vc: topController) { isDone in + let sb = UIStoryboard(name: K.StoryBoard.customAlerts, bundle: nil) + let vcPush = sb.instantiateViewController(withIdentifier: K.StoryBoardID.CustomAlerts.alertCustomVC) as! AlertCustomVC + vcPush.contentLabel = "Please Login Again" + vcPush.mainTitleText = "Session Expired." + vcPush.yesBtnText = "Login" + vcPush.onDoneBlock = { isDone in AuthFunc.shareInstance.logout() UIApplication.setRootView(LoginNavVC.instantiate(from: .AuthenticationSB)) } + vcPush.modalPresentationStyle = .overCurrentContext + vcPush.modalTransitionStyle = .crossDissolve + topController.present(vcPush, animated: true) + +// Utilities.alertWithBtnCompletion(title: "Session Expired", msgBody: "Please Login Again", okBtnStr: "OK", vc: topController) { isDone in +// AuthFunc.shareInstance.logout() +// UIApplication.setRootView(LoginNavVC.instantiate(from: .AuthenticationSB)) +// } } return } diff --git a/WOKA/Shop/Controller/ShopCategoryVC.swift b/WOKA/Shop/Controller/ShopCategoryVC.swift index 7a1383d..4d87ec5 100644 --- a/WOKA/Shop/Controller/ShopCategoryVC.swift +++ b/WOKA/Shop/Controller/ShopCategoryVC.swift @@ -20,41 +20,41 @@ class ShopCategoryVC: 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] +// // 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(){ diff --git a/WOKA/Shop/ViewModel/ShopCategoryVM.swift b/WOKA/Shop/ViewModel/ShopCategoryVM.swift index 6627ffa..77af74a 100644 --- a/WOKA/Shop/ViewModel/ShopCategoryVM.swift +++ b/WOKA/Shop/ViewModel/ShopCategoryVM.swift @@ -19,6 +19,12 @@ class ShopCategoryVM{ setupCell() addGradient() getCategory() + + let btn = BadgeBarBtn.shared.setupCartButton() + self.vc.navigationItem.rightBarButtonItems = [btn] + BadgeBarBtn.shared.tapAction = { + print("tapped") + } } func setupCell(){ diff --git a/WOKA/Shop/ViewModel/ShopListingVM.swift b/WOKA/Shop/ViewModel/ShopListingVM.swift index d0f83ef..de616d1 100644 --- a/WOKA/Shop/ViewModel/ShopListingVM.swift +++ b/WOKA/Shop/ViewModel/ShopListingVM.swift @@ -18,56 +18,20 @@ class ShopListingVM{ addGradient() getSuperCategory() - - - - let filterBtn = UIButton.init(frame: CGRect.init(x: 0, y: 0, width: 45, height: 45)) - filterBtn.setImage(UIImage(named: "CartIcon"), for: .normal) - filterBtn.addTarget(self, action: #selector(cartBtnTapped), for: .touchUpInside) - -// let lblBadge = type(of: CartDataCache.cartBadgeLabel).init(frame: CGRect.init(x: 30, y: 0, width: 18, height: 18)) - CartDataCache.cartBadgeLabel.backgroundColor = UIColor.red - CartDataCache.cartBadgeLabel.isHidden = true - CartDataCache.cartBadgeLabel.clipsToBounds = true - CartDataCache.cartBadgeLabel.layer.cornerRadius = 9 - CartDataCache.cartBadgeLabel.textColor = UIColor.white - CartDataCache.cartBadgeLabel.font = FontCustom.shareInstance.customFont(fontName: .Exo2_Regular, size: 10) - CartDataCache.cartBadgeLabel.textAlignment = .center - - filterBtn.addSubview(CartDataCache.cartBadgeLabel) - - self.vc.navigationItem.rightBarButtonItems = [UIBarButtonItem.init(customView: filterBtn)] + let btn = BadgeBarBtn.shared.setupCartButton() + self.vc.navigationItem.rightBarButtonItems = [btn] + BadgeBarBtn.shared.tapAction = { + let sb = UIStoryboard(name: K.StoryBoard.cart, bundle: nil) + let vcPush = sb.instantiateViewController(withIdentifier: K.StoryBoardID.Cart.cartListVC) as! CartListVC + self.vc.navigationController?.pushViewController(vcPush, animated: true) + } //check if cart is not fetched if !CartDataCache.isFetched{ CartDataCache.shareInstance.getCartList(vc: self.vc) } - CartDataCache.cartBadgeLabel.text = "2" -// let btn = BadgedButtonItem(with: UIImage(named: "CartIcon")!) -// btn.setBadge(with: 2) -// self.vc.navigationItem.rightBarButtonItem = btn -// -// btn.tapAction = { -// print("tapped") -// } -// let cartButton = vc.createCartButton(imageName: "CartIcon", -// badgeNumber: CartDataCache.cartCount, -// target: self, -// action: #selector(cartBtnTapped)) -// -// // 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 -// -// self.vc.navigationItem.rightBarButtonItems = [flexibleSpace, negativeSpacer, cartButton] } - - - + func setupCell(){ vc.tableView.register(UINib(nibName: K.CellIdentifier.Shop.shopListingCell, bundle: nil), forCellReuseIdentifier: K.CellIdentifier.Shop.shopListingCell) vc.tableView.delegate = vc.self @@ -124,95 +88,3 @@ class ShopListingVM{ } } } - -extension UIBarButtonItem { - - convenience init(icon: UIImage, badge: String, _ badgeBackgroundColor: UIColor = #colorLiteral(red: 0.9156965613, green: 0.380413115, blue: 0.2803866267, alpha: 1), target: Any? = UIBarButtonItem.self, action: Selector? = nil) { - let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 50, height: 50)) - imageView.image = icon - - let label = UILabel(frame: CGRect(x: 8, y: -5, width: 20, height: 20)) - label.text = badge - label.backgroundColor = badgeBackgroundColor - label.adjustsFontSizeToFitWidth = true - label.textAlignment = .center - label.font = FontCustom.shareInstance.customFont(fontName: .Exo2_Regular, size: 10) - label.clipsToBounds = true - label.layer.cornerRadius = 20 / 2 - label.textColor = .white - - let buttonView = UIView(frame: CGRect(x: 0, y: 0, width: 24, height: 24)) - buttonView.addSubview(imageView) - buttonView.addSubview(label) - buttonView.addGestureRecognizer(UITapGestureRecognizer.init(target: target, action: action)) - self.init(customView: buttonView) - } - -} - -class BadgedButtonItem1: UIBarButtonItem { - - public func setBadge(with value: Int) { - self.badgeValue = value - } - - private var badgeValue: Int? { - didSet { - if let value = badgeValue, - value > 0 { - lblBadge.isHidden = false - lblBadge.text = "\(value)" - } else { - lblBadge.isHidden = true - } - } - } - - var tapAction: (() -> Void)? - - private let filterBtn = UIButton() - private let lblBadge = UILabel() - - override init() { - super.init() - setup() - } - - init(with image: UIImage?) { - super.init() - setup(image: image) - } - - required init?(coder aDecoder: NSCoder) { - super.init(coder: aDecoder) - setup() - } - - private func setup(image: UIImage? = nil) { - - self.filterBtn.frame = CGRect(x: 0, y: 0, width: 50, height: 50) - self.filterBtn.adjustsImageWhenHighlighted = false - self.filterBtn.setImage(image, for: .normal) - self.filterBtn.addTarget(self, action: #selector(buttonPressed), for: .touchUpInside) - - self.lblBadge.frame = CGRect(x: -5, y: 0, width: 15, height: 15) - self.lblBadge.backgroundColor = .red - self.lblBadge.clipsToBounds = true - self.lblBadge.layer.cornerRadius = 7 - self.lblBadge.textColor = UIColor.white - self.lblBadge.font = UIFont.systemFont(ofSize: 10) - self.lblBadge.textAlignment = .center - self.lblBadge.isHidden = true - self.lblBadge.minimumScaleFactor = 0.1 - self.lblBadge.adjustsFontSizeToFitWidth = true - self.filterBtn.addSubview(lblBadge) - self.customView = filterBtn - } - - @objc func buttonPressed() { - if let action = tapAction { - action() - } - } - -} diff --git a/WOKA/SideBarNav/Controller/MyOrdersVC.swift b/WOKA/SideBarNav/Controller/MyOrdersVC.swift index d092cdc..2c6e9e7 100644 --- a/WOKA/SideBarNav/Controller/MyOrdersVC.swift +++ b/WOKA/SideBarNav/Controller/MyOrdersVC.swift @@ -15,7 +15,6 @@ class MyOrdersVC: UIViewController { super.viewDidLoad() vm.vc = self vm.initView() - } diff --git a/WOKA/SideBarNav/ViewModel/MyOrdersVM.swift b/WOKA/SideBarNav/ViewModel/MyOrdersVM.swift index 556315a..df7a0f4 100644 --- a/WOKA/SideBarNav/ViewModel/MyOrdersVM.swift +++ b/WOKA/SideBarNav/ViewModel/MyOrdersVM.swift @@ -15,32 +15,17 @@ class MyOrdersVM{ vc.title = "MY ORDERS".localized(loc: AuthFunc.shareInstance.languageSelected.rawValue) + let btn = BadgeBarBtn.shared.setupCartButton() + self.vc.navigationItem.rightBarButtonItems = [btn] + BadgeBarBtn.shared.tapAction = { + let sb = UIStoryboard(name: K.StoryBoard.cart, bundle: nil) + let vcPush = sb.instantiateViewController(withIdentifier: K.StoryBoardID.Cart.cartListVC) as! CartListVC + self.vc.navigationController?.pushViewController(vcPush, animated: true) + } + //check if cart is not fetched if !CartDataCache.isFetched{ CartDataCache.shareInstance.getCartList(vc: self.vc) } - - let cartButton = vc.createCartButton(imageName: "CartIcon", - badgeNumber: CartDataCache.cartCount, - target: self, - action: #selector(cartBtnTapped)) - - // 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 - self.vc.navigationItem.rightBarButtonItems = [flexibleSpace, negativeSpacer, cartButton] - - -// Timer.scheduledTimer(withTimeInterval: 2, repeats: false) { _ in -// lblBadge.text = "3" -// } - } - - @objc func cartBtnTapped(){ - print("cart tapped") } }