Manos Chorianopoulos

dynamic CouponSet categories at MyRewardsVC

......@@ -202,8 +202,7 @@ import UIKit
self.merchantCategories = categories
print("✅ [MyRewardsViewController] Loaded \(categories.count) merchant categories")
// TODO: Implement category-based filtering for coupon sets sections
// For now, create the standard coupon sets section
// Create coupon sets sections with category-based filtering
self.createCouponSetsSection()
} failureCallback: { [weak self] errorCode in
......@@ -214,44 +213,131 @@ import UIKit
}
private func createCouponSetsSection() {
// TODO: IMPLEMENT CATEGORY-BASED FILTERING
//
// Current logic: Creates one section with all coupon sets
//
// Future enhancement: Filter coupon sets into different sections based on categories
// Logic:
// 1. For each couponset, get its merchant_uuid
// 2. Find the merchant with that merchant_uuid in self.merchants
// 3. Get the merchant's category_uuid
// 4. Find the category with that category_uuid in self.merchantCategories
// 5. Group coupon sets by category
// 6. Create separate sections for each category
//
// Example structure after filtering:
// - Section: "Εκπαίδευση" (Education) - coupon sets from education merchants
// - Section: "Ψυχαγωγία" (Entertainment) - coupon sets from entertainment merchants
// - etc.
//
// Implementation steps:
// 1. Create a dictionary to group coupon sets by category: [String: [CouponSetItemModel]]
// 2. Iterate through self.couponSets
// 3. For each coupon set, find its merchant and category
// 4. Add coupon set to the appropriate category group
// 5. Create a SectionModel for each category group
// 6. Sort sections by category name or priority
print("🔍 [MyRewardsViewController] Starting coupon filtering:")
print(" - Coupon Sets: \(couponSets.count)")
print(" - Merchants: \(merchants.count)")
print(" - Categories: \(merchantCategories.count)")
// Current implementation (temporary):
// Check if we have all required data for filtering
guard !couponSets.isEmpty, !merchants.isEmpty, !merchantCategories.isEmpty else {
print("⚠️ [MyRewardsViewController] Missing data for filtering - using fallback single section")
print(" - Coupon Sets Empty: \(couponSets.isEmpty)")
print(" - Merchants Empty: \(merchants.isEmpty)")
print(" - Categories Empty: \(merchantCategories.isEmpty)")
// Fallback: Create single section with all coupon sets
createSingleCouponSetsSection()
return
}
// Group coupon sets by merchant category
var categorySections: [SectionModel] = []
var processedCouponSets: Set<String> = [] // Track processed coupon sets to avoid duplicates
print("🔄 [MyRewardsViewController] Processing categories for filtering...")
for category in merchantCategories {
// Find merchants in this category
let categoryMerchants = merchants.filter { merchant in
merchant._category_uuid == category._uuid
}
print(" - Category '\(category.displayName)' has \(categoryMerchants.count) merchants")
// Find coupon sets from merchants in this category
let categoryCouponSets = couponSets.filter { couponSet in
// Skip if already processed (avoid duplicates)
guard !processedCouponSets.contains(couponSet._uuid) else { return false }
// Check if this coupon set belongs to any merchant in this category
let belongsToCategory = categoryMerchants.contains { merchant in
merchant._uuid == couponSet._merchant_uuid
}
if belongsToCategory {
processedCouponSets.insert(couponSet._uuid)
}
return belongsToCategory
}
print(" - Category '\(category.displayName)' has \(categoryCouponSets.count) coupon sets")
// Create section if we have coupon sets for this category
if !categoryCouponSets.isEmpty {
let section = SectionModel(
sectionType: .myRewardsHorizontalCouponsets,
title: category.displayName,
items: categoryCouponSets,
itemType: .couponSets
)
categorySections.append(section)
print(" ✅ Created section for '\(category.displayName)' with \(categoryCouponSets.count) items")
}
}
// Handle any remaining unmatched coupon sets
let unmatchedCouponSets = couponSets.filter { couponSet in
!processedCouponSets.contains(couponSet._uuid)
}
if !unmatchedCouponSets.isEmpty {
print(" ⚠️ Found \(unmatchedCouponSets.count) unmatched coupon sets - adding to 'Άλλες Προσφορές' section")
let unmatchedSection = SectionModel(
sectionType: .myRewardsHorizontalCouponsets,
title: "Άλλες Προσφορές", // "Other Offers"
items: unmatchedCouponSets,
itemType: .couponSets
)
categorySections.append(unmatchedSection)
}
// Sort sections by title for consistent ordering
categorySections.sort { section1, section2 in
let title1 = section1.title ?? ""
let title2 = section2.title ?? ""
// Put "Άλλες Προσφορές" at the end
if title1 == "Άλλες Προσφορές" { return false }
if title2 == "Άλλες Προσφορές" { return true }
return title1.localizedCaseInsensitiveCompare(title2) == .orderedAscending
}
print("✅ [MyRewardsViewController] Created \(categorySections.count) category sections:")
for section in categorySections {
print(" - '\(section.title ?? "Unknown")': \(section.itemCount) coupon sets")
}
// Add category sections to main sections array
self.sections.append(contentsOf: categorySections)
// Reload table view with new sections
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
private func createSingleCouponSetsSection() {
print("📦 [MyRewardsViewController] Creating single fallback coupon sets section")
// Fallback: Single section with all coupon sets
if !self.couponSets.isEmpty {
let couponSetsSection = SectionModel(
sectionType: .myRewardsHorizontalCouponsets,
title: "Προσφορές",
title: "Προσφορές", // "Offers"
items: self.couponSets,
itemType: .couponSets
)
self.sections.append(couponSetsSection)
print("✅ [MyRewardsViewController] Created fallback section with \(self.couponSets.count) coupon sets")
} else {
print("⚠️ [MyRewardsViewController] No coupon sets available - no section created")
}
// Reload table view with new sections
DispatchQueue.main.async {
self.tableView.reloadData()
}
......