ProfileViewController.swift 14.8 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
//
//  ProfileViewController.swift
//  SwiftWarplyFramework
//
//  Created by Manos Chorianopoulos on 22/5/25.
//

import UIKit

@objc public class ProfileViewController: UIViewController {
    @IBOutlet weak var tableView: UITableView!
    
    // MARK: - Initializers
    public convenience init() {
        self.init(nibName: "ProfileViewController", bundle: MyEmptyClass.resourceBundle())
    }
    
    public override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }

    // MARK: - Dummy Data
    let allOffers: [OfferModel] = [
        // Προτάσεις για εσένα
        OfferModel(
            category: "Προτάσεις για εσένα",
            title: "15% έκπτωση",
            description: "σε όλα τα προϊόντα",
            discount: "15%",
            discountType: "percentage",
            bannerImage: "musa_banner",
            merchantLogo: "musa_logo",
            expirationDate: "έως 30-09",
            color: 0x09914E,
            isFavorite: true,
            active: false,
            redeemed: false
        ),
        OfferModel(
            category: "Προτάσεις για εσένα",
            title: "10% έκπτωση",
            description: "στην κατασκήνωση στο The Ranch",
            discount: "10%",
            discountType: "percentage",
            bannerImage: "ranch_banner",
            merchantLogo: "ranch_logo",
            expirationDate: "έως 30-09",
            color: 0xFC9F25,
            isFavorite: false,
            active: false,
            redeemed: false
        ),
        OfferModel(
            category: "Προτάσεις για εσένα",
            title: "1+1 εισιτήριο",
            description: "δώρο στην είσοδο του παιδότοπου",
            discount: "1+1",
            discountType: "buyOneGetOne",
            bannerImage: "ranch_banner",
            merchantLogo: "ranch_logo",
            expirationDate: "έως 30-09",
            color: 0xFC9F25,
            isFavorite: true,
            active: false,
            redeemed: false
        ),
        OfferModel(
            category: "Προτάσεις για εσένα",
            title: "10% έκπτωση",
            description: "σε όλες τις υπηρεσίες",
            discount: "10%",
            discountType: "percentage",
            bannerImage: "ninemia_banner",
            merchantLogo: "ninemia_logo",
            expirationDate: "έως 30-09",
            color: 0xEE417D,
            isFavorite: false,
            active: false,
            redeemed: false
        ),
        OfferModel(
            category: "Προτάσεις για εσένα",
            title: "10% έκπτωση",
            description: "για αγορές από τα φυσικά καταστήματα",
            discount: "10%",
            discountType: "percentage",
            bannerImage: "moustakas_banner",
            merchantLogo: "moustakas_logo",
            expirationDate: "έως 30-09",
            color: 0xEE417D,
            isFavorite: true,
            active: false,
            redeemed: false
        ),
        // Active
        OfferModel(
            category: "Top offers",
            title: "Μόνο 17,95",
            description: "2 πίτσες & Coca-Cola 1,5lt",
            discount: "17,95€",
            discountType: "price",
            bannerImage: "dominos_banner",
            merchantLogo: "dominos_logo",
            expirationDate: "έως 30-09",
            color: 0xEE417D,
            isFavorite: true,
            active: true,
            redeemed: false
        ),
        OfferModel(
            category: "Φαγητό και καφές",
            title: "2€ έκπτωση",
            description: "στην αγορά σου",
            discount: "2€",
            discountType: "amount",
            bannerImage: "coffeeisland_banner",
            merchantLogo: "coffeeisland_logo",
            expirationDate: "έως 30-09",
            color: 0xEE417D,
            isFavorite: false,
            active: true,
            redeemed: false
        ),
        OfferModel(
            category: "Top offers",
            title: "10% έκπτωση",
            description: "σε γλυκούς πειρασμούς",
            discount: "10%",
            discountType: "percentage",
            bannerImage: "zuccherino_banner",
            merchantLogo: "zuccherino_logo",
            expirationDate: "έως 30-09",
            color: 0xEE417D,
            isFavorite: false,
            active: true,
            redeemed: false
        ),
        // Απόδραση
        OfferModel(
            category: "Απόδραση",
            title: "10% έκπτωση",
            description: "στην διαμονή/διασκέδαση σου",
            discount: "10%",
            discountType: "percentage",
            bannerImage: "ninemia_banner",
            merchantLogo: "ninemia_logo",
            expirationDate: "έως 30-09",
            color: 0xEE417D,
            isFavorite: true,
            active: false,
            redeemed: true
        ),
        OfferModel(
            category: "Παιδί",
            title: "1+1 Δώρο",
            description: "στην είσοδο του παιδότοπου",
            discount: "1+1",
            discountType: "buyOneGetOne",
            bannerImage: "moustakas_banner",
            merchantLogo: "moustakas_logo",
            expirationDate: "έως 30-09",
            color: 0xEE417D,
            isFavorite: false,
            active: false,
            redeemed: true
        ),
        // Αγορές
        OfferModel(
            category: "Αγορές",
            title: "15€ έκπτωση",
            description: "για αγορές",
            discount: "15€",
            discountType: "amount",
            bannerImage: "migato_banner",
            merchantLogo: "migato_logo",
            expirationDate: "έως 30-09",
            color: 0xEE417D,
            isFavorite: false,
            active: false,
            redeemed: true
        )
    ]
    
    let couponFilters: [CouponFilterModel] = [
        CouponFilterModel(title: "Ενεργά"),
        CouponFilterModel(title: "Αγαπημένα"),
        CouponFilterModel(title: "Εξαργυρωμένα")
    ]
    
    var couponFilterSelected: CouponFilterModel = CouponFilterModel(title: "Ενεργά")

    var forYouOffersSection: SectionModel?
    var activeOffersSection: SectionModel?
    var favoriteOffersSection: SectionModel?
    var redeemedOffersSection: SectionModel?
    var filteredOffersSection: SectionModel?
    
    public override func viewDidLoad() {
        super.viewDidLoad()

        // Show navigation bar for this screen (with back button)
        self.navigationController?.setNavigationBarHidden(false, animated: false)
        setBackButton()
        setNavigationTitle("Το προφίλ μου")

        // Register XIBs for table view cells
         tableView.register(UINib(nibName: "ProfileHeaderTableViewCell", bundle: MyEmptyClass.resourceBundle()), forCellReuseIdentifier: "ProfileHeaderTableViewCell")
         tableView.register(UINib(nibName: "ProfileQuestionnaireTableViewCell", bundle: MyEmptyClass.resourceBundle()), forCellReuseIdentifier: "ProfileQuestionnaireTableViewCell")
         tableView.register(UINib(nibName: "MyRewardsOffersScrollTableViewCell", bundle: MyEmptyClass.resourceBundle()), forCellReuseIdentifier: "MyRewardsOffersScrollTableViewCell")
         tableView.register(UINib(nibName: "ProfileCouponFiltersTableViewCell", bundle: MyEmptyClass.resourceBundle()), forCellReuseIdentifier: "ProfileCouponFiltersTableViewCell")
         tableView.register(UINib(nibName: "ProfileCouponTableViewCell", bundle: MyEmptyClass.resourceBundle()), forCellReuseIdentifier: "ProfileCouponTableViewCell")
        
        // Set up table view
        tableView.delegate = self
        tableView.dataSource = self
        tableView.separatorStyle = .none
        tableView.estimatedRowHeight = 200
        tableView.rowHeight = UITableView.automaticDimension
        
        initializeSections()
    }

    // MARK: Function
    func initializeSections() {
        // Προτάσεις για εσένα
        let forYouOffers = allOffers.filter { $0.category == "Προτάσεις για εσένα" }
        forYouOffersSection = SectionModel(
            title: "Προτάσεις για εσένα",
            count: forYouOffers.count,
            offers: forYouOffers
        )

        // Favorite Offers
        let activeOffers = allOffers.filter { $0.active ?? false }
        activeOffersSection = SectionModel(
            title: "Ενεργά",
            count: activeOffers.count,
            offers: activeOffers
        )
        
        filteredOffersSection = activeOffersSection

        // Favorite Offers
        let favoriteOffers = allOffers.filter { $0.isFavorite }
        favoriteOffersSection = SectionModel(
            title: "Αγαπημένα",
            count: favoriteOffers.count,
            offers: favoriteOffers
        )

        // Favorite Offers
        let redeemedOffers = allOffers.filter { $0.redeemed ?? false }
        redeemedOffersSection = SectionModel(
            title: "Εξαργυρωμένα",
            count: redeemedOffers.count,
            offers: redeemedOffers
        )

        self.tableView.reloadData()
    }
    
    private func openCouponViewController(with offer: OfferModel) {
        let vc = SwiftWarplyFramework.CouponViewController(nibName: "CouponViewController", bundle: MyEmptyClass.resourceBundle())
        vc.coupon = offer
        
        self.navigationController?.pushViewController(vc, animated: true)
    }
    
    private func handleFilterPress(with filter: CouponFilterModel) {
        switch (filter.title) {
            case "Ενεργά":
                self.filteredOffersSection = activeOffersSection
            case "Αγαπημένα":
                self.filteredOffersSection = favoriteOffersSection
            case "Εξαργυρωμένα":
                self.filteredOffersSection = redeemedOffersSection
            default:
                self.filteredOffersSection = activeOffersSection
        }
    }
}

