XIBLoader.swift 3.78 KB
//
//  XIBLoader.swift
//  SwiftWarplyFramework
//
//  Created for SPM XIB loading support
//

import UIKit

public class XIBLoader {
    
    /// Safely load a view controller from XIB with proper bundle resolution
    public static func loadViewController<T: UIViewController>(
        _ type: T.Type,
        nibName: String? = nil
    ) -> T? {
        let actualNibName = nibName ?? String(describing: type)
        
        guard Bundle.frameworkBundle.path(forResource: actualNibName, ofType: "nib") != nil else {
            print("❌ [WarplySDK] XIB file '\(actualNibName).xib' not found")
            return nil
        }
        
        return T(nibName: actualNibName, bundle: Bundle.frameworkBundle)
    }
    
    /// Safely load a table view cell from XIB
    public static func loadTableViewCell<T: UITableViewCell>(
        _ type: T.Type,
        nibName: String? = nil
    ) -> T? {
        let actualNibName = nibName ?? String(describing: type)
        
        guard let nib = Bundle.safeLoadNib(named: actualNibName) else {
            return nil
        }
        
        let objects = nib.instantiate(withOwner: nil, options: nil)
        return objects.first as? T
    }
    
    /// Verify all required XIB files are present
    public static func verifyXIBFiles() -> [String: Bool] {
        let requiredXIBs = [
            "ProfileViewController",
            "CouponViewController", 
            "MyRewardsViewController",
            "ProfileCouponFiltersTableViewCell",
            "ProfileHeaderTableViewCell",
            "ProfileQuestionnaireTableViewCell",
            "MyRewardsOffersScrollTableViewCell",
            "ProfileCouponTableViewCell",
            "ProfileFilterCollectionViewCell",
            "MyRewardsOfferCollectionViewCell",
            "MyRewardsBannerOfferCollectionViewCell",
            "MyRewardsBannerOffersScrollTableViewCell"
        ]
        
        var results: [String: Bool] = [:]
        let bundle = Bundle.frameworkBundle
        
        for xibName in requiredXIBs {
            let exists = bundle.path(forResource: xibName, ofType: "nib") != nil
            results[xibName] = exists
            
            if exists {
                print("✅ [WarplySDK] XIB found: \(xibName)")
            } else {
                print("❌ [WarplySDK] XIB missing: \(xibName)")
            }
        }
        
        return results
    }
    
    /// Safe registration of table view cells with XIB
    public static func registerTableViewCell(
        _ tableView: UITableView,
        cellClass: AnyClass,
        nibName: String,
        identifier: String
    ) {
        if let nib = Bundle.safeLoadNib(named: nibName) {
            tableView.register(nib, forCellReuseIdentifier: identifier)
            print("✅ [WarplySDK] Registered table view cell: \(nibName)")
        } else {
            print("❌ [WarplySDK] Failed to register table view cell: \(nibName)")
            // Register a basic UITableViewCell as fallback
            tableView.register(UITableViewCell.self, forCellReuseIdentifier: identifier)
        }
    }
    
    /// Safe registration of collection view cells with XIB
    public static func registerCollectionViewCell(
        _ collectionView: UICollectionView,
        cellClass: AnyClass,
        nibName: String,
        identifier: String
    ) {
        if let nib = Bundle.safeLoadNib(named: nibName) {
            collectionView.register(nib, forCellWithReuseIdentifier: identifier)
            print("✅ [WarplySDK] Registered collection view cell: \(nibName)")
        } else {
            print("❌ [WarplySDK] Failed to register collection view cell: \(nibName)")
            // Register basic UICollectionViewCell as fallback
            collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: identifier)
        }
    }
}