minor changes

This commit is contained in:
Phillip Thelen 2017-10-18 13:38:18 +02:00
parent 0e2b558bdf
commit a055fb8938
8 changed files with 140 additions and 164 deletions

View file

@ -1,40 +0,0 @@
package com.habitrpg.android.habitica.helpers;
import android.content.Context;
import com.habitrpg.android.habitica.R;
import java.math.RoundingMode;
import java.text.DecimalFormat;
public class NumberAbbreviator {
public static String abbreviate(Context context, double number) {
int counter = 0;
while (number >= 1000) {
counter++;
number = number / 1000;
}
DecimalFormat formatter = new DecimalFormat("###.##"+abbreviationForCounter(context, counter));
formatter.setRoundingMode(RoundingMode.FLOOR);
return formatter.format(number);
}
private static String abbreviationForCounter(Context context, int counter) {
switch (counter) {
case 0:
return "";
case 1:
return context.getString(R.string.thousand_abbrev);
case 2:
return context.getString(R.string.million_abbrev);
case 3:
return context.getString(R.string.billion_abbrev);
default:
return "";
}
}
}

View file

@ -0,0 +1,31 @@
package com.habitrpg.android.habitica.helpers
import android.content.Context
import com.habitrpg.android.habitica.R
import java.math.RoundingMode
import java.text.DecimalFormat
object NumberAbbreviator {
fun abbreviate(context: Context, number: Double): String {
var usedNumber = number
var counter = 0
while (number >= 1000) {
counter++
usedNumber /= 1000
}
val formatter = DecimalFormat("###.##" + abbreviationForCounter(context, counter))
formatter.roundingMode = RoundingMode.FLOOR
return formatter.format(usedNumber)
}
private fun abbreviationForCounter(context: Context, counter: Int): String = when (counter) {
1 -> context.getString(R.string.thousand_abbrev)
2 -> context.getString(R.string.million_abbrev)
3 -> context.getString(R.string.billion_abbrev)
else -> ""
}
}

View file

@ -12,9 +12,6 @@ import java.util.Map;
import io.realm.OrderedRealmCollection;
import io.realm.RealmQuery;
/**
* Created by magicmicky on 02/10/15.
*/
public class TaskFilterHelper {
private List<String> tagsId;
private Map<String, String> activeFilters = new HashMap<>();

View file

@ -87,7 +87,7 @@ public class RewardViewHolder extends BaseTaskViewHolder {
public void bindHolder(Task reward, int position, boolean canBuy) {
this.task = reward;
super.bindHolder(reward, position);
this.priceLabel.setText(NumberAbbreviator.abbreviate(itemView.getContext(), this.task.value));
this.priceLabel.setText(NumberAbbreviator.INSTANCE.abbreviate(itemView.getContext(), this.task.value));
if (canBuy) {
goldIconView.setAlpha(1.0f);

View file

@ -1,111 +0,0 @@
package com.habitrpg.android.habitica.ui.views;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import com.habitrpg.android.habitica.R;
import com.habitrpg.android.habitica.helpers.NumberAbbreviator;
public class CurrencyView extends android.support.v7.widget.AppCompatTextView {
private boolean lightbackground = false;
private String currency;
private BitmapDrawable drawable;
public CurrencyView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public CurrencyView(Context context, String currency, boolean lightbackground) {
super(context);
this.lightbackground = lightbackground;
setCurrency(currency);
}
private void setIcon(Bitmap iconBitmap) {
drawable = new BitmapDrawable(getResources(), iconBitmap);
this.setCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null);
int padding = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 6, getContext().getResources().getDisplayMetrics());
setCompoundDrawablePadding(padding);
this.setGravity(Gravity.CENTER_VERTICAL);
}
public String getCurrency() {
return currency;
}
public void setCurrency(String currency) {
this.currency = currency;
if ("gold".equals(currency)) {
setIcon(HabiticaIconsHelper.imageOfGold());
if (lightbackground) {
setTextColor(ContextCompat.getColor(getContext(), R.color.yellow_5));
} else {
setTextColor(ContextCompat.getColor(getContext(), R.color.yellow_100));
}
} else if ("gems".equals(currency)) {
setIcon(HabiticaIconsHelper.imageOfGem());
if (lightbackground) {
setTextColor(ContextCompat.getColor(getContext(), R.color.green_100));
} else {
setTextColor(ContextCompat.getColor(getContext(), R.color.green_50));
}
} else if ("hourglasses".equals(currency)) {
setIcon(HabiticaIconsHelper.imageOfHourglass());
if (lightbackground) {
setTextColor(ContextCompat.getColor(getContext(), R.color.brand_300));
} else {
setTextColor(ContextCompat.getColor(getContext(), R.color.brand_500));
}
}
updateVisibility();
}
private void updateVisibility() {
if ("hourglasses".equals(currency)) {
setVisibility("0".equals(getText()) ? View.GONE : View.VISIBLE);
}
}
public void setValue(Double value) {
setText(NumberAbbreviator.abbreviate(getContext(), value));
updateVisibility();
}
public void setLocked(boolean isLocked) {
if (drawable == null) {
return;
}
if (isLocked) {
this.setTextColor(ContextCompat.getColor(getContext(), R.color.gray_300));
drawable.setAlpha(127);
} else {
drawable.setAlpha(255);
}
this.setCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null);
}
public void setCantAfford(boolean cantAfford) {
if (drawable == null) {
return;
}
if (cantAfford) {
this.setTextColor(ContextCompat.getColor(getContext(), R.color.red_50));
drawable.setAlpha(127);
} else {
drawable.setAlpha(255);
}
this.setCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null);
}
}

View file