// MARK: - TableView
extension ProfileViewController: UITableViewDelegate, UITableViewDataSource {
    
    public func numberOfSections(in tableView: UITableView) -> Int {
        // return 4 + (filteredOffersSection?.offers.count ?? 0)
        return 5
    }
    
    public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // return 1
        if (section <= 3) {
            return 1
        } else {
            return filteredOffersSection?.offers.count ?? 0
        }
    }
    
    public func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return UITableView.automaticDimension
    }
    
    public func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        return nil
//        if (section <= 3) {
//            return nil
//        } else {
//            // Return clear view for spacing
////            let headerView = UIView()
//            let headerView = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 19))
//            headerView.backgroundColor = UIColor.white
//            return headerView
//        }
    }

    public func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 0.0
//        if (section <= 3) {
//            return 0.0
//        } else {
//            return 19.0
//        }
    }

    public func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
//        return CGFloat.leastNormalMagnitude
        return 0.0
    }

    public func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        return nil
    }
    
    public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if (indexPath.section == 0) {
            let cell = tableView.dequeueReusableCell(withIdentifier: "ProfileHeaderTableViewCell", for: indexPath) as! ProfileHeaderTableViewCell
//            cell.delegate = self  // Set the banner offers delegate
//            cell.configureCell(data: self.bannerOffersSection)
//            cell.parent = self
            return cell
            
        } else if (indexPath.section == 1) {
            let cell = tableView.dequeueReusableCell(withIdentifier: "ProfileQuestionnaireTableViewCell", for: indexPath) as! ProfileQuestionnaireTableViewCell
            return cell
            
        } else if (indexPath.section == 2) {
            let cell = tableView.dequeueReusableCell(withIdentifier: "MyRewardsOffersScrollTableViewCell", for: indexPath) as! MyRewardsOffersScrollTableViewCell
            
            cell.delegate = self  // Set the offers delegate
            cell.configureCell(data: self.forYouOffersSection, isInProfile: true)
        
            return cell
            
        } else if (indexPath.section == 3) {
            let cell = tableView.dequeueReusableCell(withIdentifier: "ProfileCouponFiltersTableViewCell", for: indexPath) as! ProfileCouponFiltersTableViewCell
            
            cell.delegate = self  // Set the offers delegate
            cell.configureCell(data: self.couponFilters, couponFilterSelected: self.couponFilterSelected)
        
            return cell
            
        } else {
            let cell = tableView.dequeueReusableCell(withIdentifier: "ProfileCouponTableViewCell", for: indexPath) as! ProfileCouponTableViewCell
            if let offer = self.filteredOffersSection?.offers[indexPath.row] {
                cell.configureCell(data: offer)
            }
            return cell
        }
        
    }
    
    public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if (indexPath.section <= 3) {
            // Do nothing
        } else {
            if let offer = self.filteredOffersSection?.offers[indexPath.row] {
                openCouponViewController(with: offer)
            }
        }
    }
}

// Add delegate conformance
extension ProfileViewController: MyRewardsOffersScrollTableViewCellDelegate {
    func didSelectOffer(_ offer: OfferModel) {
        // Navigate to CouponViewController
        openCouponViewController(with: offer)
    }
}

// Add delegate conformance
extension ProfileViewController: ProfileCouponFiltersTableViewCellDelegate {
    func didSelectFilter(_ filter: CouponFilterModel) {
        self.couponFilterSelected = filter
        handleFilterPress(with: filter)
        self.tableView.reloadData()
    }
}