From 05c2c408dabdda9e042744c5f4e4f126a7fb30d1 Mon Sep 17 00:00:00 2001 From: Damyan Ivanov Date: Wed, 13 May 2020 23:23:05 +0300 Subject: [PATCH] link show comments by default flag between the UI and the DB --- .../model/MobileLedgerProfile.java | 20 +++++++++++++++---- .../ui/activity/NewTransactionModel.java | 6 ++++-- .../ui/profiles/ProfileDetailFragment.java | 7 +++++++ .../ui/profiles/ProfileDetailModel.java | 11 ++++++++++ .../utils/MobileLedgerDatabase.java | 2 +- app/src/main/res/raw/create_db.sql | 2 +- app/src/main/res/raw/sql_31.sql | 1 + app/src/main/res/raw/sql_32.sql | 1 + 8 files changed, 42 insertions(+), 8 deletions(-) create mode 100644 app/src/main/res/raw/sql_31.sql create mode 100644 app/src/main/res/raw/sql_32.sql 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 bc2373e5..b31ddef3 100644 --- a/app/src/main/java/net/ktnx/mobileledger/model/MobileLedgerProfile.java +++ b/app/src/main/java/net/ktnx/mobileledger/model/MobileLedgerProfile.java @@ -43,9 +43,11 @@ import java.util.UUID; import static net.ktnx.mobileledger.utils.Logger.debug; public final class MobileLedgerProfile { + // N.B. when adding new fields, update the copy-constructor below private String uuid; private String name; private boolean permitPosting; + private boolean showCommentsByDefault; private boolean showCommodityByDefault; private String defaultCommodity; private String preferredAccountsFilter; @@ -55,6 +57,7 @@ public final class MobileLedgerProfile { private String authPassword; private int themeHue; private int orderNo = -1; + // N.B. when adding new fields, update the copy-constructor below private FutureDates futureDates = FutureDates.None; private SendTransactionTask.API apiVersion = SendTransactionTask.API.auto; public MobileLedgerProfile() { @@ -67,6 +70,7 @@ public final class MobileLedgerProfile { uuid = origin.uuid; name = origin.name; permitPosting = origin.permitPosting; + showCommentsByDefault = origin.showCommentsByDefault; showCommodityByDefault = origin.showCommodityByDefault; preferredAccountsFilter = origin.preferredAccountsFilter; url = origin.url; @@ -88,7 +92,8 @@ public final class MobileLedgerProfile { try (Cursor cursor = db.rawQuery("SELECT uuid, name, url, use_authentication, auth_user, " + "auth_password, permit_posting, theme, order_no, " + "preferred_accounts_filter, future_dates, api_version, " + - "show_commodity_by_default, default_commodity FROM " + + "show_commodity_by_default, default_commodity, " + + "show_comments_by_default FROM " + "profiles order by order_no", null)) { while (cursor.moveToNext()) { @@ -106,6 +111,7 @@ public final class MobileLedgerProfile { item.setApiVersion(cursor.getInt(11)); item.setShowCommodityByDefault(cursor.getInt(12) == 1); item.setDefaultCommodity(cursor.getString(13)); + item.setShowCommentsByDefault(cursor.getInt(14) == 1); list.add(item); if (item.getUuid() .equals(currentProfileUUID)) @@ -132,6 +138,12 @@ public final class MobileLedgerProfile { db.endTransaction(); } } + public boolean getShowCommentsByDefault() { + return showCommentsByDefault; + } + public void setShowCommentsByDefault(boolean newValue) { + this.showCommentsByDefault = newValue; + } public boolean getShowCommodityByDefault() { return showCommodityByDefault; } @@ -239,13 +251,13 @@ public final class MobileLedgerProfile { 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) " + - "VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", + "show_commodity_by_default, default_commodity, show_comments_by_default) " + + "VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", new Object[]{uuid, name, permitPosting, url, authEnabled, authEnabled ? authUserName : null, authEnabled ? authPassword : null, themeHue, orderNo, preferredAccountsFilter, futureDates.toInt(), apiVersion.toInt(), - showCommodityByDefault, defaultCommodity + showCommodityByDefault, defaultCommodity, showCommentsByDefault }); db.setTransactionSuccessful(); } diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionModel.java b/app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionModel.java index f5568081..1c69656a 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionModel.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionModel.java @@ -58,8 +58,10 @@ public class NewTransactionModel extends ViewModel { private final AtomicInteger busyCounter = new AtomicInteger(0); private final MutableLiveData busyFlag = new MutableLiveData<>(false); private boolean observingDataProfile; - private Observer profileObserver = - profile -> showCurrency.postValue(profile.getShowCommodityByDefault()); + private Observer profileObserver = profile -> { + showCurrency.postValue(profile.getShowCommodityByDefault()); + showComments.postValue(profile.getShowCommentsByDefault()); + }; void observeShowComments(LifecycleOwner owner, Observer observer) { showComments.observe(owner, observer); } diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfileDetailFragment.java b/app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfileDetailFragment.java index 522de247..f6323d51 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfileDetailFragment.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfileDetailFragment.java @@ -266,6 +266,13 @@ public class ProfileDetailFragment extends Fragment { postingPermitted.setOnCheckedChangeListener( ((buttonView, isChecked) -> model.setPostingPermitted(isChecked))); + Switch showCommentsByDefault = context.findViewById(R.id.profile_show_comments); + model.observeShowCommentsByDefault(viewLifecycleOwner, isChecked -> { + showCommentsByDefault.setChecked(isChecked); + }); + showCommentsByDefault.setOnCheckedChangeListener( + ((buttonView, isChecked) -> model.setShowCommentsByDefault(isChecked))); + defaultCommodity = context.findViewById(R.id.default_commodity_text); futureDatesLayout = context.findViewById(R.id.future_dates_layout); diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfileDetailModel.java b/app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfileDetailModel.java index e64c4bc1..2a655dcc 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfileDetailModel.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfileDetailModel.java @@ -35,6 +35,7 @@ public class ProfileDetailModel extends ViewModel { private final MutableLiveData futureDates = new MutableLiveData<>(MobileLedgerProfile.FutureDates.None); private final MutableLiveData showCommodityByDefault = new MutableLiveData<>(false); + private final MutableLiveData showCommentsByDefault = new MutableLiveData<>(true); private final MutableLiveData useAuthentication = new MutableLiveData<>(false); private final MutableLiveData apiVersion = new MutableLiveData<>(SendTransactionTask.API.auto); @@ -69,6 +70,13 @@ public class ProfileDetailModel extends ViewModel { void observePostingPermitted(LifecycleOwner lfo, Observer o) { postingPermitted.observe(lfo, o); } + public void setShowCommentsByDefault(boolean newValue) { + if (newValue != showCommentsByDefault.getValue()) + showCommentsByDefault.setValue(newValue); + } + void observeShowCommentsByDefault(LifecycleOwner lfo, Observer o) { + showCommentsByDefault.observe(lfo, o); + } MobileLedgerProfile.FutureDates getFutureDates() { return futureDates.getValue(); } @@ -189,6 +197,7 @@ public class ProfileDetailModel extends ViewModel { if (mProfile != null) { profileName.setValue(mProfile.getName()); postingPermitted.setValue(mProfile.isPostingPermitted()); + showCommentsByDefault.setValue(mProfile.getShowCommentsByDefault()); showCommodityByDefault.setValue(mProfile.getShowCommodityByDefault()); { String comm = mProfile.getDefaultCommodity(); @@ -210,6 +219,7 @@ public class ProfileDetailModel extends ViewModel { profileName.setValue(null); url.setValue(HTTPS_URL_START); postingPermitted.setValue(true); + showCommentsByDefault.setValue(true); showCommodityByDefault.setValue(false); setFutureDates(MobileLedgerProfile.FutureDates.None); setApiVersion(SendTransactionTask.API.auto); @@ -226,6 +236,7 @@ public class ProfileDetailModel extends ViewModel { mProfile.setName(profileName.getValue()); mProfile.setUrl(url.getValue()); mProfile.setPostingPermitted(postingPermitted.getValue()); + mProfile.setShowCommentsByDefault(showCommentsByDefault.getValue()); Currency c = defaultCommodity.getValue(); mProfile.setDefaultCommodity((c == null) ? null : c.getName()); mProfile.setShowCommodityByDefault(showCommodityByDefault.getValue()); diff --git a/app/src/main/java/net/ktnx/mobileledger/utils/MobileLedgerDatabase.java b/app/src/main/java/net/ktnx/mobileledger/utils/MobileLedgerDatabase.java index 77e306db..85eacb02 100644 --- a/app/src/main/java/net/ktnx/mobileledger/utils/MobileLedgerDatabase.java +++ b/app/src/main/java/net/ktnx/mobileledger/utils/MobileLedgerDatabase.java @@ -34,7 +34,7 @@ import static net.ktnx.mobileledger.utils.Logger.debug; public class MobileLedgerDatabase extends SQLiteOpenHelper { private static final String DB_NAME = "MoLe.db"; - private static final int LATEST_REVISION = 30; + private static final int LATEST_REVISION = 32; private static final String CREATE_DB_SQL = "create_db"; private final Application mContext; diff --git a/app/src/main/res/raw/create_db.sql b/app/src/main/res/raw/create_db.sql index 50cf2fdb..583e6fc1 100644 --- a/app/src/main/res/raw/create_db.sql +++ b/app/src/main/res/raw/create_db.sql @@ -5,7 +5,7 @@ create unique index un_options on options(profile,name); create table account_values(profile varchar not null, account varchar not null, currency varchar not null default '', keep boolean, value decimal not null ); create unique index un_account_values on account_values(profile,account,currency); create table description_history(description varchar not null primary key, keep boolean, description_upper varchar); -create table profiles(uuid varchar not null primary key, name not null, url not null, use_authentication boolean not null, auth_user varchar, auth_password varchar, order_no integer, permit_posting boolean default 0, theme integer default -1, preferred_accounts_filter varchar, future_dates integer, api_version integer, show_commodity_by_default boolean default 0, default_commodity varchar); +create table profiles(uuid varchar not null primary key, name not null, url not null, use_authentication boolean not null, auth_user varchar, auth_password varchar, order_no integer, permit_posting boolean default 0, theme integer default -1, preferred_accounts_filter varchar, future_dates integer, api_version integer, show_commodity_by_default boolean default 0, default_commodity varchar, show_comments_by_default boolean default 1); create table transactions(profile varchar not null, id integer not null, data_hash varchar not null, date varchar not null, description varchar not null, keep boolean not null default 0); create unique index un_transactions_id on transactions(profile,id); create unique index un_transactions_data_hash on transactions(profile,data_hash); diff --git a/app/src/main/res/raw/sql_31.sql b/app/src/main/res/raw/sql_31.sql new file mode 100644 index 00000000..0f907e75 --- /dev/null +++ b/app/src/main/res/raw/sql_31.sql @@ -0,0 +1 @@ +alter table profiles add show_comments_by_default boolean default 0; \ No newline at end of file diff --git a/app/src/main/res/raw/sql_32.sql b/app/src/main/res/raw/sql_32.sql new file mode 100644 index 00000000..b8586440 --- /dev/null +++ b/app/src/main/res/raw/sql_32.sql @@ -0,0 +1 @@ +update profiles set show_comments_by_default = 1; \ No newline at end of file -- 2.39.5