Skip to content

Commit

Permalink
First book name.
Browse files Browse the repository at this point in the history
  • Loading branch information
pnemonic78 committed Jan 2, 2025
1 parent c93d6ef commit 3a0bdcb
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 6 deletions.
13 changes: 10 additions & 3 deletions app/src/main/java/org/gnucash/android/db/BookDbHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.text.TextUtils;

import androidx.annotation.NonNull;

Expand Down Expand Up @@ -77,8 +78,10 @@ public Book insertBlankBook(@NonNull SQLiteDatabase db) {
Book book = new Book();
DatabaseHelper helper = new DatabaseHelper(context, book.getUID());
SQLiteDatabase mainDb = helper.getWritableDatabase(); //actually create the db
AccountsDbAdapter accountsDbAdapter = new AccountsDbAdapter(mainDb,
new TransactionsDbAdapter(mainDb, new SplitsDbAdapter(mainDb)));
AccountsDbAdapter accountsDbAdapter = new AccountsDbAdapter(
mainDb,
new TransactionsDbAdapter(mainDb, new SplitsDbAdapter(mainDb))
);

String rootAccountUID = accountsDbAdapter.getOrCreateGnuCashRootAccountUID();
try {
Expand Down Expand Up @@ -121,11 +124,15 @@ public static SQLiteDatabase getDatabase(@NonNull Context context, String bookUI
* @param book Book to insert
*/
private void insertBook(SQLiteDatabase db, Book book) {
String name = book.getDisplayName();
if (TextUtils.isEmpty(name)) {
name = new BooksDbAdapter(db).generateDefaultBookName();
}
ContentValues contentValues = new ContentValues();
contentValues.put(BookEntry.COLUMN_UID, book.getUID());
contentValues.put(BookEntry.COLUMN_ROOT_GUID, book.getRootAccountUID());
contentValues.put(BookEntry.COLUMN_TEMPLATE_GUID, Book.generateUID());
contentValues.put(BookEntry.COLUMN_DISPLAY_NAME, new BooksDbAdapter(db).generateDefaultBookName());
contentValues.put(BookEntry.COLUMN_DISPLAY_NAME, name);
contentValues.put(BookEntry.COLUMN_ACTIVE, book.isActive() ? 1 : 0);

db.insert(BookEntry.TABLE_NAME, null, contentValues);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,10 @@ private String getNoActiveBookFoundExceptionInfo() {
return info.toString();
}

public Book getActiveBook() {
return getRecord(getActiveBookUID());
}

public class NoActiveBookFoundException extends RuntimeException {
public NoActiveBookFoundException(String message) {
super(message);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -718,6 +718,10 @@ public int updateRecord(@NonNull String uid, @NonNull ContentValues contentValue
return mDb.update(mTableName, contentValues, CommonColumns.COLUMN_UID + "=?", new String[]{uid});
}

public void updateRecord(Model model) {
addRecord(model, UpdateMethod.update);
}

/**
* Updates all records which match the {@code where} clause with the {@code newValue} for the column
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import org.gnucash.android.app.GnuCashApplication;
import org.gnucash.android.databinding.ActivityFirstRunWizardBinding;
import org.gnucash.android.db.adapter.BooksDbAdapter;
import org.gnucash.android.model.Book;
import org.gnucash.android.ui.account.AccountsActivity;
import org.gnucash.android.ui.util.TaskDelegate;

Expand Down Expand Up @@ -156,11 +157,17 @@ private FirstRunWizardModel createWizardModel(Bundle savedInstanceState) {
private void createAccountsAndFinish(@NonNull String accountOption, String currencyCode) {
if (accountOption.equals(mWizardModel.optionAccountDefault)) {
//save the UID of the active book, and then delete it after successful import
final String bookUID = GnuCashApplication.getActiveBookUID();
TaskDelegate callbackAfterImport = (bookUID != null) ? new TaskDelegate() {
final BooksDbAdapter dbAdapter = BooksDbAdapter.getInstance();
final String bookUID = dbAdapter.getActiveBookUID();
Book bookOld = dbAdapter.getRecord(bookUID);
final String bookName = bookOld.getDisplayName();
TaskDelegate callbackAfterImport = (!TextUtils.isEmpty(bookUID)) ? new TaskDelegate() {
@Override
public void onTaskComplete() {
BooksDbAdapter.getInstance().deleteBook(bookUID);
dbAdapter.deleteBook(bookUID);
Book book = dbAdapter.getActiveBook();
book.setDisplayName(bookName);
dbAdapter.updateRecord(book);
}
} : null;
AccountsActivity.createDefaultAccounts(currencyCode, FirstRunWizardActivity.this, callbackAfterImport);
Expand Down

0 comments on commit 3a0bdcb

Please sign in to comment.