This commit is contained in:
Phillip Thelen 2021-05-17 12:48:23 +02:00
parent ecc1abb08e
commit 2c4ec1f192
2 changed files with 56 additions and 20 deletions

View file

@ -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<RecyclerView.ViewHolder>() {
@ -30,12 +33,48 @@ class StableRecyclerAdapter : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
var animalIngredientsRetriever: ((Animal, ((Pair<Egg?, HatchingPotion?>) -> Unit)) -> Unit)? = null
var itemType: String? = null
private val equipEvents = PublishSubject.create<String>()
var ownedEggs: Map<String, OwnedItem>? = null
private var existingMounts: RealmResults<Mount>? = null
private var ownedMounts: Map<String, OwnedMount>? = null
private var ownedItems: Map<String, OwnedItem>? = null
private var ownsSaddles: Boolean = false
fun getEquipFlowable(): Flowable<String> {
return equipEvents.toFlowable(BackpressureStrategy.DROP)
}
private fun canRaiseToMount(pet: Pet): Boolean {
for (mount in existingMounts ?: emptyList<Mount>()) {
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<String, OwnedMount>) {
this.ownedMounts = ownedMounts
notifyDataSetChanged()
}
fun setOwnedItems(ownedItems: Map<String, OwnedItem>) {
this.ownedItems = ownedItems
ownsSaddles = if (ownedItems.containsKey("Saddle-food")) (ownedItems["Saddle-food"]?.numberOwned ?: 0)> 0 else false
notifyDataSetChanged()
}
fun setExistingMounts(existingMounts: RealmResults<Mount>) {
this.existingMounts = existingMounts
notifyDataSetChanged()
}
private var itemList: List<Any> = ArrayList()
fun setItemList(itemList: List<Any>) {
@ -70,13 +109,13 @@ class StableRecyclerAdapter : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
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<RecyclerView.ViewHolder>() {
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

View file

@ -165,18 +165,15 @@ class StableRecyclerFragment : BaseFragment<FragmentRecyclerviewBinding>() {
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<String, OwnedItem>()
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<String, OwnedMount>()
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<out Animal>, ownedAnimals: Map<String, OwnedObject>): ArrayList<Any> {