Skip to content

Commit c83084a

Browse files
committed
Moves account CSV export to the Settings->Accounts section (was previously in the transactions export dialog)
Export account color as hex instead of integer Localize account CSV headers
1 parent ad478f6 commit c83084a

File tree

8 files changed

+113
-75
lines changed

8 files changed

+113
-75
lines changed

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ public long bulkAddRecords(@NonNull List<Account> accountList, UpdateMethod upda
206206
stmt.bindString(3, account.getAccountType().name());
207207
stmt.bindString(4, account.getCommodity().getCurrencyCode());
208208
if (account.getColor() != Account.DEFAULT_COLOR) {
209-
stmt.bindString(5, convertToRGBHexString(account.getColor()));
209+
stmt.bindString(5, account.getColorHexString());
210210
}
211211
stmt.bindLong(6, account.isFavorite() ? 1 : 0);
212212
stmt.bindString(7, account.getFullName());
@@ -230,10 +230,6 @@ public long bulkAddRecords(@NonNull List<Account> accountList, UpdateMethod upda
230230
return stmt;
231231
}
232232

233-
private String convertToRGBHexString(int color) {
234-
return String.format("#%06X", (0xFFFFFF & color));
235-
}
236-
237233
/**
238234
* Marks all transactions for a given account as exported
239235
* @param accountUID Unique ID of the record to be marked as exported

app/src/main/java/org/gnucash/android/export/ExportFormat.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ public String getExtension(){
4848
case XML:
4949
return ".gnca";
5050
case CSVA:
51-
return ".csv";
5251
case CSVT:
5352
return ".csv";
5453
default:

app/src/main/java/org/gnucash/android/export/csv/CsvAccountExporter.java

Lines changed: 31 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,17 @@
1717
package org.gnucash.android.export.csv;
1818

1919
import android.database.sqlite.SQLiteDatabase;
20+
2021
import com.crashlytics.android.Crashlytics;
22+
23+
import org.gnucash.android.R;
2124
import org.gnucash.android.export.ExportParams;
2225
import org.gnucash.android.export.Exporter;
2326
import org.gnucash.android.model.Account;
2427

25-
import java.io.BufferedOutputStream;
26-
import java.io.FileOutputStream;
28+
import java.io.FileWriter;
2729
import java.io.IOException;
28-
import java.io.OutputStreamWriter;
29-
import java.util.ArrayList;
30+
import java.util.Arrays;
3031
import java.util.List;
3132

3233
/**
@@ -61,84 +62,51 @@ public CsvAccountExporter(ExportParams params, SQLiteDatabase db) {
6162

6263
@Override
6364
public List<String> generateExport() throws ExporterException {
64-
OutputStreamWriter writerStream = null;
65-
CsvWriter writer = null;
6665
String outputFile = getExportCacheFilePath();
67-
try {
68-
FileOutputStream fileOutputStream = new FileOutputStream(outputFile);
69-
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
70-
writerStream = new OutputStreamWriter(bufferedOutputStream);
71-
writer = new CsvWriter(writerStream);
66+
try (CsvWriter writer = new CsvWriter(new FileWriter(outputFile), mCsvSeparator + "")) {
7267
generateExport(writer);
7368
} catch (IOException ex){
7469
Crashlytics.log("Error exporting CSV");
7570
Crashlytics.logException(ex);
76-
} finally {
77-
if (writerStream != null) {
78-
try {
79-
writerStream.close();
80-
} catch (IOException e) {
81-
throw new ExporterException(mExportParams, e);
82-
}
83-
}
71+
throw new ExporterException(mExportParams, ex);
8472
}
8573

86-
List<String> exportedFiles = new ArrayList<>();
87-
exportedFiles.add(outputFile);
88-
89-
return exportedFiles;
74+
return Arrays.asList(outputFile);
9075
}
9176

92-
public void generateExport(final CsvWriter writer) throws ExporterException {
77+
/**
78+
* Writes out all the accounts in the system as CSV to the provided writer
79+
* @param csvWriter Destination for the CSV export
80+
* @throws ExporterException if an error occurred while writing to the stream
81+
*/
82+
public void generateExport(final CsvWriter csvWriter) throws ExporterException {
9383
try {
94-
String separator = mCsvSeparator + "";
95-
List<String> names = new ArrayList<String>();
96-
names.add("type");
97-
names.add("full_name");
98-
names.add("name");
99-
names.add("code");
100-
names.add("description");
101-
names.add("color");
102-
names.add("notes");
103-
names.add("commoditym");
104-
names.add("commodityn");
105-
names.add("hidden");
106-
names.add("tax");
107-
names.add("place_holder");
108-
84+
List<String> names = Arrays.asList(mContext.getResources().getStringArray(R.array.csv_account_headers));
10985
List<Account> accounts = mAccountsDbAdapter.getAllRecords();
11086

11187
for(int i = 0; i < names.size(); i++) {
112-
writer.write(names.get(i) + separator);
88+
csvWriter.writeToken(names.get(i));
11389
}
114-
writer.write("\n");
115-
for(int i = 0; i < accounts.size(); i++) {
116-
Account account = accounts.get(i);
117-
118-
writer.write(account.getAccountType().toString() + separator);
119-
writer.write(account.getFullName() + separator);
120-
writer.write(account.getName() + separator);
121-
122-
//Code
123-
writer.write(separator);
124-
125-
writer.write(account.getDescription() + separator);
126-
writer.write(account.getColor() + separator);
127-
128-
//Notes
129-
writer.write(separator);
13090

131-
writer.write(account.getCommodity().getCurrencyCode() + separator);
132-
writer.write("CURRENCY" + separator);
133-
writer.write(account.isHidden()?"T":"F" + separator);
91+
csvWriter.newLine();
92+
for (Account account : accounts) {
93+
csvWriter.writeToken(account.getAccountType().toString());
94+
csvWriter.writeToken(account.getFullName());
95+
csvWriter.writeToken(account.getName());
13496

135-
writer.write("F" + separator);
97+
csvWriter.writeToken(null); //Account code
98+
csvWriter.writeToken(account.getDescription());
99+
csvWriter.writeToken(account.getColorHexString());
100+
csvWriter.writeToken(null); //Account notes
136101

137-
writer.write(account.isPlaceholderAccount()?"T":"F" + separator);
102+
csvWriter.writeToken(account.getCommodity().getCurrencyCode());
103+
csvWriter.writeToken("CURRENCY");
104+
csvWriter.writeToken(account.isHidden() ? "T" : "F");
138105

139-
writer.write("\n");
106+
csvWriter.writeToken("F"); //Tax
107+
csvWriter.writeEndToken(account.isPlaceholderAccount() ? "T": "F");
140108
}
141-
} catch (Exception e) {
109+
} catch (IOException e) {
142110
Crashlytics.logException(e);
143111
throw new ExporterException(mExportParams, e);
144112
}

app/src/main/java/org/gnucash/android/export/csv/CsvTransactionsExporter.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
import java.io.IOException;
3535
import java.text.DateFormat;
3636
import java.text.SimpleDateFormat;
37-
import java.util.ArrayList;
3837
import java.util.Arrays;
3938
import java.util.Date;
4039
import java.util.List;
@@ -82,12 +81,10 @@ public List<String> generateExport() throws ExporterException {
8281
} catch (IOException ex){
8382
Crashlytics.log("Error exporting CSV");
8483
Crashlytics.logException(ex);
84+
throw new ExporterException(mExportParams, ex);
8585
}
8686

87-
List<String> exportedFiles = new ArrayList<>();
88-
exportedFiles.add(outputFile);
89-
90-
return exportedFiles;
87+
return Arrays.asList(outputFile);
9188
}
9289

9390
/**

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,14 @@ public int getColor() {
282282
return mColor;
283283
}
284284

285+
/**
286+
* Returns the account color as an RGB hex string
287+
* @return Hex color of the account
288+
*/
289+
public String getColorHexString(){
290+
return String.format("#%06X", (0xFFFFFF & mColor));
291+
}
292+
285293
/**
286294
* Sets the color of the account.
287295
* @param color Color as an int as returned by {@link Color}.

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

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,26 @@
2727
import android.support.v7.preference.ListPreference;
2828
import android.support.v7.preference.Preference;
2929
import android.support.v7.preference.PreferenceFragmentCompat;
30+
import android.widget.Toast;
31+
32+
import com.crashlytics.android.Crashlytics;
3033

3134
import org.gnucash.android.R;
3235
import org.gnucash.android.app.GnuCashApplication;
3336
import org.gnucash.android.db.DatabaseSchema;
37+
import org.gnucash.android.db.adapter.BooksDbAdapter;
3438
import org.gnucash.android.db.adapter.CommoditiesDbAdapter;
39+
import org.gnucash.android.export.ExportAsyncTask;
40+
import org.gnucash.android.export.ExportFormat;
41+
import org.gnucash.android.export.ExportParams;
42+
import org.gnucash.android.export.Exporter;
3543
import org.gnucash.android.model.Money;
3644
import org.gnucash.android.ui.account.AccountsActivity;
3745
import org.gnucash.android.ui.settings.dialog.DeleteAllAccountsConfirmationDialog;
3846

3947
import java.util.ArrayList;
4048
import java.util.List;
49+
import java.util.concurrent.ExecutionException;
4150

4251
/**
4352
* Account settings fragment inside the Settings activity
@@ -48,6 +57,8 @@
4857
public class AccountPreferencesFragment extends PreferenceFragmentCompat implements
4958
Preference.OnPreferenceChangeListener, Preference.OnPreferenceClickListener{
5059

60+
private static final int REQUEST_EXPORT_FILE = 0xC5;
61+
5162
List<CharSequence> mCurrencyEntries = new ArrayList<>();
5263
List<CharSequence> mCurrencyEntryValues = new ArrayList<>();
5364

@@ -91,6 +102,9 @@ public void onResume() {
91102
Preference preference = findPreference(getString(R.string.key_import_accounts));
92103
preference.setOnPreferenceClickListener(this);
93104

105+
preference = findPreference(getString(R.string.key_export_accounts_csv));
106+
preference.setOnPreferenceClickListener(this);
107+
94108
preference = findPreference(getString(R.string.key_delete_all_accounts));
95109
preference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
96110
@Override
@@ -137,9 +151,29 @@ public boolean onPreferenceClick(Preference preference) {
137151
return true;
138152
}
139153

154+
if (key.equals(getString(R.string.key_export_accounts_csv))){
155+
selectExportFile();
156+
return true;
157+
}
158+
140159
return false;
141160
}
142161

162+
/**
163+
* Open a chooser for user to pick a file to export to
164+
*/
165+
private void selectExportFile() {
166+
Intent createIntent = new Intent(Intent.ACTION_CREATE_DOCUMENT);
167+
createIntent.setType("*/*").addCategory(Intent.CATEGORY_OPENABLE);
168+
String bookName = BooksDbAdapter.getInstance().getActiveBookDisplayName();
169+
170+
String filename = Exporter.buildExportFilename(ExportFormat.CSVA, bookName);
171+
createIntent.setType("application/text");
172+
173+
createIntent.putExtra(Intent.EXTRA_TITLE, filename);
174+
startActivityForResult(createIntent, REQUEST_EXPORT_FILE);
175+
}
176+
143177
@Override
144178
public boolean onPreferenceChange(Preference preference, Object newValue) {
145179
if (preference.getKey().equals(getString(R.string.key_default_currency))){
@@ -167,6 +201,22 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) {
167201
AccountsActivity.importXmlFileFromIntent(getActivity(), data, null);
168202
}
169203
break;
204+
205+
case REQUEST_EXPORT_FILE:
206+
if (resultCode == Activity.RESULT_OK && data != null){
207+
ExportParams exportParams = new ExportParams(ExportFormat.CSVA);
208+
exportParams.setExportTarget(ExportParams.ExportTarget.URI);
209+
exportParams.setExportLocation(data.getData().toString());
210+
ExportAsyncTask exportTask = new ExportAsyncTask(getActivity(), GnuCashApplication.getActiveDb());
211+
212+
try {
213+
exportTask.execute(exportParams).get();
214+
} catch (InterruptedException | ExecutionException e) {
215+
Crashlytics.logException(e);
216+
Toast.makeText(getActivity(), "An error occurred during the Accounts CSV export",
217+
Toast.LENGTH_LONG).show();
218+
}
219+
}
170220
}
171221
}
172222
}

