117 lines
3.9 KiB
Swift
117 lines
3.9 KiB
Swift
//
|
|
// CartDataCache.swift
|
|
// WOKA
|
|
//
|
|
// Created by MacBook Pro on 29/07/24.
|
|
//
|
|
|
|
import Foundation
|
|
import Alamofire
|
|
|
|
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{
|
|
// 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()]
|
|
|
|
NetworkManager.shareInstance.apiRequest(url: APIEndPoints.Cart.cart_listing, method: .get,headers: headers, queue : QueueHelper.background) { (result : Result<BaseResponseModel<CartListingDM>, NetworkManager.APIError>) in
|
|
switch result{
|
|
case .success(let data):
|
|
switch data.success{
|
|
case 0:
|
|
/*
|
|
Error
|
|
*/
|
|
Utilities.dismissProgressHUD()
|
|
case 1:
|
|
Utilities.dismissProgressHUD()
|
|
|
|
guard let data = data.data?.result else{return}
|
|
CartDataCache.cartListData = data
|
|
CartDataCache.isFetched = true
|
|
default:
|
|
Utilities.dismissProgressHUD()
|
|
break
|
|
}
|
|
case .failure(let error):
|
|
Utilities.dismissProgressHUD()
|
|
Utilities.alert(title: "Error", message: error.localizedDescription, viewController: vc)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
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()
|
|
}
|
|
}
|
|
}
|