]> git.ktnx.net Git - mobile-ledger.git/commitdiff
visual improvements in template list
authorDamyan Ivanov <dam+mobileledger@ktnx.net>
Sun, 9 May 2021 17:19:54 +0000 (20:19 +0300)
committerDamyan Ivanov <dam+mobileledger@ktnx.net>
Sun, 9 May 2021 18:32:54 +0000 (21:32 +0300)
add a divider between items, similarly to the account list

app/src/main/java/net/ktnx/mobileledger/ui/templates/TemplateListDivider.java [new file with mode: 0644]
app/src/main/java/net/ktnx/mobileledger/ui/templates/TemplateListFragment.java
app/src/main/java/net/ktnx/mobileledger/ui/templates/TemplatesRecyclerViewAdapter.java
app/src/main/res/layout/account_list_row.xml
app/src/main/res/layout/templates_fallback_divider.xml

diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/templates/TemplateListDivider.java b/app/src/main/java/net/ktnx/mobileledger/ui/templates/TemplateListDivider.java
new file mode 100644 (file)
index 0000000..9860e80
--- /dev/null
@@ -0,0 +1,168 @@
+/*
+ * 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.
+ *
+ * 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 MoLe. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+//
+// Substantial portions taken from DividerItemDecoration subject to the following license terms:
+//
+// Copyright 2018 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+package net.ktnx.mobileledger.ui.templates;
+
+import android.content.Context;
+import android.graphics.Canvas;
+import android.graphics.Rect;
+import android.graphics.drawable.Drawable;
+import android.view.View;
+
+import androidx.recyclerview.widget.DividerItemDecoration;
+import androidx.recyclerview.widget.RecyclerView;
+
+import java.util.Objects;
+
+class TemplateListDivider extends DividerItemDecoration {
+    private final Rect mBounds = new Rect();
+    private int mOrientation;
+    public TemplateListDivider(Context context, int orientation) {
+        super(context, orientation);
+        mOrientation = orientation;
+    }
+    @Override
+    public void setOrientation(int orientation) {
+        super.setOrientation(orientation);
+        mOrientation = orientation;
+    }
+    @Override
+    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
+        if (parent.getLayoutManager() == null || getDrawable() == null) {
+            return;
+        }
+        if (mOrientation == VERTICAL) {
+            drawVertical(c, parent);
+        }
+        else {
+            drawHorizontal(c, parent);
+        }
+    }
+
+    private void drawVertical(Canvas canvas, RecyclerView parent) {
+        canvas.save();
+        final int left;
+        final int right;
+        //noinspection AndroidLintNewApi - NewApi lint fails to handle overrides.
+        if (parent.getClipToPadding()) {
+            left = parent.getPaddingLeft();
+            right = parent.getWidth() - parent.getPaddingRight();
+            canvas.clipRect(left, parent.getPaddingTop(), right,
+                    parent.getHeight() - parent.getPaddingBottom());
+        }
+        else {
+            left = 0;
+            right = parent.getWidth();
+        }
+
+        final Drawable divider = Objects.requireNonNull(getDrawable());
+        final int childCount = parent.getChildCount();
+        final TemplatesRecyclerViewAdapter adapter =
+                (TemplatesRecyclerViewAdapter) Objects.requireNonNull(parent.getAdapter());
+        final int itemCount = adapter.getItemCount();
+        for (int i = 0; i < childCount; i++) {
+            final View child = parent.getChildAt(i);
+            final int childAdapterPosition = parent.getChildAdapterPosition(child);
+            if (adapter.getItemViewType(childAdapterPosition) ==
+                TemplatesRecyclerViewAdapter.ITEM_TYPE_DIVIDER ||
+                childAdapterPosition + 1 < itemCount &&
+                adapter.getItemViewType(childAdapterPosition + 1) ==
+                TemplatesRecyclerViewAdapter.ITEM_TYPE_DIVIDER)
+                continue;
+            parent.getDecoratedBoundsWithMargins(child, mBounds);
+            final int bottom = mBounds.bottom + Math.round(child.getTranslationY());
+            final int top = bottom - divider.getIntrinsicHeight();
+            divider.setBounds(left, top, right, bottom);
+            divider.draw(canvas);
+        }
+        canvas.restore();
+    }
+
+    private void drawHorizontal(Canvas canvas, RecyclerView parent) {
+        canvas.save();
+        final int top;
+        final int bottom;
+        //noinspection AndroidLintNewApi - NewApi lint fails to handle overrides.
+        if (parent.getClipToPadding()) {
+            top = parent.getPaddingTop();
+            bottom = parent.getHeight() - parent.getPaddingBottom();
+            canvas.clipRect(parent.getPaddingLeft(), top,
+                    parent.getWidth() - parent.getPaddingRight(), bottom);
+        }
+        else {
+            top = 0;
+            bottom = parent.getHeight();
+        }
+
+        final Drawable divider = Objects.requireNonNull(getDrawable());
+        final int childCount = parent.getChildCount();
+        final TemplatesRecyclerViewAdapter adapter =
+                (TemplatesRecyclerViewAdapter) Objects.requireNonNull(parent.getAdapter());
+        final int itemCount = adapter.getItemCount();
+        for (int i = 0; i < childCount; i++) {
+            final View child = parent.getChildAt(i);
+            final int childAdapterPosition = parent.getChildAdapterPosition(child);
+            if (adapter.getItemViewType(childAdapterPosition) ==
+                TemplatesRecyclerViewAdapter.ITEM_TYPE_DIVIDER ||
+                childAdapterPosition + 1 < itemCount &&
+                adapter.getItemViewType(childAdapterPosition + 1) ==
+                TemplatesRecyclerViewAdapter.ITEM_TYPE_DIVIDER)
+                continue;
+            parent.getLayoutManager()
+                  .getDecoratedBoundsWithMargins(child, mBounds);
+            final int right = mBounds.right + Math.round(child.getTranslationX());
+            final int left = right - divider.getIntrinsicWidth();
+            divider.setBounds(left, top, right, bottom);
+            divider.draw(canvas);
+        }
+        canvas.restore();
+    }
+    @Override
+    public void getItemOffsets(Rect outRect, View child, RecyclerView parent,
+                               RecyclerView.State state) {
+        final int childAdapterPosition = parent.getChildAdapterPosition(child);
+        final TemplatesRecyclerViewAdapter adapter =
+                (TemplatesRecyclerViewAdapter) Objects.requireNonNull(parent.getAdapter());
+        final int itemCount = adapter.getItemCount();
+
+        if (adapter.getItemViewType(childAdapterPosition) ==
+            TemplatesRecyclerViewAdapter.ITEM_TYPE_DIVIDER ||
+            childAdapterPosition + 1 < itemCount &&
+            adapter.getItemViewType(childAdapterPosition + 1) ==
+            TemplatesRecyclerViewAdapter.ITEM_TYPE_DIVIDER)
+            return;
+
+        super.getItemOffsets(outRect, child, parent, state);
+    }
+}
index eb291194890d518be7871493379d9f3c9188c055..53eb604f3df6497cca2ccc5182918b98bab0bfca 100644 (file)
@@ -33,6 +33,7 @@ import androidx.lifecycle.Lifecycle;
 import androidx.lifecycle.LifecycleEventObserver;
 import androidx.lifecycle.LifecycleOwner;
 import androidx.lifecycle.LiveData;
+import androidx.recyclerview.widget.DividerItemDecoration;
 import androidx.recyclerview.widget.LinearLayoutManager;
 import androidx.recyclerview.widget.RecyclerView;
 
@@ -97,6 +98,10 @@ public class TemplateListFragment extends Fragment {
                              Bundle savedInstanceState) {
         Logger.debug("flow", "PatternListFragment.onCreateView()");
         b = FragmentTemplateListBinding.inflate(inflater);
+        FragmentActivity activity = requireActivity();
+
+        if (activity instanceof FabManager.FabHandler)
+            FabManager.handle((FabManager.FabHandler) activity, b.templateList);
 
         TemplatesRecyclerViewAdapter modelAdapter = new TemplatesRecyclerViewAdapter();
 
@@ -108,10 +113,9 @@ public class TemplateListFragment extends Fragment {
         LinearLayoutManager llm = new LinearLayoutManager(getContext());
         llm.setOrientation(RecyclerView.VERTICAL);
         b.templateList.setLayoutManager(llm);
-
-        FragmentActivity activity = requireActivity();
-        if (activity instanceof FabManager.FabHandler)
-            FabManager.handle((FabManager.FabHandler) activity, b.templateList);
+        DividerItemDecoration did =
+                new TemplateListDivider(activity, DividerItemDecoration.VERTICAL);
+        b.templateList.addItemDecoration(did);
 
         return b.getRoot();
     }
index 320adc576589f542a9c9c20b0d2c677504a9b229..0b1c827b478391f976b225fc9c58302c6fe34955 100644 (file)
@@ -35,8 +35,8 @@ import java.util.ArrayList;
 import java.util.List;
 
 public class TemplatesRecyclerViewAdapter extends RecyclerView.Adapter<BaseTemplateViewHolder> {
-    private static final int ITEM_TYPE_TEMPLATE = 1;
-    private static final int ITEM_TYPE_DIVIDER = 2;
+    static final int ITEM_TYPE_TEMPLATE = 1;
+    static final int ITEM_TYPE_DIVIDER = 2;
     private final AsyncListDiffer<BaseTemplateItem> listDiffer;
 
     public TemplatesRecyclerViewAdapter() {
index 083eb5abe740ecdd15f07c74f949ba1f07ff325a..185faf800d46390ac1bd95966ebc9d52a9dd5713 100644 (file)
             android:gravity="center_vertical"
             android:longClickable="true"
             android:paddingStart="8dp"
-            android:text="AccountName"
+            android:text="Example AccountName That Is Too Long And Has to Be Wrapped On More Than One Line Words Words Words"
             android:textAppearance="@android:style/TextAppearance.Material.Medium"
             app:layout_constrainedWidth="true"
             app:layout_constraintBottom_toBottomOf="parent"
             app:layout_constraintEnd_toStartOf="@id/account_expander_container"
-            app:layout_constraintHorizontal_bias="0.0"
-            app:layout_constraintHorizontal_chainStyle="packed"
             app:layout_constraintStart_toStartOf="parent"
             app:layout_constraintTop_toTopOf="parent"
             tools:ignore="HardcodedText"
index 0276db96b0f9ee9213bf0f30f964852590c528ca..bac6bd209651bec12e272e10f30ca947230503a2 100644 (file)
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     >
-    <TextView
-        android:id="@+id/top_text"
-        android:layout_width="wrap_content"
-        android:layout_height="wrap_content"
-        android:text=" "
-        app:layout_constraintStart_toStartOf="parent"
-        app:layout_constraintTop_toTopOf="parent"
-        />
     <TextView
         android:id="@+id/label"
         android:layout_width="0dp"
         android:layout_height="wrap_content"
-        android:layout_marginEnd="@dimen/text_margin"
-        android:gravity="end"
+        android:background="?table_row_light_bg"
+        android:gravity="center_vertical|end"
+        android:minHeight="@dimen/thumb_row_height"
+        android:paddingHorizontal="@dimen/text_margin"
+        android:paddingVertical="@dimen/half_text_margin"
         android:text="@string/fallback_templates_divider"
+        android:textAppearance="@android:style/TextAppearance.Material.Widget.Toolbar.Title"
         android:textStyle="italic"
-        android:textAppearance="@android:style/TextAppearance.Small"
-        app:layout_constraintEnd_toEndOf="parent"
-        app:layout_constraintTop_toBottomOf="@id/divider"
-        />
-    <View
-        android:id="@+id/divider"
-        android:layout_width="0dp"
-        android:layout_height="1dp"
-        android:layout_marginHorizontal="@dimen/text_margin"
-        android:background="?colorPrimary"
+        android:textColor="?textColor"
         app:layout_constraintEnd_toEndOf="parent"
         app:layout_constraintStart_toStartOf="parent"
-        app:layout_constraintTop_toBottomOf="@id/top_text"
+        app:layout_constraintTop_toTopOf="parent"
         />
 </androidx.constraintlayout.widget.ConstraintLayout>
\ No newline at end of file