Skip to content

Commit

Permalink
codinguser#876 - Add AccountType parameter in displayBalance(...)
Browse files Browse the repository at this point in the history
  • Loading branch information
JeanGarf committed Mar 6, 2020
1 parent b44c207 commit b7c042f
Showing 1 changed file with 100 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.ColorInt;
import android.support.annotation.NonNull;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.TabLayout;
Expand All @@ -50,6 +51,7 @@
import org.gnucash.android.db.adapter.AccountsDbAdapter;
import org.gnucash.android.db.adapter.TransactionsDbAdapter;
import org.gnucash.android.model.Account;
import org.gnucash.android.model.AccountType;
import org.gnucash.android.model.Money;
import org.gnucash.android.ui.account.AccountsActivity;
import org.gnucash.android.ui.account.AccountsListFragment;
Expand Down Expand Up @@ -78,7 +80,7 @@ public class TransactionsActivity extends BaseDrawerActivity implements
/**
* Logging tag
*/
protected static final String TAG = "TransactionsActivity";
protected static final String LOG_TAG = "TransactionsActivity";

/**
* ViewPager index for sub-accounts fragment
Expand Down Expand Up @@ -120,18 +122,26 @@ public class TransactionsActivity extends BaseDrawerActivity implements
private SparseArray<Refreshable> mFragmentPageReferenceMap = new SparseArray<>();

/**
* Flag for determining is the currently displayed account is a placeholder account or not.
* Flag for determining if the currently displayed account is a placeholder account or not.
* This will determine if the transactions tab is displayed or not
*/
private boolean mIsPlaceholderAccount;

