Skip to content

Commit 77d9323

Browse files
committed
Clear book shared preferences when the book is deleted from db - fixes #561
Refactored deleteBook method in to the correct adapter
1 parent 41f368b commit 77d9323

File tree

11 files changed

+50
-41
lines changed

11 files changed

+50
-41
lines changed

app/src/androidTest/java/org/gnucash/android/test/ui/ExportTransactionsTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ public void setUp() throws Exception {
148148
*/
149149
@Test
150150
public void testOfxExport(){
151-
SharedPreferences.Editor prefsEditor = PreferenceActivity.getActiveBookSharedPreferences(mAcccountsActivity)
151+
SharedPreferences.Editor prefsEditor = PreferenceActivity.getActiveBookSharedPreferences()
152152
.edit();
153153
prefsEditor.putBoolean(mAcccountsActivity.getString(R.string.key_use_double_entry), false)
154154
.commit();
@@ -159,7 +159,7 @@ public void testOfxExport(){
159159

160160
@Test
161161
public void whenInSingleEntry_shouldHideXmlExportOption(){
162-
SharedPreferences.Editor prefsEditor = PreferenceActivity.getActiveBookSharedPreferences(mAcccountsActivity)
162+
SharedPreferences.Editor prefsEditor = PreferenceActivity.getActiveBookSharedPreferences()
163163
.edit();
164164
prefsEditor.putBoolean(mAcccountsActivity.getString(R.string.key_use_double_entry), false)
165165
.commit();
@@ -232,7 +232,7 @@ public void testDeleteTransactionsAfterExport(){
232232
editor.putBoolean(mAcccountsActivity.getString(R.string.key_delete_transactions_after_export), true);
233233
editor.commit();
234234

235-
PreferenceActivity.getActiveBookSharedPreferences(getActivity())
235+
PreferenceActivity.getActiveBookSharedPreferences()
236236
.edit()
237237
.putBoolean(mAcccountsActivity.getString(R.string.key_use_double_entry), true)
238238
.apply();

app/src/androidTest/java/org/gnucash/android/test/ui/PieChartReportTest.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,21 @@
1717
package org.gnucash.android.test.ui;
1818

1919
import android.content.Context;
20-
import android.content.Intent;
21-
import android.database.SQLException;
22-
import android.database.sqlite.SQLiteDatabase;
2320
import android.support.test.espresso.ViewAction;
2421
import android.support.test.espresso.action.CoordinatesProvider;
2522
import android.support.test.espresso.action.GeneralClickAction;
2623
import android.support.test.espresso.action.Press;
2724
import android.support.test.espresso.action.Tap;
2825
import android.support.test.rule.ActivityTestRule;
2926
import android.support.test.runner.AndroidJUnit4;
30-
import android.util.Log;
3127
import android.view.View;
3228

3329
import org.gnucash.android.R;
3430
import org.gnucash.android.app.GnuCashApplication;
35-
import org.gnucash.android.db.DatabaseHelper;
3631
import org.gnucash.android.db.adapter.AccountsDbAdapter;
3732
import org.gnucash.android.db.adapter.BooksDbAdapter;
3833
import org.gnucash.android.db.adapter.CommoditiesDbAdapter;
3934
import org.gnucash.android.db.adapter.DatabaseAdapter;
40-
import org.gnucash.android.db.adapter.SplitsDbAdapter;
4135
import org.gnucash.android.db.adapter.TransactionsDbAdapter;
4236
import org.gnucash.android.importer.GncXmlImporter;
4337
import org.gnucash.android.model.AccountType;
@@ -47,7 +41,6 @@
4741
import org.gnucash.android.model.Transaction;
4842
import org.gnucash.android.model.TransactionType;
4943
import org.gnucash.android.test.ui.util.DisableAnimationsRule;
50-
import org.gnucash.android.ui.account.AccountsActivity;
5144
import org.gnucash.android.ui.report.BaseReportFragment;
5245
import org.gnucash.android.ui.report.ReportsActivity;
5346
import org.gnucash.android.ui.report.piechart.PieChartFragment;
@@ -131,7 +124,7 @@ public static void prepareTestCase() throws Exception {
131124

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

134-
PreferenceActivity.getActiveBookSharedPreferences(context).edit()
127+
PreferenceActivity.getActiveBookSharedPreferences().edit()
135128
.putString(context.getString(R.string.key_default_currency), CURRENCY.getCurrencyCode())
136129
.commit();
137130
}

app/src/androidTest/java/org/gnucash/android/test/ui/TransactionsActivityTest.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import android.content.SharedPreferences;
2323
import android.content.SharedPreferences.Editor;
2424
import android.support.test.espresso.Espresso;
25-
import android.support.test.espresso.contrib.RecyclerViewActions;
2625
import android.support.test.rule.ActivityTestRule;
2726
import android.support.test.runner.AndroidJUnit4;
2827

@@ -61,23 +60,20 @@
6160
import java.util.List;
6261
import java.util.Locale;
6362

64-
import static android.support.test.espresso.Espresso.onData;
6563
import static android.support.test.espresso.Espresso.onView;
6664
import static android.support.test.espresso.action.ViewActions.clearText;
6765
import static android.support.test.espresso.action.ViewActions.click;
6866
import static android.support.test.espresso.action.ViewActions.typeText;
6967
import static android.support.test.espresso.assertion.ViewAssertions.matches;
7068
import static android.support.test.espresso.matcher.RootMatchers.withDecorView;
7169
import static android.support.test.espresso.matcher.ViewMatchers.hasDescendant;
72-
import static android.support.test.espresso.matcher.ViewMatchers.hasSibling;
7370
import static android.support.test.espresso.matcher.ViewMatchers.isChecked;
7471
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
7572
import static android.support.test.espresso.matcher.ViewMatchers.withId;
7673
import static android.support.test.espresso.matcher.ViewMatchers.withParent;
7774
import static android.support.test.espresso.matcher.ViewMatchers.withText;
7875
import static org.assertj.core.api.Assertions.assertThat;
7976
import static org.hamcrest.Matchers.allOf;
80-
import static org.hamcrest.Matchers.instanceOf;
8177
import static org.hamcrest.Matchers.is;
8278
import static org.hamcrest.Matchers.not;
8379

@@ -451,7 +447,7 @@ public void testSplitEditor(){
451447

452448

453449
private void setDoubleEntryEnabled(boolean enabled){
454-
SharedPreferences prefs = PreferenceActivity.getActiveBookSharedPreferences(mTransactionsActivity);
450+
SharedPreferences prefs = PreferenceActivity.getActiveBookSharedPreferences();
455451
Editor editor = prefs.edit();
456452
editor.putBoolean(mTransactionsActivity.getString(R.string.key_use_double_entry), enabled);
457453
editor.apply();
@@ -466,7 +462,7 @@ public void testDefaultTransactionType(){
466462
}
467463

468464
private void setDefaultTransactionType(TransactionType type) {
469-
SharedPreferences prefs = PreferenceActivity.getActiveBookSharedPreferences(mTransactionsActivity);
465+
SharedPreferences prefs = PreferenceActivity.getActiveBookSharedPreferences();
470466
Editor editor = prefs.edit();
471467
editor.putString(mTransactionsActivity.getString(R.string.key_default_transaction_type), type.name());
472468
editor.commit();

app/src/main/java/org/gnucash/android/app/GnuCashApplication.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ public static boolean isRoboUnitTest(){
252252
* @return <code>true</code> if double entry is enabled, <code>false</code> otherwise
253253
*/
254254
public static boolean isDoubleEntryEnabled(){
255-
SharedPreferences sharedPrefs = PreferenceActivity.getActiveBookSharedPreferences(context);
255+
SharedPreferences sharedPrefs = PreferenceActivity.getActiveBookSharedPreferences();
256256
return sharedPrefs.getBoolean(context.getString(R.string.key_use_double_entry), true);
257257
}
258258

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

app/src/main/java/org/gnucash/android/db/adapter/BooksDbAdapter.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.gnucash.android.db.adapter;
1818

1919
import android.content.ContentValues;
20+
import android.content.Context;
2021
import android.database.Cursor;
2122
import android.database.sqlite.SQLiteDatabase;
2223
import android.database.sqlite.SQLiteStatement;
@@ -26,6 +27,7 @@
2627
import org.gnucash.android.app.GnuCashApplication;
2728
import org.gnucash.android.db.DatabaseSchema.BookEntry;
2829
import org.gnucash.android.model.Book;
30+
import org.gnucash.android.ui.settings.PreferenceActivity;
2931
import org.gnucash.android.util.TimestampHelper;
3032

3133
/**
@@ -92,6 +94,24 @@ public Book buildModelInstance(@NonNull Cursor cursor) {
9294
return stmt;
9395
}
9496

97+
98+
/**
99+
* Deletes a book - removes the book record from the database and deletes the database file from the disk
100+
* @param bookUID GUID of the book
101+
* @return <code>true</code> if deletion was successful, <code>false</code> otherwise
102+
* @see #deleteRecord(String)
103+
*/
104+
public boolean deleteBook(@NonNull String bookUID){
105+
Context context = GnuCashApplication.getAppContext();
106+
boolean result = context.deleteDatabase(bookUID);
107+
if (result) //delete the db entry only if the file deletion was successful
108+
result &= deleteRecord(bookUID);
109+
110+
PreferenceActivity.getBookSharedPreferences(bookUID).edit().clear().apply();
111+
112+
return result;
113+
}
114+
95115
/**
96116
* Sets the book with unique identifier {@code uid} as active and all others as inactive
97117
* <p>If the parameter is null, then the currently active book is not changed</p>

app/src/main/java/org/gnucash/android/db/adapter/DatabaseAdapter.java

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
package org.gnucash.android.db.adapter;
1818

1919
import android.content.ContentValues;
20+
import android.content.Context;
21+
import android.content.SharedPreferences;
2022
import android.database.Cursor;
2123
import android.database.sqlite.SQLiteDatabase;
2224
import android.database.sqlite.SQLiteStatement;
@@ -32,6 +34,7 @@
3234
import org.gnucash.android.db.DatabaseSchema.TransactionEntry;
3335
import org.gnucash.android.model.AccountType;
3436
import org.gnucash.android.model.BaseModel;
37+
import org.gnucash.android.ui.settings.PreferenceActivity;
3538
import org.gnucash.android.util.TimestampHelper;
3639

3740
import java.util.ArrayList;
@@ -712,19 +715,6 @@ public boolean deleteRecord(@NonNull String uid){
712715
return deleteRecord(getID(uid));
713716
}
714717

715-
/**
716-
* Deletes a book - removes the book record from the database and deletes the database file from the disk
717-
* @param bookUID GUID of the book
718-
* @return <code>true</code> if deletion was successful, <code>false</code> otherwise
719-
* @see #deleteRecord(String)
720-
*/
721-
public boolean deleteBook(@NonNull String bookUID){
722-
boolean result = GnuCashApplication.getAppContext().deleteDatabase(bookUID);
723-
if (result) //delete the db entry only if the file deletion was successful
724-
result &= deleteRecord(bookUID);
725-
return result;
726-
}
727-
728718
/**
729719
* Returns an attribute from a specific column in the database for a specific record.
730720
* <p>The attribute is returned as a string which can then be converted to another type if

app/src/main/java/org/gnucash/android/ui/account/AccountFormFragment.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ public void onCreate(Bundle savedInstanceState) {
238238
setHasOptionsMenu(true);
239239
mAccountsDbAdapter = AccountsDbAdapter.getInstance();
240240

241-
SharedPreferences sharedPrefs = PreferenceActivity.getActiveBookSharedPreferences(getActivity());
241+
SharedPreferences sharedPrefs = PreferenceActivity.getActiveBookSharedPreferences();
242242
mUseDoubleEntry = sharedPrefs.getBoolean(getString(R.string.key_use_double_entry), true);
243243
}
244244

app/src/main/java/org/gnucash/android/ui/settings/PreferenceActivity.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@
3434
import android.view.View;
3535

3636
import org.gnucash.android.R;
37+
import org.gnucash.android.app.GnuCashApplication;
3738
import org.gnucash.android.db.adapter.BooksDbAdapter;
39+
import org.gnucash.android.model.Book;
3840
import org.gnucash.android.ui.passcode.PasscodeLockActivity;
3941

4042
import butterknife.Bind;
@@ -147,12 +149,20 @@ public boolean onOptionsItemSelected(MenuItem item) {
147149
/**
148150
* Returns the shared preferences file for the currently active book.
149151
* Should be used instead of {@link PreferenceManager#getDefaultSharedPreferences(Context)}
150-
* @param context Application context
151152
* @return Shared preferences file
152153
*/
153-
public static SharedPreferences getActiveBookSharedPreferences(Context context){
154-
//// TODO: 21.12.2015 optimize the getActiveBookUID retrieval by caching it somewhere
155-
return context.getSharedPreferences(BooksDbAdapter.getInstance().getActiveBookUID(), Context.MODE_PRIVATE);
154+
public static SharedPreferences getActiveBookSharedPreferences(){
155+
return getBookSharedPreferences(BooksDbAdapter.getInstance().getActiveBookUID());
156+
}
157+
158+
/**
159+
* Return the {@link SharedPreferences} for a specific book
160+
* @param bookUID GUID of the book
161+
* @return Shared preferences
162+
*/
163+
public static SharedPreferences getBookSharedPreferences(String bookUID){
164+
Context context = GnuCashApplication.getAppContext();
165+
return context.getSharedPreferences(bookUID, Context.MODE_PRIVATE);
156166
}
157167

158168
@Override

app/src/main/java/org/gnucash/android/ui/transaction/TransactionFormFragment.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ public void onActivityCreated(Bundle savedInstanceState) {
293293
super.onActivityCreated(savedInstanceState);
294294
setHasOptionsMenu(true);
295295

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

540540
mTransactionTypeSwitch.setAccountType(mAccountType);
541-
String typePref = PreferenceActivity.getActiveBookSharedPreferences(getActivity()).getString(getString(R.string.key_default_transaction_type), "DEBIT");
541+
String typePref = PreferenceActivity.getActiveBookSharedPreferences().getString(getString(R.string.key_default_transaction_type), "DEBIT");
542542
mTransactionTypeSwitch.setChecked(TransactionType.valueOf(typePref));
543543

544544
String code = GnuCashApplication.getDefaultCurrencyCode();

app/src/main/java/org/gnucash/android/ui/transaction/TransactionsListFragment.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public void onCreate(Bundle savedInstanceState) {
9595
Bundle args = getArguments();
9696
mAccountUID = args.getString(UxArgument.SELECTED_ACCOUNT_UID);
9797

98-
mUseCompactView = PreferenceActivity.getActiveBookSharedPreferences(getActivity())
98+
mUseCompactView = PreferenceActivity.getActiveBookSharedPreferences()
9999
.getBoolean(getActivity().getString(R.string.key_use_compact_list), !GnuCashApplication.isDoubleEntryEnabled());
100100
//if there was a local override of the global setting, respect it
101101
if (savedInstanceState != null)

0 commit comments

Comments
 (0)