Skip to content

Commit

Permalink
Clean code for nestedcategoruy
Browse files Browse the repository at this point in the history
Step 2 for remove unused code #1792
  • Loading branch information
wolfsolver committed Dec 1, 2024
1 parent 832f848 commit 43ac9b2
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 138 deletions.
45 changes: 8 additions & 37 deletions app/src/main/java/com/money/manager/ex/budget/BudgetAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@
import com.money.manager.ex.R;
import com.money.manager.ex.core.UIHelper;
import com.money.manager.ex.currency.CurrencyService;
import com.money.manager.ex.database.QueryCategorySubCategory;
import com.money.manager.ex.database.ViewMobileData;
import com.money.manager.ex.datalayer.BudgetEntryRepository;
import com.money.manager.ex.domainmodel.BudgetEntry;
import com.money.manager.ex.nestedcategory.QueryNestedCategory;
import com.money.manager.ex.settings.AppSettings;
import com.squareup.sqlbrite3.BriteDatabase;

Expand Down Expand Up @@ -82,11 +82,6 @@ public BudgetAdapter(Context context, Cursor cursor, String[] from, int[] to, in

MmexApplication.getApp().iocComponent.inject(this);

try {
useNestedCategory = (new AppSettings(context).getBehaviourSettings().getUseNestedCategory());
} catch (Exception e) {
}

}

