Skip to content

Commit

Permalink
Merge pull request #13 from airrobe/eli/bestCategoryMappingLogicUpdate
Browse files Browse the repository at this point in the history
Eli/best category mapping logic update
  • Loading branch information
creative-dev-lab authored Apr 27, 2022
2 parents 81b5a86 + 0f8f21f commit 7264aec
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 31 deletions.
Binary file added AirRobeWidget/releases/AirRobeWidget-1.0.2.aar
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ object AirRobeWidget {
getShoppingDataController.airRobeGetShoppingDataListener = object : AirRobeGetShoppingDataListener {
override fun onSuccessGetShoppingDataApi(shopModel: AirRobeGetShoppingDataModel) {
widgetInstance.shopModel = shopModel
for (i in 0 until shopModel.data.shop.categoryMappings.count()) {
widgetInstance.categoryMapping.categoryMappingsHashmap[shopModel.data.shop.categoryMappings[i].from] = shopModel.data.shop.categoryMappings[i]
}
}

override fun onFailedGetShoppingDataApi(error: String?) {
Expand All @@ -72,16 +75,11 @@ object AirRobeWidget {
getShoppingDataController.start(config.appId, config.mode)
}

fun checkMultiOptInEligibility(items: Array<CharSequence>): Boolean {
if (widgetInstance.shopModel == null || items.isNullOrEmpty()) {
fun checkMultiOptInEligibility(items: ArrayList<String>): Boolean {
if (widgetInstance.shopModel == null || items.isNullOrEmpty() || widgetInstance.categoryMapping.categoryMappingsHashmap.isNullOrEmpty()) {
return false
}

val newItems = arrayListOf<String>()
for (item in items) {
newItems.add(item.toString())
}
val to = widgetInstance.shopModel!!.checkCategoryEligible(newItems)
val to = widgetInstance.categoryMapping.checkCategoryEligible(items)
return to != null
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.airrobe.widgetsdk.airrobewidget.config

import com.airrobe.widgetsdk.airrobewidget.service.models.AirRobeCategoryMappingHashMap
import com.airrobe.widgetsdk.airrobewidget.service.models.AirRobeGetShoppingDataModel

internal object AirRobeWidgetInstance {
Expand All @@ -8,6 +9,7 @@ internal object AirRobeWidgetInstance {
field = value
changeListener?.onShopModelChange()
}
var categoryMapping = AirRobeCategoryMappingHashMap(HashMap())
var configuration: AirRobeWidgetConfig? = null
set(value) {
field = value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,21 @@ package com.airrobe.widgetsdk.airrobewidget.service.models

internal data class AirRobeGetShoppingDataModel (
var data: AirRobeShoppingDataModel
) {
fun isBelowPriceThreshold(department: String?, price: Float) : Boolean {
if (department.isNullOrEmpty()) return false
val applicablePriceThreshold = data.shop.minimumPriceThresholds.firstOrNull {
it.department?.lowercase() == department.lowercase()
} ?: data.shop.minimumPriceThresholds.firstOrNull {
it.default
} ?: return false

return price < (applicablePriceThreshold.minimumPriceCents / 100)
}
}

internal data class AirRobeCategoryMappingHashMap(
var categoryMappingsHashmap: HashMap<String, AirRobeCategoryMapping>
) {
fun checkCategoryEligible(items: ArrayList<String>): String? {
val eligibleItem = items.firstOrNull { bestCategoryMapping(factorize(it)) != null }
Expand All @@ -22,9 +37,8 @@ internal data class AirRobeGetShoppingDataModel (
}

private fun bestCategoryMapping(categoryArray: List<String>): String? {
val categoryMappings = data.shop.categoryMappings
for (category in categoryArray) {
val filteredMapping = categoryMappings.firstOrNull { it.from == category }
val filteredMapping = categoryMappingsHashmap[category]
if (filteredMapping != null) {
return if (filteredMapping.to.isNullOrEmpty() || filteredMapping.excluded) {
null
Expand All @@ -35,17 +49,6 @@ internal data class AirRobeGetShoppingDataModel (
}
return null
}

fun isBelowPriceThreshold(department: String?, price: Float) : Boolean {
if (department.isNullOrEmpty()) return false
val applicablePriceThreshold = data.shop.minimumPriceThresholds.firstOrNull {
it.department?.lowercase() == department.lowercase()
} ?: data.shop.minimumPriceThresholds.firstOrNull {
it.default
} ?: return false

return price < (applicablePriceThreshold.minimumPriceCents / 100)
}
}

internal data class AirRobeShoppingDataModel(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class AirRobeMultiOptIn @JvmOverloads constructor(
Closed
}
private var expandType: ExpandType = ExpandType.Closed
private var items: Array<CharSequence>? = arrayOf()
private var items: ArrayList<String> = arrayListOf()

var borderColor: Int =
if (widgetInstance.borderColor == 0)
Expand Down Expand Up @@ -245,7 +245,7 @@ class AirRobeMultiOptIn @JvmOverloads constructor(
}

fun initialize(
items: Array<CharSequence> = arrayOf()
items: ArrayList<String> = arrayListOf()
) {
this.items = items
initializeOptInWidget()
Expand All @@ -258,7 +258,7 @@ class AirRobeMultiOptIn @JvmOverloads constructor(
AirRobeSharedPreferenceManager.setOrderOptedIn(context, false)
return
}
if (widgetInstance.shopModel == null) {
if (widgetInstance.shopModel == null || widgetInstance.categoryMapping.categoryMappingsHashmap.isNullOrEmpty()) {
Log.e(TAG, "Category Mapping Info is not loaded")
visibility = GONE
AirRobeSharedPreferenceManager.setOrderOptedIn(context, false)
Expand All @@ -271,11 +271,7 @@ class AirRobeMultiOptIn @JvmOverloads constructor(
return
}

val newItems = arrayListOf<String>()
for (item in items!!) {
newItems.add(item.toString())
}
val to = widgetInstance.shopModel!!.checkCategoryEligible(newItems)
val to = widgetInstance.categoryMapping.checkCategoryEligible(items)
if (to != null) {
visibility = VISIBLE
AirRobeSharedPreferenceManager.setOrderOptedIn(context, AirRobeSharedPreferenceManager.getOptedIn(context))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ class AirRobeOptIn @JvmOverloads constructor(
visibility = GONE
return
}
val to = widgetInstance.shopModel!!.checkCategoryEligible(arrayListOf(category!!))
val to = widgetInstance.categoryMapping.checkCategoryEligible(arrayListOf(category!!))
if (to != null) {
if (widgetInstance.shopModel!!.isBelowPriceThreshold(department, priceCents)) {
visibility = GONE
Expand Down
Binary file modified demo/libs/AirRobeWidget.aar
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class MultiOptInActivity : AppCompatActivity() {
setContentView(R.layout.activity_multi_opt_in)
val optInWidget = findViewById<AirRobeMultiOptIn>(R.id.multi_opt_in_widget)
optInWidget.initialize(
arrayOf("Accessories", "Accessories")
arrayListOf("Accessories", "Accessories")
)
// Way to set widget colors
// optInWidget.borderColor = Color.BLUE
Expand Down

0 comments on commit 7264aec

Please sign in to comment.