Skip to content

Commit

Permalink
Fixed categories report calculation. Categories report includes tags
Browse files Browse the repository at this point in the history
  • Loading branch information
mvarnagiris committed Nov 6, 2014
1 parent d176b45 commit ed2aa66
Show file tree
Hide file tree
Showing 8 changed files with 147 additions and 76 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
###Version: 0.12.2 - Not Release Yet
- ```new``` Landscape layout for categories report.
- ```new``` Categories report breaks down each category into tags.
- ```fix``` Tags are now wrapping in transactions list to avoid strange symbols when they do not fit.
- ```fix``` Can create income categories again.
- ```new``` Landscape layout for categories report.
- ```fix``` Fixed calculation differences between categories amount and transactions list.

###Version: 0.12.1
- ```fix``` Fixed crash when refreshing currencies or trying to enter or edit a transaction.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import android.widget.ListView;

import com.code44.finance.R;
import com.code44.finance.adapters.CategoriesReportAdapter;
import com.code44.finance.adapters.NavigationAdapter;
import com.code44.finance.common.model.TransactionType;
import com.code44.finance.data.db.Tables;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.code44.finance.adapters;
package com.code44.finance.ui.reports.categories;

import android.content.Context;
import android.support.v4.util.Pair;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
Expand All @@ -10,9 +11,11 @@

import com.code44.finance.R;
import com.code44.finance.data.model.Currency;
import com.code44.finance.ui.reports.categories.CategoriesReportData;
import com.code44.finance.data.model.Tag;
import com.code44.finance.utils.MoneyFormatter;

import java.util.List;