@Inject Lazy<BriteDatabase> databaseLazy;
Expand All @@ -96,8 +91,6 @@ public BudgetAdapter(Context context, Cursor cursor, String[] from, int[] to, in
private long mBudgetYearId;
private HashMap<String, BudgetEntry> mBudgetEntries;

private boolean useNestedCategory = false; // new NestedCateg


@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
Expand All @@ -111,28 +104,18 @@ public void bindView(View view, Context context, Cursor cursor) {
// Category

boolean hasSubcategory;
if (!useNestedCategory) {
hasSubcategory = cursor.getInt(cursor.getColumnIndex(QueryCategorySubCategory.SUBCATEGID)) != Constants.NOT_SET;
} else {
hasSubcategory = false;
}
hasSubcategory = false;

TextView categoryTextView = view.findViewById(R.id.categoryTextView);
if (categoryTextView != null) {
int categoryColumnIndex = cursor.getColumnIndex(
(!useNestedCategory ) ? QueryCategorySubCategory.CATEGSUBNAME : QueryCategorySubCategory.CATEGNAME
);
int categoryColumnIndex = cursor.getColumnIndex( QueryNestedCategory.CATEGNAME );
categoryTextView.setText(cursor.getString(categoryColumnIndex));
}

long categoryId;
long subCategoryId;
if (!useNestedCategory) {
categoryId = cursor.getInt(cursor.getColumnIndex(BudgetQuery.CATEGID));
subCategoryId = cursor.getInt(cursor.getColumnIndex(BudgetQuery.SUBCATEGID));
} else {
categoryId = cursor.getInt(cursor.getColumnIndex(BudgetNestedQuery.CATEGID));
subCategoryId = -1;
}
categoryId = cursor.getInt(cursor.getColumnIndex(BudgetNestedQuery.CATEGID));
subCategoryId = -1;

// Frequency

Expand Down Expand Up @@ -228,20 +211,8 @@ public void setBudgetYearId(long budgetYearId) {
private double getActualAmount(boolean hasSubcategory, Cursor cursor) {
double actual;
// wolfsolver since category can be neested we need to consider always category as master
// probabily until we don't handle third and other level actual value does not count correctlry
if (!useNestedCategory) {
if (!hasSubcategory) {
long categoryId = cursor.getLong(cursor.getColumnIndex(BudgetQuery.CATEGID));
actual = getAmountForCategory(categoryId);
} else {
long subCategoryId = cursor.getLong(cursor.getColumnIndex(BudgetQuery.SUBCATEGID));
actual = getAmountForCategory(subCategoryId);
// actual = getAmountForSubCategory(subCategoryId);
}
} else {
long categoryId = cursor.getLong(cursor.getColumnIndex(BudgetNestedQuery.CATEGID));
actual = getAmountForCategory(categoryId);
}
long categoryId = cursor.getLong(cursor.getColumnIndex(BudgetNestedQuery.CATEGID));
actual = getAmountForCategory(categoryId);

return actual;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import com.money.manager.ex.core.MenuHelper;
import com.money.manager.ex.datalayer.BudgetRepository;
import com.money.manager.ex.domainmodel.Budget;
import com.money.manager.ex.settings.AppSettings;
import com.money.manager.ex.utils.MmxDate;

import timber.log.Timber;
Expand All @@ -49,8 +48,6 @@ public class BudgetEditActivity extends MmxBaseFragmentActivity {
private TextView budgetYearTextView;
private TextView budgetMonthTextView;

private boolean useNestedCategory = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand All @@ -60,8 +57,6 @@ protected void onCreate(Bundle savedInstanceState) {
budgetYearTextView = findViewById(R.id.budgetYearTextView);
budgetMonthTextView = findViewById(R.id.budgetMonthTextView);

useNestedCategory = (new AppSettings(this.getBaseContext()).getBehaviourSettings().getUseNestedCategory());

initializeToolbar();

initializeModel();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import com.money.manager.ex.R;
import com.money.manager.ex.common.BaseListFragment;
import com.money.manager.ex.common.MmxCursorLoader;
import com.money.manager.ex.database.QueryCategorySubCategory;
import com.money.manager.ex.datalayer.Select;
import com.money.manager.ex.nestedcategory.QueryNestedCategory;
import com.money.manager.ex.settings.AppSettings;
Expand All @@ -49,8 +48,6 @@ public class BudgetEntryFragment
private long mBudgetYearId = Constants.NOT_SET;
private String mBudgetName;
private View mHeader;
private boolean useNestedCategory = false; // new NestedCateg


/**
* Use this factory method to create a new instance of
Expand Down Expand Up @@ -81,7 +78,7 @@ public void onCreate(Bundle savedInstanceState) {
mBudgetYearId = getArguments().getLong(ARG_BUDGET_YEAR_ID);
mBudgetName = getArguments().getString(ARG_BUDGET_NAME_ID);
}
useNestedCategory = (new AppSettings(getContext()).getBehaviourSettings().getUseNestedCategory());

}

@Override
Expand Down Expand Up @@ -133,19 +130,11 @@ private void displayBudget() {

private void setUpAdapter() {
BudgetAdapter adapter;
if (!useNestedCategory) {
adapter = new BudgetAdapter(getActivity(),
null,
new String[]{BudgetQuery.CATEGNAME},
new int[]{R.id.categoryTextView},
0);
} else {
adapter = new BudgetAdapter(getActivity(),
null,
new String[]{BudgetNestedQuery.CATEGNAME},
new int[]{R.id.categoryTextView},
0);
}
adapter = new BudgetAdapter(getActivity(),
null,
new String[]{BudgetNestedQuery.CATEGNAME},
new int[]{R.id.categoryTextView},
0);
adapter.setBudgetName(mBudgetName);
adapter.setBudgetYearId(mBudgetYearId);

Expand All @@ -160,19 +149,11 @@ public Loader<Cursor> onCreateLoader(int id, Bundle args) {
Loader<Cursor> result = null;

if (id == LOADER_BUDGET) {
if (!useNestedCategory) {
QueryCategorySubCategory categories = new QueryCategorySubCategory(getActivity());
Select query = new Select(categories.getAllColumns())
.orderBy(QueryCategorySubCategory.CATEGSUBNAME);

result = new MmxCursorLoader(getActivity(), categories.getUri(), query);
} else {
// useNestedCategory
QueryNestedCategory categories = new QueryNestedCategory(getActivity());
Select query = new Select(categories.getAllColumns())
.orderBy(QueryNestedCategory.CATEGNAME);
result = new MmxCursorLoader(getActivity(), categories.getUri(), query);
}
// useNestedCategory
QueryNestedCategory categories = new QueryNestedCategory(getActivity());
Select query = new Select(categories.getAllColumns())
.orderBy(QueryNestedCategory.CATEGNAME);
result = new MmxCursorLoader(getActivity(), categories.getUri(), query);
}
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ public class CategoryListActivity
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
boolean useNestedCategory = (new AppSettings(this).getBehaviourSettings().getUseNestedCategory());

setContentView(R.layout.base_toolbar_activity);

Expand All @@ -69,36 +68,20 @@ protected void onCreate(Bundle savedInstanceState) {
// management fragment
FragmentManager fm = getSupportFragmentManager();
if (fm.findFragmentById(R.id.content) == null) {
if (!useNestedCategory) {
// todo: use replace?
fm.beginTransaction()
.add(R.id.content, listFragment, FRAGMENTTAG)
.commit();
} else {
fm.beginTransaction()
.add(R.id.content, nestedListFragment, FRAGMENTTAG)
.commit();
}
}
}

@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
// set result and terminate activity
boolean useNestedCategory = (new AppSettings(this).getBehaviourSettings().getUseNestedCategory());
if (!useNestedCategory) {
CategoryListFragment fragment =
(CategoryListFragment) getSupportFragmentManager().findFragmentByTag(FRAGMENTTAG);
if (fragment != null) {
fragment.setResultAndFinish();
}
} else {
NestedCategoryListFragment fragment =
(NestedCategoryListFragment) getSupportFragmentManager().findFragmentByTag(FRAGMENTTAG);
if (fragment != null) {
fragment.setResultAndFinish();
}
NestedCategoryListFragment fragment =
(NestedCategoryListFragment) getSupportFragmentManager().findFragmentByTag(FRAGMENTTAG);
if (fragment != null) {
fragment.setResultAndFinish();
}
}
return super.onKeyUp(keyCode, event);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,8 @@ public class BudgetEntryRepository

public static final String TABLE_NAME = "budgettable_v1";

private boolean useNestedCategory = false; // new NestedCateg

public BudgetEntryRepository(Context context) {
super(context, TABLE_NAME, DatasetType.TABLE, "budgettable");
try {
useNestedCategory = (new AppSettings(context).getBehaviourSettings().getUseNestedCategory());
} catch (Exception e) {

}
}

@Override
Expand Down Expand Up @@ -105,38 +98,20 @@ public HashMap<String, BudgetEntry> loadForYear(long budgetYearId) {

HashMap<String, BudgetEntry> budgetEntryHashMap = new HashMap<>();

if (!useNestedCategory) {
// use old way
CategoryRepository categoryRepository = new CategoryRepository(getContext());

while (cursor.moveToNext()) {
BudgetEntry budgetEntry = new BudgetEntry();
budgetEntry.loadFromCursor(cursor);
// use nested category
QueryNestedCategory categoryRepositoryNested = new QueryNestedCategory(null);
while (cursor.moveToNext()) {
BudgetEntry budgetEntry = new BudgetEntry();
budgetEntry.loadFromCursor(cursor);

long categoryId = cursor.getLong(cursor.getColumnIndex(BudgetEntry.CATEGID));
Category category = categoryRepository.load(categoryId);
if (category == null) {
continue;
}
budgetEntryHashMap.put(getKeyForCategories(category.getParentId(), categoryId), budgetEntry);
NestedCategoryEntity nestedCategory = categoryRepositoryNested.getOneCategoryEntity(budgetEntry.getCategId());
if (nestedCategory == null) {
continue;
}
cursor.close();
} else {
// use nested category
QueryNestedCategory categoryRepositoryNested = new QueryNestedCategory(null);
while (cursor.moveToNext()) {
BudgetEntry budgetEntry = new BudgetEntry();
budgetEntry.loadFromCursor(cursor);

NestedCategoryEntity nestedCategory = categoryRepositoryNested.getOneCategoryEntity(budgetEntry.getCategId());
if (nestedCategory == null) {
continue;
}
budgetEntryHashMap.put(getKeyForCategories(nestedCategory.getParentId(), nestedCategory.getCategoryId()), budgetEntry);
}
cursor.close();

budgetEntryHashMap.put(getKeyForCategories(nestedCategory.getParentId(), nestedCategory.getCategoryId()), budgetEntry);
}
cursor.close();

return budgetEntryHashMap;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -616,12 +616,7 @@ public boolean onDrawerMenuAndOptionMenuSelected(DrawerMenuItem item) {
} else if (itemId == R.id.menu_account) {
showFragment(AccountListFragment.class);
} else if (itemId == R.id.menu_category) {
boolean useNestedCategory = (new AppSettings(this).getBehaviourSettings().getUseNestedCategory());
if (!useNestedCategory) {
showFragment(CategoryListFragment.class);
} else {
showFragment(NestedCategoryListFragment.class);
}
showFragment(NestedCategoryListFragment.class);
} else if (itemId == R.id.menu_currency) {
Intent intent = new Intent(MainActivity.this, CurrencyListActivity.class);
intent.setAction(Intent.ACTION_EDIT);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -727,9 +727,8 @@ private void displaySearchCriteria(View view) {
viewHolder.txtSelectPayee.setText(searchParameters.payeeName);
// Category
displayCategory(searchParameters.category);
boolean useNestedCategory = (new AppSettings(getContext()).getBehaviourSettings().getUseNestedCategory());
cbxSearchSubCategory.setEnabled(useNestedCategory);
cbxSearchSubCategory.setChecked(useNestedCategory);
cbxSearchSubCategory.setEnabled(true);
cbxSearchSubCategory.setChecked(true);

// Transaction number
viewHolder.txtTransNumber.setText(searchParameters.transactionNumber);
Expand Down

0 comments on commit 43ac9b2

Please sign in to comment.