Skip to content

Commit

Permalink
Nullable commodity
Browse files Browse the repository at this point in the history
  • Loading branch information
pnemonic78 committed Oct 31, 2024
1 parent 7efa299 commit eefabbd
Show file tree
Hide file tree
Showing 12 changed files with 83 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ public void editingAccountShouldNotDeleteTransactions() {
isDisplayed())).perform(click());

Account account = new Account("Transfer Account");
account.setCommodity(Commodity.getInstance(ACCOUNTS_CURRENCY.getCurrencyCode()));
account.setCommodity(ACCOUNTS_CURRENCY);
Transaction transaction = new Transaction("Simple transaction");
transaction.setCommodity(ACCOUNTS_CURRENCY);
Split split = new Split(new Money(BigDecimal.TEN, ACCOUNTS_CURRENCY), account.getUID());
Expand Down
52 changes: 34 additions & 18 deletions app/src/main/java/org/gnucash/android/app/GnuCashApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@
import android.graphics.Color;
import android.os.Build;
import android.os.SystemClock;
import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.preference.PreferenceManager;

import com.google.firebase.FirebaseApp;
Expand Down Expand Up @@ -86,26 +88,27 @@ public class GnuCashApplication extends Application {
public static long PASSCODE_SESSION_INIT_TIME = 0L;

private static Context context;

@Nullable
private static AccountsDbAdapter mAccountsDbAdapter;

@Nullable
private static TransactionsDbAdapter mTransactionsDbAdapter;

@Nullable
private static SplitsDbAdapter mSplitsDbAdapter;

@Nullable
private static ScheduledActionDbAdapter mScheduledActionDbAdapter;

@Nullable
private static CommoditiesDbAdapter mCommoditiesDbAdapter;

@Nullable
private static PricesDbAdapter mPricesDbAdapter;

@Nullable
private static BudgetsDbAdapter mBudgetsDbAdapter;

@Nullable
private static BudgetAmountsDbAdapter mBudgetAmountsDbAdapter;

@Nullable
private static RecurrenceDbAdapter mRecurrenceDbAdapter;

@Nullable
private static BooksDbAdapter mBooksDbAdapter;
@Nullable
private static DatabaseHelper mDbHelper;

/**
Expand Down Expand Up @@ -158,13 +161,13 @@ public static void initializeDatabaseAdapters(Context context) {
mDbHelper.getReadableDatabase().close();
}

String bookUID;
String bookUID = null;
try {
bookUID = mBooksDbAdapter.getActiveBookUID();
} catch (BooksDbAdapter.NoActiveBookFoundException e) {
mBooksDbAdapter.fixBooksDatabase();
bookUID = mBooksDbAdapter.getActiveBookUID();
}
if (TextUtils.isEmpty(bookUID)) return;
mDbHelper = new DatabaseHelper(context, bookUID);
SQLiteDatabase mainDb;
try {
Expand Down Expand Up @@ -262,65 +265,78 @@ private static void destroyDatabaseAdapters() {
}
}

@Nullable
public static AccountsDbAdapter getAccountsDbAdapter() {
return mAccountsDbAdapter;
}

@Nullable
public static TransactionsDbAdapter getTransactionDbAdapter() {
return mTransactionsDbAdapter;
}

@Nullable
public static SplitsDbAdapter getSplitsDbAdapter() {
return mSplitsDbAdapter;
}

@Nullable
public static ScheduledActionDbAdapter getScheduledEventDbAdapter() {
return mScheduledActionDbAdapter;
}

@Nullable
public static CommoditiesDbAdapter getCommoditiesDbAdapter() {
return mCommoditiesDbAdapter;
}

@Nullable
public static PricesDbAdapter getPricesDbAdapter() {
return mPricesDbAdapter;
}

@Nullable
public static BudgetsDbAdapter getBudgetDbAdapter() {
return mBudgetsDbAdapter;
}

@Nullable
public static RecurrenceDbAdapter getRecurrenceDbAdapter() {
return mRecurrenceDbAdapter;
}

@Nullable
public static BudgetAmountsDbAdapter getBudgetAmountsDbAdapter() {
return mBudgetAmountsDbAdapter;
}

@Nullable
public static BooksDbAdapter getBooksDbAdapter() {
return mBooksDbAdapter;
}

@NonNull
@Nullable
public static String getActiveBookUID() {
return getBooksDbAdapter().getActiveBookUID();
BooksDbAdapter adapter = getBooksDbAdapter();
return (adapter != null) ? adapter.getActiveBookUID() : null;
}

/**
* Returns the currently active database in the application
*
* @return Currently active {@link SQLiteDatabase}
*/
@Nullable
public static SQLiteDatabase getActiveDb() {
return mDbHelper.getWritableDatabase();
return (mDbHelper != null) ? mDbHelper.getWritableDatabase() : null;
}

/**
* Returns the application context
*
* @return Application {@link Context} object
*/
@NonNull
public static Context getAppContext() {
return GnuCashApplication.context;
}
Expand Down Expand Up @@ -370,7 +386,7 @@ public static boolean shouldSaveOpeningBalances(boolean defaultValue) {
public static String getDefaultCurrencyCode() {
Locale locale = getDefaultLocale();

String currencyCode = "USD"; //start with USD as the default
String currencyCode = Money.DEFAULT_CURRENCY_CODE; //start with USD as the default
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
try { //there are some strange locales out there
currencyCode = Currency.getInstance(locale).getCurrencyCode();
Expand Down Expand Up @@ -414,8 +430,7 @@ public static void setDefaultCurrencyCode(@NonNull Context context, @NonNull Str
.edit()
.putString(context.getString(R.string.key_default_currency), currencyCode)
.apply();
Money.DEFAULT_CURRENCY_CODE = currencyCode;
Commodity.DEFAULT_COMMODITY = mCommoditiesDbAdapter.getCommodity(currencyCode);
Commodity.DEFAULT_COMMODITY = Commodity.getInstance(currencyCode);
}

/**
Expand All @@ -424,6 +439,7 @@ public static void setDefaultCurrencyCode(@NonNull Context context, @NonNull Str
*
* @return The default locale for this device
*/
@NonNull
public static Locale getDefaultLocale() {
Locale locale = Locale.getDefault();
//sometimes the locale en_UK is returned which causes a crash with Currency
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,19 +79,21 @@ public class AccountsDbAdapter extends DatabaseAdapter<Account> {
/**
* Transactions database adapter for manipulating transactions associated with accounts
*/
@NonNull
private final TransactionsDbAdapter mTransactionsAdapter;

/**
* Commodities database adapter for commodity manipulation
*/
@NonNull
private final CommoditiesDbAdapter mCommoditiesDbAdapter;

/**
* Overloaded constructor. Creates an adapter for an already open database
*
* @param db SQliteDatabase instance
*/
public AccountsDbAdapter(SQLiteDatabase db, TransactionsDbAdapter transactionsDbAdapter) {
public AccountsDbAdapter(@NonNull SQLiteDatabase db, @NonNull TransactionsDbAdapter transactionsDbAdapter) {
super(db, AccountEntry.TABLE_NAME, new String[]{
AccountEntry.COLUMN_NAME,
AccountEntry.COLUMN_DESCRIPTION,
Expand Down Expand Up @@ -120,24 +122,7 @@ public AccountsDbAdapter(SQLiteDatabase db, TransactionsDbAdapter transactionsDb
* @param db Database to create an adapter for
*/
public AccountsDbAdapter(SQLiteDatabase db) {
super(db, AccountEntry.TABLE_NAME, new String[]{
AccountEntry.COLUMN_NAME,
AccountEntry.COLUMN_DESCRIPTION,
AccountEntry.COLUMN_TYPE,
AccountEntry.COLUMN_CURRENCY,
AccountEntry.COLUMN_COLOR_CODE,
AccountEntry.COLUMN_FAVORITE,
AccountEntry.COLUMN_FULL_NAME,
AccountEntry.COLUMN_PLACEHOLDER,
AccountEntry.COLUMN_CREATED_AT,
AccountEntry.COLUMN_HIDDEN,
AccountEntry.COLUMN_COMMODITY_UID,
AccountEntry.COLUMN_PARENT_ACCOUNT_UID,
AccountEntry.COLUMN_DEFAULT_TRANSFER_ACCOUNT_UID
});

mTransactionsAdapter = new TransactionsDbAdapter(db, new SplitsDbAdapter(db));
mCommoditiesDbAdapter = new CommoditiesDbAdapter(db);
this(db, new TransactionsDbAdapter(db, new SplitsDbAdapter(db)));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public void initCommon() {
Commodity.DEFAULT_COMMODITY = getCommodity(GnuCashApplication.getDefaultCurrencyCode());
}

@Nullable
public static CommoditiesDbAdapter getInstance() {
return GnuCashApplication.getCommoditiesDbAdapter();
}
Expand All @@ -86,6 +87,7 @@ public static CommoditiesDbAdapter getInstance() {

@Override
public Commodity buildModelInstance(@NonNull final Cursor cursor) {
String uid = cursor.getString(cursor.getColumnIndexOrThrow(CommodityEntry.COLUMN_UID));
String fullname = cursor.getString(cursor.getColumnIndexOrThrow(CommodityEntry.COLUMN_FULLNAME));
String mnemonic = cursor.getString(cursor.getColumnIndexOrThrow(CommodityEntry.COLUMN_MNEMONIC));
String namespace = cursor.getString(cursor.getColumnIndexOrThrow(CommodityEntry.COLUMN_NAMESPACE));
Expand All @@ -96,6 +98,7 @@ public Commodity buildModelInstance(@NonNull final Cursor cursor) {
int quoteFlag = cursor.getInt(cursor.getColumnIndexOrThrow(CommodityEntry.COLUMN_QUOTE_FLAG));

Commodity commodity = new Commodity(fullname, mnemonic, fraction);
commodity.setUID(uid);
commodity.setNamespace(Commodity.Namespace.valueOf(namespace));
commodity.setCusip(cusip);
commodity.setQuoteFlag(quoteFlag);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,6 @@ public void onReceive(Context context, Intent intent) {

String currencyCode = args.getString(Account.EXTRA_CURRENCY_CODE);
Commodity commodity = Commodity.getInstance(currencyCode);
if (commodity == null) {
commodity = Commodity.DEFAULT_COMMODITY;
}
account.setCommodity(commodity);

String uid = args.getString(Intent.EXTRA_UID);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,6 @@ public void onReceive(Context context, Intent intent) {

String currencyCode = args.getString(Account.EXTRA_CURRENCY_CODE);
Commodity commodity = Commodity.getInstance(currencyCode);
if (commodity == null) {
commodity = Commodity.DEFAULT_COMMODITY;
}

Transaction transaction = new Transaction(name);
transaction.setTime(System.currentTimeMillis());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,6 @@ private void initializeViews() {
mBinding.inputColorPicker.setBackgroundColor(Color.LTGRAY);
mParentAccountUID = getArguments().getString(UxArgument.PARENT_ACCOUNT_UID);


if (mParentAccountUID != null) {
AccountType parentAccountType = mAccountsDbAdapter.getAccountType(mParentAccountUID);
setAccountTypeSelection(parentAccountType);
Expand Down Expand Up @@ -397,6 +396,7 @@ private void setDefaultTransferAccountInputsVisible(boolean visible) {
*/
private void setSelectedCurrency(String currencyCode) {
CommoditiesDbAdapter commodityDbAdapter = CommoditiesDbAdapter.getInstance();
if (commodityDbAdapter == null) return;
long commodityId = commodityDbAdapter.getID(commodityDbAdapter.getCommodityUID(currencyCode));
int position = 0;
for (int i = 0; i < mBinding.inputCurrencySpinner.getCount(); i++) {
Expand Down
21 changes: 7 additions & 14 deletions app/src/main/kotlin/org/gnucash/android/model/Account.kt
Original file line number Diff line number Diff line change
Expand Up @@ -105,25 +105,14 @@ class Account : BaseModel {
*/
private var _isHidden = false

/**
* Constructor
* Creates a new account with the default currency and a generated unique ID
*
* @param name Name of the account
*/
constructor(name: String) {
setName(name)
fullName = this.name
commodity = Commodity.DEFAULT_COMMODITY
}

/**
* Overloaded constructor
*
* @param name Name of the account
* @param commodity [Commodity] to be used by transactions in this account
*/
constructor(name: String, commodity: Commodity) {
@JvmOverloads
constructor(name: String, commodity: Commodity = Commodity.DEFAULT_COMMODITY) {
setName(name)
fullName = this.name
this.commodity = commodity
Expand Down Expand Up @@ -380,7 +369,7 @@ class Account : BaseModel {
// TODO: get it from a theme value?
@ColorInt
@JvmField
val DEFAULT_COLOR = Color.rgb(237,236,235)
val DEFAULT_COLOR = Color.rgb(237, 236, 235)

/**
* An extra key for passing the currency code (according ISO 4217) in an intent
Expand Down Expand Up @@ -410,17 +399,21 @@ class Account : BaseModel {
return when (accountType) {
AccountType.CREDIT,
AccountType.LIABILITY -> OfxAccountType.CREDITLINE

AccountType.CASH,
AccountType.INCOME,
AccountType.EXPENSE,
AccountType.PAYABLE,
AccountType.RECEIVABLE -> OfxAccountType.CHECKING

AccountType.BANK,
AccountType.ASSET -> OfxAccountType.SAVINGS

AccountType.MUTUAL,
AccountType.STOCK,
AccountType.EQUITY,
AccountType.CURRENCY -> OfxAccountType.MONEYMRKT

else -> OfxAccountType.CHECKING
}
}
Expand Down
Loading

0 comments on commit eefabbd

Please sign in to comment.