mirror of
https://github.com/sudoxnym/habitica-android.git
synced 2026-07-13 17:51:57 +00:00
Handle item selling locally
This commit is contained in:
parent
2a0743767b
commit
01ac5f6263
5 changed files with 21 additions and 11 deletions
|
|
@ -56,7 +56,7 @@ interface InventoryRepository : ContentRepository {
|
||||||
fun changeOwnedCount(type: String, key: String, amountToAdd: Int)
|
fun changeOwnedCount(type: String, key: String, amountToAdd: Int)
|
||||||
|
|
||||||
fun sellItem(user: User?, type: String, key: String): Flowable<User>
|
fun sellItem(user: User?, type: String, key: String): Flowable<User>
|
||||||
fun sellItem(user: User?, item: Item): Flowable<User>
|
fun sellItem(user: User?, item: OwnedItem): Flowable<User>
|
||||||
|
|
||||||
fun equipGear(user: User?, equipment: String, asCostume: Boolean): Flowable<Items>
|
fun equipGear(user: User?, equipment: String, asCostume: Boolean): Flowable<Items>
|
||||||
fun equip(user: User?, type: String, key: String): Flowable<Items>
|
fun equip(user: User?, type: String, key: String): Flowable<Items>
|
||||||
|
|
|
||||||
|
|
@ -104,11 +104,22 @@ class InventoryRepositoryImpl(localRepository: InventoryLocalRepository, apiClie
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun sellItem(user: User?, type: String, key: String): Flowable<User> {
|
override fun sellItem(user: User?, type: String, key: String): Flowable<User> {
|
||||||
return localRepository.getItem(type, key)
|
return localRepository.getOwnedItem(userID, type, key)
|
||||||
.flatMap { item -> sellItem(user, item) }
|
.flatMap { item -> sellItem(user, item) }
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun sellItem(user: User?, item: Item): Flowable<User> {
|
override fun sellItem(user: User?, ownedItem: OwnedItem): Flowable<User> {
|
||||||
|
return localRepository.getItem(ownedItem.itemType ?: "", ownedItem.key ?: "")
|
||||||
|
.flatMap { item -> sellItem(user, item, ownedItem) }
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun sellItem(user: User?, item: Item, ownedItem: OwnedItem): Flowable<User> {
|
||||||
|
if (user != null && appConfigManager.enableLocalChanges()) {
|
||||||
|
localRepository.executeTransaction {
|
||||||
|
ownedItem.numberOwned -= 1
|
||||||
|
user.stats?.gp = (user.stats?.gp ?: 0.0) + item.value
|
||||||
|
}
|
||||||
|
}
|
||||||
return apiClient.sellItem(item.type, item.key)
|
return apiClient.sellItem(item.type, item.key)
|
||||||
.map { user1 ->
|
.map { user1 ->
|
||||||
localRepository.executeTransaction { realm ->
|
localRepository.executeTransaction { realm ->
|
||||||
|
|
@ -118,8 +129,6 @@ class InventoryRepositoryImpl(localRepository: InventoryLocalRepository, apiClie
|
||||||
items.userId = user.id
|
items.userId = user.id
|
||||||
val newItems = realm.copyToRealmOrUpdate(items)
|
val newItems = realm.copyToRealmOrUpdate(items)
|
||||||
user.items = newItems
|
user.items = newItems
|
||||||
} else {
|
|
||||||
//item.owned = item.owned - 1
|
|
||||||
}
|
}
|
||||||
val stats = user1.stats
|
val stats = user1.stats
|
||||||
if (stats != null) {
|
if (stats != null) {
|
||||||
|
|
|
||||||
|
|
@ -43,6 +43,7 @@ interface InventoryLocalRepository : ContentLocalRepository {
|
||||||
fun changeOwnedCount(item: OwnedItem, amountToAdd: Int?)
|
fun changeOwnedCount(item: OwnedItem, amountToAdd: Int?)
|
||||||
|
|
||||||
fun getItem(type: String, key: String): Flowable<Item>
|
fun getItem(type: String, key: String): Flowable<Item>
|
||||||
|
fun getOwnedItem(userID: String, type: String, key: String): Flowable<OwnedItem>
|
||||||
|
|
||||||
fun decrementMysteryItemCount(user: User?)
|
fun decrementMysteryItemCount(user: User?)
|
||||||
fun saveInAppRewards(onlineItems: List<ShopItem>)
|
fun saveInAppRewards(onlineItems: List<ShopItem>)
|
||||||
|
|
|
||||||
|
|
@ -175,7 +175,7 @@ class RealmInventoryLocalRepository(realm: Realm, private val context: Context)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun changeOwnedCount(type: String, key: String, userID: String, amountToAdd: Int) {
|
override fun changeOwnedCount(type: String, key: String, userID: String, amountToAdd: Int) {
|
||||||
getOwnedItem(type, key, userID).firstElement().subscribe( Consumer { changeOwnedCount(it, amountToAdd)}, RxErrorHandler.handleEmptyError())
|
getOwnedItem(userID, type, key).firstElement().subscribe( Consumer { changeOwnedCount(it, amountToAdd)}, RxErrorHandler.handleEmptyError())
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun changeOwnedCount(item: OwnedItem, amountToAdd: Int?) {
|
override fun changeOwnedCount(item: OwnedItem, amountToAdd: Int?) {
|
||||||
|
|
@ -184,7 +184,7 @@ class RealmInventoryLocalRepository(realm: Realm, private val context: Context)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun getOwnedItem(type: String, key: String, userID: String): Flowable<OwnedItem> {
|
override fun getOwnedItem(userID: String, type: String, key: String): Flowable<OwnedItem> {
|
||||||
return realm.where(OwnedItem::class.java)
|
return realm.where(OwnedItem::class.java)
|
||||||
.equalTo("itemType", type)
|
.equalTo("itemType", type)
|
||||||
.equalTo("key", key)
|
.equalTo("key", key)
|
||||||
|
|
|
||||||
|
|
@ -2,10 +2,10 @@ package com.habitrpg.android.habitica.ui.adapter.inventory
|
||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.content.res.Resources
|
import android.content.res.Resources
|
||||||
import androidx.recyclerview.widget.RecyclerView
|
|
||||||
import android.view.View
|
import android.view.View
|
||||||
import android.view.ViewGroup
|
import android.view.ViewGroup
|
||||||
import android.widget.TextView
|
import android.widget.TextView
|
||||||
|
import androidx.recyclerview.widget.RecyclerView
|
||||||
import com.facebook.drawee.view.SimpleDraweeView
|
import com.facebook.drawee.view.SimpleDraweeView
|
||||||
import com.habitrpg.android.habitica.R
|
import com.habitrpg.android.habitica.R
|
||||||
import com.habitrpg.android.habitica.events.OpenMysteryItemEvent
|
import com.habitrpg.android.habitica.events.OpenMysteryItemEvent
|
||||||
|
|
@ -45,10 +45,10 @@ class ItemRecyclerAdapter(data: OrderedRealmCollection<OwnedItem>?, autoUpdate:
|
||||||
notifyDataSetChanged()
|
notifyDataSetChanged()
|
||||||
}
|
}
|
||||||
|
|
||||||
private val sellItemEvents = PublishSubject.create<Item>()
|
private val sellItemEvents = PublishSubject.create<OwnedItem>()
|
||||||
private val questInvitationEvents = PublishSubject.create<QuestContent>()
|
private val questInvitationEvents = PublishSubject.create<QuestContent>()
|
||||||
|
|
||||||
fun getSellItemFlowable(): Flowable<Item> {
|
fun getSellItemFlowable(): Flowable<OwnedItem> {
|
||||||
return sellItemEvents.toFlowable(BackpressureStrategy.DROP)
|
return sellItemEvents.toFlowable(BackpressureStrategy.DROP)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -160,7 +160,7 @@ class ItemRecyclerAdapter(data: OrderedRealmCollection<OwnedItem>?, autoUpdate:
|
||||||
menu.setSelectionRunnable { index ->
|
menu.setSelectionRunnable { index ->
|
||||||
item.notNull { selectedItem ->
|
item.notNull { selectedItem ->
|
||||||
if (!(selectedItem is QuestContent || selectedItem is SpecialItem) && index == 0) {
|
if (!(selectedItem is QuestContent || selectedItem is SpecialItem) && index == 0) {
|
||||||
sellItemEvents.onNext(selectedItem)
|
ownedItem?.let { selectedOwnedItem -> sellItemEvents.onNext(selectedOwnedItem) }
|
||||||
return@notNull
|
return@notNull
|
||||||
}
|
}
|
||||||
when (selectedItem) {
|
when (selectedItem) {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue