Skip to content

Commit

Permalink
Moves account CSV export to the Settings->Accounts section (was previ…
Browse files Browse the repository at this point in the history
…ously in the transactions export dialog)

Export account color as hex instead of integer
Localize account CSV headers
  • Loading branch information
codinguser committed Jun 6, 2018
1 parent ad478f6 commit c83084a
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ public long bulkAddRecords(@NonNull List<Account> accountList, UpdateMethod upda
stmt.bindString(3, account.getAccountType().name());
stmt.bindString(4, account.getCommodity().getCurrencyCode());
if (account.getColor() != Account.DEFAULT_COLOR) {
stmt.bindString(5, convertToRGBHexString(account.getColor()));
stmt.bindString(5, account.getColorHexString());
}
stmt.bindLong(6, account.isFavorite() ? 1 : 0);
stmt.bindString(7, account.getFullName());
Expand All @@ -230,10 +230,6 @@ public long bulkAddRecords(@NonNull List<Account> accountList, UpdateMethod upda
return stmt;
}

private String convertToRGBHexString(int color) {
return String.format("#%06X", (0xFFFFFF & color));
}

/**
* Marks all transactions for a given account as exported
* @param accountUID Unique ID of the record to be marked as exported
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ public String getExtension(){
case XML:
return ".gnca";
case CSVA:
return ".csv";
case CSVT:
return ".csv";
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,17 @@
package org.gnucash.android.export.csv;

import android.database.sqlite.SQLiteDatabase;

import com.crashlytics.android.Crashlytics;

import org.gnucash.android.R;
import org.gnucash.android.export.ExportParams;
import org.gnucash.android.export.Exporter;
import org.gnucash.android.model.Account;

import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
Expand Down Expand Up @@ -61,84 +62,51 @@ public CsvAccountExporter(ExportParams params, SQLiteDatabase db) {

@Override
public List<String> generateExport() throws ExporterException {
OutputStreamWriter writerStream = null;
CsvWriter writer = null;
String outputFile = getExportCacheFilePath();
try {
FileOutputStream fileOutputStream = new FileOutputStream(outputFile);
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
writerStream = new OutputStreamWriter(bufferedOutputStream);
writer = new CsvWriter(writerStream);
try (CsvWriter writer = new CsvWriter(new FileWriter(outputFile), mCsvSeparator + "")) {
generateExport(writer);
} catch (IOException ex){
Crashlytics.log("Error exporting CSV");
Crashlytics.logException(ex);
} finally {
if (writerStream != null) {
try {
writerStream.close();
} catch (IOException e) {
throw new ExporterException(mExportParams, e);
}
}
throw new ExporterException(mExportParams, ex);
}

List<String> exportedFiles = new ArrayList<>();
exportedFiles.add(outputFile);

return exportedFiles;
return Arrays.asList(outputFile);
}

public void generateExport(final CsvWriter writer) throws ExporterException {
/**
* Writes out all the accounts in the system as CSV to the provided writer
* @param csvWriter Destination for the CSV export
* @throws ExporterException if an error occurred while writing to the stream
*/
public void generateExport(final CsvWriter csvWriter) throws ExporterException {
try {
String separator = mCsvSeparator + "";
List<String> names = new ArrayList<String>();
names.add("type");
names.add("full_name");
names.add("name");
names.add("code");
names.add("description");
names.add("color");
names.add("notes");
names.add("commoditym");
names.add("commodityn");
names.add("hidden");
names.add("tax");
names.add("place_holder");

List<String> names = Arrays.asList(mContext.getResources().getStringArray(R.array.csv_account_headers));
List<Account> accounts = mAccountsDbAdapter.getAllRecords();

for(int i = 0; i < names.size(); i++) {
writer.write(names.get(i) + separator);
csvWriter.writeToken(names.get(i));
}
writer.write("\n");
for(int i = 0; i < accounts.size(); i++) {
Account account = accounts.get(i);

writer.write(account.getAccountType().toString() + separator);
writer.write(account.getFullName() + separator);
writer.write(account.getName() + separator);

//Code
writer.write(separator);

writer.write(account.getDescription() + separator);
writer.write(account.getColor() + separator);

//Notes
writer.write(separator);

writer.write(account.getCommodity().getCurrencyCode() + separator);
writer.write("CURRENCY" + separator);
writer.write(account.isHidden()?"T":"F" + separator);
csvWriter.newLine();
for (Account account : accounts) {
csvWriter.writeToken(account.getAccountType().toString());
csvWriter.writeToken(account.getFullName());
csvWriter.writeToken(account.getName());

writer.write("F" + separator);
csvWriter.writeToken(null); //Account code
csvWriter.writeToken(account.getDescription());
csvWriter.writeToken(account.getColorHexString());
csvWriter.writeToken(null); //Account notes

writer.write(account.isPlaceholderAccount()?"T":"F" + separator);
csvWriter.writeToken(account.getCommodity().getCurrencyCode());
csvWriter.writeToken("CURRENCY");
csvWriter.writeToken(account.isHidden() ? "T" : "F");

writer.write("\n");
csvWriter.writeToken("F"); //Tax
csvWriter.writeEndToken(account.isPlaceholderAccount() ? "T": "F");
}
} catch (Exception e) {
} catch (IOException e) {
Crashlytics.logException(e);
throw new ExporterException(mExportParams, e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
Expand Down Expand Up @@ -82,12 +81,10 @@ public List<String> generateExport() throws ExporterException {
} catch (IOException ex){
Crashlytics.log("Error exporting CSV");
Crashlytics.logException(ex);
throw new ExporterException(mExportParams, ex);
}

List<String> exportedFiles = new ArrayList<>();
exportedFiles.add(outputFile);

return exportedFiles;
return Arrays.asList(outputFile);
}

/**
Expand Down
8 changes: 8 additions & 0 deletions app/src/main/java/org/gnucash/android/model/Account.java
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,14 @@ public int getColor() {
return mColor;
}

/**
* Returns the account color as an RGB hex string
* @return Hex color of the account
*/
public String getColorHexString(){
return String.format("#%06X", (0xFFFFFF & mColor));
}

/**
* Sets the color of the account.
* @param color Color as an int as returned by {@link Color}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,26 @@
import android.support.v7.preference.ListPreference;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceFragmentCompat;
import android.widget.Toast;

import com.crashlytics.android.Crashlytics;

import org.gnucash.android.R;
import org.gnucash.android.app.GnuCashApplication;
import org.gnucash.android.db.DatabaseSchema;
import org.gnucash.android.db.adapter.BooksDbAdapter;
import org.gnucash.android.db.adapter.CommoditiesDbAdapter;
import org.gnucash.android.export.ExportAsyncTask;
import org.gnucash.android.export.ExportFormat;
import org.gnucash.android.export.ExportParams;
import org.gnucash.android.export.Exporter;
import org.gnucash.android.model.Money;
import org.gnucash.android.ui.account.AccountsActivity;
import org.gnucash.android.ui.settings.dialog.DeleteAllAccountsConfirmationDialog;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;

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

private static final int REQUEST_EXPORT_FILE = 0xC5;

List<CharSequence> mCurrencyEntries = new ArrayList<>();
List<CharSequence> mCurrencyEntryValues = new ArrayList<>();

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

preference = findPreference(getString(R.string.key_export_accounts_csv));
preference.setOnPreferenceClickListener(this);

preference = findPreference(getString(R.string.key_delete_all_accounts));
preference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
Expand Down Expand Up @@ -137,9 +151,29 @@ public boolean onPreferenceClick(Preference preference) {
return true;
}

if (key.equals(getString(R.string.key_export_accounts_csv))){
selectExportFile();
return true;
}

return false;
}

/**
* Open a chooser for user to pick a file to export to
*/
private void selectExportFile() {
Intent createIntent = new Intent(Intent.ACTION_CREATE_DOCUMENT);
createIntent.setType("*/*").addCategory(Intent.CATEGORY_OPENABLE);
String bookName = BooksDbAdapter.getInstance().getActiveBookDisplayName();

String filename = Exporter.buildExportFilename(ExportFormat.CSVA, bookName);
createIntent.setType("application/text");

createIntent.putExtra(Intent.EXTRA_TITLE, filename);
startActivityForResult(createIntent, REQUEST_EXPORT_FILE);
}

@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
if (preference.getKey().equals(getString(R.string.key_default_currency))){
Expand Down Expand Up @@ -167,6 +201,22 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) {
AccountsActivity.importXmlFileFromIntent(getActivity(), data, null);
}
break;

case REQUEST_EXPORT_FILE:
if (resultCode == Activity.RESULT_OK && data != null){
ExportParams exportParams = new ExportParams(ExportFormat.CSVA);
exportParams.setExportTarget(ExportParams.ExportTarget.URI);
exportParams.setExportLocation(data.getData().toString());
ExportAsyncTask exportTask = new ExportAsyncTask(getActivity(), GnuCashApplication.getActiveDb());

try {
exportTask.execute(exportParams).get();
} catch (InterruptedException | ExecutionException e) {
Crashlytics.logException(e);
Toast.makeText(getActivity(), "An error occurred during the Accounts CSV export",
Toast.LENGTH_LONG).show();
}
}
}
}
}
19 changes: 18 additions & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@
<string name="toast_transanction_amount_required">Enter an amount to save the transaction</string>
<string name="toast_error_importing_accounts">An error occurred while importing the GnuCash accounts</string>
<string name="toast_success_importing_accounts">GnuCash Accounts successfully imported</string>
<string name="summary_import_accounts">Import account structure exported from GnuCash desktop</string>
<string name="summary_import_accounts">Import account structure from GnuCash XML</string>
<string name="title_import_accounts">Import GnuCash XML</string>
<string name="summary_delete_all_accounts">Delete all accounts in the database. All transactions will be deleted as
well.
Expand Down Expand Up @@ -463,6 +463,9 @@
<string name="label_dropbox_export_destination">Export to \'/Apps/GnuCash Android/\' folder on Dropbox</string>
<string name="title_section_preferences">Preferences</string>
<string name="yes_sure">Yes, I\'m sure</string>
<string name="key_export_accounts_csv">export_accounts_csv_key</string>
<string name="summary_export_accounts_csv">Export all accounts (without transactions) to CSV</string>
<string name="title_export_accounts_csv">Export as CSV</string>
<string-array name="csv_transaction_headers">
<item>Date</item>
<item>Transaction ID</item>
Expand All @@ -481,4 +484,18 @@
<item>Reconcile Date</item>
<item>Rate/Price</item>
</string-array>
<string-array name="csv_account_headers">
<item>Type</item>
<item>Full Name</item>
<item>Name</item>
<item>Code</item>
<item>Description</item>
<item>Color</item>
<item>Notes</item>
<item>Commoditym</item>
<item>Commodityn</item>
<item>Hidden</item>
<item>Tax</item>
<item>Placeholder</item>
</string-array>
</resources>
3 changes: 3 additions & 0 deletions app/src/main/res/xml/fragment_account_preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
<Preference android:key="@string/key_import_accounts"
android:summary="@string/summary_import_accounts"
android:title="@string/title_import_accounts" />
<Preference android:key="@string/key_export_accounts_csv"
android:summary="@string/summary_export_accounts_csv"
android:title="@string/title_export_accounts_csv" />
<Preference android:key="@string/key_delete_all_accounts"
android:summary="@string/summary_delete_all_accounts"
android:title="@string/title_delete_all_accounts" />
Expand Down

0 comments on commit c83084a

Please sign in to comment.