fix profile color selector
authorDamyan Ivanov <dam+mobileledger@ktnx.net>
Thu, 28 Feb 2019 23:20:02 +0000 (01:20 +0200)
committerDamyan Ivanov <dam+mobileledger@ktnx.net>
Thu, 28 Feb 2019 23:20:02 +0000 (01:20 +0200)
app/src/main/java/net/ktnx/mobileledger/ui/profiles/MoLeColoredTextView.java [new file with mode: 0644]
app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfileDetailFragment.java
app/src/main/res/layout/color_selector_item.xml

diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/profiles/MoLeColoredTextView.java b/app/src/main/java/net/ktnx/mobileledger/ui/profiles/MoLeColoredTextView.java
new file mode 100644 (file)
index 0000000..cfd78b0
--- /dev/null
@@ -0,0 +1,44 @@
+/*
+ * Copyright © 2019 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.
+ *
+ *  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 <https://www.gnu.org/licenses/>.
+ */
+
+package net.ktnx.mobileledger.ui.profiles;
+
+import android.content.Context;
+import android.util.AttributeSet;
+
+import net.ktnx.mobileledger.utils.Colors;
+
+import androidx.appcompat.widget.AppCompatTextView;
+
+public class MoLeColoredTextView extends AppCompatTextView {
+    public MoLeColoredTextView(Context context) {
+        super(context);
+    }
+    public MoLeColoredTextView(Context context, AttributeSet attrs) {
+        super(context, attrs);
+    }
+    public MoLeColoredTextView(Context context, AttributeSet attrs, int defStyleAttr) {
+        super(context, attrs, defStyleAttr);
+    }
+    @Override
+    public void setText(CharSequence text, BufferType type) {
+        super.setText(text, type);
+        int deg = (text == null || text.equals("")) ? -1 : Integer.valueOf(String.valueOf(text));
+        if (deg == -1) deg = Colors.DEFAULT_HUE_DEG;
+        setBackgroundColor(Colors.getPrimaryColorForHue(deg));
+    }
+}
index 0f307e0..59f27a7 100644 (file)
@@ -19,7 +19,7 @@ package net.ktnx.mobileledger.ui.profiles;
 
 import android.app.Activity;
 import android.content.Context;
-import android.os.Build;
+import android.graphics.drawable.ColorDrawable;
 import android.os.Bundle;
 import android.text.Editable;
 import android.text.TextWatcher;
@@ -47,8 +47,6 @@ import net.ktnx.mobileledger.model.MobileLedgerProfile;
 import net.ktnx.mobileledger.ui.activity.ProfileDetailActivity;
 import net.ktnx.mobileledger.utils.Colors;
 
-import org.jetbrains.annotations.NotNull;
-
 import java.util.List;
 
 import androidx.annotation.ColorInt;
@@ -194,11 +192,12 @@ public class ProfileDetailFragment extends Fragment {
         passwordLayout = rootView.findViewById(R.id.password_layout);
         colorSpinner = rootView.findViewById(R.id.colorSpinner);
 
-        ArrayAdapter<CharSequence> adapter = ColorListAdapter
-                .createFromResource(rootView.getContext(), R.array.profile_colors,
-                        R.layout.color_selector_item);
-//        Log.d("profiles", String.format("color count: %s", adapter.getCount()));
-        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
+        ColorListAdapter adapter =
+                new ColorListAdapter(rootView.getContext(), R.layout.color_selector_item);
+        adapter.add(-1);
+        for (int i = 0; i < 360; i += 15) adapter.add(i);
+        Log.d("profiles", String.format("color count: %s", adapter.getCount()));
+//        adapter.setDropDownViewResource(R.layout.color_selector_item);
         colorSpinner.setAdapter(adapter);
         colorSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
             @Override
@@ -208,17 +207,9 @@ public class ProfileDetailFragment extends Fragment {
             @Override
             public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                 final int primaryColor;
-                final int degrees =
-                        Integer.valueOf((String) (parent.getAdapter().getItem(position)));
+                final int degrees = (Integer) parent.getAdapter().getItem(position);
                 if (degrees < 0) {
-                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
-                        if (getActivity() != null) primaryColor = getResources()
-                                .getColor(R.color.colorPrimary, getActivity().getTheme());
-                        else primaryColor = Colors.getPrimaryColorForHue(Colors.DEFAULT_HUE_DEG);
-                    }
-                    else {
-                        primaryColor = getResources().getColor(R.color.colorPrimary);
-                    }
+                    primaryColor = Colors.getPrimaryColorForHue(Colors.DEFAULT_HUE_DEG);
                 }
                 else primaryColor = Colors.getPrimaryColorForHue(degrees);
 
@@ -332,41 +323,42 @@ public class ProfileDetailFragment extends Fragment {
 
         return valid;
     }
-    private class ColorListAdapter extends ArrayAdapter<String> {
+    private class ColorListAdapter extends ArrayAdapter<Integer> {
         public ColorListAdapter(@NonNull Context context, int resource) {
             super(context, resource);
         }
         public ColorListAdapter(@NonNull Context context, int resource, int textViewResourceId) {
             super(context, resource, textViewResourceId);
         }
-        public ColorListAdapter(@NonNull Context context, int resource, @NonNull String[] objects) {
+        public ColorListAdapter(@NonNull Context context, int resource,
+                                @NonNull Integer[] objects) {
             super(context, resource, objects);
         }
         public ColorListAdapter(@NonNull Context context, int resource, int textViewResourceId,
-                                @NonNull String[] objects) {
+                                @NonNull Integer[] objects) {
             super(context, resource, textViewResourceId, objects);
         }
         public ColorListAdapter(@NonNull Context context, int resource,
-                                @NonNull List<String> objects) {
+                                @NonNull List<Integer> objects) {
             super(context, resource, objects);
         }
         public ColorListAdapter(@NonNull Context context, int resource, int textViewResourceId,
-                                @NonNull List<String> objects) {
+                                @NonNull List<Integer> objects) {
             super(context, resource, textViewResourceId, objects);
         }
-        @NotNull
         @Override
-        public View getView(int position, View convertView, @NotNull ViewGroup parent) {
-            String hueStr = getItem(position);
-            int hue = (hueStr == null) ? -1 : Integer.valueOf(hueStr);
+        public @NonNull
+        View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
+            Integer hueDeg = getItem(position);
+            int hue = ((hueDeg == null) || (hueDeg == -1)) ? Colors.DEFAULT_HUE_DEG : hueDeg;
             @ColorInt int primaryColor = Colors.getPrimaryColorForHue(hue);
 
             View view = convertView;
             if (convertView == null) {
-                view = getLayoutInflater().inflate(R.layout.color_selector_item, parent);
+                view = getLayoutInflater().inflate(R.layout.color_selector_item, null);
             }
 
-            view.setBackgroundColor(primaryColor);
+            view.setBackground(new ColorDrawable(primaryColor));
             return view;
         }
     }
index 66b95d1..fa14dff 100644 (file)
@@ -1,6 +1,5 @@
 <?xml version="1.0" encoding="utf-8"?>
-<TextView xmlns:android="http://schemas.android.com/apk/res/android"
+<net.ktnx.mobileledger.ui.profiles.MoLeColoredTextView xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent" android:layout_height="80dp"
     android:gravity="center_vertical|center_horizontal"
-    android:textColor="@android:color/transparent">
-</TextView>
\ No newline at end of file
+    android:textColor="@android:color/transparent"></net.ktnx.mobileledger.ui.profiles.MoLeColoredTextView>
\ No newline at end of file