Skip to content

Commit

Permalink
Store account color as an int.
Browse files Browse the repository at this point in the history
All uses of Account.getColorHexCode() were being converted to an int
with Color.parseColor().
  • Loading branch information
rivaldi8 committed Apr 4, 2016
1 parent 6d31dc8 commit 17d64d5
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 29 deletions.
12 changes: 8 additions & 4 deletions app/src/main/java/org/gnucash/android/db/AccountsDbAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,8 @@ protected SQLiteStatement compileReplaceStatement(@NonNull final Account account
mReplaceStatement.bindString(3, account.getDescription());
mReplaceStatement.bindString(4, account.getAccountType().name());
mReplaceStatement.bindString(5, account.getCurrency().getCurrencyCode());
if (account.getColorHexCode() != Account.DEFAULT_COLOR) {
mReplaceStatement.bindString(6, account.getColorHexCode());
if (account.getColor() != Account.DEFAULT_COLOR) {
mReplaceStatement.bindString(6, convertToRGBHexString(account.getColor()));
}
mReplaceStatement.bindLong(7, account.isFavorite() ? 1 : 0);
mReplaceStatement.bindString(8, account.getFullName());
Expand Down Expand Up @@ -205,6 +205,10 @@ protected SQLiteStatement compileReplaceStatement(@NonNull final Account account
return mReplaceStatement;
}

private String convertToRGBHexString(int color) {
return String.format("#%06X", (0xFFFFFF & color));
}

/**
* Marks all transactions for a given account as exported
* @param accountUID Unique ID of the record to be marked as exported
Expand Down Expand Up @@ -416,7 +420,7 @@ private Account buildSimpleAccountInstance(Cursor c) {
account.setDefaultTransferAccountUID(c.getString(c.getColumnIndexOrThrow(AccountEntry.COLUMN_DEFAULT_TRANSFER_ACCOUNT_UID)));
String color = c.getString(c.getColumnIndexOrThrow(AccountEntry.COLUMN_COLOR_CODE));
if (color != null)
account.setColorCode(color);
account.setColor(color);
account.setFavorite(c.getInt(c.getColumnIndexOrThrow(AccountEntry.COLUMN_FAVORITE)) == 1);
account.setFullName(c.getString(c.getColumnIndexOrThrow(AccountEntry.COLUMN_FULL_NAME)));
account.setHidden(c.getInt(c.getColumnIndexOrThrow(AccountEntry.COLUMN_HIDDEN)) == 1);
Expand Down Expand Up @@ -563,7 +567,7 @@ public String getOrCreateImbalanceAccountUID(Currency currency){
account.setAccountType(AccountType.BANK);
account.setParentUID(getOrCreateGnuCashRootAccountUID());
account.setHidden(!GnuCashApplication.isDoubleEntryEnabled());
account.setColorCode("#964B00");
account.setColor("#964B00");
addRecord(account);
uid = account.getUID();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ public void endElement(String uri, String localName, String qualifiedName) throw
color = "#" + color.replaceAll(".(.)?", "$1").replace("null", "");
try {
if (mAccount != null)
mAccount.setColorCode(color);
mAccount.setColor(color);
} catch (IllegalArgumentException ex) {
//sometimes the color entry in the account file is "Not set" instead of just blank. So catch!
Log.e(LOG_TAG, "Invalid color code '" + color + "' for account " + mAccount.getName());
Expand Down
36 changes: 23 additions & 13 deletions app/src/main/java/org/gnucash/android/model/Account.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,9 @@


import android.graphics.Color;
import android.preference.PreferenceManager;
import android.support.annotation.NonNull;

import org.gnucash.android.BuildConfig;
import org.gnucash.android.app.GnuCashApplication;
import org.gnucash.android.export.Exporter;
import org.gnucash.android.export.ofx.OfxHelper;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
Expand Down Expand Up @@ -72,9 +69,10 @@ public class Account extends BaseModel{
public static final String COLOR_HEX_REGEX = "^#(?:[0-9a-fA-F]{3}){1,2}$";

/**
* Default color, if not set explicitly through {@link #setColorCode(String)}.
* Default color, if not set explicitly through {@link #setColor(String)}.
*/
public static final String DEFAULT_COLOR = "#cccccc"; // Color.LT_GRAY
// TODO: get it from a theme value?
public static final int DEFAULT_COLOR = Color.LTGRAY;

/**
* Accounts types which are used by the OFX standard
Expand Down Expand Up @@ -139,7 +137,7 @@ public enum OfxAccountType {CHECKING, SAVINGS, MONEYMRKT, CREDITLINE }
/**
* Account color field in hex format #rrggbb
*/
private String mColorCode = DEFAULT_COLOR;
private int mColor = DEFAULT_COLOR;

/**
* Flag which marks this account as a favorite account
Expand Down Expand Up @@ -300,23 +298,35 @@ public Money getBalance(){
}

/**
* Returns the color code of the account in the format #rrggbb
* @return Color code of the account
* Returns the color of the account.
* @return Color of the account as an int as returned by {@link Color}.
*/
public String getColorHexCode() {
return mColorCode;
public int getColor() {
return mColor;
}

/**
* Sets the color of the account.
* @param color Color as an int as returned by {@link Color}.
* @throws java.lang.IllegalArgumentException if the color is transparent,
* which is not supported.
*/
public void setColor(int color) {
if (Color.alpha(color) < 255)
throw new IllegalArgumentException("Transparent colors are not supported: " + color);
mColor = color;
}

/**
* Sets the color code of the account.
* Sets the color of the account.
* @param colorCode Color code to be set in the format #rrggbb or #rgb
* @throws java.lang.IllegalArgumentException if the color code is not properly formatted
*/
public void setColorCode(@NonNull String colorCode) {
public void setColor(@NonNull String colorCode) {
if (!Pattern.matches(COLOR_HEX_REGEX, colorCode))
throw new IllegalArgumentException("Invalid color hex code: " + colorCode);

this.mColorCode = colorCode;
setColor(Color.parseColor(colorCode));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ public class AccountFormFragment extends Fragment {
*/
private boolean mUseDoubleEntry;

private String mSelectedColor = Account.DEFAULT_COLOR;
private int mSelectedColor = Account.DEFAULT_COLOR;

/**
* Trigger for color picker dialog
Expand All @@ -214,7 +214,7 @@ public class AccountFormFragment extends Fragment {
@Override
public void onColorSelected(int color) {
mColorSquare.setBackgroundColor(color);
mSelectedColor = String.format("#%06X", (0xFFFFFF & color));
mSelectedColor = color;
}
};

Expand Down Expand Up @@ -414,7 +414,7 @@ private void initializeViewsWithAccount(Account account){
}

mPlaceholderCheckBox.setChecked(account.isPlaceholderAccount());
mColorSquare.setBackgroundColor(Color.parseColor(account.getColorHexCode()));
mColorSquare.setBackgroundColor(account.getColor());

setAccountTypeSelection(account.getAccountType());
}
Expand Down Expand Up @@ -535,7 +535,7 @@ private void showColorPickerDialog(){
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
int currentColor = Color.LTGRAY;
if (mAccount != null){
currentColor = Color.parseColor(mAccount.getColorHexCode());
currentColor = mAccount.getColor();
}

ColorPickerDialog colorPickerDialogFragment = ColorPickerDialog.newInstance(
Expand Down Expand Up @@ -757,7 +757,7 @@ private void saveAccount() {

mAccount.setDescription(mDescriptionEditText.getText().toString());
mAccount.setPlaceHolderFlag(mPlaceholderCheckBox.isChecked());
mAccount.setColorCode(mSelectedColor);
mAccount.setColor(mSelectedColor);

long newParentAccountId;
String newParentAccountUID;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ private BarData getData() {
if (!accountToColorMap.containsKey(account.getUID())) {
Integer color;
if (mUseAccountColor) {
color = Color.parseColor(account.getColorHexCode());
color = account.getColor();
} else {
color = COLORS[accountToColorMap.size() % COLORS.length];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ private PieData getData() {
if (balance > 0) {
dataSet.addEntry(new Entry((float) balance, dataSet.getEntryCount()));
colors.add(mUseAccountColor
? Color.parseColor(account.getColorHexCode())
? account.getColor()
: ReportsActivity.COLORS[(dataSet.getEntryCount() - 1) % ReportsActivity.COLORS.length]);
labels.add(account.getName());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
package org.gnucash.android.ui.report;

import android.content.res.ColorStateList;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
Expand Down Expand Up @@ -200,7 +199,7 @@ private PieData getData() {
Collections.singletonList(account.getUID()), start, end).asDouble();
if (balance > 0) {
dataSet.addEntry(new Entry((float) balance, dataSet.getEntryCount()));
colors.add(Color.parseColor(account.getColorHexCode()));
colors.add(account.getColor());
labels.add(account.getName());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package org.gnucash.android.test.unit.model;

import android.graphics.Color;

import org.gnucash.android.BuildConfig;
import org.gnucash.android.model.Account;
import org.gnucash.android.model.Commodity;
Expand Down Expand Up @@ -66,7 +68,13 @@ public void testTransactionsHaveSameCurrencyAsAccount(){
@Test(expected = IllegalArgumentException.class)
public void testSetInvalidColorCode(){
Account account = new Account("Test");
account.setColorCode("443859");
account.setColor("443859");
}

@Test(expected = IllegalArgumentException.class)
public void testSetColorWithAlphaComponent(){
Account account = new Account("Test");
account.setColor(Color.parseColor("#aa112233"));
}

@Test
Expand Down Expand Up @@ -108,6 +116,6 @@ public void settingCurrencyCode_shouldNotSetCommodity(){
public void newInstance_shouldReturnNonNullValues() {
Account account = new Account("Test account");
assertThat(account.getDescription()).isEqualTo("");
assertThat(account.getColorHexCode()).isEqualTo(Account.DEFAULT_COLOR);
assertThat(account.getColor()).isEqualTo(Account.DEFAULT_COLOR);
}
}

0 comments on commit 17d64d5

Please sign in to comment.