public class CategoriesReportAdapter extends BaseAdapter {
private final Context context;
private final Currency defaultCurrency;
Expand Down Expand Up @@ -51,6 +54,7 @@ public CategoriesReportAdapter(Context context, Currency defaultCurrency) {
holder.percent_TV.setText((percent == 0 ? "<1" : (percent == 100 && getCount() > 1 ? ">99" : percent)) + "%");
holder.title_TV.setText(item.getCategory().getTitle());
holder.amount_TV.setText(MoneyFormatter.format(defaultCurrency, item.getAmount()));
bindTags((ViewGroup) convertView, item.getTags());

return convertView;
}
Expand All @@ -65,6 +69,28 @@ private int getPercent(long amount) {
return Math.round(100.0f * amount / totalAmount);
}

private void bindTags(ViewGroup parent, List<Pair<Tag, Long>> tags) {
final int staticViewCount = 1;
final int currentCount = parent.getChildCount() - staticViewCount;
final int newCount = tags.size();
if (newCount > currentCount) {
for (int i = currentCount; i < newCount; i++) {
final View view = LayoutInflater.from(context).inflate(R.layout.li_category_report_tag, parent, false);
new TagViewHolder(view);
parent.addView(view);
}
} else {
parent.removeViews(staticViewCount, currentCount - newCount);
}

for (int i = staticViewCount, size = staticViewCount + tags.size(); i < size; i++) {
final TagViewHolder holder = (TagViewHolder) parent.getChildAt(i).getTag();
final Pair<Tag, Long> tagAmount = tags.get(i - staticViewCount);
holder.title_TV.setText(tagAmount.first.getTitle());
holder.amount_TV.setText(MoneyFormatter.format(defaultCurrency, tagAmount.second));
}
}

private static final class ViewHolder {
public final ImageView color_IV;
public final TextView percent_TV;
Expand All @@ -79,4 +105,15 @@ public ViewHolder(View view) {
view.setTag(this);
}
}

private static final class TagViewHolder {
public final TextView title_TV;
public final TextView amount_TV;

public TagViewHolder(View view) {
title_TV = (TextView) view.findViewById(R.id.titleTextView);
amount_TV = (TextView) view.findViewById(R.id.amountTextView);
view.setTag(this);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import android.content.Context;
import android.database.Cursor;
import android.util.Pair;
import android.support.v4.util.Pair;

import com.code44.finance.R;
import com.code44.finance.common.model.TransactionState;
Expand All @@ -19,7 +19,6 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

public class CategoriesReportData {
private final PieChartData pieChartData;
Expand All @@ -42,30 +41,32 @@ public CategoriesReportData(Context context, Cursor cursor, Currency mainCurrenc
} while (cursor.moveToNext());
}

final TreeMap<Category, Long> sortedExpenses = new TreeMap<>(new CategoryExpenseComparator(categoryExpenses));
sortedExpenses.putAll(categoryExpenses);

final List<Pair<Category, Long>> sortedExpenses = new ArrayList<>();
for (Category category : categoryExpenses.keySet()) {
sortedExpenses.add(Pair.create(category, categoryExpenses.get(category)));
}
Collections.sort(sortedExpenses, new CategoryExpenseComparator());

categoriesReportItems = new ArrayList<>();
final PieChartData.Builder builder = PieChartData.builder();
for (Category category : sortedExpenses.descendingKeySet()) {
final Long amount = sortedExpenses.get(category);
builder.addValues(new PieChartValue(amount, category.getColor()));
for (Pair<Category, Long> category : sortedExpenses) {
final Long amount = category.second;
builder.addValues(new PieChartValue(amount, category.first.getColor()));

final List<Pair<Tag, Long>> tags;
final Map<Tag, Long> tagExpenses = categoryTagExpenses.get(category);
final Map<Tag, Long> tagExpenses = categoryTagExpenses.get(category.first);
if (tagExpenses != null) {
final TreeMap<Tag, Long> sortedTagExpenses = new TreeMap<>(new TagExpenseComparator(tagExpenses));
sortedTagExpenses.putAll(tagExpenses);

tags = new ArrayList<>();
for (Tag tag : sortedTagExpenses.descendingKeySet()) {
tags.add(Pair.create(tag, sortedTagExpenses.get(tag)));
for (Tag tag : tagExpenses.keySet()) {
tags.add(Pair.create(tag, tagExpenses.get(tag)));
}
Collections.sort(tags, new TagExpenseComparator());
} else {
tags = Collections.emptyList();
}

categoriesReportItems.add(new CategoriesReportItem(category, amount, tags));
categoriesReportItems.add(new CategoriesReportItem(category.first, amount, tags));
}
pieChartData = builder.build();
}
Expand Down Expand Up @@ -123,7 +124,11 @@ private void increaseCategoryTagsExpenses(Map<Category, Map<Tag, Long>> category
tagExpense = new HashMap<>();
tagExpenseAmount = amount;
} else {
tagExpenseAmount = tagExpense.get(tag) + amount;
tagExpenseAmount = tagExpense.get(tag);
if (tagExpenseAmount == null) {
tagExpenseAmount = 0L;
}
tagExpenseAmount += amount;
}
tagExpense.put(tag, tagExpenseAmount);
categoryTagExpenses.put(category, tagExpense);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,16 @@
package com.code44.finance.ui.reports.categories;

import android.support.v4.util.Pair;

import com.code44.finance.data.model.Category;

import java.util.Comparator;
import java.util.Map;

class CategoryExpenseComparator implements Comparator<Category> {
final Map<Category, Long> base;

public CategoryExpenseComparator(Map<Category, Long> base) {
this.base = base;
}

@Override public int compare(Category category1, Category category2) {
final Long category1Total = base.get(category1);
final Long category2Total = base.get(category2);
if (category1Total > category2Total) {
class CategoryExpenseComparator implements Comparator<Pair<Category, Long>> {
@Override public int compare(Pair<Category, Long> leftValue, Pair<Category, Long> rightValue) {
if (leftValue.second < rightValue.second) {
return 1;
} else if (base.get(category1) < base.get(category2)) {
} else if (leftValue.second > rightValue.second) {
return -1;
} else {
return 0;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,16 @@
package com.code44.finance.ui.reports.categories;

import android.support.v4.util.Pair;

import com.code44.finance.data.model.Tag;

import java.util.Comparator;
import java.util.Map;

class TagExpenseComparator implements Comparator<Tag> {
final Map<Tag, Long> base;

public TagExpenseComparator(Map<Tag, Long> base) {
this.base = base;
}

@Override public int compare(Tag tag1, Tag tag2) {
final Long tag1Total = base.get(tag1);
final Long tag2Total = base.get(tag2);
if (tag1Total > tag2Total) {
class TagExpenseComparator implements Comparator<Pair<Tag, Long>> {
@Override public int compare(Pair<Tag, Long> leftValue, Pair<Tag, Long> rightValue) {
if (leftValue.second < rightValue.second) {
return 1;
} else if (base.get(tag1) < base.get(tag2)) {
} else if (leftValue.second > rightValue.second) {
return -1;
} else {
return 0;
Expand Down
62 changes: 31 additions & 31 deletions financius/src/main/res/layout/li_category_report.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,45 +3,46 @@
style="@style/ListItem"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal">

<FrameLayout
android:layout_width="@dimen/keyline_between"
android:layout_height="@dimen/keyline_between"
android:layout_gravity="center_vertical"
android:layout_marginRight="@dimen/keyline"
tools:ignore="RtlHardcoded">

<ImageView
android:id="@+id/colorImageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:src="@drawable/circle"
tools:ignore="ContentDescription" />

<TextView
android:id="@+id/percent_TV"
style="@style/Text.Caption"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textColor="@color/text_primary_inverse"
tools:text="100%"
tools:textColor="@color/text_primary" />

</FrameLayout>
android:orientation="vertical">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical">
android:orientation="horizontal">

<FrameLayout
android:layout_width="@dimen/keyline_between"
android:layout_height="@dimen/keyline_between"
android:layout_gravity="center_vertical"
android:layout_marginRight="@dimen/keyline"
tools:ignore="RtlHardcoded">

<ImageView
android:id="@+id/colorImageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:src="@drawable/circle"
tools:ignore="ContentDescription" />

<TextView
android:id="@+id/percent_TV"
style="@style/Text.Caption"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textColor="@color/text_primary_inverse"
tools:text="100%"
tools:textColor="@color/text_primary" />

</FrameLayout>

<TextView
android:id="@+id/titleTextView"
style="@style/Text.Body2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:ellipsize="end"
android:singleLine="true"
Expand All @@ -52,11 +53,10 @@
style="@style/Text.Body2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_gravity="right|center_vertical"
android:layout_marginLeft="@dimen/space_normal"
tools:ignore="RtlHardcoded"
tools:text="58.99 $" />

</LinearLayout>

</LinearLayout>
42 changes: 42 additions & 0 deletions financius/src/main/res/layout/li_category_report_tag.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
style="@style/ListItem"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:minHeight="0dp"
android:orientation="horizontal"
android:paddingBottom="0dp"
android:paddingLeft="0dp"
android:paddingRight="0dp"
android:paddingTop="0dp">

<Space
android:layout_width="@dimen/keyline_between"
android:layout_height="1dp"
android:layout_marginRight="@dimen/keyline"
tools:ignore="RtlHardcoded" />

<TextView
android:id="@+id/titleTextView"
style="@style/Text.Body2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:ellipsize="end"
android:singleLine="true"
android:textColor="@color/text_secondary"
tools:text="Lunch" />

<TextView
android:id="@+id/amountTextView"
style="@style/Text.Body2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|center_vertical"
android:layout_marginLeft="@dimen/space_normal"
android:textColor="@color/text_secondary"
tools:ignore="RtlHardcoded"
tools:text="58.99 $" />

</LinearLayout>

0 comments on commit ed2aa66

Please sign in to comment.