@ -0,0 +1,99 @@
package com.habitrpg.android.habitica.ui.views
import android.content.Context
import android.graphics.Bitmap
import android.graphics.drawable.BitmapDrawable
import android.support.v4.content.ContextCompat
import android.util.AttributeSet
import android.util.TypedValue
import android.view.Gravity
import android.view.View
import com.habitrpg.android.habitica.R
import com.habitrpg.android.habitica.helpers.NumberAbbreviator
class CurrencyView : android.support.v7.widget.AppCompatTextView {
private var lightbackground = false
var currency: String? = null
set(currency) {
field = currency
if ("gold" == currency) {
setIcon(HabiticaIconsHelper.imageOfGold())
if (lightbackground) {
setTextColor(ContextCompat.getColor(context, R.color.yellow_5))
} else {
setTextColor(ContextCompat.getColor(context, R.color.yellow_100))
}
} else if ("gems" == currency) {
setIcon(HabiticaIconsHelper.imageOfGem())
if (lightbackground) {
setTextColor(ContextCompat.getColor(context, R.color.green_100))
} else {
setTextColor(ContextCompat.getColor(context, R.color.green_50))
}
} else if ("hourglasses" == currency) {
setIcon(HabiticaIconsHelper.imageOfHourglass())
if (lightbackground) {
setTextColor(ContextCompat.getColor(context, R.color.brand_300))
} else {
setTextColor(ContextCompat.getColor(context, R.color.brand_500))
}
}
updateVisibility()
}
private var drawable: BitmapDrawable? = null
constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {}
constructor(context: Context, currency: String, lightbackground: Boolean) : super(context) {
this.lightbackground = lightbackground
this.currency = currency
}
private fun setIcon(iconBitmap: Bitmap) {
drawable = BitmapDrawable(resources, iconBitmap)
this.setCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null)
val padding = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 6f, context.resources.displayMetrics).toInt()
compoundDrawablePadding = padding
this.gravity = Gravity.CENTER_VERTICAL
}
private fun updateVisibility() {
if ("hourglasses" == this.currency) {
visibility = if ("0" == text) View.GONE else View.VISIBLE
}
}
fun setValue(value: Double?) {
text = NumberAbbreviator.abbreviate(context, value!!)
updateVisibility()
}
fun setLocked(isLocked: Boolean) {
if (drawable == null) {
return
}
if (isLocked) {
this.setTextColor(ContextCompat.getColor(context, R.color.gray_300))
drawable!!.alpha = 127
} else {
drawable!!.alpha = 255
}
this.setCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null)
}
fun setCantAfford(cantAfford: Boolean) {
if (drawable == null) {
return
}
if (cantAfford) {
this.setTextColor(ContextCompat.getColor(context, R.color.red_50))
drawable!!.alpha = 127
} else {
drawable!!.alpha = 255
}
this.setCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null)
}
}

View file

@ -101,7 +101,7 @@ public class AvatarStatsWidgetProvider extends BaseWidgetProvider {
remoteViews.setProgressBar(R.id.mp_bar, stats.getMaxMP(), stats.getMp().intValue(), false);
remoteViews.setViewVisibility(R.id.mp_wrapper, (stats.getHabitClass() == null || stats.getLvl() < 10 || user.getPreferences().getDisableClasses()) ? View.GONE : View.VISIBLE);
remoteViews.setTextViewText(R.id.gold_tv, NumberAbbreviator.abbreviate(context, stats.getGp()));
remoteViews.setTextViewText(R.id.gold_tv, NumberAbbreviator.INSTANCE.abbreviate(context, stats.getGp()));
remoteViews.setTextViewText(R.id.gems_tv, String.valueOf((int) (user.getBalance() * 4)));
int hourGlassCount = user.getHourglassCount();
if (hourGlassCount == 0) {

View file

@ -27,34 +27,34 @@ public class NumberAbbreviatorTest {
@Test
public void testThatItDoesntAbbreviatesSmallNumbers() {
assertEquals("215", NumberAbbreviator.abbreviate(context, 215));
assertEquals("2.05", NumberAbbreviator.abbreviate(context, 2.05));
assertEquals("215", NumberAbbreviator.INSTANCE.abbreviate(context, 215));
assertEquals("2.05", NumberAbbreviator.INSTANCE.abbreviate(context, 2.05));
}
@Test
public void testThatItAbbreviatesThousand() {
assertEquals("1.55k", NumberAbbreviator.abbreviate(context, 1550));
assertEquals("1.55k", NumberAbbreviator.INSTANCE.abbreviate(context, 1550));
}
@Test
public void testThatItAbbreviatesMillion() {
assertEquals("9.99m", NumberAbbreviator.abbreviate(context, 9990000));
assertEquals("9.99m", NumberAbbreviator.INSTANCE.abbreviate(context, 9990000));
}
@Test
public void testThatItAbbreviatesBillion() {
assertEquals("1.99b", NumberAbbreviator.abbreviate(context, 1990000000));
assertEquals("1.99b", NumberAbbreviator.INSTANCE.abbreviate(context, 1990000000));
}
@Test
public void testThatItAbbreviatesThousandWithoutAdditionalDecimals() {
assertEquals("1k", NumberAbbreviator.abbreviate(context, 1000));
assertEquals("1.5k", NumberAbbreviator.abbreviate(context, 1500));
assertEquals("1k", NumberAbbreviator.INSTANCE.abbreviate(context, 1000));
assertEquals("1.5k", NumberAbbreviator.INSTANCE.abbreviate(context, 1500));
}
@Test
public void voidtestThatitRoundsCorrectly() {
assertEquals("9.99k", NumberAbbreviator.abbreviate(context, 9999));
assertEquals("9.99k", NumberAbbreviator.INSTANCE.abbreviate(context, 9999));
}
}