1717package org .gnucash .android .model ;
1818
1919
20- import android .preference .PreferenceManager ;
20+ import android .graphics .Color ;
21+ import android .support .annotation .NonNull ;
2122
2223import org .gnucash .android .BuildConfig ;
23- import org .gnucash .android .app .GnuCashApplication ;
24- import org .gnucash .android .export .Exporter ;
2524import org .gnucash .android .export .ofx .OfxHelper ;
2625import org .w3c .dom .Document ;
2726import org .w3c .dom .Element ;
3029import java .util .ArrayList ;
3130import java .util .Currency ;
3231import java .util .List ;
33- import java .util .regex .Pattern ;
3432
3533/**
3634 * An account represents a transaction account in with {@link Transaction}s may be recorded
@@ -51,25 +49,13 @@ public class Account extends BaseModel{
5149 */
5250 public static final String MIME_TYPE = "vnd.android.cursor.item/vnd." + BuildConfig .APPLICATION_ID + ".account" ;
5351
54- /*
55- ^ anchor for start of string
56- # the literal #
57- ( start of group
58- ?: indicate a non-capturing group that doesn't generate back-references
59- [0-9a-fA-F] hexadecimal digit
60- {3} three times
61- ) end of group
62- {1,2} repeat either once or twice
63- $ anchor for end of string
64- */
65- /**
66- * Regular expression for validating color code strings.
67- * Accepts #rgb and #rrggbb
68- */
69- //TODO: Allow use of #aarrggbb format as well
70- public static final String COLOR_HEX_REGEX = "^#(?:[0-9a-fA-F]{3}){1,2}$" ;
52+ /**
53+ * Default color, if not set explicitly through {@link #setColor(String)}.
54+ */
55+ // TODO: get it from a theme value?
56+ public static final int DEFAULT_COLOR = Color .LTGRAY ;
7157
72- /**
58+ /**
7359 * Accounts types which are used by the OFX standard
7460 */
7561 public enum OfxAccountType {CHECKING , SAVINGS , MONEYMRKT , CREDITLINE }
@@ -88,7 +74,7 @@ public enum OfxAccountType {CHECKING, SAVINGS, MONEYMRKT, CREDITLINE }
8874 /**
8975 * Account description
9076 */
91- private String mDescription ;
77+ private String mDescription = "" ;
9278
9379 /**
9480 * Currency used by transactions in this account
@@ -106,7 +92,7 @@ public enum OfxAccountType {CHECKING, SAVINGS, MONEYMRKT, CREDITLINE }
10692 * Defaults to {@link AccountType#CASH}
10793 */
10894 private AccountType mAccountType = AccountType .CASH ;
109-
95+
11096 /**
11197 * List of transactions in this account
11298 */
@@ -132,7 +118,7 @@ public enum OfxAccountType {CHECKING, SAVINGS, MONEYMRKT, CREDITLINE }
132118 /**
133119 * Account color field in hex format #rrggbb
134120 */
135- private String mColorCode ;
121+ private int mColor = DEFAULT_COLOR ;
136122
137123 /**
138124 * Flag which marks this account as a favorite account
@@ -148,13 +134,13 @@ public enum OfxAccountType {CHECKING, SAVINGS, MONEYMRKT, CREDITLINE }
148134 * An extra key for passing the currency code (according ISO 4217) in an intent
149135 */
150136 public static final String EXTRA_CURRENCY_CODE = "org.gnucash.android.extra.currency_code" ;
151-
137+
152138 /**
153- * Extra key for passing the unique ID of the parent account when creating a
139+ * Extra key for passing the unique ID of the parent account when creating a
154140 * new account using Intents
155141 */
156142 public static final String EXTRA_PARENT_UID = "org.gnucash.android.extra.parent_uid" ;
157-
143+
158144 /**
159145 * Constructor
160146 * Creates a new account with the default currency and a generated unique ID
@@ -165,7 +151,7 @@ public Account(String name) {
165151 this .mFullName = mName ;
166152 setCommodity (Commodity .DEFAULT_COMMODITY );
167153 }
168-
154+
169155 /**
170156 * Overloaded constructor
171157 * @param name Name of the account
@@ -211,18 +197,18 @@ public void setFullName(String fullName) {
211197 }
212198
213199 /**
214- * Returns the account mDescription
215- * @return String with mDescription
200+ * Returns the account description
201+ * @return String with description
216202 */
217203 public String getDescription () {
218204 return mDescription ;
219205 }
220206
221207 /**
222- * Sets the account mDescription
223- * @param description String mDescription
208+ * Sets the account description
209+ * @param description Account description
224210 */
225- public void setDescription (String description ) {
211+ public void setDescription (@ NonNull String description ) {
226212 this .mDescription = description ;
227213 }
228214
@@ -251,11 +237,11 @@ public void addTransaction(Transaction transaction){
251237 transaction .setCommodity (mCommodity );
252238 mTransactionsList .add (transaction );
253239 }
254-
240+
255241 /**
256242 * Sets a list of transactions for this account.
257243 * Overrides any previous transactions with those in the list.
258- * The account UID and currency of the transactions will be set to the unique ID
244+ * The account UID and currency of the transactions will be set to the unique ID
259245 * and currency of the account respectively
260246 * @param transactionsList List of {@link Transaction}s to be set.
261247 */
@@ -270,7 +256,7 @@ public void setTransactions(List<Transaction> transactionsList){
270256 public List <Transaction > getTransactions (){
271257 return mTransactionsList ;
272258 }
273-
259+
274260 /**
275261 * Returns the number of transactions in this account
276262 * @return Number transactions in account
@@ -293,26 +279,34 @@ public Money getBalance(){
293279 }
294280
295281 /**
296- * Returns the color code of the account in the format #rrggbb
297- * @return Color code of the account
282+ * Returns the color of the account.
283+ * @return Color of the account as an int as returned by {@link Color}.
298284 */
299- public String getColorHexCode () {
300- return mColorCode ;
285+ public int getColor () {
286+ return mColor ;
301287 }
302288
289+ /**
290+ * Sets the color of the account.
291+ * @param color Color as an int as returned by {@link Color}.
292+ * @throws java.lang.IllegalArgumentException if the color is transparent,
293+ * which is not supported.
294+ */
295+ public void setColor (int color ) {
296+ if (Color .alpha (color ) < 255 )
297+ throw new IllegalArgumentException ("Transparent colors are not supported: " + color );
298+ mColor = color ;
299+ }
300+
303301 /**
304- * Sets the color code of the account.
305- * @param colorCode Color code to be set in the format #rrggbb or #rgb
306- * @throws java.lang.IllegalArgumentException if the color code is not properly formatted
302+ * Sets the color of the account.
303+ * @param colorCode Color code to be set in the format #rrggbb
304+ * @throws java.lang.IllegalArgumentException if the color code is not properly formatted or
305+ * the color is transparent.
307306 */
308- public void setColorCode (String colorCode ) {
309- if (colorCode == null )
310- return ;
311-
312- if (!Pattern .matches (COLOR_HEX_REGEX , colorCode ))
313- throw new IllegalArgumentException ("Invalid color hex code: " + colorCode );
314-
315- this .mColorCode = colorCode ;
307+ //TODO: Allow use of #aarrggbb format as well
308+ public void setColor (@ NonNull String colorCode ) {
309+ setColor (Color .parseColor (colorCode ));
316310 }
317311
318312 /**
@@ -332,7 +326,7 @@ public void setFavorite(boolean isFavorite) {
332326 }
333327
334328 /**
335- * @return the mCurrency
329+ * Returns the currency for this account.
336330 */
337331 public Currency getCurrency () {
338332 return Currency .getInstance (mCurrencyCode );
@@ -348,7 +342,6 @@ public void setCurrencyCode(String currencyCode){
348342
349343 /**
350344 * Return the commodity for this account
351- * @return
352345 */
353346 public Commodity getCommodity (){
354347 return mCommodity ;
0 commit comments