private AdapterView.OnItemSelectedListener mTransactionListNavigationListener = new AdapterView.OnItemSelectedListener() {

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
public void onItemSelected(AdapterView<?> parent, View spinnerSelectedItemView, int position, long id) {

mAccountUID = mAccountsDbAdapter.getUID(id);
getIntent().putExtra(UxArgument.SELECTED_ACCOUNT_UID, mAccountUID); //update the intent in case the account gets rotated
mIsPlaceholderAccount = mAccountsDbAdapter.isPlaceholderAccount(mAccountUID);
getIntent().putExtra(UxArgument.SELECTED_ACCOUNT_UID,
getCurrentAccountUID()); //update the intent in case the account gets rotated

//
// Show Transaction Page if not a PlaceHolder, hide otherwise
//

mIsPlaceholderAccount = mAccountsDbAdapter.isPlaceholderAccount(getCurrentAccountUID());

if (mIsPlaceholderAccount){
if (mTabLayout.getTabCount() > 1) {
mPagerAdapter.notifyDataSetChanged();
Expand All @@ -143,9 +153,9 @@ public void onItemSelected(AdapterView<?> parent, View view, int position, long
mTabLayout.addTab(mTabLayout.newTab().setText(R.string.section_header_transactions));
}
}
if (view != null) {
if (spinnerSelectedItemView != null) {
// Hide the favorite icon of the selected account to avoid clutter
((TextView) view).setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
((TextView) spinnerSelectedItemView).setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
}
//refresh any fragments in the tab with the new account UID
refresh();
Expand Down Expand Up @@ -229,7 +239,8 @@ public int getCount() {
private AccountsListFragment prepareSubAccountsListFragment(){
AccountsListFragment subAccountsListFragment = new AccountsListFragment();
Bundle args = new Bundle();
args.putString(UxArgument.PARENT_ACCOUNT_UID, mAccountUID);
args.putString(UxArgument.PARENT_ACCOUNT_UID,
getCurrentAccountUID());
subAccountsListFragment.setArguments(args);
return subAccountsListFragment;
}
Expand All @@ -238,12 +249,17 @@ private AccountsListFragment prepareSubAccountsListFragment(){
* Creates and initializes fragment for displaying transactions
* @return {@link TransactionsListFragment} initialized with the current account transactions
*/
private TransactionsListFragment prepareTransactionsListFragment(){
private TransactionsListFragment prepareTransactionsListFragment() {

TransactionsListFragment transactionsListFragment = new TransactionsListFragment();
Bundle args = new Bundle();
args.putString(UxArgument.SELECTED_ACCOUNT_UID, mAccountUID);
Bundle args = new Bundle();
args.putString(UxArgument.SELECTED_ACCOUNT_UID,
getCurrentAccountUID());
transactionsListFragment.setArguments(args);
Log.i(TAG, "Opening transactions for account: " + mAccountUID);

Log.i(LOG_TAG,
"Opening transactions for account: " + getCurrentAccountUID());

return transactionsListFragment;
}
}
Expand All @@ -260,13 +276,15 @@ public void refresh(String accountUID) {
if (mPagerAdapter != null)
mPagerAdapter.notifyDataSetChanged();

new AccountBalanceTask(mSumTextView).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, mAccountUID);
new AccountBalanceTask(mSumTextView).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,
getCurrentAccountUID());

}

@Override
public void refresh(){
refresh(mAccountUID);

refresh(getCurrentAccountUID());
setTitleIndicatorColor();
}

Expand All @@ -282,14 +300,19 @@ public int getTitleRes() {

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

getSupportActionBar().setDisplayShowTitleEnabled(false);

mAccountUID = getIntent().getStringExtra(UxArgument.SELECTED_ACCOUNT_UID);
mAccountsDbAdapter = AccountsDbAdapter.getInstance();

mIsPlaceholderAccount = mAccountsDbAdapter.isPlaceholderAccount(mAccountUID);
//
// Add Tranbsaction Page
//

mIsPlaceholderAccount = mAccountsDbAdapter.isPlaceholderAccount(getCurrentAccountUID());

mTabLayout.addTab(mTabLayout.newTab().setText(R.string.section_header_subaccounts));
if (!mIsPlaceholderAccount) {
Expand Down Expand Up @@ -320,8 +343,9 @@ public void onTabReselected(TabLayout.Tab tab) {
});

//if there are no transactions, and there are sub-accounts, show the sub-accounts
if (TransactionsDbAdapter.getInstance().getTransactionsCount(mAccountUID) == 0
&& mAccountsDbAdapter.getSubAccountCount(mAccountUID) > 0){
if (TransactionsDbAdapter.getInstance()
.getTransactionsCount(getCurrentAccountUID()) == 0
&& mAccountsDbAdapter.getSubAccountCount(getCurrentAccountUID()) > 0) {
mViewPager.setCurrentItem(INDEX_SUB_ACCOUNTS_FRAGMENT);
} else {
mViewPager.setCurrentItem(INDEX_TRANSACTIONS_FRAGMENT);
Expand All @@ -335,13 +359,14 @@ public void onClick(View v) {
Intent addAccountIntent = new Intent(TransactionsActivity.this, FormActivity.class);
addAccountIntent.setAction(Intent.ACTION_INSERT_OR_EDIT);
addAccountIntent.putExtra(UxArgument.FORM_TYPE, FormActivity.FormType.ACCOUNT.name());
addAccountIntent.putExtra(UxArgument.PARENT_ACCOUNT_UID, mAccountUID);
addAccountIntent.putExtra(UxArgument.PARENT_ACCOUNT_UID,
getCurrentAccountUID());
startActivityForResult(addAccountIntent, AccountsActivity.REQUEST_EDIT_ACCOUNT);
;
break;

case INDEX_TRANSACTIONS_FRAGMENT:
createNewTransaction(mAccountUID);
createNewTransaction(getCurrentAccountUID());
break;

}
Expand All @@ -359,7 +384,8 @@ protected void onResume() {
* Sets the color for the ViewPager title indicator to match the account color
*/
private void setTitleIndicatorColor() {
int iColor = AccountsDbAdapter.getActiveAccountColorResource(mAccountUID);

int iColor = AccountsDbAdapter.getActiveAccountColorResource(getCurrentAccountUID());

mTabLayout.setBackgroundColor(iColor);

Expand All @@ -374,7 +400,11 @@ private void setTitleIndicatorColor() {
* Set up action bar navigation list and listener callbacks
*/
private void setupActionBarNavigation() {

//
// set up spinner adapter for navigation list
//

if (mAccountsCursor != null) {
mAccountsCursor.close();
}
Expand All @@ -387,17 +417,17 @@ private void setupActionBarNavigation() {
mToolbarSpinner.setOnItemSelectedListener(mTransactionListNavigationListener);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);

updateNavigationSelection();
selectCurrentAccountInToolbarSpinner();
}

/**
* Updates the action bar navigation list selection to that of the current account
* whose transactions are being displayed/manipulated
*/
public void updateNavigationSelection() {
// set the selected item in the spinner
int i = 0;
Cursor accountsCursor = mAccountsDbAdapter.fetchAllRecordsOrderedByFullName();
public void selectCurrentAccountInToolbarSpinner() {
// set the selected item in the spinner
int i = 0;
Cursor accountsCursor = mAccountsDbAdapter.fetchAllRecordsOrderedByFullName();
while (accountsCursor.moveToNext()) {
String uid = accountsCursor.getString(accountsCursor.getColumnIndexOrThrow(DatabaseSchema.AccountEntry.COLUMN_UID));
if (mAccountUID.equals(uid)) {
Expand All @@ -407,7 +437,7 @@ public void updateNavigationSelection() {
++i;
}
accountsCursor.close();
}
}

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
Expand All @@ -416,7 +446,8 @@ public boolean onPrepareOptionsMenu(Menu menu) {
if (favoriteAccountMenuItem == null) //when the activity is used to edit a transaction
return super.onPrepareOptionsMenu(menu);

boolean isFavoriteAccount = AccountsDbAdapter.getInstance().isFavoriteAccount(mAccountUID);
boolean isFavoriteAccount = AccountsDbAdapter.getInstance()
.isFavoriteAccount(getCurrentAccountUID());

int favoriteIcon = isFavoriteAccount ? R.drawable.ic_star_white_24dp : R.drawable.ic_star_border_white_24dp;
favoriteAccountMenuItem.setIcon(favoriteIcon);
Expand All @@ -432,8 +463,8 @@ public boolean onOptionsItemSelected(MenuItem item) {

case R.id.menu_favorite_account:
AccountsDbAdapter accountsDbAdapter = AccountsDbAdapter.getInstance();
long accountId = accountsDbAdapter.getID(mAccountUID);
boolean isFavorite = accountsDbAdapter.isFavoriteAccount(mAccountUID);
long accountId = accountsDbAdapter.getID(getCurrentAccountUID());
boolean isFavorite = accountsDbAdapter.isFavoriteAccount(getCurrentAccountUID());
//toggle favorite preference
accountsDbAdapter.updateAccount(accountId, DatabaseSchema.AccountEntry.COLUMN_FAVORITE, isFavorite ? "0" : "1");
supportInvalidateOptionsMenu();
Expand All @@ -442,7 +473,8 @@ public boolean onOptionsItemSelected(MenuItem item) {
case R.id.menu_edit_account:
Intent editAccountIntent = new Intent(this, FormActivity.class);
editAccountIntent.setAction(Intent.ACTION_INSERT_OR_EDIT);
editAccountIntent.putExtra(UxArgument.SELECTED_ACCOUNT_UID, mAccountUID);
editAccountIntent.putExtra(UxArgument.SELECTED_ACCOUNT_UID,
getCurrentAccountUID());
editAccountIntent.putExtra(UxArgument.FORM_TYPE, FormActivity.FormType.ACCOUNT.name());
startActivityForResult(editAccountIntent, AccountsActivity.REQUEST_EDIT_ACCOUNT);
return true;
Expand All @@ -465,7 +497,7 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
@Override
protected void onDestroy() {
super.onDestroy();
mAccountsCursor.close();
getAccountsCursor().close();
}

/**
Expand All @@ -476,28 +508,55 @@ public String getCurrentAccountUID(){
return mAccountUID;
}

public Cursor getAccountsCursor() {

return mAccountsCursor;
}

// TODO TW C 2020-03-06 : A mettre ailleurs, car c'est pas spécifique de cette Activité
/**
* Display the balance of a transaction in a text view and format the text color to match the sign of the amount
* @param balanceTextView {@link android.widget.TextView} where balance is to be displayed
* @param balance {@link org.gnucash.android.model.Money} balance to display
*/
public static void displayBalance(TextView balanceTextView,
Money balance) {
public static void displayBalance(final TextView balanceTextView,
final Money balance,
final AccountType accountType) {

//
// Display amount
//

// TODO TW C 2020-03-06 : Déterminer qui doit dire s'il faut afficher un nombre négatif ou sa valeur absolue
balanceTextView.setText(balance.formattedString());

Context context = GnuCashApplication.getAppContext();
//
// Define amount color
//

int fontColor = balance.isNegative()
? context.getResources()
.getColor(R.color.debit_red)
: context.getResources()
.getColor(R.color.credit_green);
@ColorInt int fontColor;

if (balance.asBigDecimal()
.compareTo(BigDecimal.ZERO) == 0) {
// balance is null

Context context = GnuCashApplication.getAppContext();

fontColor = context.getResources()
.getColor(android.R.color.black);

} else {
// balance is not null

final boolean isCredit = balance.isNegative();

// fontColor = isCredit
// ? context.getResources()
// .getColor(R.color.debit_red)
// : context.getResources()
// .getColor(R.color.credit_green);
fontColor = AccountType.getAmountColor(isCredit,
accountType);
}

balanceTextView.setTextColor(fontColor);
Expand Down Expand Up @@ -539,7 +598,8 @@ public void createNewTransaction(String accountUID) {
public void editTransaction(String transactionUID){
Intent createTransactionIntent = new Intent(this.getApplicationContext(), FormActivity.class);
createTransactionIntent.setAction(Intent.ACTION_INSERT_OR_EDIT);
createTransactionIntent.putExtra(UxArgument.SELECTED_ACCOUNT_UID, mAccountUID);
createTransactionIntent.putExtra(UxArgument.SELECTED_ACCOUNT_UID,
getCurrentAccountUID());
createTransactionIntent.putExtra(UxArgument.SELECTED_TRANSACTION_UID, transactionUID);
createTransactionIntent.putExtra(UxArgument.FORM_TYPE, FormActivity.FormType.TRANSACTION.name());
startActivity(createTransactionIntent);
Expand Down

0 comments on commit b7c042f

Please sign in to comment.