// // LoadingIndicatorImageView.swift // WOKA // // Created by MacBook Pro on 08/05/24. // import UIKit import SDWebImage enum ImageType{ case homeAvatar case ads case none } extension UIImageView { func imageURL(_ url: String, color : UIColor = UIColor.appColor(.TextDarkBlue)!, type : ImageType = .none, completionHandler: ((Bool) -> Void)? = nil) { let customIndicator = SDWebImageActivityIndicator.medium customIndicator.indicatorView.color = color self.sd_imageIndicator = customIndicator self.sd_setImage(with: URL(string: url.replacingOccurrences(of: " ", with: "%20"))) { [weak self] (image, error, cacheType, url) in if error != nil{ completionHandler?(false) // do a placeholder image here guard let self else{return} handlePlaceHolder(type: type) }else{ completionHandler?(true) } } } //Update the placeholder images respective to the type func handlePlaceHolder(type : ImageType){ switch type{ case .homeAvatar: self.image = UIImage(named: "DefaultAvatar") case .ads: break case .none: self.image = UIImage(named: "EpisodePlaceHolder") } } //Old Detained Code // func imageURL(_ url: String, color : UIColor = UIColor.black) { // // let activityIndicator = UIActivityIndicatorView(style: .medium) // activityIndicator.tintColor = UIColor.darkGray // activityIndicator.color = color // activityIndicator.frame = CGRect(x: 0, y: 0, width: 64, height: 64) // activityIndicator.hidesWhenStopped = true // DispatchQueue.main.async { // activityIndicator.startAnimating() // } // activityIndicator.translatesAutoresizingMaskIntoConstraints = true // // // activityIndicator.center = CGPoint(x: self.frame.size.width / 2, y: self.frame.size.height / 2) // activityIndicator.center = CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds)) // // // bottom is for resizing the indicator to be perfect in center // activityIndicator.autoresizingMask = (UIView.AutoresizingMask(rawValue: UIView.AutoresizingMask.RawValue(UInt8(UIView.AutoresizingMask.flexibleRightMargin.rawValue) | UInt8(UIView.AutoresizingMask.flexibleLeftMargin.rawValue) | UInt8(UIView.AutoresizingMask.flexibleBottomMargin.rawValue) | UInt8(UIView.AutoresizingMask.flexibleTopMargin.rawValue)))) // // self.addSubview(activityIndicator) // // self.sd_setImage(with: URL(string: url.replacingOccurrences(of: " ", with: "%20"))) { (image, error, cacheType, url) in //// activityIndicator.stopAnimating() // DispatchQueue.main.async { // activityIndicator.stopAnimating() //// activityIndicator.removeFromSuperview() // } // } // } } extension UIButton { func setImage(from url: String, for state: UIControl.State, color: UIColor = .black) { // Ensure the button's background is clear self.backgroundColor = .clear // Ensure the button's image view background is clear self.imageView?.backgroundColor = .clear let activityIndicator = UIActivityIndicatorView(style: .medium) activityIndicator.tintColor = .darkGray activityIndicator.color = color activityIndicator.frame = CGRect(x: 0, y: 0, width: 64, height: 64) activityIndicator.hidesWhenStopped = true DispatchQueue.main.async { activityIndicator.startAnimating() } activityIndicator.translatesAutoresizingMaskIntoConstraints = true activityIndicator.center = CGPoint(x: self.frame.size.width / 2, y: self.frame.size.height / 2) // Resizing the indicator to be perfectly centered activityIndicator.autoresizingMask = [ .flexibleRightMargin, .flexibleLeftMargin, .flexibleBottomMargin, .flexibleTopMargin ] self.addSubview(activityIndicator) // Setting the image using SDWebImage self.sd_setImage(with: URL(string: url.replacingOccurrences(of: " ", with: "%20")), for: state) { (image, error, cacheType, url) in DispatchQueue.main.async { activityIndicator.stopAnimating() activityIndicator.removeFromSuperview() } } } }