import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.content.res.Resources;
-import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
-import net.ktnx.mobileledger.db.DB;
import net.ktnx.mobileledger.model.Data;
import net.ktnx.mobileledger.ui.profiles.ProfileDetailModel;
import net.ktnx.mobileledger.utils.Colors;
import net.ktnx.mobileledger.utils.Globals;
import net.ktnx.mobileledger.utils.Logger;
-import net.ktnx.mobileledger.utils.MobileLedgerDatabase;
import org.jetbrains.annotations.NotNull;
public static final String PREF_PROFILE_ID = "profile-id";
public static App instance;
private static ProfileDetailModel profileModel;
- private MobileLedgerDatabase dbHelper;
private boolean monthNamesPrepared = false;
- public static SQLiteDatabase getDatabase() {
- if (instance == null)
- throw new RuntimeException("Application not created yet");
-
- return instance.getDB();
- }
public static void prepareMonthNames() {
instance.prepareMonthNames(false);
}
monthNamesPrepared = true;
}
@Override
- public void onTerminate() {
- Logger.debug("flow", "App onTerminate()");
- if (dbHelper != null)
- dbHelper.close();
- super.onTerminate();
- }
- @Override
public void onConfigurationChanged(@NotNull Configuration newConfig) {
super.onConfigurationChanged(newConfig);
prepareMonthNames(true);
Data.refreshCurrencyData(Locale.getDefault());
Data.locale.setValue(Locale.getDefault());
}
- public SQLiteDatabase getDB() {
- if (dbHelper == null)
- initDb();
-
- return dbHelper.getWritableDatabase();
- }
- private synchronized void initDb() {
- if (dbHelper != null)
- return;
-
- // Let Room do any possible migrations
- // this method may be removed when all DB access is made via Room
- DB.get()
- .compileStatement("select count(*) from profiles");
- dbHelper = new MobileLedgerDatabase(this);
- }
}
package net.ktnx.mobileledger.async;
import android.annotation.SuppressLint;
-import android.database.sqlite.SQLiteDatabase;
import android.os.AsyncTask;
import android.os.OperationCanceledException;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.RuntimeJsonMappingException;
-import net.ktnx.mobileledger.App;
import net.ktnx.mobileledger.dao.AccountDAO;
import net.ktnx.mobileledger.dao.AccountValueDAO;
import net.ktnx.mobileledger.dao.TransactionAccountDAO;
throw new HTTPException(http.getResponseCode(), http.getResponseMessage());
}
publishProgress(Progress.indeterminate());
- SQLiteDatabase db = App.getDatabase();
ArrayList<LedgerAccount> list = new ArrayList<>();
HashMap<String, LedgerAccount> map = new HashMap<>();
throwIfCancelled();
package net.ktnx.mobileledger.model;
-import android.database.Cursor;
-import android.database.sqlite.SQLiteDatabase;
-
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
-import net.ktnx.mobileledger.App;
import net.ktnx.mobileledger.db.Profile;
import net.ktnx.mobileledger.db.Transaction;
import net.ktnx.mobileledger.db.TransactionAccount;
return ledgerId;
}
protected void fillDataHash() {
- loadData(App.getDatabase());
if (dataHash != null)
return;
try {
String.format("Unable to get instance of %s digest", DIGEST_TYPE), e);
}
}
- public synchronized void loadData(SQLiteDatabase db) {
- if (dataLoaded)
- return;
-
- try (Cursor cTr = db.rawQuery(
- "SELECT year, month, day, description, comment from transactions WHERE id=?",
- new String[]{String.valueOf(ledgerId)}))
- {
- if (cTr.moveToFirst()) {
- date = new SimpleDate(cTr.getInt(0), cTr.getInt(1), cTr.getInt(2));
- description = cTr.getString(3);
- comment = cTr.getString(4);
-
- accounts.clear();
-
- try (Cursor cAcc = db.rawQuery(
- "SELECT account_name, amount, currency, comment FROM " +
- "transaction_accounts WHERE transaction_id = ?",
- new String[]{String.valueOf(ledgerId)}))
- {
- while (cAcc.moveToNext()) {
-// debug("transactions",
-// String.format("Loaded %d: %s %1.2f %s", id, cAcc.getString(0),
-// cAcc.getFloat(1), cAcc.getString(2)));
- addAccount(new LedgerTransactionAccount(cAcc.getString(0), cAcc.getFloat(1),
- cAcc.getString(2), cAcc.getString(3)));
- }
-
- finishLoading();
- }
- }
- }
-
- }
public String getDataHash() {
fillDataHash();
return dataHash;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
-import net.ktnx.mobileledger.App;
import net.ktnx.mobileledger.utils.SimpleDate;
import org.jetbrains.annotations.NotNull;
public SimpleDate getDate() {
if (date != null)
return date;
- if (type == Type.HEADER)
- throw new IllegalStateException("Header item has no date");
- transaction.loadData(App.getDatabase());
+ if (type != Type.TRANSACTION)
+ throw new IllegalStateException("Only transaction items have a date");
return transaction.getDate();
}
public boolean isMonthShown() {