]> git.ktnx.net Git - mobile-ledger.git/commitdiff
link show comments by default flag between the UI and the DB
authorDamyan Ivanov <dam+mobileledger@ktnx.net>
Wed, 13 May 2020 20:23:05 +0000 (23:23 +0300)
committerDamyan Ivanov <dam+mobileledger@ktnx.net>
Thu, 14 May 2020 17:39:53 +0000 (17:39 +0000)
app/src/main/java/net/ktnx/mobileledger/model/MobileLedgerProfile.java
app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionModel.java
app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfileDetailFragment.java
app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfileDetailModel.java
app/src/main/java/net/ktnx/mobileledger/utils/MobileLedgerDatabase.java
app/src/main/res/raw/create_db.sql
app/src/main/res/raw/sql_31.sql [new file with mode: 0644]
app/src/main/res/raw/sql_32.sql [new file with mode: 0644]

index bc2373e5f2f2f1bf59b99a33b92a80a79ba7d397..b31ddef396741d50971c2716de52a6e4ab4a5d81 100644 (file)
@@ -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();
         }
index f5568081c6a88ba98e74f2a142c9864655a68334..1c69656a10e630abc6ad0196e050ce953cff1067 100644 (file)
@@ -58,8 +58,10 @@ public class NewTransactionModel extends ViewModel {
     private final AtomicInteger busyCounter = new AtomicInteger(0);
     private final MutableLiveData<Boolean> busyFlag = new MutableLiveData<>(false);
     private boolean observingDataProfile;
-    private Observer<MobileLedgerProfile> profileObserver =
-            profile -> showCurrency.postValue(profile.getShowCommodityByDefault());
+    private Observer<MobileLedgerProfile> profileObserver = profile -> {
+        showCurrency.postValue(profile.getShowCommodityByDefault());
+        showComments.postValue(profile.getShowCommentsByDefault());
+    };
     void observeShowComments(LifecycleOwner owner, Observer<? super Boolean> observer) {
         showComments.observe(owner, observer);
     }
index 522de24787b3374e8cbf9d18b79aa79cce2047a0..f6323d5134571b2443d2c6e5700b28faec202e30 100644 (file)
@@ -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);
index e64c4bc1dc13c8a19caee33efab7eef631aeaf7d..2a655dccd224b11d537ab24c9a4d89c0bded23b2 100644 (file)
@@ -35,6 +35,7 @@ public class ProfileDetailModel extends ViewModel {
     private final MutableLiveData<MobileLedgerProfile.FutureDates> futureDates =
             new MutableLiveData<>(MobileLedgerProfile.FutureDates.None);
     private final MutableLiveData<Boolean> showCommodityByDefault = new MutableLiveData<>(false);
+    private final MutableLiveData<Boolean> showCommentsByDefault = new MutableLiveData<>(true);
     private final MutableLiveData<Boolean> useAuthentication = new MutableLiveData<>(false);
     private final MutableLiveData<SendTransactionTask.API> apiVersion =
             new MutableLiveData<>(SendTransactionTask.API.auto);
@@ -69,6 +70,13 @@ public class ProfileDetailModel extends ViewModel {
     void observePostingPermitted(LifecycleOwner lfo, Observer<Boolean> o) {
         postingPermitted.observe(lfo, o);
     }
+    public void setShowCommentsByDefault(boolean newValue) {
+        if (newValue != showCommentsByDefault.getValue())
+            showCommentsByDefault.setValue(newValue);
+    }
+    void observeShowCommentsByDefault(LifecycleOwner lfo, Observer<Boolean> 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());
index 77e306db9ff9d95758b30f35288fc8fb27d5f3be..85eacb025c3a2a848ba11674a90c96eeb6412ee6 100644 (file)
@@ -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;
index 50cf2fdb867e115f9a2c21c16869ba050629fe5f..583e6fc1af0c66f79bd876d4efb0f981970039b7 100644 (file)
@@ -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 (file)
index 0000000..0f907e7
--- /dev/null
@@ -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 (file)
index 0000000..b858644
--- /dev/null
@@ -0,0 +1 @@
+update profiles set show_comments_by_default = 1;
\ No newline at end of file