From 01ac5f62635f796c93b23ab6c4d954ff774c9852 Mon Sep 17 00:00:00 2001 From: Phillip Thelen Date: Tue, 30 Apr 2019 15:23:45 +0200 Subject: [PATCH] Handle item selling locally --- .../habitica/data/InventoryRepository.kt | 2 +- .../implementation/InventoryRepositoryImpl.kt | 17 +++++++++++++---- .../data/local/InventoryLocalRepository.kt | 1 + .../RealmInventoryLocalRepository.kt | 4 ++-- .../ui/adapter/inventory/ItemRecyclerAdapter.kt | 8 ++++---- 5 files changed, 21 insertions(+), 11 deletions(-) diff --git a/Habitica/src/main/java/com/habitrpg/android/habitica/data/InventoryRepository.kt b/Habitica/src/main/java/com/habitrpg/android/habitica/data/InventoryRepository.kt index fbf284d96..8b18f57a4 100644 --- a/Habitica/src/main/java/com/habitrpg/android/habitica/data/InventoryRepository.kt +++ b/Habitica/src/main/java/com/habitrpg/android/habitica/data/InventoryRepository.kt @@ -56,7 +56,7 @@ interface InventoryRepository : ContentRepository { fun changeOwnedCount(type: String, key: String, amountToAdd: Int) fun sellItem(user: User?, type: String, key: String): Flowable - fun sellItem(user: User?, item: Item): Flowable + fun sellItem(user: User?, item: OwnedItem): Flowable fun equipGear(user: User?, equipment: String, asCostume: Boolean): Flowable fun equip(user: User?, type: String, key: String): Flowable diff --git a/Habitica/src/main/java/com/habitrpg/android/habitica/data/implementation/InventoryRepositoryImpl.kt b/Habitica/src/main/java/com/habitrpg/android/habitica/data/implementation/InventoryRepositoryImpl.kt index 161715b46..98779a2a8 100644 --- a/Habitica/src/main/java/com/habitrpg/android/habitica/data/implementation/InventoryRepositoryImpl.kt +++ b/Habitica/src/main/java/com/habitrpg/android/habitica/data/implementation/InventoryRepositoryImpl.kt @@ -104,11 +104,22 @@ class InventoryRepositoryImpl(localRepository: InventoryLocalRepository, apiClie } override fun sellItem(user: User?, type: String, key: String): Flowable { - return localRepository.getItem(type, key) + return localRepository.getOwnedItem(userID, type, key) .flatMap { item -> sellItem(user, item) } } - override fun sellItem(user: User?, item: Item): Flowable { + override fun sellItem(user: User?, ownedItem: OwnedItem): Flowable { + return localRepository.getItem(ownedItem.itemType ?: "", ownedItem.key ?: "") + .flatMap { item -> sellItem(user, item, ownedItem) } + } + + private fun sellItem(user: User?, item: Item, ownedItem: OwnedItem): Flowable { + 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) .map { user1 -> localRepository.executeTransaction { realm -> @@ -118,8 +129,6 @@ class InventoryRepositoryImpl(localRepository: InventoryLocalRepository, apiClie items.userId = user.id val newItems = realm.copyToRealmOrUpdate(items) user.items = newItems - } else { - //item.owned = item.owned - 1 } val stats = user1.stats if (stats != null) { diff --git a/Habitica/src/main/java/com/habitrpg/android/habitica/data/local/InventoryLocalRepository.kt b/Habitica/src/main/java/com/habitrpg/android/habitica/data/local/InventoryLocalRepository.kt index 80a355806..d7591b842 100644 --- a/Habitica/src/main/java/com/habitrpg/android/habitica/data/local/InventoryLocalRepository.kt +++ b/Habitica/src/main/java/com/habitrpg/android/habitica/data/local/InventoryLocalRepository.kt @@ -43,6 +43,7 @@ interface InventoryLocalRepository : ContentLocalRepository { fun changeOwnedCount(item: OwnedItem, amountToAdd: Int?) fun getItem(type: String, key: String): Flowable + fun getOwnedItem(userID: String, type: String, key: String): Flowable fun decrementMysteryItemCount(user: User?) fun saveInAppRewards(onlineItems: List) diff --git a/Habitica/src/main/java/com/habitrpg/android/habitica/data/local/implementation/RealmInventoryLocalRepository.kt b/Habitica/src/main/java/com/habitrpg/android/habitica/data/local/implementation/RealmInventoryLocalRepository.kt index 7c59db296..b99595958 100644 --- a/Habitica/src/main/java/com/habitrpg/android/habitica/data/local/implementation/RealmInventoryLocalRepository.kt +++ b/Habitica/src/main/java/com/habitrpg/android/habitica/data/local/implementation/RealmInventoryLocalRepository.kt @@ -175,7 +175,7 @@ class RealmInventoryLocalRepository(realm: Realm, private val context: Context) } 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?) { @@ -184,7 +184,7 @@ class RealmInventoryLocalRepository(realm: Realm, private val context: Context) } } - fun getOwnedItem(type: String, key: String, userID: String): Flowable { + override fun getOwnedItem(userID: String, type: String, key: String): Flowable { return realm.where(OwnedItem::class.java) .equalTo("itemType", type) .equalTo("key", key) diff --git a/Habitica/src/main/java/com/habitrpg/android/habitica/ui/adapter/inventory/ItemRecyclerAdapter.kt b/Habitica/src/main/java/com/habitrpg/android/habitica/ui/adapter/inventory/ItemRecyclerAdapter.kt index b87809165..3f58e5956 100644 --- a/Habitica/src/main/java/com/habitrpg/android/habitica/ui/adapter/inventory/ItemRecyclerAdapter.kt +++ b/Habitica/src/main/java/com/habitrpg/android/habitica/ui/adapter/inventory/ItemRecyclerAdapter.kt @@ -2,10 +2,10 @@ package com.habitrpg.android.habitica.ui.adapter.inventory import android.content.Context import android.content.res.Resources -import androidx.recyclerview.widget.RecyclerView import android.view.View import android.view.ViewGroup import android.widget.TextView +import androidx.recyclerview.widget.RecyclerView import com.facebook.drawee.view.SimpleDraweeView import com.habitrpg.android.habitica.R import com.habitrpg.android.habitica.events.OpenMysteryItemEvent @@ -45,10 +45,10 @@ class ItemRecyclerAdapter(data: OrderedRealmCollection?, autoUpdate: notifyDataSetChanged() } - private val sellItemEvents = PublishSubject.create() + private val sellItemEvents = PublishSubject.create() private val questInvitationEvents = PublishSubject.create() - fun getSellItemFlowable(): Flowable { + fun getSellItemFlowable(): Flowable { return sellItemEvents.toFlowable(BackpressureStrategy.DROP) } @@ -160,7 +160,7 @@ class ItemRecyclerAdapter(data: OrderedRealmCollection?, autoUpdate: menu.setSelectionRunnable { index -> item.notNull { selectedItem -> if (!(selectedItem is QuestContent || selectedItem is SpecialItem) && index == 0) { - sellItemEvents.onNext(selectedItem) + ownedItem?.let { selectedOwnedItem -> sellItemEvents.onNext(selectedOwnedItem) } return@notNull } when (selectedItem) {