Panagiotis Triantafyllou

changed sorting categories in couponsets

...@@ -143,7 +143,7 @@ public class Couponset implements Parcelable, Serializable { ...@@ -143,7 +143,7 @@ public class Couponset implements Parcelable, Serializable {
143 private String endDate; 143 private String endDate;
144 private JSONObject hideCondition; 144 private JSONObject hideCondition;
145 private JSONObject showAsBanner; 145 private JSONObject showAsBanner;
146 - private String segment; 146 + private JSONObject segment;
147 private String offerCategory; 147 private String offerCategory;
148 private JSONObject tagging; 148 private JSONObject tagging;
149 private JSONArray regions; 149 private JSONArray regions;
...@@ -325,7 +325,7 @@ public class Couponset implements Parcelable, Serializable { ...@@ -325,7 +325,7 @@ public class Couponset implements Parcelable, Serializable {
325 this.endDate = optNullableString(json, END_DATE); 325 this.endDate = optNullableString(json, END_DATE);
326 this.hideCondition = json.optJSONObject(HIDE_CONDITION); 326 this.hideCondition = json.optJSONObject(HIDE_CONDITION);
327 this.showAsBanner = json.optJSONObject(SHOW_AS_BANNER); 327 this.showAsBanner = json.optJSONObject(SHOW_AS_BANNER);
328 - this.segment = optNullableString(json, SEGMENT); 328 + this.segment = json.optJSONObject(SEGMENT);
329 this.offerCategory = optNullableString(json, OFFER_CATEGORY); 329 this.offerCategory = optNullableString(json, OFFER_CATEGORY);
330 this.tagging = json.optJSONObject(TAGGING); 330 this.tagging = json.optJSONObject(TAGGING);
331 this.regions = json.optJSONArray(REGIONS); 331 this.regions = json.optJSONArray(REGIONS);
...@@ -376,7 +376,7 @@ public class Couponset implements Parcelable, Serializable { ...@@ -376,7 +376,7 @@ public class Couponset implements Parcelable, Serializable {
376 this.endDate = optNullableString(json, END_DATE); 376 this.endDate = optNullableString(json, END_DATE);
377 this.hideCondition = json.optJSONObject(HIDE_CONDITION); 377 this.hideCondition = json.optJSONObject(HIDE_CONDITION);
378 this.showAsBanner = json.optJSONObject(SHOW_AS_BANNER); 378 this.showAsBanner = json.optJSONObject(SHOW_AS_BANNER);
379 - this.segment = optNullableString(json, SEGMENT); 379 + this.segment = json.optJSONObject(SEGMENT);
380 this.offerCategory = optNullableString(json, OFFER_CATEGORY); 380 this.offerCategory = optNullableString(json, OFFER_CATEGORY);
381 this.tagging = json.optJSONObject(TAGGING); 381 this.tagging = json.optJSONObject(TAGGING);
382 this.regions = json.optJSONArray(REGIONS); 382 this.regions = json.optJSONArray(REGIONS);
...@@ -469,7 +469,12 @@ public class Couponset implements Parcelable, Serializable { ...@@ -469,7 +469,12 @@ public class Couponset implements Parcelable, Serializable {
469 } catch (JSONException e) { 469 } catch (JSONException e) {
470 this.showAsBanner = null; 470 this.showAsBanner = null;
471 } 471 }
472 - this.segment = source.readString(); 472 + try {
473 + String segmentStr = source.readString();
474 + this.segment = segmentStr != null ? new JSONObject(segmentStr) : null;
475 + } catch (JSONException e) {
476 + this.segment = null;
477 + }
473 this.offerCategory = source.readString(); 478 this.offerCategory = source.readString();
474 try { 479 try {
475 String taggingStr = source.readString(); 480 String taggingStr = source.readString();
...@@ -541,7 +546,7 @@ public class Couponset implements Parcelable, Serializable { ...@@ -541,7 +546,7 @@ public class Couponset implements Parcelable, Serializable {
541 dest.writeString(this.endDate); 546 dest.writeString(this.endDate);
542 dest.writeString(this.hideCondition != null ? this.hideCondition.toString() : null); 547 dest.writeString(this.hideCondition != null ? this.hideCondition.toString() : null);
543 dest.writeString(this.showAsBanner != null ? this.showAsBanner.toString() : null); 548 dest.writeString(this.showAsBanner != null ? this.showAsBanner.toString() : null);
544 - dest.writeString(this.segment); 549 + dest.writeString(this.segment != null ? this.segment.toString() : null);
545 dest.writeString(this.offerCategory); 550 dest.writeString(this.offerCategory);
546 dest.writeString(this.tagging != null ? this.tagging.toString() : null); 551 dest.writeString(this.tagging != null ? this.tagging.toString() : null);
547 dest.writeString(this.regions != null ? this.regions.toString() : null); 552 dest.writeString(this.regions != null ? this.regions.toString() : null);
...@@ -603,7 +608,7 @@ public class Couponset implements Parcelable, Serializable { ...@@ -603,7 +608,7 @@ public class Couponset implements Parcelable, Serializable {
603 jObj.put(END_DATE, this.endDate != null ? this.endDate : JSONObject.NULL); 608 jObj.put(END_DATE, this.endDate != null ? this.endDate : JSONObject.NULL);
604 jObj.put(HIDE_CONDITION, this.hideCondition != null ? this.hideCondition : JSONObject.NULL); 609 jObj.put(HIDE_CONDITION, this.hideCondition != null ? this.hideCondition : JSONObject.NULL);
605 jObj.put(SHOW_AS_BANNER, this.showAsBanner != null ? this.showAsBanner : JSONObject.NULL); 610 jObj.put(SHOW_AS_BANNER, this.showAsBanner != null ? this.showAsBanner : JSONObject.NULL);
606 - jObj.put(SEGMENT, this.segment != null ? this.segment : JSONObject.NULL); 611 + jObj.put(SEGMENT, this.segment != null ? this.segment : JSONObject.NULL); // segment is JSONObject
607 jObj.put(OFFER_CATEGORY, this.offerCategory != null ? this.offerCategory : JSONObject.NULL); 612 jObj.put(OFFER_CATEGORY, this.offerCategory != null ? this.offerCategory : JSONObject.NULL);
608 jObj.put(TAGGING, this.tagging != null ? this.tagging : JSONObject.NULL); 613 jObj.put(TAGGING, this.tagging != null ? this.tagging : JSONObject.NULL);
609 jObj.put(REGIONS, this.regions != null ? this.regions : JSONObject.NULL); 614 jObj.put(REGIONS, this.regions != null ? this.regions : JSONObject.NULL);
...@@ -852,10 +857,14 @@ public class Couponset implements Parcelable, Serializable { ...@@ -852,10 +857,14 @@ public class Couponset implements Parcelable, Serializable {
852 return showAsBanner; 857 return showAsBanner;
853 } 858 }
854 859
855 - public String getSegment() { 860 + public JSONObject getSegment() {
856 return segment; 861 return segment;
857 } 862 }
858 863
864 + public String getSegmentValue() {
865 + return segment != null ? segment.optString("value", null) : null;
866 + }
867 +
859 public String getOfferCategory() { 868 public String getOfferCategory() {
860 return offerCategory; 869 return offerCategory;
861 } 870 }
...@@ -1068,7 +1077,7 @@ public class Couponset implements Parcelable, Serializable { ...@@ -1068,7 +1077,7 @@ public class Couponset implements Parcelable, Serializable {
1068 this.showAsBanner = showAsBanner; 1077 this.showAsBanner = showAsBanner;
1069 } 1078 }
1070 1079
1071 - public void setSegment(String segment) { 1080 + public void setSegment(JSONObject segment) {
1072 this.segment = segment; 1081 this.segment = segment;
1073 } 1082 }
1074 1083
......
...@@ -429,18 +429,40 @@ public class WarplyManager { ...@@ -429,18 +429,40 @@ public class WarplyManager {
429 } 429 }
430 } 430 }
431 431
432 + // Get user segments once
433 + ArrayList<String> userSegments = new ArrayList<>();
434 + User user = WarplyManagerHelper.getUser();
435 + if (user != null && user.getUserSegmentsList() != null) {
436 + userSegments = user.getUserSegmentsList();
437 + }
438 + boolean userHasGreenPass = userSegments.contains("GreenPass");
439 +
440 + // Eligibility groups: one ArrayList per matching user segment value
441 + LinkedHashMap<String, ArrayList<Couponset>> eligibilityGroups = new LinkedHashMap<>();
432 ArrayList<Couponset> topOffers = new ArrayList<>(); 442 ArrayList<Couponset> topOffers = new ArrayList<>();
443 + ArrayList<Couponset> green = new ArrayList<>();
433 LinkedHashMap<String, ArrayList<Couponset>> categoryGroups = new LinkedHashMap<>(); 444 LinkedHashMap<String, ArrayList<Couponset>> categoryGroups = new LinkedHashMap<>();
434 445
435 for (Couponset couponset : resultCouponsets) { 446 for (Couponset couponset : resultCouponsets) {
436 - if (couponset.isPromoted()) { 447 + String segmentValue = couponset.getSegmentValue();
448 +
449 + if ("sustainability".equals(segmentValue)) {
450 + // Only add to Green if user has GreenPass, otherwise ignore
451 + if (userHasGreenPass) {
452 + green.add(couponset);
453 + }
454 + } else if (segmentValue != null && !segmentValue.isEmpty() && userSegments.contains(segmentValue)) {
455 + // Eligibility: one dedicated category per matching segment value
456 + eligibilityGroups.computeIfAbsent(segmentValue, k -> new ArrayList<>()).add(couponset);
457 + } else if (couponset.isPromoted()) {
458 + // Top Offers
437 topOffers.add(couponset); 459 topOffers.add(couponset);
438 } else { 460 } else {
461 + // Rest: group by offerCategory
439 String category = couponset.getOfferCategory(); 462 String category = couponset.getOfferCategory();
440 if (category == null || category.isEmpty()) { 463 if (category == null || category.isEmpty()) {
441 category = "Other"; 464 category = "Other";
442 } 465 }
443 - // If this category is a child, map it to its parent
444 if (childToParentMap.containsKey(category)) { 466 if (childToParentMap.containsKey(category)) {
445 category = childToParentMap.get(category); 467 category = childToParentMap.get(category);
446 } 468 }
...@@ -448,16 +470,19 @@ public class WarplyManager { ...@@ -448,16 +470,19 @@ public class WarplyManager {
448 } 470 }
449 } 471 }
450 472
473 + // Build final map in order: Eligibilities → Top Offers → Green → Rest
474 + for (Map.Entry<String, ArrayList<Couponset>> entry : eligibilityGroups.entrySet()) {
475 + if (!entry.getValue().isEmpty()) {
476 + categorizedMap.put(entry.getKey(), entry.getValue());
477 + }
478 + }
479 +
451 if (!topOffers.isEmpty()) { 480 if (!topOffers.isEmpty()) {
452 categorizedMap.put("Top offers", topOffers); 481 categorizedMap.put("Top offers", topOffers);
453 } 482 }
454 483
455 - String[] priorityCategories = {"Αγορές", "Φαγητό και καφές"}; 484 + if (!green.isEmpty()) {
456 - for (String priorityCategory : priorityCategories) { 485 + categorizedMap.put("Green", green);
457 - ArrayList<Couponset> priorityList = categoryGroups.remove(priorityCategory);
458 - if (priorityList != null && !priorityList.isEmpty()) {
459 - categorizedMap.put(priorityCategory, priorityList);
460 - }
461 } 486 }
462 487
463 for (Map.Entry<String, ArrayList<Couponset>> entry : categoryGroups.entrySet()) { 488 for (Map.Entry<String, ArrayList<Couponset>> entry : categoryGroups.entrySet()) {
......