X-Git-Url: https://git.ktnx.net/?a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fnet%2Fktnx%2Fmobileledger%2Fmodel%2FMobileLedgerProfile.java;h=b8085765eb069048f78cfe5621e93ef8e5887268;hb=423e6077aaa672ef1cb9e6c489c8144811f5c682;hp=2b587c8ed4ad6d01331577122702e2f8c8ef6eec;hpb=e809f9bacb61890374d1b9c8c46edeaed8a838b0;p=mobile-ledger.git
diff --git a/app/src/main/java/net/ktnx/mobileledger/model/MobileLedgerProfile.java b/app/src/main/java/net/ktnx/mobileledger/model/MobileLedgerProfile.java
index 2b587c8e..b8085765 100644
--- a/app/src/main/java/net/ktnx/mobileledger/model/MobileLedgerProfile.java
+++ b/app/src/main/java/net/ktnx/mobileledger/model/MobileLedgerProfile.java
@@ -1,87 +1,163 @@
/*
- * Copyright © 2019 Damyan Ivanov.
- * This file is part of Mobile-Ledger.
- * Mobile-Ledger is free software: you can distribute it and/or modify it
+ * Copyright © 2021 Damyan Ivanov.
+ * This file is part of MoLe.
+ * MoLe is free software: you can distribute it and/or modify it
* under the term of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your opinion), any later version.
*
- * Mobile-Ledger is distributed in the hope that it will be useful,
+ * MoLe is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License terms for details.
*
* You should have received a copy of the GNU General Public License
- * along with Mobile-Ledger. If not, see .
+ * along with MoLe. If not, see .
*/
package net.ktnx.mobileledger.model;
+import android.content.Context;
+import android.content.Intent;
+import android.content.res.Resources;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
-import android.util.Log;
+import android.os.Bundle;
+import android.text.TextUtils;
+import android.util.SparseArray;
-import net.ktnx.mobileledger.utils.Globals;
-import net.ktnx.mobileledger.utils.MLDB;
+import androidx.annotation.Nullable;
+
+import net.ktnx.mobileledger.App;
+import net.ktnx.mobileledger.R;
+import net.ktnx.mobileledger.async.DbOpQueue;
+import net.ktnx.mobileledger.json.API;
+import net.ktnx.mobileledger.ui.profiles.ProfileDetailActivity;
+import net.ktnx.mobileledger.ui.profiles.ProfileDetailFragment;
+import net.ktnx.mobileledger.utils.Logger;
+import net.ktnx.mobileledger.utils.Misc;
+import net.ktnx.mobileledger.utils.SimpleDate;
+
+import org.jetbrains.annotations.Contract;
import java.util.ArrayList;
+import java.util.HashMap;
import java.util.List;
-import java.util.UUID;
+import java.util.Locale;
+import java.util.Map;
+import java.util.Objects;
+
+import static net.ktnx.mobileledger.utils.Logger.debug;
public final class MobileLedgerProfile {
- private String uuid;
+ // N.B. when adding new fields, update the copy-constructor below
+ private final String uuid;
private String name;
+ private boolean permitPosting;
+ private boolean showCommentsByDefault;
+ private boolean showCommodityByDefault;
+ private String defaultCommodity;
+ private String preferredAccountsFilter;
private String url;
private boolean authEnabled;
private String authUserName;
private String authPassword;
- public MobileLedgerProfile(String uuid, String name, String url, boolean authEnabled,
- String authUserName, String authPassword) {
+ private int themeHue;
+ private int orderNo = -1;
+ private API apiVersion = API.auto;
+ private FutureDates futureDates = FutureDates.None;
+ private boolean accountsLoaded;
+ private boolean transactionsLoaded;
+ private HledgerVersion detectedVersion;
+ // N.B. when adding new fields, update the copy-constructor below
+ transient private AccountAndTransactionListSaver accountAndTransactionListSaver;
+ public MobileLedgerProfile(String uuid) {
this.uuid = uuid;
- this.name = name;
- this.url = url;
- this.authEnabled = authEnabled;
- this.authUserName = authUserName;
- this.authPassword = authPassword;
}
- public MobileLedgerProfile(CharSequence name, CharSequence url, boolean authEnabled,
- CharSequence authUserName, CharSequence authPassword) {
- this.uuid = String.valueOf(UUID.randomUUID());
- this.name = String.valueOf(name);
- this.url = String.valueOf(url);
- this.authEnabled = authEnabled;
- this.authUserName = String.valueOf(authUserName);
- this.authPassword = String.valueOf(authPassword);
+ public MobileLedgerProfile(MobileLedgerProfile origin) {
+ uuid = origin.uuid;
+ name = origin.name;
+ permitPosting = origin.permitPosting;
+ showCommentsByDefault = origin.showCommentsByDefault;
+ showCommodityByDefault = origin.showCommodityByDefault;
+ preferredAccountsFilter = origin.preferredAccountsFilter;
+ url = origin.url;
+ authEnabled = origin.authEnabled;
+ authUserName = origin.authUserName;
+ authPassword = origin.authPassword;
+ themeHue = origin.themeHue;
+ orderNo = origin.orderNo;
+ futureDates = origin.futureDates;
+ apiVersion = origin.apiVersion;
+ defaultCommodity = origin.defaultCommodity;
+ accountsLoaded = origin.accountsLoaded;
+ transactionsLoaded = origin.transactionsLoaded;
+ if (origin.detectedVersion != null)
+ detectedVersion = new HledgerVersion(origin.detectedVersion);
}
// loads all profiles into Data.profiles
// returns the profile with the given UUID
- public static MobileLedgerProfile loadAllFromDB(String currentProfileUUID) {
+ public static MobileLedgerProfile loadAllFromDB(@Nullable String currentProfileUUID) {
MobileLedgerProfile result = null;
- List list = new ArrayList<>();
- SQLiteDatabase db = MLDB.getReadableDatabase();
+ ArrayList list = new ArrayList<>();
+ SQLiteDatabase db = App.getDatabase();
try (Cursor cursor = db.rawQuery("SELECT uuid, name, url, use_authentication, auth_user, " +
- "auth_password FROM profiles order by order_no", null))
+ "auth_password, permit_posting, theme, order_no, " +
+ "preferred_accounts_filter, future_dates, api_version, " +
+ "show_commodity_by_default, default_commodity, " +
+ "show_comments_by_default, detected_version_pre_1_19, " +
+ "detected_version_major, detected_version_minor FROM " +
+ "profiles order by order_no", null))
{
while (cursor.moveToNext()) {
- MobileLedgerProfile item =
- new MobileLedgerProfile(cursor.getString(0), cursor.getString(1),
- cursor.getString(2), cursor.getInt(3) == 1, cursor.getString(4),
- cursor.getString(5));
+ MobileLedgerProfile item = new MobileLedgerProfile(cursor.getString(0));
+ item.setName(cursor.getString(1));
+ item.setUrl(cursor.getString(2));
+ item.setAuthEnabled(cursor.getInt(3) == 1);
+ item.setAuthUserName(cursor.getString(4));
+ item.setAuthPassword(cursor.getString(5));
+ item.setPostingPermitted(cursor.getInt(6) == 1);
+ item.setThemeId(cursor.getInt(7));
+ item.orderNo = cursor.getInt(8);
+ item.setPreferredAccountsFilter(cursor.getString(9));
+ item.setFutureDates(cursor.getInt(10));
+ item.setApiVersion(cursor.getInt(11));
+ item.setShowCommodityByDefault(cursor.getInt(12) == 1);
+ item.setDefaultCommodity(cursor.getString(13));
+ item.setShowCommentsByDefault(cursor.getInt(14) == 1);
+ {
+ boolean pre_1_20 = cursor.getInt(15) == 1;
+ int major = cursor.getInt(16);
+ int minor = cursor.getInt(17);
+
+ if (!pre_1_20 && major == 0 && minor == 0) {
+ item.detectedVersion = null;
+ }
+ else if (pre_1_20) {
+ item.detectedVersion = new HledgerVersion(true);
+ }
+ else {
+ item.detectedVersion = new HledgerVersion(major, minor);
+ }
+ }
list.add(item);
- if (item.getUuid().equals(currentProfileUUID)) result = item;
+ if (item.getUuid()
+ .equals(currentProfileUUID))
+ result = item;
}
}
- Data.profiles.setList(list);
+ Data.profiles.postValue(list);
return result;
}
public static void storeProfilesOrder() {
- SQLiteDatabase db = MLDB.getWritableDatabase();
- db.beginTransaction();
+ SQLiteDatabase db = App.getDatabase();
+ db.beginTransactionNonExclusive();
try {
int orderNo = 0;
- for (MobileLedgerProfile p : Data.profiles.getList()) {
+ for (MobileLedgerProfile p : Objects.requireNonNull(Data.profiles.getValue())) {
db.execSQL("update profiles set order_no=? where uuid=?",
new Object[]{orderNo, p.getUuid()});
+ p.orderNo = orderNo;
orderNo++;
}
db.setTransactionSuccessful();
@@ -90,43 +166,120 @@ public final class MobileLedgerProfile {
db.endTransaction();
}
}
- public static List createInitialProfileList() {
- List result = new ArrayList<>();
- MobileLedgerProfile first =
- new MobileLedgerProfile(UUID.randomUUID().toString(), "default", "", false, "", "");
- first.storeInDB();
- result.add(first);
-
- return result;
- }
- public static MobileLedgerProfile loadUUIDFromDB(String profileUUID) {
- SQLiteDatabase db = MLDB.getReadableDatabase();
- String name;
- String url;
- String authUser;
- String authPassword;
- Boolean useAuthentication;
- try (Cursor cursor = db.rawQuery("SELECT name, url, use_authentication, auth_user, " +
- "auth_password FROM profiles WHERE uuid=?",
- new String[]{profileUUID}))
- {
- if (cursor.moveToNext()) {
- name = cursor.getString(0);
- url = cursor.getString(1);
- useAuthentication = cursor.getInt(2) == 1;
- authUser = useAuthentication ? cursor.getString(3) : null;
- authPassword = useAuthentication ? cursor.getString(4) : null;
- }
- else {
- name = "Unknown profile";
- url = "Https://server/url";
- useAuthentication = false;
- authUser = authPassword = null;
- }
+ static public void startEditProfileActivity(Context context, MobileLedgerProfile profile) {
+ Intent intent = new Intent(context, ProfileDetailActivity.class);
+ Bundle args = new Bundle();
+ if (profile != null) {
+ int index = Data.getProfileIndex(profile);
+ if (index != -1)
+ intent.putExtra(ProfileDetailFragment.ARG_ITEM_ID, index);
}
+ intent.putExtras(args);
+ context.startActivity(intent, args);
+ }
+ public HledgerVersion getDetectedVersion() {
+ return detectedVersion;
+ }
+ public void setDetectedVersion(HledgerVersion detectedVersion) {
+ this.detectedVersion = detectedVersion;
+ }
+ @Contract(value = "null -> false", pure = true)
+ @Override
+ public boolean equals(@Nullable Object obj) {
+ if (obj == null)
+ return false;
+ if (obj == this)
+ return true;
+ if (obj.getClass() != this.getClass())
+ return false;
- return new MobileLedgerProfile(profileUUID, name, url, useAuthentication, authUser,
- authPassword);
+ MobileLedgerProfile p = (MobileLedgerProfile) obj;
+ if (!uuid.equals(p.uuid))
+ return false;
+ if (!name.equals(p.name))
+ return false;
+ if (permitPosting != p.permitPosting)
+ return false;
+ if (showCommentsByDefault != p.showCommentsByDefault)
+ return false;
+ if (showCommodityByDefault != p.showCommodityByDefault)
+ return false;
+ if (!Objects.equals(defaultCommodity, p.defaultCommodity))
+ return false;
+ if (!Objects.equals(preferredAccountsFilter, p.preferredAccountsFilter))
+ return false;
+ if (!Objects.equals(url, p.url))
+ return false;
+ if (authEnabled != p.authEnabled)
+ return false;
+ if (!Objects.equals(authUserName, p.authUserName))
+ return false;
+ if (!Objects.equals(authPassword, p.authPassword))
+ return false;
+ if (themeHue != p.themeHue)
+ return false;
+ if (apiVersion != p.apiVersion)
+ return false;
+ if (!Objects.equals(detectedVersion, p.detectedVersion))
+ return false;
+ return futureDates == p.futureDates;
+ }
+ public boolean getShowCommentsByDefault() {
+ return showCommentsByDefault;
+ }
+ public void setShowCommentsByDefault(boolean newValue) {
+ this.showCommentsByDefault = newValue;
+ }
+ public boolean getShowCommodityByDefault() {
+ return showCommodityByDefault;
+ }
+ public void setShowCommodityByDefault(boolean showCommodityByDefault) {
+ this.showCommodityByDefault = showCommodityByDefault;
+ }
+ public String getDefaultCommodity() {
+ return defaultCommodity;
+ }
+ public void setDefaultCommodity(String defaultCommodity) {
+ this.defaultCommodity = defaultCommodity;
+ }
+ public void setDefaultCommodity(CharSequence defaultCommodity) {
+ if (defaultCommodity == null)
+ this.defaultCommodity = null;
+ else
+ this.defaultCommodity = String.valueOf(defaultCommodity);
+ }
+ public API getApiVersion() {
+ return apiVersion;
+ }
+ public void setApiVersion(API apiVersion) {
+ this.apiVersion = apiVersion;
+ }
+ public void setApiVersion(int apiVersion) {
+ this.apiVersion = API.valueOf(apiVersion);
+ }
+ public FutureDates getFutureDates() {
+ return futureDates;
+ }
+ public void setFutureDates(int anInt) {
+ futureDates = FutureDates.valueOf(anInt);
+ }
+ public void setFutureDates(FutureDates futureDates) {
+ this.futureDates = futureDates;
+ }
+ public String getPreferredAccountsFilter() {
+ return preferredAccountsFilter;
+ }
+ public void setPreferredAccountsFilter(String preferredAccountsFilter) {
+ this.preferredAccountsFilter = preferredAccountsFilter;
+ }
+ public void setPreferredAccountsFilter(CharSequence preferredAccountsFilter) {
+ setPreferredAccountsFilter(String.valueOf(preferredAccountsFilter));
+ }
+ public boolean isPostingPermitted() {
+ return permitPosting;
+ }
+ public void setPostingPermitted(boolean permitPosting) {
+ this.permitPosting = permitPosting;
}
public String getUuid() {
return uuid;
@@ -134,21 +287,21 @@ public final class MobileLedgerProfile {
public String getName() {
return name;
}
- public void setName(String name) {
- this.name = name;
- }
public void setName(CharSequence text) {
setName(String.valueOf(text));
}
+ public void setName(String name) {
+ this.name = name;
+ }
public String getUrl() {
return url;
}
- public void setUrl(String url) {
- this.url = url;
- }
public void setUrl(CharSequence text) {
setUrl(String.valueOf(text));
}
+ public void setUrl(String url) {
+ this.url = url;
+ }
public boolean isAuthEnabled() {
return authEnabled;
}
@@ -158,29 +311,44 @@ public final class MobileLedgerProfile {
public String getAuthUserName() {
return authUserName;
}
- public void setAuthUserName(String authUserName) {
- this.authUserName = authUserName;
- }
public void setAuthUserName(CharSequence text) {
setAuthUserName(String.valueOf(text));
}
+ public void setAuthUserName(String authUserName) {
+ this.authUserName = authUserName;
+ }
public String getAuthPassword() {
return authPassword;
}
- public void setAuthPassword(String authPassword) {
- this.authPassword = authPassword;
- }
public void setAuthPassword(CharSequence text) {
setAuthPassword(String.valueOf(text));
}
+ public void setAuthPassword(String authPassword) {
+ this.authPassword = authPassword;
+ }
public void storeInDB() {
- SQLiteDatabase db = MLDB.getWritableDatabase();
- db.beginTransaction();
+ SQLiteDatabase db = App.getDatabase();
+ db.beginTransactionNonExclusive();
try {
- db.execSQL("REPLACE INTO profiles(uuid, name, url, use_authentication, auth_user, " +
- "auth_password) VALUES(?, ?, ?, ?, ?, ?)",
- new Object[]{uuid, name, url, authEnabled, authEnabled ? authUserName : null,
- authEnabled ? authPassword : null
+// debug("profiles", String.format("Storing profile in DB: uuid=%s, name=%s, " +
+// "url=%s, permit_posting=%s, authEnabled=%s, " +
+// "themeHue=%d", uuid, name, url,
+// permitPosting ? "TRUE" : "FALSE", authEnabled ? "TRUE" : "FALSE", themeHue));
+ db.execSQL("REPLACE INTO profiles(uuid, name, permit_posting, url, " +
+ "use_authentication, auth_user, auth_password, theme, order_no, " +
+ "preferred_accounts_filter, future_dates, api_version, " +
+ "show_commodity_by_default, default_commodity, show_comments_by_default," +
+ "detected_version_pre_1_19, detected_version_major, " +
+ "detected_version_minor) " +
+ "VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
+ new Object[]{uuid, name, permitPosting, url, authEnabled,
+ authEnabled ? authUserName : null,
+ authEnabled ? authPassword : null, themeHue, orderNo,
+ preferredAccountsFilter, futureDates.toInt(), apiVersion.toInt(),
+ showCommodityByDefault, defaultCommodity, showCommentsByDefault,
+ (detectedVersion != null) && detectedVersion.isPre_1_20_1(),
+ (detectedVersion == null) ? 0 : detectedVersion.getMajor(),
+ (detectedVersion == null) ? 0 : detectedVersion.getMinor()
});
db.setTransactionSuccessful();
}
@@ -188,50 +356,93 @@ public final class MobileLedgerProfile {
db.endTransaction();
}
}
- public void storeAccount(LedgerAccount acc) {
- SQLiteDatabase db = MLDB.getWritableDatabase();
-
+ public void storeAccount(SQLiteDatabase db, int generation, LedgerAccount acc,
+ boolean storeUiFields) {
// replace into is a bad idea because it would reset hidden to its default value
// we like the default, but for new accounts only
- db.execSQL("update accounts set level = ?, keep = 1 where profile=? and name = ?",
- new Object[]{acc.getLevel(), uuid, acc.getName()});
- db.execSQL("insert into accounts(profile, name, name_upper, parent_name, level) " +
- "select ?,?,?,?,? where (select changes() = 0)",
+ String sql = "update accounts set generation = ?";
+ List