From 2c4ec1f19297e9bc42312919ec05d235b07c070b Mon Sep 17 00:00:00 2001 From: Phillip Thelen Date: Mon, 17 May 2021 12:48:23 +0200 Subject: [PATCH] Fixes #1531 --- .../inventory/StableRecyclerAdapter.kt | 57 ++++++++++++++++--- .../stable/StableRecyclerFragment.kt | 19 +++---- 2 files changed, 56 insertions(+), 20 deletions(-) diff --git a/Habitica/src/main/java/com/habitrpg/android/habitica/ui/adapter/inventory/StableRecyclerAdapter.kt b/Habitica/src/main/java/com/habitrpg/android/habitica/ui/adapter/inventory/StableRecyclerAdapter.kt index d0db7ff2e..35fe8f3b2 100644 --- a/Habitica/src/main/java/com/habitrpg/android/habitica/ui/adapter/inventory/StableRecyclerAdapter.kt +++ b/Habitica/src/main/java/com/habitrpg/android/habitica/ui/adapter/inventory/StableRecyclerAdapter.kt @@ -13,6 +13,8 @@ import com.habitrpg.android.habitica.extensions.inflate import com.habitrpg.android.habitica.helpers.MainNavigationController import com.habitrpg.android.habitica.models.inventory.* import com.habitrpg.android.habitica.models.user.OwnedItem +import com.habitrpg.android.habitica.models.user.OwnedMount +import com.habitrpg.android.habitica.models.user.OwnedPet import com.habitrpg.android.habitica.ui.fragments.inventory.stable.StableFragmentDirections import com.habitrpg.android.habitica.ui.helpers.loadImage import com.habitrpg.android.habitica.ui.viewHolders.MountViewHolder @@ -21,6 +23,7 @@ import com.habitrpg.android.habitica.ui.viewHolders.SectionViewHolder import io.reactivex.rxjava3.core.BackpressureStrategy import io.reactivex.rxjava3.core.Flowable import io.reactivex.rxjava3.subjects.PublishSubject +import io.realm.RealmResults class StableRecyclerAdapter : RecyclerView.Adapter() { @@ -30,12 +33,48 @@ class StableRecyclerAdapter : RecyclerView.Adapter() { var animalIngredientsRetriever: ((Animal, ((Pair) -> Unit)) -> Unit)? = null var itemType: String? = null private val equipEvents = PublishSubject.create() - var ownedEggs: Map? = null + + private var existingMounts: RealmResults? = null + private var ownedMounts: Map? = null + private var ownedItems: Map? = null + private var ownsSaddles: Boolean = false fun getEquipFlowable(): Flowable { return equipEvents.toFlowable(BackpressureStrategy.DROP) } + private fun canRaiseToMount(pet: Pet): Boolean { + for (mount in existingMounts ?: emptyList()) { + if (mount.key == pet.key) { + return !(ownedMounts?.get(mount.key)?.owned ?: false) + } + } + return false + } + + private fun eggCount(pet: Pet): Int { + return ownedItems?.get(pet.animal + "-eggs")?.numberOwned ?: 0 + } + private fun potionCount(pet: Pet): Int { + return ownedItems?.get(pet.color + "-hatchingPotions")?.numberOwned ?: 0 + } + + fun setOwnedMounts(ownedMounts: Map) { + this.ownedMounts = ownedMounts + notifyDataSetChanged() + } + + fun setOwnedItems(ownedItems: Map) { + this.ownedItems = ownedItems + ownsSaddles = if (ownedItems.containsKey("Saddle-food")) (ownedItems["Saddle-food"]?.numberOwned ?: 0)> 0 else false + notifyDataSetChanged() + } + + fun setExistingMounts(existingMounts: RealmResults) { + this.existingMounts = existingMounts + notifyDataSetChanged() + } + private var itemList: List = ArrayList() fun setItemList(itemList: List) { @@ -70,13 +109,13 @@ class StableRecyclerAdapter : RecyclerView.Adapter() { if (item is Pet) { (holder as? PetViewHolder)?.bind(item, item.numberOwned, - canRaiseToMount = false, - eggCount = 0, - potionCount = 0, - ownsSaddles = false, - hasUnlockedEgg = false, - hasUnlockedPotion = false, - hasMount = false) + eggCount(item), + potionCount(item), + canRaiseToMount(item), + ownsSaddles, + ownedItems?.get(item.animal + "-eggs") != null, + ownedItems?.get(item.color + "-hatchingPotions") != null, + ownedMounts?.containsKey(item.key) == true) } else if (item is Mount) { (holder as? MountViewHolder)?.bind(item, item.numberOwned > 0) } @@ -165,7 +204,7 @@ class StableRecyclerAdapter : RecyclerView.Adapter() { ownedTextView.visibility = View.VISIBLE imageView.loadImage(imageName) - val alpha = if (item.numberOwned <= 0 && (ownedEggs?.containsKey(item.animal) != true || itemType == "mounts")) 0.2f else 1.0f + val alpha = if (item.numberOwned <= 0 && (ownedItems?.containsKey(item.animal) != true || itemType == "mounts")) 0.2f else 1.0f this.imageView.alpha = alpha this.titleView.alpha = alpha this.ownedTextView.alpha = alpha diff --git a/Habitica/src/main/java/com/habitrpg/android/habitica/ui/fragments/inventory/stable/StableRecyclerFragment.kt b/Habitica/src/main/java/com/habitrpg/android/habitica/ui/fragments/inventory/stable/StableRecyclerFragment.kt index abebebd38..719c1e5ff 100644 --- a/Habitica/src/main/java/com/habitrpg/android/habitica/ui/fragments/inventory/stable/StableRecyclerFragment.kt +++ b/Habitica/src/main/java/com/habitrpg/android/habitica/ui/fragments/inventory/stable/StableRecyclerFragment.kt @@ -165,18 +165,15 @@ class StableRecyclerFragment : BaseFragment() { compositeSubscription.add(observable.zipWith(ownedObservable, { unsortedAnimals, ownedAnimals -> mapAnimals(unsortedAnimals, ownedAnimals) }).subscribe({ items -> adapter?.setItemList(items) }, RxErrorHandler.handleEmptyError())) - - compositeSubscription.add(inventoryRepository.getOwnedItems("eggs") - .map { - val map = mutableMapOf() - it.forEach { item -> - map[item.key ?: ""] = item - } - map + compositeSubscription.add(inventoryRepository.getOwnedItems(true).subscribe({ adapter?.setOwnedItems(it) }, RxErrorHandler.handleEmptyError())) + compositeSubscription.add(inventoryRepository.getMounts().subscribe({ adapter?.setExistingMounts(it) }, RxErrorHandler.handleEmptyError())) + compositeSubscription.add(inventoryRepository.getOwnedMounts() + .map { ownedMounts -> + val mountMap = mutableMapOf() + ownedMounts.forEach { mountMap[it.key ?: ""] = it } + return@map mountMap } - .subscribe({ - adapter?.ownedEggs = it - }, RxErrorHandler.handleEmptyError())) + .subscribe({ adapter?.setOwnedMounts(it) }, RxErrorHandler.handleEmptyError())) } private fun mapAnimals(unsortedAnimals: RealmResults, ownedAnimals: Map): ArrayList {