]> git.ktnx.net Git - mobile-ledger.git/commitdiff
UI for setting default profile comodity
authorDamyan Ivanov <dam+mobileledger@ktnx.net>
Fri, 17 Apr 2020 19:17:41 +0000 (22:17 +0300)
committerDamyan Ivanov <dam+mobileledger@ktnx.net>
Fri, 17 Apr 2020 19:17:41 +0000 (22:17 +0300)
app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfileDetailFragment.java
app/src/main/java/net/ktnx/mobileledger/utils/Misc.java
app/src/main/res/layout/profile_detail.xml
app/src/main/res/values-bg/strings.xml
app/src/main/res/values/strings.xml

index bed7527ebf82063e61009624a1699ea355c092f0..0ca2028a85c8af8fc4e6b272c67084a0e58eb7ca 100644 (file)
@@ -19,6 +19,7 @@ package net.ktnx.mobileledger.ui.profiles;
 
 import android.app.Activity;
 import android.app.AlertDialog;
+import android.graphics.Typeface;
 import android.os.Bundle;
 import android.text.Editable;
 import android.text.TextWatcher;
@@ -35,6 +36,7 @@ import android.widget.TextView;
 
 import androidx.annotation.NonNull;
 import androidx.annotation.Nullable;
+import androidx.appcompat.app.AppCompatActivity;
 import androidx.fragment.app.Fragment;
 import androidx.fragment.app.FragmentActivity;
 
@@ -45,11 +47,15 @@ import com.google.android.material.textfield.TextInputLayout;
 import net.ktnx.mobileledger.BuildConfig;
 import net.ktnx.mobileledger.R;
 import net.ktnx.mobileledger.async.SendTransactionTask;
+import net.ktnx.mobileledger.model.Currency;
 import net.ktnx.mobileledger.model.Data;
 import net.ktnx.mobileledger.model.MobileLedgerProfile;
+import net.ktnx.mobileledger.ui.CurrencySelectorFragment;
 import net.ktnx.mobileledger.ui.HueRingDialog;
+import net.ktnx.mobileledger.ui.OnCurrencySelectedListener;
 import net.ktnx.mobileledger.ui.activity.ProfileDetailActivity;
 import net.ktnx.mobileledger.utils.Colors;
+import net.ktnx.mobileledger.utils.Misc;
 
 import org.jetbrains.annotations.NonNls;
 import org.jetbrains.annotations.NotNull;
@@ -66,7 +72,8 @@ import static net.ktnx.mobileledger.utils.Logger.debug;
  * a {@link ProfileDetailActivity}
  * on handsets.
  */
-public class ProfileDetailFragment extends Fragment implements HueRingDialog.HueSelectedListener {
+public class ProfileDetailFragment extends Fragment
+        implements HueRingDialog.HueSelectedListener, OnCurrencySelectedListener {
     /**
      * The fragment argument representing the item ID that this fragment
      * represents.
@@ -77,12 +84,14 @@ public class ProfileDetailFragment extends Fragment implements HueRingDialog.Hue
     private static final String HTTPS_URL_START = "https://";
 
     /**
-     * The dummy content this fragment is presenting.
+     * The content this fragment is presenting.
      */
     private MobileLedgerProfile mProfile;
     private TextView url;
     private Switch postingPermitted;
     private Switch showCommodityByDefault;
+    private TextView defaultCommodity;
+    private boolean defaultCommoditySet;
     private TextInputLayout urlLayout;
     private LinearLayout authParams;
     private Switch useAuthentication;
@@ -205,6 +214,7 @@ public class ProfileDetailFragment extends Fragment implements HueRingDialog.Hue
         urlLayout = context.findViewById(R.id.url_layout);
         postingPermitted = context.findViewById(R.id.profile_permit_posting);
         showCommodityByDefault = context.findViewById(R.id.profile_show_commodity);
+        defaultCommodity = context.findViewById(R.id.default_commodity_text);
         futureDatesLayout = context.findViewById(R.id.future_dates_layout);
         futureDatesText = context.findViewById(R.id.future_dates_text);
         context.findViewById(R.id.future_dates_layout)
@@ -307,6 +317,13 @@ public class ProfileDetailFragment extends Fragment implements HueRingDialog.Hue
             profileName.setText(mProfile.getName());
             postingPermitted.setChecked(mProfile.isPostingPermitted());
             showCommodityByDefault.setChecked(mProfile.getShowCommodityByDefault());
+            {
+                String comm = mProfile.getDefaultCommodity();
+                if (Misc.isEmptyOrNull(comm))
+                    resetDefaultCommodity();
+                else
+                    setDefaultCommodity(comm);
+            }
             futureDates = mProfile.getFutureDates();
             futureDatesText.setText(futureDates.getText(getResources()));
             apiVersion = mProfile.getApiVersion();
@@ -324,6 +341,7 @@ public class ProfileDetailFragment extends Fragment implements HueRingDialog.Hue
             url.setText(HTTPS_URL_START);
             postingPermitted.setChecked(true);
             showCommodityByDefault.setChecked(false);
+            resetDefaultCommodity();
             futureDates = MobileLedgerProfile.FutureDates.None;
             futureDatesText.setText(futureDates.getText(getResources()));
             apiVersion = SendTransactionTask.API.auto;
@@ -366,6 +384,12 @@ public class ProfileDetailFragment extends Fragment implements HueRingDialog.Hue
             d.setColorSelectedListener(this);
         });
 
