vds_ios/VDS/Classes/BundleManager.swift
Matt Bruce 0f6a80a3be added debug logic
Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
2023-08-15 13:47:31 -05:00

77 lines
2.1 KiB
Swift

//
// BundleManager.swift
// VDS
//
// Created by Matt Bruce on 1/9/23.
//
import Foundation
import UIKit
public func DebugLog(_ message: String, file: String = #file, line: Int = #line, function: String = #function) {
#if DEBUG
let path = (file as NSString).lastPathComponent
// using print because NSLog crashes when passing certain optionals
print("[\(path)] \(function) [Line \(line)] : \(message)")
#endif
}
/// Bundle Manager keeps all bundles together for ease of use for searching within any of them for a specific asset
public class BundleManager {
public static var shared = BundleManager()
public var bundles: [Bundle] = []
private var paths: [String] = []
private init(){
bundles.append(Bundle(for: Self.self))
}
private func updateBundles() {
bundles.removeAll()
bundles.append(Bundle(for: Self.self))
paths.forEach({ path in
if let bundle = Bundle(path: path) {
bundles.append(bundle)
}
})
}
/// With take a location and if found append to the local array for use later for asset searching.
/// - Parameter path: Location of the bundle
public static func register(_ path: String) {
if !shared.paths.contains(where: {$0 == path}) {
if let bundle = Bundle(path: path) {
shared.paths.append(path)
shared.bundles.append(bundle)
}
}
}
}
extension BundleManager {
/// Searches through all registered bundles for an image
/// - Parameter name: Name of the image
/// - Returns: Will find an image or not.
public func image(for name: String) -> UIImage? {
var foundImage: UIImage?
if let image = UIImage(named: name) {
foundImage = image
} else {
for bundle in bundles {
if let image = UIImage(named: name, in: bundle, with: nil) {
foundImage = image
break
}
}
}
return foundImage
}
}