Skip to content

Commit

Permalink
Improve empty states
Browse files Browse the repository at this point in the history
  • Loading branch information
phillipthelen committed May 30, 2024
1 parent d4c410b commit 8ab3f22
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Habitica/res/layout/shop_section_empty.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="wrap_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center"
Expand Down
2 changes: 1 addition & 1 deletion Habitica/res/values-de/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -729,7 +729,7 @@ Die Quest-Schriftrolle wird an den Quest-Besitzer zurückgegeben.</string>
<string name="complete">Erledigen</string>
<string name="reject">Ablehnen</string>
<string name="accept">Akzeptieren</string>
<string name="equip_automatically">Neue Ausrüstung automatisch anziehen</string>
<string name="equip_automatically">Neues automatisch anziehen</string>
<string name="leave_challenges">Herausforderung verlassen</string>
<string name="keep_challenges">Herausforderungen behalten</string>
<string name="inviteFriendDescription">Du hast einen Freund oder Freunde eingeladen, die Dich auf Deinem Abenteuer begleiten!</string>
Expand Down
2 changes: 2 additions & 0 deletions Habitica/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1527,6 +1527,8 @@
<string name="warriors">Warriors</string>
<string name="mages">Mages</string>
<string name="customization_shop">Customization Shop</string>
<string name="equipment">Equipment</string>
<string name="customizing_your_avatar">customizing your avatar</string>

<plurals name="you_x_others">
<item quantity="zero">You</item>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package com.habitrpg.android.habitica.models.shops

import android.content.Context
import android.text.Spannable
import android.text.SpannableString
import android.text.style.ForegroundColorSpan
import androidx.core.content.ContextCompat
import com.habitrpg.android.habitica.R