app/src/main/res/values/strings.xml

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@
126126
<string name="toast_transanction_amount_required">Enter an amount to save the transaction</string>
127127
<string name="toast_error_importing_accounts">An error occurred while importing the GnuCash accounts</string>
128128
<string name="toast_success_importing_accounts">GnuCash Accounts successfully imported</string>
129-
<string name="summary_import_accounts">Import account structure exported from GnuCash desktop</string>
129+
<string name="summary_import_accounts">Import account structure from GnuCash XML</string>
130130
<string name="title_import_accounts">Import GnuCash XML</string>
131131
<string name="summary_delete_all_accounts">Delete all accounts in the database. All transactions will be deleted as
132132
well.
@@ -463,6 +463,9 @@
463463
<string name="label_dropbox_export_destination">Export to \'/Apps/GnuCash Android/\' folder on Dropbox</string>
464464
<string name="title_section_preferences">Preferences</string>
465465
<string name="yes_sure">Yes, I\'m sure</string>
466+
<string name="key_export_accounts_csv">export_accounts_csv_key</string>
467+
<string name="summary_export_accounts_csv">Export all accounts (without transactions) to CSV</string>
468+
<string name="title_export_accounts_csv">Export as CSV</string>
466469
<string-array name="csv_transaction_headers">
467470
<item>Date</item>
468471
<item>Transaction ID</item>
@@ -481,4 +484,18 @@
481484
<item>Reconcile Date</item>
482485
<item>Rate/Price</item>
483486
</string-array>
487+
<string-array name="csv_account_headers">
488+
<item>Type</item>
489+
<item>Full Name</item>
490+
<item>Name</item>
491+
<item>Code</item>
492+
<item>Description</item>
493+
<item>Color</item>
494+
<item>Notes</item>
495+
<item>Commoditym</item>
496+
<item>Commodityn</item>
497+
<item>Hidden</item>
498+
<item>Tax</item>
499+
<item>Placeholder</item>
500+
</string-array>
484501
</resources>

app/src/main/res/xml/fragment_account_preferences.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@
2828
<Preference android:key="@string/key_import_accounts"
2929
android:summary="@string/summary_import_accounts"
3030
android:title="@string/title_import_accounts" />
31+
<Preference android:key="@string/key_export_accounts_csv"
32+
android:summary="@string/summary_export_accounts_csv"
33+
android:title="@string/title_export_accounts_csv" />
3134
<Preference android:key="@string/key_delete_all_accounts"
3235
android:summary="@string/summary_delete_all_accounts"
3336
android:title="@string/title_delete_all_accounts" />

0 commit comments

Comments
 (0)