mirror of
https://github.com/sudoxnym/habitica-android.git
synced 2026-07-15 18:51:58 +00:00
refactor top viewmodel to kotlin
This commit is contained in:
parent
4104e893f8
commit
e58cb400b4
3 changed files with 151 additions and 188 deletions
|
|
@ -1,187 +0,0 @@
|
|||
package com.habitrpg.android.habitica.ui;
|
||||
|
||||
import android.annotation.TargetApi;
|
||||
import android.content.Context;
|
||||
import android.graphics.drawable.BitmapDrawable;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.os.Build;
|
||||
import android.util.Log;
|
||||
import android.view.View;
|
||||
import android.widget.TextView;
|
||||
|
||||
import com.habitrpg.android.habitica.R;
|
||||
import com.habitrpg.android.habitica.events.BoughtGemsEvent;
|
||||
import com.habitrpg.android.habitica.events.commands.OpenGemPurchaseFragmentCommand;
|
||||
import com.habitrpg.android.habitica.events.commands.OpenMenuItemCommand;
|
||||
import com.habitrpg.android.habitica.models.Avatar;
|
||||
import com.habitrpg.android.habitica.models.user.Stats;
|
||||
import com.habitrpg.android.habitica.models.user.User;
|
||||
import com.habitrpg.android.habitica.ui.fragments.NavigationDrawerFragment;
|
||||
import com.habitrpg.android.habitica.ui.views.CurrencyViews;
|
||||
import com.habitrpg.android.habitica.ui.views.HabiticaIconsHelper;
|
||||
import com.habitrpg.android.habitica.ui.views.ValueBar;
|
||||
|
||||
import org.greenrobot.eventbus.EventBus;
|
||||
import org.greenrobot.eventbus.Subscribe;
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
import butterknife.BindView;
|
||||
import butterknife.ButterKnife;
|
||||
import butterknife.OnClick;
|
||||
|
||||
public class AvatarWithBarsViewModel {
|
||||
@BindView(R.id.hpBar)
|
||||
ValueBar hpBar;
|
||||
@BindView(R.id.xpBar)
|
||||
ValueBar xpBar;
|
||||
@BindView(R.id.mpBar)
|
||||
ValueBar mpBar;
|
||||
@BindView(R.id.avatarView)
|
||||
AvatarView avatarView;
|
||||
|
||||
private Context context;
|
||||
|
||||
@BindView(R.id.lvl_tv)
|
||||
TextView lvlText;
|
||||
@BindView(R.id.currencyView)
|
||||
CurrencyViews currencyView;
|
||||
|
||||
private Avatar userObject;
|
||||
|
||||
private int cachedMaxHealth, cachedMaxExp, cachedMaxMana;
|
||||
|
||||
public AvatarWithBarsViewModel(Context context, View v) {
|
||||
this.context = context;
|
||||
|
||||
if (v == null) {
|
||||
Log.w("AvatarWithBarsViewModel", "View is null");
|
||||
return;
|
||||
}
|
||||
|
||||
ButterKnife.bind(this, v);
|
||||
|
||||
hpBar.setIcon(HabiticaIconsHelper.imageOfHeartLightBg());
|
||||
xpBar.setIcon(HabiticaIconsHelper.imageOfExperience());
|
||||
mpBar.setIcon(HabiticaIconsHelper.imageOfMagic());
|
||||
|
||||
setHpBarData(0, 50);
|
||||
setXpBarData(0, 1);
|
||||
setMpBarData(0, 1);
|
||||
}
|
||||
|
||||
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
|
||||
public void updateData(Avatar user) {
|
||||
userObject = user;
|
||||
|
||||
Stats stats = user.getStats();
|
||||
|
||||
String userClass = "";
|
||||
|
||||
avatarView.setAvatar(user);
|
||||
|
||||
if (stats.getHabitClass() != null) {
|
||||
userClass = stats.getTranslatedClassName(context);
|
||||
}
|
||||
|
||||
mpBar.setVisibility((stats.getHabitClass() == null || stats.getLvl() < 10 || user.getPreferences().getDisableClasses()) ? View.GONE : View.VISIBLE);
|
||||
|
||||
if (!user.hasClass()) {
|
||||
lvlText.setText(context.getString(R.string.user_level, user.getStats().getLvl()));
|
||||
lvlText.setCompoundDrawables(null, null, null, null);
|
||||
} else {
|
||||
lvlText.setText(context.getString(R.string.user_level_with_class, user.getStats().getLvl(), userClass.substring(0, 1).toUpperCase(Locale.getDefault()) + userClass.substring(1)));
|
||||
Drawable drawable = null;
|
||||
switch (stats.getHabitClass()) {
|
||||
case Stats.WARRIOR:
|
||||
drawable = new BitmapDrawable(context.getResources(), HabiticaIconsHelper.imageOfWarriorDarkBg());
|
||||
break;
|
||||
case Stats.ROGUE:
|
||||
drawable = new BitmapDrawable(context.getResources(), HabiticaIconsHelper.imageOfRogueDarkBg());
|
||||
break;
|
||||
case Stats.MAGE:
|
||||
drawable = new BitmapDrawable(context.getResources(), HabiticaIconsHelper.imageOfMageDarkBg());
|
||||
break;
|
||||
case Stats.HEALER:
|
||||
drawable = new BitmapDrawable(context.getResources(), HabiticaIconsHelper.imageOfHealerDarkBg());
|
||||
break;
|
||||
}
|
||||
if (drawable != null) {
|
||||
drawable.setBounds(0, 0, drawable.getMinimumWidth(),
|
||||
drawable.getMinimumHeight());
|
||||
}
|
||||
lvlText.setCompoundDrawables(drawable, null, null, null);
|
||||
}
|
||||
|
||||
setHpBarData(stats.getHp().floatValue(), stats.getMaxHealth());
|
||||
setXpBarData(stats.getExp().floatValue(), stats.getToNextLevel());
|
||||
setMpBarData(stats.getMp().floatValue(), stats.getMaxMP());
|
||||
|
||||
currencyView.setGold(stats.getGp());
|
||||
if (user instanceof User) {
|
||||
currencyView.setHourglasses(user.getHourglassCount());
|
||||
currencyView.setGems(user.getGemCount());
|
||||
}
|
||||
}
|
||||
|
||||
private void setHpBarData(float value, int valueMax) {
|
||||
if (valueMax == 0) {
|
||||
valueMax = cachedMaxHealth;
|
||||
} else {
|
||||
cachedMaxHealth = valueMax;
|
||||
}
|
||||
hpBar.set(Math.ceil(value), valueMax);
|
||||
}
|
||||
|
||||
private void setXpBarData(float value, int valueMax) {
|
||||
if (valueMax == 0) {
|
||||
valueMax = cachedMaxExp;
|
||||
} else {
|
||||
cachedMaxExp = valueMax;
|
||||
}
|
||||
xpBar.set(Math.floor(value), valueMax);
|
||||
}
|
||||
|
||||
private void setMpBarData(float value, int valueMax) {
|
||||
if (valueMax == 0) {
|
||||
valueMax = cachedMaxMana;
|
||||
} else {
|
||||
cachedMaxMana = valueMax;
|
||||
}
|
||||
mpBar.set(Math.floor(value), valueMax);
|
||||
}
|
||||
|
||||
@Subscribe
|
||||
public void onEvent(BoughtGemsEvent gemsEvent) {
|
||||
Integer gems = userObject.getGemCount();
|
||||
gems += gemsEvent.NewGemsToAdd;
|
||||
currencyView.setGems(gems);
|
||||
}
|
||||
|
||||
@OnClick(R.id.currencyView)
|
||||
public void gemTextClicked() {
|
||||
EventBus.getDefault().post(new OpenGemPurchaseFragmentCommand());
|
||||
}
|
||||
|
||||
@OnClick(R.id.avatarView)
|
||||
public void avatarViewClicked() {
|
||||
OpenMenuItemCommand event = new OpenMenuItemCommand();
|
||||
event.identifier = NavigationDrawerFragment.SIDEBAR_AVATAR;
|
||||
EventBus.getDefault().post(event);
|
||||
}
|
||||
|
||||
public void valueBarLabelsToBlack() {
|
||||
hpBar.setLightBackground(true);
|
||||
xpBar.setLightBackground(true);
|
||||
mpBar.setLightBackground(true);
|
||||
}
|
||||
|
||||
public static void setHpBarData(ValueBar valueBar, Stats stats) {
|
||||
Integer maxHP = stats.getMaxHealth();
|
||||
if (maxHP == null || maxHP == 0) {
|
||||
maxHP = 50;
|
||||
}
|
||||
|
||||
valueBar.set((float) Math.ceil(stats.getHp().floatValue()), maxHP);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,150 @@
|
|||
package com.habitrpg.android.habitica.ui
|
||||
|
||||
import android.annotation.TargetApi
|
||||
import android.content.Context
|
||||
import android.graphics.drawable.BitmapDrawable
|
||||
import android.graphics.drawable.Drawable
|
||||
import android.os.Build
|
||||
import android.view.View
|
||||
import android.widget.TextView
|
||||
import com.habitrpg.android.habitica.R
|
||||
import com.habitrpg.android.habitica.events.BoughtGemsEvent
|
||||
import com.habitrpg.android.habitica.events.commands.OpenGemPurchaseFragmentCommand
|
||||
import com.habitrpg.android.habitica.events.commands.OpenMenuItemCommand
|
||||
import com.habitrpg.android.habitica.extensions.bindView
|
||||
import com.habitrpg.android.habitica.models.Avatar
|
||||
import com.habitrpg.android.habitica.models.user.Stats
|
||||
import com.habitrpg.android.habitica.models.user.User
|
||||
import com.habitrpg.android.habitica.ui.fragments.NavigationDrawerFragment
|
||||
import com.habitrpg.android.habitica.ui.views.CurrencyViews
|
||||
import com.habitrpg.android.habitica.ui.views.HabiticaIconsHelper
|
||||
import com.habitrpg.android.habitica.ui.views.ValueBar
|
||||
import org.greenrobot.eventbus.EventBus
|
||||
import org.greenrobot.eventbus.Subscribe
|
||||
import java.util.*
|
||||
|
||||
class AvatarWithBarsViewModel(private val context: Context, view: View) {
|
||||
private val hpBar: ValueBar by bindView(view, R.id.hpBar)
|
||||
private val xpBar: ValueBar by bindView(view, R.id.xpBar)
|
||||
private val mpBar: ValueBar by bindView(view, R.id.mpBar)
|
||||
private val avatarView: AvatarView by bindView(view, R.id.avatarView)
|
||||
private val lvlText: TextView by bindView(view, R.id.lvl_tv)
|
||||
private val currencyView: CurrencyViews by bindView(view, R.id.currencyView)
|
||||
|
||||
private var userObject: Avatar? = null
|
||||
|
||||
private var cachedMaxHealth: Int = 0
|
||||
private var cachedMaxExp: Int = 0
|
||||
private var cachedMaxMana: Int = 0
|
||||
|
||||
init {
|
||||
hpBar.setIcon(HabiticaIconsHelper.imageOfHeartLightBg())
|
||||
xpBar.setIcon(HabiticaIconsHelper.imageOfExperience())
|
||||
mpBar.setIcon(HabiticaIconsHelper.imageOfMagic())
|
||||
|
||||
setHpBarData(0f, 50)
|
||||
setXpBarData(0f, 1)
|
||||
setMpBarData(0f, 1)
|
||||
}
|
||||
|
||||
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
|
||||
fun updateData(user: Avatar) {
|
||||
userObject = user
|
||||
|
||||
val stats = user.stats
|
||||
|
||||
var userClass = ""
|
||||
|
||||
avatarView.setAvatar(user)
|
||||
|
||||
if (stats.getHabitClass() != null) {
|
||||
userClass = stats.getTranslatedClassName(context)
|
||||
}
|
||||
|
||||
mpBar.visibility = if (stats.getHabitClass() == null || stats.getLvl() < 10 || user.preferences.disableClasses) View.GONE else View.VISIBLE
|
||||
|
||||
if (!user.hasClass()) {
|
||||
lvlText.text = context.getString(R.string.user_level, user.stats.getLvl())
|
||||
lvlText.setCompoundDrawables(null, null, null, null)
|
||||
} else {
|
||||
lvlText.text = context.getString(R.string.user_level_with_class, user.stats.getLvl(), userClass.substring(0, 1).toUpperCase(Locale.getDefault()) + userClass.substring(1))
|
||||
var drawable: Drawable? = null
|
||||
when (stats.getHabitClass()) {
|
||||
Stats.WARRIOR -> drawable = BitmapDrawable(context.resources, HabiticaIconsHelper.imageOfWarriorDarkBg())
|
||||
Stats.ROGUE -> drawable = BitmapDrawable(context.resources, HabiticaIconsHelper.imageOfRogueDarkBg())
|
||||
Stats.MAGE -> drawable = BitmapDrawable(context.resources, HabiticaIconsHelper.imageOfMageDarkBg())
|
||||
Stats.HEALER -> drawable = BitmapDrawable(context.resources, HabiticaIconsHelper.imageOfHealerDarkBg())
|
||||
}
|
||||
if (drawable != null) {
|
||||
drawable.setBounds(0, 0, drawable.minimumWidth,
|
||||
drawable.minimumHeight)
|
||||
}
|
||||
lvlText.setCompoundDrawables(drawable, null, null, null)
|
||||
}
|
||||
|
||||
setHpBarData(stats.hp.toFloat(), stats.maxHealth)
|
||||
setXpBarData(stats.exp.toFloat(), stats.toNextLevel)
|
||||
setMpBarData(stats.mp.toFloat(), stats.maxMP)
|
||||
|
||||
currencyView.gold = stats.getGp()
|
||||
if (user is User) {
|
||||
currencyView.hourglasses = user.getHourglassCount().toDouble()
|
||||
currencyView.gems = user.getGemCount().toDouble()
|
||||
}
|
||||
|
||||
currencyView.setOnClickListener {
|
||||
EventBus.getDefault().post(OpenGemPurchaseFragmentCommand())
|
||||
}
|
||||
avatarView.setOnClickListener {
|
||||
val event = OpenMenuItemCommand()
|
||||
event.identifier = NavigationDrawerFragment.SIDEBAR_AVATAR
|
||||
EventBus.getDefault().post(event)
|
||||
}
|
||||
}
|
||||
|
||||
private fun setHpBarData(value: Float, valueMax: Int) {
|
||||
if (valueMax == 0) {
|
||||
cachedMaxHealth = valueMax
|
||||
}
|
||||
hpBar.set(Math.ceil(value.toDouble()), cachedMaxHealth.toDouble())
|
||||
}
|
||||
|
||||
private fun setXpBarData(value: Float, valueMax: Int) {
|
||||
if (valueMax != 0) {
|
||||
cachedMaxExp = valueMax
|
||||
}
|
||||
xpBar.set(Math.floor(value.toDouble()), cachedMaxExp.toDouble())
|
||||
}
|
||||
|
||||
private fun setMpBarData(value: Float, valueMax: Int) {
|
||||
if (valueMax != 0) {
|
||||
cachedMaxMana = valueMax
|
||||
}
|
||||
mpBar.set(Math.floor(value.toDouble()), cachedMaxMana.toDouble())
|
||||
}
|
||||
|
||||
@Subscribe
|
||||
fun onEvent(gemsEvent: BoughtGemsEvent) {
|
||||
var gems = userObject?.gemCount ?: 0
|
||||
gems += gemsEvent.NewGemsToAdd
|
||||
currencyView.gems = gems.toDouble()
|
||||
}
|
||||
|
||||
fun valueBarLabelsToBlack() {
|
||||
hpBar.setLightBackground(true)
|
||||
xpBar.setLightBackground(true)
|
||||
mpBar.setLightBackground(true)
|
||||
}
|
||||
|
||||
companion object {
|
||||
|
||||
fun setHpBarData(valueBar: ValueBar, stats: Stats) {
|
||||
var maxHP = stats.maxHealth
|
||||
if (maxHP == null || maxHP == 0) {
|
||||
maxHP = 50
|
||||
}
|
||||
|
||||
valueBar.set(Math.ceil(stats.hp.toDouble()), maxHP.toDouble())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -95,7 +95,7 @@ public class PartyMemberRecyclerViewAdapter extends RealmRecyclerViewAdapter<Mem
|
|||
public void bind(Member user) {
|
||||
avatarView.setAvatar(user);
|
||||
|
||||
AvatarWithBarsViewModel.setHpBarData(hpBar, user.getStats());
|
||||
AvatarWithBarsViewModel.Companion.setHpBarData(hpBar, user.getStats());
|
||||
|
||||
lvl.setText(context.getString(R.string.user_level, user.getStats().getLvl()));
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue