Skip to content

Commit

Permalink
Feature: Color code accounts
Browse files Browse the repository at this point in the history
Color codes in GnuCash accounts structure file are now imported
The color of the account themes the tab display of the account view and any child accounts which do not have an own color
  • Loading branch information
codinguser committed Dec 19, 2013
1 parent 3b6c654 commit 3f3597f
Show file tree
Hide file tree
Showing 12 changed files with 332 additions and 11 deletions.
4 changes: 4 additions & 0 deletions app/res/layout/list_item_account.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
android:paddingRight="@dimen/edge_padding"
android:orientation="horizontal"
style="@style/ListItem">

<View android:id="@+id/account_color_strip"
android:layout_width="@dimen/strip_width"
android:layout_height="match_parent" />
<include
layout="@layout/list_item_2_lines"
android:layout_height="wrap_content"
Expand Down
5 changes: 5 additions & 0 deletions app/res/values/colors.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,9 @@
<color name="light_gray">#FFAAAAAA</color>
<color name="abs__holo_blue_light">#ff33b5e5</color>
<color name="title_green">#319D67</color>
<color name="account_green">#319D67</color>
<color name="account_red">#D13E29</color>
<color name="account_gold">#EE8600</color>
<color name="account_blue">#1469EB</color>
<color name="account_purple">#B304AD</color>
</resources>
1 change: 1 addition & 0 deletions app/res/values/dimens.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@
<dimen name="widget_margin">8dp</dimen>
<dimen name="section_header_height">20dp</dimen>
<dimen name="form_row_bottom_margin">10dp</dimen>
<dimen name="strip_width">5dp</dimen>
</resources>
37 changes: 32 additions & 5 deletions app/src/org/gnucash/android/data/Account.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import java.util.Locale;
import java.util.UUID;

import android.content.Context;
import android.graphics.Color;
import org.gnucash.android.app.GnuCashApplication;
import org.gnucash.android.db.AccountsDbAdapter;
import org.gnucash.android.export.ofx.OfxExporter;
Expand Down Expand Up @@ -127,6 +127,11 @@ public enum OfxAccountType {CHECKING, SAVINGS, MONEYMRKT, CREDITLINE };
*/
private boolean mPlaceholderAccount;

/**
* Account color field in hex format #rrggbb
*/
private String mColorCode;

/**
* An extra key for passing the currency code (according ISO 4217) in an intent
*/
Expand Down Expand Up @@ -311,13 +316,35 @@ public boolean hasUnexportedTransactions(){
public Money getBalance(){
//TODO: Consider double entry transactions
Money balance = new Money(new BigDecimal(0), this.mCurrency);
for (Transaction transx : mTransactionsList) {
balance = balance.add(transx.getAmount());
for (Transaction transaction : mTransactionsList) {
balance = balance.add(transaction.getAmount());
}
return balance;
}

/**

/**
* Returns the color code of the account in the format #rrggbb
* @return Color code of the account
*/
public String getColorCode() {
return mColorCode;
}

/**
* Sets the color code of the account.
* @param colorCode Color code to be set in the format #rrggbb
* @throws java.lang.IllegalArgumentException if the color code is not properly formatted
*/
public void setColorCode(String colorCode) {
//TODO: Proper regex validation
if (!colorCode.startsWith("#") || colorCode.length() < 4 || colorCode.length() > 9){
throw new IllegalArgumentException("Invalid color code for account");
}
this.mColorCode = colorCode;
}


/**
* @return the mCurrency
*/
public Currency getCurrency() {
Expand Down
22 changes: 21 additions & 1 deletion app/src/org/gnucash/android/db/AccountsDbAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ public long addAccount(Account account){
contentValues.put(DatabaseHelper.KEY_PARENT_ACCOUNT_UID, account.getParentUID());
contentValues.put(DatabaseHelper.KEY_DEFAULT_TRANSFER_ACCOUNT_UID, account.getDefaultTransferAccountUID());
contentValues.put(DatabaseHelper.KEY_PLACEHOLDER, account.isPlaceholderAccount() ? 1 : 0);
contentValues.put(DatabaseHelper.KEY_COLOR_CODE, account.getColorCode());

long rowId = -1;
if ((rowId = getAccountID(account.getUID())) > 0){
Expand Down Expand Up @@ -182,6 +183,7 @@ public Account buildAccountInstance(Cursor c){
account.setTransactions(mTransactionsAdapter.getAllTransactionsForAccount(uid));
account.setPlaceHolderFlag(c.getInt(DatabaseAdapter.COLUMN_PLACEHOLDER) == 1);
account.setDefaultTransferAccountUID(c.getString(DatabaseAdapter.COLUMN_DEFAULT_TRANSFER_ACCOUNT_UID));
account.setColorCode(c.getString(DatabaseAdapter.COLUMN_COLOR_CODE));
return account;
}

Expand Down Expand Up @@ -269,7 +271,25 @@ public String getAccountUID(long id){
}
return uid;
}


/**
* Returns the color code for the account in format #rrggbb
* @param accountId Database row ID of the account
* @return String color code of account or null if none
*/
public String getAccountColorCode(long accountId){
String colorCode = null;
Cursor c = mDb.query(DatabaseHelper.ACCOUNTS_TABLE_NAME,
new String[]{DatabaseHelper.KEY_ROW_ID, DatabaseHelper.KEY_COLOR_CODE},
DatabaseHelper.KEY_ROW_ID + "=" + accountId,
null, null, null, null);
if (c != null && c.moveToFirst()){
colorCode = c.getString(c.getColumnIndexOrThrow(DatabaseHelper.KEY_COLOR_CODE));
c.close();
}
return colorCode;
}

/**
* Returns the {@link AccountType} of the account with unique ID <code>uid</code>
* @param uid Unique ID of the account
Expand Down
1 change: 1 addition & 0 deletions app/src/org/gnucash/android/db/DatabaseAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public abstract class DatabaseAdapter {
public static final int COLUMN_PARENT_ACCOUNT_UID = 5;
public static final int COLUMN_PLACEHOLDER = 6;
public static final int COLUMN_DEFAULT_TRANSFER_ACCOUNT_UID = 7;
public static final int COLUMN_COLOR_CODE = 8;

/**
* {@link DatabaseHelper} for creating and opening the database
Expand Down
10 changes: 10 additions & 0 deletions app/src/org/gnucash/android/db/DatabaseHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,11 @@ public class DatabaseHelper extends SQLiteOpenHelper {
*/
public static final String KEY_DEFAULT_TRANSFER_ACCOUNT_UID = "default_transfer_account_uid";

/**
* Color code for the account
*/
public static final String KEY_COLOR_CODE = "color_code";

/**
* Transaction description database column
*/
Expand Down Expand Up @@ -158,6 +163,7 @@ public class DatabaseHelper extends SQLiteOpenHelper {
+ KEY_PARENT_ACCOUNT_UID + " varchar(255), "
+ KEY_PLACEHOLDER + " tinyint default 0, "
+ KEY_DEFAULT_TRANSFER_ACCOUNT_UID + " varchar(255), "
+ KEY_COLOR_CODE + " varchar(255), "
+ "UNIQUE (" + KEY_UID + ")"
+ ");";

Expand Down Expand Up @@ -245,8 +251,12 @@ public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
String addDefaultTransferAccount = "ALTER TABLE " + ACCOUNTS_TABLE_NAME
+ " ADD COLUMN " + KEY_DEFAULT_TRANSFER_ACCOUNT_UID + " varchar(255)";

String addAccountColor = " ALTER TABLE " + ACCOUNTS_TABLE_NAME
+ " ADD COLUMN " + KEY_COLOR_CODE + " varchar(255)";

db.execSQL(addRecurrencePeriod);
db.execSQL(addDefaultTransferAccount);
db.execSQL(addAccountColor);

oldVersion = 4;
}
Expand Down
6 changes: 6 additions & 0 deletions app/src/org/gnucash/android/ui/accounts/AccountsActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -337,24 +337,30 @@ public void onClick(DialogInterface dialog, int which) {
switch (index) {
case 0:
account.setAccountType(AccountType.EXPENSE);
account.setColorCode(getString(R.color.account_red));
break;

case 1:
account.setAccountType(AccountType.INCOME);
account.setColorCode(getString(R.color.account_green));
break;

case 2:
account.setAccountType(AccountType.ASSET);
account.setColorCode(getString(R.color.account_gold));
break;
case 3:
account.setAccountType(AccountType.EQUITY);
account.setColorCode(getString(R.color.account_blue));
break;
case 4:
account.setAccountType(AccountType.LIABILITY);
account.setColorCode(getString(R.color.account_purple));
break;

default:
account.setAccountType(AccountType.CASH);
account.setColorCode(getString(R.color.account_green));
break;
}
dbAdapter.addAccount(account);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Color;
import android.graphics.Rect;
import android.os.AsyncTask;
import android.os.Bundle;
Expand Down Expand Up @@ -673,8 +674,14 @@ public void bindView(View v, Context context, Cursor cursor) {
.findViewById(R.id.transactions_summary);
new AccountBalanceTask(accountBalanceTextView, getActivity()).execute(accountId);

boolean isPlaceholderAccount = mAccountsDbAdapter.isPlaceholderAccount(accountId);
View colorStripView = v.findViewById(R.id.account_color_strip);
String accountColor = cursor.getString(DatabaseAdapter.COLUMN_COLOR_CODE);
if (accountColor != null){
int color = Color.parseColor(accountColor);
colorStripView.setBackgroundColor(color);
}

boolean isPlaceholderAccount = mAccountsDbAdapter.isPlaceholderAccount(accountId);
ImageButton newTransactionButton = (ImageButton) v.findViewById(R.id.btn_new_transaction);
if (isPlaceholderAccount){
newTransactionButton.setVisibility(View.GONE);
Expand Down
Loading

0 comments on commit 3f3597f

Please sign in to comment.