diff --git a/AirRobeWidget/releases/AirRobeWidget-1.0.2.aar b/AirRobeWidget/releases/AirRobeWidget-1.0.2.aar new file mode 100644 index 0000000..17892b4 Binary files /dev/null and b/AirRobeWidget/releases/AirRobeWidget-1.0.2.aar differ diff --git a/AirRobeWidget/src/main/kotlin/com/airrobe/widgetsdk/airrobewidget/AirRobeWidget.kt b/AirRobeWidget/src/main/kotlin/com/airrobe/widgetsdk/airrobewidget/AirRobeWidget.kt index 404b937..5a90d67 100644 --- a/AirRobeWidget/src/main/kotlin/com/airrobe/widgetsdk/airrobewidget/AirRobeWidget.kt +++ b/AirRobeWidget/src/main/kotlin/com/airrobe/widgetsdk/airrobewidget/AirRobeWidget.kt @@ -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?) { @@ -72,16 +75,11 @@ object AirRobeWidget { getShoppingDataController.start(config.appId, config.mode) } - fun checkMultiOptInEligibility(items: Array): Boolean { - if (widgetInstance.shopModel == null || items.isNullOrEmpty()) { + fun checkMultiOptInEligibility(items: ArrayList): Boolean { + if (widgetInstance.shopModel == null || items.isNullOrEmpty() || widgetInstance.categoryMapping.categoryMappingsHashmap.isNullOrEmpty()) { return false } - - val newItems = arrayListOf() - for (item in items) { - newItems.add(item.toString()) - } - val to = widgetInstance.shopModel!!.checkCategoryEligible(newItems) + val to = widgetInstance.categoryMapping.checkCategoryEligible(items) return to != null } diff --git a/AirRobeWidget/src/main/kotlin/com/airrobe/widgetsdk/airrobewidget/config/AirRobeWidgetInstance.kt b/AirRobeWidget/src/main/kotlin/com/airrobe/widgetsdk/airrobewidget/config/AirRobeWidgetInstance.kt index f947dde..f0595f4 100644 --- a/AirRobeWidget/src/main/kotlin/com/airrobe/widgetsdk/airrobewidget/config/AirRobeWidgetInstance.kt +++ b/AirRobeWidget/src/main/kotlin/com/airrobe/widgetsdk/airrobewidget/config/AirRobeWidgetInstance.kt @@ -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 { @@ -8,6 +9,7 @@ internal object AirRobeWidgetInstance { field = value changeListener?.onShopModelChange() } + var categoryMapping = AirRobeCategoryMappingHashMap(HashMap()) var configuration: AirRobeWidgetConfig? = null set(value) { field = value diff --git a/AirRobeWidget/src/main/kotlin/com/airrobe/widgetsdk/airrobewidget/service/models/AirRobeGetShoppingDataModel.kt b/AirRobeWidget/src/main/kotlin/com/airrobe/widgetsdk/airrobewidget/service/models/AirRobeGetShoppingDataModel.kt index 97f4635..bd0fc17 100644 --- a/AirRobeWidget/src/main/kotlin/com/airrobe/widgetsdk/airrobewidget/service/models/AirRobeGetShoppingDataModel.kt +++ b/AirRobeWidget/src/main/kotlin/com/airrobe/widgetsdk/airrobewidget/service/models/AirRobeGetShoppingDataModel.kt @@ -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 ) { fun checkCategoryEligible(items: ArrayList): String? { val eligibleItem = items.firstOrNull { bestCategoryMapping(factorize(it)) != null } @@ -22,9 +37,8 @@ internal data class AirRobeGetShoppingDataModel ( } private fun bestCategoryMapping(categoryArray: List): 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 @@ -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( diff --git a/AirRobeWidget/src/main/kotlin/com/airrobe/widgetsdk/airrobewidget/widgets/AirRobeMultiOptIn.kt b/AirRobeWidget/src/main/kotlin/com/airrobe/widgetsdk/airrobewidget/widgets/AirRobeMultiOptIn.kt index df8ffd6..e39a4b8 100644 --- a/AirRobeWidget/src/main/kotlin/com/airrobe/widgetsdk/airrobewidget/widgets/AirRobeMultiOptIn.kt +++ b/AirRobeWidget/src/main/kotlin/com/airrobe/widgetsdk/airrobewidget/widgets/AirRobeMultiOptIn.kt @@ -43,7 +43,7 @@ class AirRobeMultiOptIn @JvmOverloads constructor( Closed } private var expandType: ExpandType = ExpandType.Closed - private var items: Array? = arrayOf() + private var items: ArrayList = arrayListOf() var borderColor: Int = if (widgetInstance.borderColor == 0) @@ -245,7 +245,7 @@ class AirRobeMultiOptIn @JvmOverloads constructor( } fun initialize( - items: Array = arrayOf() + items: ArrayList = arrayListOf() ) { this.items = items initializeOptInWidget() @@ -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) @@ -271,11 +271,7 @@ class AirRobeMultiOptIn @JvmOverloads constructor( return } - val newItems = arrayListOf() - 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)) diff --git a/AirRobeWidget/src/main/kotlin/com/airrobe/widgetsdk/airrobewidget/widgets/AirRobeOptIn.kt b/AirRobeWidget/src/main/kotlin/com/airrobe/widgetsdk/airrobewidget/widgets/AirRobeOptIn.kt index a824243..3745afb 100644 --- a/AirRobeWidget/src/main/kotlin/com/airrobe/widgetsdk/airrobewidget/widgets/AirRobeOptIn.kt +++ b/AirRobeWidget/src/main/kotlin/com/airrobe/widgetsdk/airrobewidget/widgets/AirRobeOptIn.kt @@ -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 diff --git a/demo/libs/AirRobeWidget.aar b/demo/libs/AirRobeWidget.aar index f17e122..17892b4 100644 Binary files a/demo/libs/AirRobeWidget.aar and b/demo/libs/AirRobeWidget.aar differ diff --git a/demo/src/main/java/com/airrobe/widgetsdk/airrobedemo/activities/MultiOptInActivity.kt b/demo/src/main/java/com/airrobe/widgetsdk/airrobedemo/activities/MultiOptInActivity.kt index d743381..90ec74d 100644 --- a/demo/src/main/java/com/airrobe/widgetsdk/airrobedemo/activities/MultiOptInActivity.kt +++ b/demo/src/main/java/com/airrobe/widgetsdk/airrobedemo/activities/MultiOptInActivity.kt @@ -12,7 +12,7 @@ class MultiOptInActivity : AppCompatActivity() { setContentView(R.layout.activity_multi_opt_in) val optInWidget = findViewById(R.id.multi_opt_in_widget) optInWidget.initialize( - arrayOf("Accessories", "Accessories") + arrayListOf("Accessories", "Accessories") ) // Way to set widget colors // optInWidget.borderColor = Color.BLUE