class EmptyShopCategory(categoryIdentifier: String, context: Context?) {
class EmptyShopCategory(val categoryIdentifier: String, context: Context?) {
val title: String = context?.getString(R.string.you_own_all_items) ?: ""
val description: String
val description: Spannable
init {
val stringId = when (categoryIdentifier) {
"backgrounds" -> R.string.try_on_next_month
Expand All @@ -14,6 +18,19 @@ class EmptyShopCategory(categoryIdentifier: String, context: Context?) {
"mystery_sets" -> R.string.try_on_equipment
else -> R.string.try_on_customize
}
description = context?.getString(stringId) ?: ""
if (context != null) {
val descriptionString = context.getString(stringId)
val words = listOf(context.getString(R.string.equipment), context.getString(R.string.customizing_your_avatar))
val spannable = SpannableString(descriptionString)
for (word in words) {
val index = spannable.indexOf(word)
if (index >= 0) {
spannable.setSpan(ForegroundColorSpan(ContextCompat.getColor(context, R.color.brand_400)), index, index + word.length, Spannable.SPAN_INCLUSIVE_EXCLUSIVE)
}
}
description = spannable
} else {
description = SpannableString("")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class ShopRecyclerAdapter : androidx.recyclerview.widget.RecyclerView.Adapter<Vi
var armoireItem: ShopItem? = null

var changeClassEvents: ((String) -> Unit)? = null
var emptySectionClickedEvents: ((String) -> Unit)? = null

var shopSpriteSuffix: String? = null
set(value) {
Expand Down Expand Up @@ -93,7 +94,6 @@ class ShopRecyclerAdapter : androidx.recyclerview.widget.RecyclerView.Adapter<Vi
items.add(category)
if (category.items.isEmpty()) {
items.add(EmptyShopCategory(category.identifier, context))

} else {
for (item in category.items) {
item.categoryIdentifier = category.identifier
Expand Down Expand Up @@ -217,7 +217,12 @@ class ShopRecyclerAdapter : androidx.recyclerview.widget.RecyclerView.Adapter<Vi
itemHolder.isCompleted = completedQuests.contains(obj.key)
}

is EmptyShopCategory -> (holder as? EmptyShopSectionViewHolder)?.bind(obj)
is EmptyShopCategory -> {
(holder as? EmptyShopSectionViewHolder)?.bind(obj)
(holder as? EmptyShopSectionViewHolder)?.onClicked = {
emptySectionClickedEvents?.invoke(obj.categoryIdentifier)
}
}

is String -> (holder as? EmptyStateViewHolder)?.text = obj
is Pair<*, *> ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import androidx.compose.foundation.layout.Row
import androidx.compose.runtime.mutableStateOf
import androidx.compose.ui.platform.ComposeView
import androidx.compose.ui.unit.dp
import androidx.core.os.bundleOf
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.GridLayoutManager
import com.habitrpg.android.habitica.R
Expand All @@ -23,7 +24,6 @@ import com.habitrpg.android.habitica.helpers.HitType
import com.habitrpg.android.habitica.models.shops.Shop
import com.habitrpg.android.habitica.models.shops.ShopCategory
import com.habitrpg.android.habitica.models.shops.ShopItem
import com.habitrpg.android.habitica.models.social.Group
import com.habitrpg.android.habitica.ui.adapter.inventory.ShopRecyclerAdapter
import com.habitrpg.android.habitica.ui.fragments.BaseMainFragment
import com.habitrpg.android.habitica.ui.fragments.purchases.EventOutcomeSubscriptionBottomSheetFragment
Expand All @@ -36,10 +36,10 @@ import com.habitrpg.android.habitica.ui.views.dialogs.HabiticaProgressDialog
import com.habitrpg.android.habitica.ui.views.insufficientCurrency.InsufficientGemsDialog
import com.habitrpg.android.habitica.ui.views.shops.PurchaseDialog
import com.habitrpg.common.habitica.helpers.ExceptionHandler
import com.habitrpg.common.habitica.helpers.MainNavigationController
import com.habitrpg.common.habitica.helpers.RecyclerViewState
import com.habitrpg.common.habitica.helpers.launchCatching
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.flow.firstOrNull
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch
Expand Down Expand Up @@ -160,6 +160,34 @@ open class ShopFragment : BaseMainFragment<FragmentRefreshRecyclerviewBinding>()
adapter?.changeClassEvents = {
showClassChangeDialog(it)
}
adapter?.emptySectionClickedEvents = {
if (shopIdentifier == Shop.CUSTOMIZATIONS) {
var navigationID = R.id.ComposeAvatarCustomizationFragment
var type = ""
var category: String? = null
if (it == "color") {
type = "hair"
category = "color"
} else if (it == "facialHair") {
type = "hair"
category = "beard"
} else if (it == "base") {
type = "hair"
category = "base"
} else if (it == "animalEars") {
navigationID = R.id.composeAvatarEquipmentFragment
category = "headAccessory"
} else if (it == "animalTails") {
navigationID = R.id.composeAvatarEquipmentFragment
category = "back"
} else if (it == "backgrounds") {
category = "background"
}
MainNavigationController.navigate(navigationID, bundleOf("category" to category, "type" to type))
} else if (shopIdentifier == Shop.TIME_TRAVELERS_SHOP) {
MainNavigationController.navigate(R.id.equipmentOverviewFragment)
}
}

lifecycleScope.launchCatching {
inventoryRepository.getInAppReward("armoire").collect {
Expand Down Expand Up @@ -337,9 +365,11 @@ open class ShopFragment : BaseMainFragment<FragmentRefreshRecyclerviewBinding>()
specialCategory.items.add(ShopItem.makeFortifyItem(context?.resources))
newShop.categories.add(specialCategory)
}

Shop.TIME_TRAVELERS_SHOP -> {
formatTimeTravelersShop(newShop)
}

Shop.SEASONAL_SHOP -> {
newShop.categories.sortWith(
compareBy<ShopCategory> { it.items.firstOrNull()?.currency != "gold" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ import com.habitrpg.android.habitica.models.shops.EmptyShopCategory
class EmptyShopSectionViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
private val binding = ShopSectionEmptyBinding.bind(itemView)

var onClicked: (() -> Unit)? = null

init {
binding.root.setOnClickListener {
onClicked?.invoke()
}
}

fun bind(emptyShopCategory: EmptyShopCategory) {
binding.titleView.text = emptyShopCategory.title
binding.descriptionView.text = emptyShopCategory.description
Expand Down

0 comments on commit 8ab3f22

Please sign in to comment.