Skip to content

Commit

Permalink
Clear book shared preferences when the book is deleted from db - fixes
Browse files Browse the repository at this point in the history
…codinguser#561

Refactored deleteBook method in to the correct adapter
  • Loading branch information
codinguser committed Aug 31, 2016
1 parent 41f368b commit 77d9323
Show file tree
Hide file tree
Showing 11 changed files with 50 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ public void setUp() throws Exception {
*/
@Test
public void testOfxExport(){
SharedPreferences.Editor prefsEditor = PreferenceActivity.getActiveBookSharedPreferences(mAcccountsActivity)
SharedPreferences.Editor prefsEditor = PreferenceActivity.getActiveBookSharedPreferences()
.edit();
prefsEditor.putBoolean(mAcccountsActivity.getString(R.string.key_use_double_entry), false)
.commit();
Expand All @@ -159,7 +159,7 @@ public void testOfxExport(){

@Test
public void whenInSingleEntry_shouldHideXmlExportOption(){
SharedPreferences.Editor prefsEditor = PreferenceActivity.getActiveBookSharedPreferences(mAcccountsActivity)
SharedPreferences.Editor prefsEditor = PreferenceActivity.getActiveBookSharedPreferences()
.edit();
prefsEditor.putBoolean(mAcccountsActivity.getString(R.string.key_use_double_entry), false)
.commit();
Expand Down Expand Up @@ -232,7 +232,7 @@ public void testDeleteTransactionsAfterExport(){
editor.putBoolean(mAcccountsActivity.getString(R.string.key_delete_transactions_after_export), true);
editor.commit();

PreferenceActivity.getActiveBookSharedPreferences(getActivity())
PreferenceActivity.getActiveBookSharedPreferences()
.edit()
.putBoolean(mAcccountsActivity.getString(R.string.key_use_double_entry), true)
.apply();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,21 @@
package org.gnucash.android.test.ui;

import android.content.Context;
import android.content.Intent;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.support.test.espresso.ViewAction;
import android.support.test.espresso.action.CoordinatesProvider;
import android.support.test.espresso.action.GeneralClickAction;
import android.support.test.espresso.action.Press;
import android.support.test.espresso.action.Tap;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;
import android.util.Log;
import android.view.View;

import org.gnucash.android.R;
import org.gnucash.android.app.GnuCashApplication;
import org.gnucash.android.db.DatabaseHelper;
import org.gnucash.android.db.adapter.AccountsDbAdapter;
import org.gnucash.android.db.adapter.BooksDbAdapter;
import org.gnucash.android.db.adapter.CommoditiesDbAdapter;
import org.gnucash.android.db.adapter.DatabaseAdapter;
import org.gnucash.android.db.adapter.SplitsDbAdapter;
import org.gnucash.android.db.adapter.TransactionsDbAdapter;
import org.gnucash.android.importer.GncXmlImporter;
import org.gnucash.android.model.AccountType;
Expand All @@ -47,7 +41,6 @@
import org.gnucash.android.model.Transaction;
import org.gnucash.android.model.TransactionType;
import org.gnucash.android.test.ui.util.DisableAnimationsRule;
import org.gnucash.android.ui.account.AccountsActivity;
import org.gnucash.android.ui.report.BaseReportFragment;
import org.gnucash.android.ui.report.ReportsActivity;
import org.gnucash.android.ui.report.piechart.PieChartFragment;
Expand Down Expand Up @@ -131,7 +124,7 @@ public static void prepareTestCase() throws Exception {

CURRENCY = CommoditiesDbAdapter.getInstance().getCommodity("USD");

PreferenceActivity.getActiveBookSharedPreferences(context).edit()
PreferenceActivity.getActiveBookSharedPreferences().edit()
.putString(context.getString(R.string.key_default_currency), CURRENCY.getCurrencyCode())
.commit();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.support.test.espresso.Espresso;
import android.support.test.espresso.contrib.RecyclerViewActions;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;

Expand Down Expand Up @@ -61,23 +60,20 @@
import java.util.List;
import java.util.Locale;

import static android.support.test.espresso.Espresso.onData;
import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.clearText;
import static android.support.test.espresso.action.ViewActions.click;
import static android.support.test.espresso.action.ViewActions.typeText;
import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.RootMatchers.withDecorView;
import static android.support.test.espresso.matcher.ViewMatchers.hasDescendant;
import static android.support.test.espresso.matcher.ViewMatchers.hasSibling;
import static android.support.test.espresso.matcher.ViewMatchers.isChecked;
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withParent;
import static android.support.test.espresso.matcher.ViewMatchers.withText;
import static org.assertj.core.api.Assertions.assertThat;
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;

Expand Down Expand Up @@ -451,7 +447,7 @@ public void testSplitEditor(){


private void setDoubleEntryEnabled(boolean enabled){
SharedPreferences prefs = PreferenceActivity.getActiveBookSharedPreferences(mTransactionsActivity);
SharedPreferences prefs = PreferenceActivity.getActiveBookSharedPreferences();
Editor editor = prefs.edit();
editor.putBoolean(mTransactionsActivity.getString(R.string.key_use_double_entry), enabled);
editor.apply();
Expand All @@ -466,7 +462,7 @@ public void testDefaultTransactionType(){
}

private void setDefaultTransactionType(TransactionType type) {
SharedPreferences prefs = PreferenceActivity.getActiveBookSharedPreferences(mTransactionsActivity);
SharedPreferences prefs = PreferenceActivity.getActiveBookSharedPreferences();
Editor editor = prefs.edit();
editor.putString(mTransactionsActivity.getString(R.string.key_default_transaction_type), type.name());
editor.commit();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ public static boolean isRoboUnitTest(){
* @return <code>true</code> if double entry is enabled, <code>false</code> otherwise
*/
public static boolean isDoubleEntryEnabled(){
SharedPreferences sharedPrefs = PreferenceActivity.getActiveBookSharedPreferences(context);
SharedPreferences sharedPrefs = PreferenceActivity.getActiveBookSharedPreferences();
return sharedPrefs.getBoolean(context.getString(R.string.key_use_double_entry), true);
}

Expand All @@ -263,7 +263,7 @@ public static boolean isDoubleEntryEnabled(){
* @return <code>true</code> if opening balances should be saved, <code>false</code> otherwise
*/
public static boolean shouldSaveOpeningBalances(boolean defaultValue){
SharedPreferences sharedPrefs = PreferenceActivity.getActiveBookSharedPreferences(context);
SharedPreferences sharedPrefs = PreferenceActivity.getActiveBookSharedPreferences();
return sharedPrefs.getBoolean(context.getString(R.string.key_save_opening_balances), defaultValue);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.gnucash.android.db.adapter;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;
Expand All @@ -26,6 +27,7 @@
import org.gnucash.android.app.GnuCashApplication;
import org.gnucash.android.db.DatabaseSchema.BookEntry;
import org.gnucash.android.model.Book;
import org.gnucash.android.ui.settings.PreferenceActivity;
import org.gnucash.android.util.TimestampHelper;

/**
Expand Down Expand Up @@ -92,6 +94,24 @@ public Book buildModelInstance(@NonNull Cursor cursor) {
return stmt;
}


/**
* Deletes a book - removes the book record from the database and deletes the database file from the disk
* @param bookUID GUID of the book
* @return <code>true</code> if deletion was successful, <code>false</code> otherwise
* @see #deleteRecord(String)
*/
public boolean deleteBook(@NonNull String bookUID){
Context context = GnuCashApplication.getAppContext();
boolean result = context.deleteDatabase(bookUID);
if (result) //delete the db entry only if the file deletion was successful
result &= deleteRecord(bookUID);

PreferenceActivity.getBookSharedPreferences(bookUID).edit().clear().apply();

return result;
}

/**
* Sets the book with unique identifier {@code uid} as active and all others as inactive
* <p>If the parameter is null, then the currently active book is not changed</p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package org.gnucash.android.db.adapter;

import android.content.ContentValues;
import android.content.Context;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;
Expand All @@ -32,6 +34,7 @@
import org.gnucash.android.db.DatabaseSchema.TransactionEntry;
import org.gnucash.android.model.AccountType;
import org.gnucash.android.model.BaseModel;
import org.gnucash.android.ui.settings.PreferenceActivity;
import org.gnucash.android.util.TimestampHelper;

import java.util.ArrayList;
Expand Down Expand Up @@ -712,19 +715,6 @@ public boolean deleteRecord(@NonNull String uid){
return deleteRecord(getID(uid));
}

/**
* Deletes a book - removes the book record from the database and deletes the database file from the disk
* @param bookUID GUID of the book
* @return <code>true</code> if deletion was successful, <code>false</code> otherwise
* @see #deleteRecord(String)
*/
public boolean deleteBook(@NonNull String bookUID){
boolean result = GnuCashApplication.getAppContext().deleteDatabase(bookUID);
if (result) //delete the db entry only if the file deletion was successful
result &= deleteRecord(bookUID);
return result;
}

/**
* Returns an attribute from a specific column in the database for a specific record.
* <p>The attribute is returned as a string which can then be converted to another type if
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ public void onCreate(Bundle savedInstanceState) {
setHasOptionsMenu(true);
mAccountsDbAdapter = AccountsDbAdapter.getInstance();

SharedPreferences sharedPrefs = PreferenceActivity.getActiveBookSharedPreferences(getActivity());
SharedPreferences sharedPrefs = PreferenceActivity.getActiveBookSharedPreferences();
mUseDoubleEntry = sharedPrefs.getBoolean(getString(R.string.key_use_double_entry), true);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@
import android.view.View;

import org.gnucash.android.R;
import org.gnucash.android.app.GnuCashApplication;
import org.gnucash.android.db.adapter.BooksDbAdapter;
import org.gnucash.android.model.Book;
import org.gnucash.android.ui.passcode.PasscodeLockActivity;

import butterknife.Bind;
Expand Down Expand Up @@ -147,12 +149,20 @@ public boolean onOptionsItemSelected(MenuItem item) {
/**
* Returns the shared preferences file for the currently active book.
* Should be used instead of {@link PreferenceManager#getDefaultSharedPreferences(Context)}
* @param context Application context
* @return Shared preferences file
*/
public static SharedPreferences getActiveBookSharedPreferences(Context context){
//// TODO: 21.12.2015 optimize the getActiveBookUID retrieval by caching it somewhere
return context.getSharedPreferences(BooksDbAdapter.getInstance().getActiveBookUID(), Context.MODE_PRIVATE);
public static SharedPreferences getActiveBookSharedPreferences(){
return getBookSharedPreferences(BooksDbAdapter.getInstance().getActiveBookUID());
}

/**
* Return the {@link SharedPreferences} for a specific book
* @param bookUID GUID of the book
* @return Shared preferences
*/
public static SharedPreferences getBookSharedPreferences(String bookUID){
Context context = GnuCashApplication.getAppContext();
return context.getSharedPreferences(bookUID, Context.MODE_PRIVATE);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setHasOptionsMenu(true);

SharedPreferences sharedPrefs = PreferenceActivity.getActiveBookSharedPreferences(getActivity());
SharedPreferences sharedPrefs = PreferenceActivity.getActiveBookSharedPreferences();
mUseDoubleEntry = sharedPrefs.getBoolean(getString(R.string.key_use_double_entry), false);
if (!mUseDoubleEntry){
mDoubleEntryLayout.setVisibility(View.GONE);
Expand Down Expand Up @@ -538,7 +538,7 @@ private void initalizeViews() {
mTime = mDate = Calendar.getInstance();

mTransactionTypeSwitch.setAccountType(mAccountType);
String typePref = PreferenceActivity.getActiveBookSharedPreferences(getActivity()).getString(getString(R.string.key_default_transaction_type), "DEBIT");
String typePref = PreferenceActivity.getActiveBookSharedPreferences().getString(getString(R.string.key_default_transaction_type), "DEBIT");
mTransactionTypeSwitch.setChecked(TransactionType.valueOf(typePref));

String code = GnuCashApplication.getDefaultCurrencyCode();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public void onCreate(Bundle savedInstanceState) {
Bundle args = getArguments();
mAccountUID = args.getString(UxArgument.SELECTED_ACCOUNT_UID);

mUseCompactView = PreferenceActivity.getActiveBookSharedPreferences(getActivity())
mUseCompactView = PreferenceActivity.getActiveBookSharedPreferences()
.getBoolean(getActivity().getString(R.string.key_use_compact_list), !GnuCashApplication.isDoubleEntryEnabled());
//if there was a local override of the global setting, respect it
if (savedInstanceState != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public static void setLastExportTime(Timestamp lastExportTime, String bookUID) {
* @return A {@link Timestamp} with the time.
*/
public static Timestamp getLastExportTime() {
final String utcString = PreferenceActivity.getActiveBookSharedPreferences(GnuCashApplication.getAppContext())
final String utcString = PreferenceActivity.getActiveBookSharedPreferences()
.getString(PREFERENCE_LAST_EXPORT_TIME_KEY,
TimestampHelper.getUtcStringFromTimestamp(TimestampHelper.getTimestampFromEpochZero()));
Log.d(LOG_TAG, "Retrieving '" + utcString + "' as lastExportTime from Android Preferences.");
Expand Down

0 comments on commit 77d9323

Please sign in to comment.