Skip to content

Commit 0d8a52d

Browse files
committed
Merge branch 'hotfix/patches'
2 parents 9326155 + cbc3f7f commit 0d8a52d

File tree

9 files changed

+107
-16
lines changed

9 files changed

+107
-16
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
Change Log
22
===============================================================================
3+
Version 1.6.2 *(2015-07-16)*
4+
----------------------------
5+
* Fixed: Editing accounts causing the account's transactions to be deleted
6+
37
Version 1.6.1 *(2015-07-08)*
48
----------------------------
59
* Fixed: Crash when importing some scheduled transations with custom period strings

app/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ apply plugin: 'crashlytics'
55

66
def versionMajor = 1
77
def versionMinor = 6
8-
def versionPatch = 1
8+
def versionPatch = 2
99
def versionBuild = 0
1010

1111
def buildTime() {

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

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737
import org.gnucash.android.model.Account;
3838
import org.gnucash.android.model.AccountType;
3939
import org.gnucash.android.model.Money;
40+
import org.gnucash.android.model.Split;
41+
import org.gnucash.android.model.Transaction;
4042
import org.gnucash.android.receivers.AccountCreator;
4143
import org.gnucash.android.ui.account.AccountsActivity;
4244
import org.gnucash.android.ui.account.AccountsListFragment;
@@ -45,6 +47,7 @@
4547
import org.junit.Test;
4648
import org.junit.runner.RunWith;
4749

50+
import java.math.BigDecimal;
4851
import java.util.Currency;
4952
import java.util.List;
5053

@@ -71,6 +74,7 @@
7174
@RunWith(AndroidJUnit4.class)
7275
public class AccountsActivityTest extends ActivityInstrumentationTestCase2<AccountsActivity> {
7376
private static final String DUMMY_ACCOUNT_CURRENCY_CODE = "USD";
77+
private static final Currency DUMMY_ACCOUNT_CURRENCY = Currency.getInstance(DUMMY_ACCOUNT_CURRENCY_CODE);
7478
private static final String DUMMY_ACCOUNT_NAME = "Dummy account";
7579
public static final String DUMMY_ACCOUNT_UID = "dummy-account";
7680
private DatabaseHelper mDbHelper;
@@ -226,7 +230,7 @@ public void testChangeParentAccount() {
226230
*/
227231
@Test
228232
public void shouldHideParentAccountViewWhenNoParentsExist(){
229-
onView(withText(DUMMY_ACCOUNT_NAME)).perform(click());
233+
onView(allOf(withText(DUMMY_ACCOUNT_NAME), isDisplayed())).perform(click());
230234
onView(withId(R.id.fragment_transaction_list)).perform(swipeRight());
231235
onView(withText(R.string.label_create_account)).check(matches(isDisplayed())).perform(click());
232236
sleep(1000);
@@ -264,6 +268,31 @@ public void testEditAccount(){
264268
assertThat(latest.getCurrency().getCurrencyCode()).isEqualTo(DUMMY_ACCOUNT_CURRENCY_CODE);
265269
}
266270

271+
@Test
272+
public void editingAccountShouldNotDeleteTransactions(){
273+
onView(allOf(withText(DUMMY_ACCOUNT_NAME), isDisplayed()))
274+
.perform(longClick());
275+
Account account = new Account("Transfer Account");
276+
277+
Transaction transaction = new Transaction("Simple trxn");
278+
Split split = new Split(new Money(BigDecimal.TEN, DUMMY_ACCOUNT_CURRENCY), account.getUID());
279+
transaction.addSplit(split);
280+
transaction.addSplit(split.createPair(DUMMY_ACCOUNT_UID));
281+
account.addTransaction(transaction);
282+
mAccountsDbAdapter.addAccount(account);
283+
284+
assertThat(mAccountsDbAdapter.getAccount(DUMMY_ACCOUNT_UID).getTransactionCount()).isEqualTo(1);
285+
assertThat(mSplitsDbAdapter.getSplitsForTransaction(transaction.getUID())).hasSize(2);
286+
287+
onView(withId(R.id.context_menu_edit_accounts)).perform(click());
288+
289+
onView(withId(R.id.menu_save)).perform(click());
290+
assertThat(mAccountsDbAdapter.getAccount(DUMMY_ACCOUNT_UID).getTransactionCount()).isEqualTo(1);
291+
assertThat(mSplitsDbAdapter.fetchSplitsForAccount(DUMMY_ACCOUNT_UID).getCount()).isEqualTo(1);
292+
assertThat(mSplitsDbAdapter.getSplitsForTransaction(transaction.getUID())).hasSize(2);
293+
294+
}
295+
267296
/**
268297
* Sleep the thread for a specified period
269298
* @param millis Duration to sleep in milliseconds

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

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -159,16 +159,7 @@ public static boolean shouldSaveOpeningBalances(boolean defaultValue){
159159
* @return Default currency code string for the application
160160
*/
161161
public static String getDefaultCurrencyCode(){
162-
Locale locale = Locale.getDefault();
163-
//sometimes the locale en_UK is returned which causes a crash with Currency
164-
if (locale.getCountry().equals("UK")) {
165-
locale = new Locale(locale.getLanguage(), "GB");
166-
}
167-
168-
//for unsupported locale es_LG
169-
if (locale.getCountry().equals("LG")){
170-
locale = new Locale(locale.getLanguage(), "ES");
171-
}
162+
Locale locale = getDefaultLocale();
172163

173164
String currencyCode = "USD"; //start with USD as the default
174165
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
@@ -183,6 +174,29 @@ public static String getDefaultCurrencyCode(){
183174
return currencyCode;
184175
}
185176

177+
/**
178+
* Returns the default locale which is used for currencies, while handling special cases for
179+
* locales which are not supported for currency such as en_GB
180+
* @return The default locale for this device
181+
*/
182+
public static Locale getDefaultLocale() {
183+
Locale locale = Locale.getDefault();
184+
//sometimes the locale en_UK is returned which causes a crash with Currency
185+
if (locale.getCountry().equals("UK")) {
186+
locale = new Locale(locale.getLanguage(), "GB");
187+
}
188+
189+
//for unsupported locale es_LG
190+
if (locale.getCountry().equals("LG")){
191+
locale = new Locale(locale.getLanguage(), "ES");
192+
}
193+
194+
if (locale.getCountry().equals("en")){
195+
locale = Locale.US;
196+
}
197+
return locale;
198+
}
199+
186200
/**
187201
* Starts the service for scheduled events and schedules an alarm to call the service twice daily.
188202
* <p>If the alarm already exists, this method does nothing. If not, the alarm will be created

app/src/main/java/org/gnucash/android/db/AccountsDbAdapter.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ public long addAccount(Account account){
148148
* @return number of rows inserted
149149
*/
150150
public long bulkAddAccounts(List<Account> accountList){
151+
List<Transaction> transactionList = new ArrayList<>(accountList.size()*2);
151152
long nRow = 0;
152153
try {
153154
mDb.beginTransaction();
@@ -187,12 +188,17 @@ public long bulkAddAccounts(List<Account> accountList){
187188
//Log.d(LOG_TAG, "Replacing account in db");
188189
replaceStatement.execute();
189190
nRow ++;
191+
transactionList.addAll(account.getTransactions());
190192
}
191193
mDb.setTransactionSuccessful();
192194
}
193195
finally {
194196
mDb.endTransaction();
195197
}
198+
199+
if (nRow > 0 && !transactionList.isEmpty()){
200+
mTransactionsAdapter.bulkAddTransactions(transactionList);
201+
}
196202
return nRow;
197203
}
198204
/**

app/src/main/java/org/gnucash/android/model/Money.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,14 +92,18 @@ public final class Money implements Comparable<Money>{
9292
* A zero instance with the currency of the default locale.
9393
* This can be used anywhere where a starting amount is required without having to create a new object
9494
*/
95-
private static final Money sDefaultZero = Money.createZeroInstance(GnuCashApplication.getDefaultCurrencyCode());
95+
private static Money sDefaultZero;
9696

9797
/**
9898
* Returns a Money instance initialized to the local currency and value 0
9999
* @return Money instance of value 0 in locale currency
100100
*/
101101
public static Money getZeroInstance(){
102-
return sDefaultZero;
102+
if (sDefaultZero == null) {
103+
String currencyCode = Currency.getInstance(GnuCashApplication.getDefaultLocale()).getCurrencyCode();
104+
sDefaultZero = new Money(BigDecimal.ZERO, Currency.getInstance(currencyCode));
105+
}
106+
return sDefaultZero;
103107
}
104108

105109
/**
@@ -180,7 +184,7 @@ public static Money createZeroInstance(String currencyCode){
180184
*/
181185
private void init() {
182186
mCurrency = Currency.getInstance(Money.DEFAULT_CURRENCY_CODE);
183-
mAmount = new BigDecimal(0).setScale(DEFAULT_DECIMAL_PLACES, DEFAULT_ROUNDING_MODE);
187+
mAmount = BigDecimal.ZERO.setScale(DEFAULT_DECIMAL_PLACES, DEFAULT_ROUNDING_MODE);
184188
}
185189

186190
/**

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import android.support.v4.app.FragmentManager;
3131
import android.support.v4.widget.SimpleCursorAdapter;
3232
import android.text.TextUtils;
33+
import android.util.Log;
3334
import android.view.LayoutInflater;
3435
import android.view.View;
3536
import android.view.ViewGroup;
@@ -701,8 +702,9 @@ public void onDestroy() {
701702
* Reads the fields from the account form and saves as a new account
702703
*/
703704
private void saveAccount() {
705+
Log.i("AccountFormFragment", "Saving account");
704706
// accounts to update, in case we're updating full names of a sub account tree
705-
ArrayList<Account> accountsToUpdate = new ArrayList<Account>();
707+
ArrayList<Account> accountsToUpdate = new ArrayList<>();
706708
boolean nameChanged = false;
707709
if (mAccount == null){
708710
String name = getEnteredName();

app/src/test/java/org/gnucash/android/test/unit/db/AccountsDbAdapterTest.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.robolectric.annotation.Config;
2323

2424
import java.math.BigDecimal;
25+
import java.util.ArrayList;
2526
import java.util.Currency;
2627
import java.util.List;
2728

@@ -64,6 +65,30 @@ public void shouldBeAlphabeticallySortedByDefault(){
6465
assertThat(accountsList).contains(second, Index.atIndex(1));
6566
}
6667

68+
@Test
69+
public void bulkAddAccountsShouldNotModifyTransactions(){
70+
Account account1 = new Account("AlphaAccount");
71+
Account account2 = new Account("BetaAccount");
72+
Transaction transaction = new Transaction("MyTransaction");
73+
Split split = new Split(Money.getZeroInstance(), account1.getUID());
74+
transaction.addSplit(split);
75+
transaction.addSplit(split.createPair(account2.getUID()));
76+
account1.addTransaction(transaction);
77+
account2.addTransaction(transaction);
78+
79+
List<Account> accounts = new ArrayList<>();
80+
accounts.add(account1);
81+
accounts.add(account2);
82+
83+
mAccountsDbAdapter.bulkAddAccounts(accounts);
84+
85+
SplitsDbAdapter splitsDbAdapter = SplitsDbAdapter.getInstance();
86+
assertThat(splitsDbAdapter.getSplitsForTransactionInAccount(transaction.getUID(), account1.getUID())).hasSize(1);
87+
assertThat(splitsDbAdapter.getSplitsForTransactionInAccount(transaction.getUID(), account2.getUID())).hasSize(1);
88+
89+
assertThat(mAccountsDbAdapter.getAccount(account1.getUID()).getTransactions()).hasSize(1);
90+
}
91+
6792
@Test
6893
public void shouldAddAccountsToDatabase(){
6994
Account account1 = new Account("AlphaAccount");

app/src/test/java/org/gnucash/android/test/unit/util/GnucashTestRunner.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
import org.junit.runners.model.InitializationError;
44
import org.robolectric.RobolectricGradleTestRunner;
5+
import org.robolectric.annotation.Config;
56
import org.robolectric.internal.bytecode.ClassInfo;
67
import org.robolectric.internal.bytecode.InstrumentingClassLoaderConfig;
78
import org.robolectric.internal.bytecode.ShadowMap;
9+
import org.robolectric.manifest.AndroidManifest;
810

911
import java.util.Arrays;
1012
import java.util.Collections;
@@ -29,6 +31,11 @@ protected ShadowMap createShadowMap() {
2931
.newBuilder().addShadowClass(ShadowCrashlytics.class).build();
3032
}
3133

34+
@Override
35+
protected AndroidManifest getAppManifest(Config config) {
36+
return super.getAppManifest(config);
37+
}
38+
3239
@Override
3340
public InstrumentingClassLoaderConfig createSetup() {
3441
return new InstrumenterConfig();

0 commit comments

Comments
 (0)