Make setup customization more robust

This commit is contained in:
Phillip Thelen 2019-11-06 18:01:18 +01:00
parent 643e4ec11e
commit 487458e37d
5 changed files with 63 additions and 63 deletions

View file

@ -252,7 +252,6 @@ abstract class HabiticaBaseApplication : MultiDexApplication() {
fun checkUserAuthentication(context: Context, hostConfig: HostConfig?): Boolean {
if (hostConfig?.apiKey == null || hostConfig.apiKey == "" || hostConfig.userID == "") {
startActivity(IntroActivity::class.java, context)
return false
}

View file

@ -8,4 +8,20 @@ interface SetupCustomizationRepository {
fun getCustomizations(type: String, user: User): List<SetupCustomization>
fun getCustomizations(type: String, subtype: String?, user: User): List<SetupCustomization>
companion object {
const val CATEGORY_BODY = "body"
const val CATEGORY_SKIN = "skin"
const val CATEGORY_HAIR = "hair"
const val CATEGORY_EXTRAS = "extras"
const val SUBCATEGORY_SIZE = "size"
const val SUBCATEGORY_SHIRT = "shirt"
const val SUBCATEGORY_COLOR = "color"
const val SUBCATEGORY_PONYTAIL = "ponytail"
const val SUBCATEGORY_BANGS = "bangs"
const val SUBCATEGORY_FLOWER = "flower"
const val SUBCATEGORY_WHEELCHAIR = "wheelchair"
const val SUBCATEGORY_GLASSES = "glasses"
}
}

View file

@ -5,7 +5,6 @@ import com.habitrpg.android.habitica.R
import com.habitrpg.android.habitica.data.SetupCustomizationRepository
import com.habitrpg.android.habitica.models.SetupCustomization
import com.habitrpg.android.habitica.models.user.User
import java.util.*
import javax.inject.Inject
@Suppress("StringLiteralDuplication")
@ -36,27 +35,27 @@ constructor(private val context: Context) : SetupCustomizationRepository {
override fun getCustomizations(type: String, subtype: String?, user: User): List<SetupCustomization> {
return when (type) {
"body" -> {
SetupCustomizationRepository.CATEGORY_BODY -> {
when (subtype) {
"size" -> sizes
"shirt" -> getShirts(user.preferences?.size ?: "slim")
SetupCustomizationRepository.SUBCATEGORY_SIZE -> sizes
SetupCustomizationRepository.SUBCATEGORY_SHIRT -> getShirts(user.preferences?.size ?: "slim")
else -> emptyList()
}
}
"skin" -> skins
"hair" -> {
SetupCustomizationRepository.CATEGORY_SKIN -> skins
SetupCustomizationRepository.CATEGORY_HAIR -> {
when (subtype) {
"bangs" -> getBangs(user.preferences?.hair?.color ?: "")
"ponytail" -> getHairBases(user.preferences?.hair?.color ?: "")
"color" -> hairColors
SetupCustomizationRepository.SUBCATEGORY_BANGS -> getBangs(user.preferences?.hair?.color ?: "")
SetupCustomizationRepository.SUBCATEGORY_PONYTAIL -> getHairBases(user.preferences?.hair?.color ?: "")
SetupCustomizationRepository.SUBCATEGORY_COLOR -> hairColors
else -> emptyList()
}
}
"extras" -> {
SetupCustomizationRepository.CATEGORY_EXTRAS -> {
when (subtype) {
"flower" -> flowers
"glasses" -> glasses
"wheelchair" -> wheelchairs
SetupCustomizationRepository.SUBCATEGORY_FLOWER -> flowers
SetupCustomizationRepository.SUBCATEGORY_GLASSES -> glasses
SetupCustomizationRepository.SUBCATEGORY_WHEELCHAIR -> wheelchairs
else -> emptyList()
}
}

View file

@ -9,6 +9,7 @@ import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import com.habitrpg.android.habitica.R
import com.habitrpg.android.habitica.data.SetupCustomizationRepository
import com.habitrpg.android.habitica.extensions.inflate
import com.habitrpg.android.habitica.models.SetupCustomization
import com.habitrpg.android.habitica.models.user.User
@ -44,37 +45,36 @@ internal class CustomizationSetupAdapter : RecyclerView.Adapter<CustomizationSet
return customizationList.size
}
@Suppress("ReturnCount")
private fun isCustomizationActive(customization: SetupCustomization): Boolean {
val prefs = this.user?.preferences ?: return false
when (customization.category) {
"body" -> {
return when (customization.category) {
SetupCustomizationRepository.CATEGORY_BODY -> {
when (customization.subcategory) {
"size" -> return customization.key == prefs.size
"shirt" -> return customization.key == prefs.shirt
SetupCustomizationRepository.SUBCATEGORY_SIZE -> customization.key == prefs.size
SetupCustomizationRepository.SUBCATEGORY_SHIRT -> customization.key == prefs.shirt
else -> false
}
}
"skin" -> return customization.key == prefs.skin
"background" -> return customization.key == prefs.background
"hair" -> {
SetupCustomizationRepository.CATEGORY_SKIN -> customization.key == prefs.skin
SetupCustomizationRepository.CATEGORY_HAIR -> {
when (customization.subcategory) {
"bangs" -> return Integer.parseInt(customization.key) == prefs.hair?.bangs
"base" -> return Integer.parseInt(customization.key) == prefs.hair?.base
"color" -> return customization.key == prefs.hair?.color
"flower" -> return Integer.parseInt(customization.key) == prefs.hair?.flower
"beard" -> return Integer.parseInt(customization.key) == prefs.hair?.beard
"mustache" -> return Integer.parseInt(customization.key) == prefs.hair?.mustache
SetupCustomizationRepository.SUBCATEGORY_BANGS -> Integer.parseInt(customization.key) == prefs.hair?.bangs
SetupCustomizationRepository.SUBCATEGORY_PONYTAIL -> Integer.parseInt(customization.key) == prefs.hair?.base
SetupCustomizationRepository.SUBCATEGORY_COLOR -> customization.key == prefs.hair?.color
SetupCustomizationRepository.SUBCATEGORY_FLOWER -> Integer.parseInt(customization.key) == prefs.hair?.flower
else -> false
}
}
"extras" -> {
SetupCustomizationRepository.CATEGORY_EXTRAS -> {
when (customization.subcategory) {
"glasses" -> return customization.key == this.user?.items?.gear?.equipped?.eyeWear || "eyewear_base_0" == this.user?.items?.gear?.equipped?.eyeWear && customization.key.isEmpty()
"flower" -> return Integer.parseInt(customization.key) == prefs.hair?.flower
"wheelchair" -> return "chair_" + customization.key == prefs.chair || customization.key == prefs.chair || customization.key == "none" && prefs.chair == null
SetupCustomizationRepository.SUBCATEGORY_GLASSES -> customization.key == this.user?.items?.gear?.equipped?.eyeWear || "eyewear_base_0" == this.user?.items?.gear?.equipped?.eyeWear && customization.key.isEmpty()
SetupCustomizationRepository.SUBCATEGORY_FLOWER -> Integer.parseInt(customization.key) == prefs.hair?.flower
SetupCustomizationRepository.SUBCATEGORY_WHEELCHAIR -> "chair_" + customization.key == prefs.chair || customization.key == prefs.chair || customization.key == "none" && prefs.chair == null
else -> false
}
}
else -> false
}
return false
}
internal inner class CustomizationViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView), View.OnClickListener {

View file

@ -149,9 +149,9 @@ class AvatarSetupFragment : BaseFragment() {
private fun selectedBodyCategory() {
activateButton(bodyButton)
this.activeCategory = CATEGORY_BODY
this.activeCategory = SetupCustomizationRepository.CATEGORY_BODY
this.subCategoryTabs?.removeAllTabs()
this.subcategories = listOf(SUBCATEGORY_SIZE, SUBCATEGORY_SHIRT)
this.subcategories = listOf(SetupCustomizationRepository.SUBCATEGORY_SIZE, SetupCustomizationRepository.SUBCATEGORY_SHIRT)
subCategoryTabs?.newTab()?.setText(R.string.avatar_size)?.let { this.subCategoryTabs?.addTab(it) }
subCategoryTabs?.newTab()?.setText(R.string.avatar_shirt)?.let { this.subCategoryTabs?.addTab(it) }
loadCustomizations()
@ -159,18 +159,18 @@ class AvatarSetupFragment : BaseFragment() {
private fun selectedSkinCategory() {
activateButton(skinButton)
this.activeCategory = CATEGORY_SKIN
this.activeCategory = SetupCustomizationRepository.CATEGORY_SKIN
this.subCategoryTabs?.removeAllTabs()
this.subcategories = listOf(SUBCATEGORY_COLOR)
this.subcategories = listOf(SetupCustomizationRepository.SUBCATEGORY_COLOR)
subCategoryTabs?.newTab()?.setText(R.string.avatar_skin_color)?.let { this.subCategoryTabs?.addTab(it) }
loadCustomizations()
}
private fun selectedHairCategory() {
activateButton(hairButton)
this.activeCategory = CATEGORY_HAIR
this.activeCategory = SetupCustomizationRepository.CATEGORY_HAIR
this.subCategoryTabs?.removeAllTabs()
this.subcategories = listOf(SUBCATEGORY_BANGS, SUBCATEGORY_COLOR, SUBCATEGORY_PONYTAIL)
this.subcategories = listOf(SetupCustomizationRepository.SUBCATEGORY_BANGS, SetupCustomizationRepository.SUBCATEGORY_COLOR, SetupCustomizationRepository.SUBCATEGORY_PONYTAIL)
subCategoryTabs?.newTab()?.setText(R.string.avatar_hair_bangs)?.let { this.subCategoryTabs?.addTab(it) }
subCategoryTabs?.newTab()?.setText(R.string.avatar_hair_color)?.let { this.subCategoryTabs?.addTab(it) }
subCategoryTabs?.newTab()?.setText(R.string.avatar_hair_ponytail)?.let { this.subCategoryTabs?.addTab(it) }
@ -179,9 +179,9 @@ class AvatarSetupFragment : BaseFragment() {
private fun selectedExtrasCategory() {
activateButton(extrasButton)
this.activeCategory = CATEGORY_EXTRAS
this.activeCategory = SetupCustomizationRepository.CATEGORY_EXTRAS
this.subCategoryTabs?.removeAllTabs()
this.subcategories = listOf(SUBCATEGORY_GLASSES, SUBCATEGORY_FLOWER, SUBCATEGORY_WHEELCHAIR)
this.subcategories = listOf(SetupCustomizationRepository.SUBCATEGORY_GLASSES, SetupCustomizationRepository.SUBCATEGORY_FLOWER, SetupCustomizationRepository.SUBCATEGORY_WHEELCHAIR)
subCategoryTabs?.newTab()?.setText(R.string.avatar_glasses)?.let { this.subCategoryTabs?.addTab(it) }
subCategoryTabs?.newTab()?.setText(R.string.avatar_flower)?.let { this.subCategoryTabs?.addTab(it) }
subCategoryTabs?.newTab()?.setText(R.string.avatar_wheelchair)?.let { this.subCategoryTabs?.addTab(it) }
@ -191,14 +191,14 @@ class AvatarSetupFragment : BaseFragment() {
private fun randomizeCharacter() {
val user = this.user ?: return
val updateData = HashMap<String, Any>()
updateData["preferences.size"] = chooseRandomKey(customizationRepository.getCustomizations(CATEGORY_BODY, SUBCATEGORY_SIZE, user), false)
updateData["preferences.shirt"] = chooseRandomKey(customizationRepository.getCustomizations(CATEGORY_BODY, SUBCATEGORY_SHIRT, user), false)
updateData["preferences.skin"] = chooseRandomKey(customizationRepository.getCustomizations(CATEGORY_SKIN, SUBCATEGORY_COLOR, user), false)
updateData["preferences.hair.color"] = chooseRandomKey(customizationRepository.getCustomizations(CATEGORY_HAIR, SUBCATEGORY_COLOR, user), false)
updateData["preferences.hair.base"] = chooseRandomKey(customizationRepository.getCustomizations(CATEGORY_HAIR, SUBCATEGORY_PONYTAIL, user), false)
updateData["preferences.hair.bangs"] = chooseRandomKey(customizationRepository.getCustomizations(CATEGORY_HAIR, SUBCATEGORY_BANGS, user), false)
updateData["preferences.hair.flower"] = chooseRandomKey(customizationRepository.getCustomizations(CATEGORY_EXTRAS, SUBCATEGORY_FLOWER, user), true)
updateData["preferences.chair"] = chooseRandomKey(customizationRepository.getCustomizations(CATEGORY_EXTRAS, SUBCATEGORY_WHEELCHAIR, user), true)
updateData["preferences.size"] = chooseRandomKey(customizationRepository.getCustomizations(SetupCustomizationRepository.CATEGORY_BODY, SetupCustomizationRepository.SUBCATEGORY_SIZE, user), false)
updateData["preferences.shirt"] = chooseRandomKey(customizationRepository.getCustomizations(SetupCustomizationRepository.CATEGORY_BODY, SetupCustomizationRepository.SUBCATEGORY_SHIRT, user), false)
updateData["preferences.skin"] = chooseRandomKey(customizationRepository.getCustomizations(SetupCustomizationRepository.CATEGORY_SKIN, SetupCustomizationRepository.SUBCATEGORY_COLOR, user), false)
updateData["preferences.hair.color"] = chooseRandomKey(customizationRepository.getCustomizations(SetupCustomizationRepository.CATEGORY_HAIR, SetupCustomizationRepository.SUBCATEGORY_COLOR, user), false)
updateData["preferences.hair.base"] = chooseRandomKey(customizationRepository.getCustomizations(SetupCustomizationRepository.CATEGORY_HAIR, SetupCustomizationRepository.SUBCATEGORY_PONYTAIL, user), false)
updateData["preferences.hair.bangs"] = chooseRandomKey(customizationRepository.getCustomizations(SetupCustomizationRepository.CATEGORY_HAIR, SetupCustomizationRepository.SUBCATEGORY_BANGS, user), false)
updateData["preferences.hair.flower"] = chooseRandomKey(customizationRepository.getCustomizations(SetupCustomizationRepository.CATEGORY_EXTRAS, SetupCustomizationRepository.SUBCATEGORY_FLOWER, user), true)
updateData["preferences.chair"] = chooseRandomKey(customizationRepository.getCustomizations(SetupCustomizationRepository.CATEGORY_EXTRAS, SetupCustomizationRepository.SUBCATEGORY_WHEELCHAIR, user), true)
compositeSubscription.add(userRepository.updateUser(user, updateData).subscribeWithErrorHandler(Consumer {}))
}
@ -230,19 +230,5 @@ class AvatarSetupFragment : BaseFragment() {
this.caretView?.layoutParams = params
}
companion object {
const val CATEGORY_BODY = "body"
const val CATEGORY_SKIN = "skin"
const val CATEGORY_HAIR = "hair"
const val CATEGORY_EXTRAS = "extras"
const val SUBCATEGORY_SIZE = "size"
const val SUBCATEGORY_SHIRT = "shirt"
const val SUBCATEGORY_COLOR = "color"
const val SUBCATEGORY_PONYTAIL = "ponytail"
const val SUBCATEGORY_BANGS = "bangs"
const val SUBCATEGORY_FLOWER = "flower"
const val SUBCATEGORY_WHEELCHAIR = "wheelchair"
const val SUBCATEGORY_GLASSES = "glasses"
}
}