+        context.findViewById(R.id.default_commodity_layout).setOnClickListener(v -> {
+            CurrencySelectorFragment cpf = CurrencySelectorFragment.newInstance(CurrencySelectorFragment.DEFAULT_COLUMN_COUNT, false);
+            cpf.setOnCurrencySelectedListener(this);
+            final AppCompatActivity activity = (AppCompatActivity) v.getContext();
+            cpf.show(activity.getSupportFragmentManager(), "currency-selector");
+        });
         profileName.requestFocus();
     }
     private void onSaveFabClicked() {
@@ -404,6 +428,7 @@ public class ProfileDetailFragment extends Fragment implements HueRingDialog.Hue
         mProfile.setName(profileName.getText());
         mProfile.setUrl(url.getText());
         mProfile.setPostingPermitted(postingPermitted.isChecked());
+        mProfile.setDefaultCommodity(defaultCommoditySet ? defaultCommodity.getText() : null);
         mProfile.setShowCommodityByDefault(showCommodityByDefault.isChecked());
         mProfile.setPreferredAccountsFilter(preferredAccountsFilter.getText());
         mProfile.setAuthEnabled(useAuthentication.isChecked());
@@ -521,4 +546,21 @@ public class ProfileDetailFragment extends Fragment implements HueRingDialog.Hue
         huePickerView.setBackgroundColor(Colors.getPrimaryColorForHue(hue));
         huePickerView.setTag(hue);
     }
+    @Override
+    public void onCurrencySelected(Currency item) {
+        if (item == null)
+            resetDefaultCommodity();
+        else
+            setDefaultCommodity(item.getName());
+    }
+    private void resetDefaultCommodity() {
+        defaultCommoditySet = false;
+        defaultCommodity.setText(R.string.btn_no_currency);
+        defaultCommodity.setTypeface(defaultCommodity.getTypeface(), Typeface.ITALIC);
+    }
+    private void setDefaultCommodity(@NonNull @NotNull String name) {
+        defaultCommoditySet = true;
+        defaultCommodity.setText(name);
+        defaultCommodity.setTypeface(defaultCommodity.getTypeface(), Typeface.BOLD);
+    }
 }
index d333d4796c3c5dc85f1756134e8227709c451135..4443f2303dfe3eff7aee5e779cd2bcbfd8c03589 100644 (file)
@@ -62,4 +62,8 @@ public class Misc {
     public static String nullIsEmpty(String str) {
         return (str == null) ? "" : str;
     }
+    public static boolean isEmptyOrNull (String str) {
+        if (str == null) return true;
+        return str.isEmpty();
+    }
 }
index fd39b41d3e4b2d81895a2303e3238e89faf8cfbc..f640f3e65b1234e1218235ae795486b56191f00b 100644 (file)
             android:text="@string/posting_permitted"
             android:textAppearance="?android:textAppearanceListItem" />
 
+        <LinearLayout
+            android:id="@+id/default_commodity_layout"
+            android:orientation="vertical"
+            android:layout_width="match_parent"
+            android:layout_height="wrap_content"
+            android:clickable="true"
+            android:focusable="true"
+            android:layout_marginBottom="16dp">
+
+            <TextView
+                android:layout_width="match_parent"
+                android:layout_height="wrap_content"
+                android:text="@string/profile_default_commodity"
+                android:textAppearance="?android:textAppearanceListItem" />
+
+            <TextView
+                android:id="@+id/default_commodity_text"
+                android:layout_width="match_parent"
+                android:layout_height="wrap_content"
+                android:textAppearance="?android:textAppearanceListItemSecondary"
+                android:textColor="?attr/textColor"
+                android:text="@string/btn_no_currency"/>
+        </LinearLayout>
+
         <Switch
             android:id="@+id/profile_show_commodity"
             android:layout_width="match_parent"
index 382698008a52a48653debeffee0404369f68ae43..7a0e92ceb4f0b64faec6a56787bbfc39e0735e4e 100644 (file)
     <string name="new_currency_name_hint">валута/ценност</string>
     <string name="show_currency_input">Валута</string>
     <string name="currency_input_by_default">Показване по подразбиране на полето за валута</string>
+    <string name="profile_default_commodity">Валута по подразбиране</string>
 
 </resources>
index 6a4a42a87590f55a5f3374011ae0f58c26e2ca8b..a26763078996edf3998b9fa58d6296dfcd46c15f 100644 (file)
     <string name="currency_has_gap">Offset from the value</string>
     <string name="show_currency_input">Currency</string>
     <string name="currency_input_by_default">Commodity input visible by default</string>
+    <string name="profile_default_commodity">Default commodity</string>
 </resources>