]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/templates/TemplateListDivider.java
more pronounced day/month delimiters in the transaction list
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / templates / TemplateListDivider.java
1 /*
2  * Copyright © 2022 Damyan Ivanov.
3  * This file is part of MoLe.
4  * MoLe is free software: you can distribute it and/or modify it
5  * under the term of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your opinion), any later version.
8  *
9  * MoLe is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License terms for details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with MoLe. If not, see <https://www.gnu.org/licenses/>.
16  */
17
18 package net.ktnx.mobileledger.ui.templates;
19
20 import android.content.Context;
21 import android.graphics.Canvas;
22 import android.graphics.Rect;
23 import android.graphics.drawable.Drawable;
24 import android.view.View;
25
26 import androidx.recyclerview.widget.DividerItemDecoration;
27 import androidx.recyclerview.widget.RecyclerView;
28
29 import java.util.Objects;
30
31 class TemplateListDivider extends DividerItemDecoration {
32     private final Rect mBounds = new Rect();
33     private int mOrientation;
34     public TemplateListDivider(Context context, int orientation) {
35         super(context, orientation);
36         mOrientation = orientation;
37     }
38     @Override
39     public void setOrientation(int orientation) {
40         super.setOrientation(orientation);
41         mOrientation = orientation;
42     }
43     @Override
44     public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
45         if (parent.getLayoutManager() == null || getDrawable() == null) {
46             return;
47         }
48         if (mOrientation == VERTICAL) {
49             drawVertical(c, parent);
50         }
51         else {
52             drawHorizontal(c, parent);
53         }
54     }
55
56     private void drawVertical(Canvas canvas, RecyclerView parent) {
57         canvas.save();
58         final int left;
59         final int right;
60         //noinspection AndroidLintNewApi - NewApi lint fails to handle overrides.
61         if (parent.getClipToPadding()) {
62             left = parent.getPaddingLeft();
63             right = parent.getWidth() - parent.getPaddingRight();
64             canvas.clipRect(left, parent.getPaddingTop(), right,
65                     parent.getHeight() - parent.getPaddingBottom());
66         }
67         else {
68             left = 0;
69             right = parent.getWidth();
70         }
71
72         final Drawable divider = Objects.requireNonNull(getDrawable());
73         final int childCount = parent.getChildCount();
74         final TemplatesRecyclerViewAdapter adapter =
75                 (TemplatesRecyclerViewAdapter) Objects.requireNonNull(parent.getAdapter());
76         final int itemCount = adapter.getItemCount();
77         for (int i = 0; i < childCount; i++) {
78             final View child = parent.getChildAt(i);
79             final int childAdapterPosition = parent.getChildAdapterPosition(child);
80             if (childAdapterPosition == RecyclerView.NO_POSITION ||
81                 adapter.getItemViewType(childAdapterPosition) ==
82                 TemplatesRecyclerViewAdapter.ITEM_TYPE_DIVIDER ||
83                 childAdapterPosition + 1 < itemCount &&
84                 adapter.getItemViewType(childAdapterPosition + 1) ==
85                 TemplatesRecyclerViewAdapter.ITEM_TYPE_DIVIDER)
86                 continue;
87             parent.getDecoratedBoundsWithMargins(child, mBounds);
88             final int bottom = mBounds.bottom + Math.round(child.getTranslationY());
89             final int top = bottom - divider.getIntrinsicHeight();
90             divider.setBounds(left, top, right, bottom);
91             divider.draw(canvas);
92         }
93         canvas.restore();
94     }
95
96     private void drawHorizontal(Canvas canvas, RecyclerView parent) {
97         final RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
98         if (layoutManager == null)
99             return;
100
101         canvas.save();
102         final int top;
103         final int bottom;
104         //noinspection AndroidLintNewApi - NewApi lint fails to handle overrides.
105         if (parent.getClipToPadding()) {
106             top = parent.getPaddingTop();
107             bottom = parent.getHeight() - parent.getPaddingBottom();
108             canvas.clipRect(parent.getPaddingLeft(), top,
109                     parent.getWidth() - parent.getPaddingRight(), bottom);
110         }
111         else {
112             top = 0;
113             bottom = parent.getHeight();
114         }
115
116         final Drawable divider = Objects.requireNonNull(getDrawable());
117         final int childCount = parent.getChildCount();
118         final TemplatesRecyclerViewAdapter adapter =
119                 (TemplatesRecyclerViewAdapter) Objects.requireNonNull(parent.getAdapter());
120         final int itemCount = adapter.getItemCount();
121         for (int i = 0; i < childCount; i++) {
122             final View child = parent.getChildAt(i);
123             final int childAdapterPosition = parent.getChildAdapterPosition(child);
124             if (childAdapterPosition == RecyclerView.NO_POSITION ||
125                 adapter.getItemViewType(childAdapterPosition) ==
126                 TemplatesRecyclerViewAdapter.ITEM_TYPE_DIVIDER ||
127                 childAdapterPosition + 1 < itemCount &&
128                 adapter.getItemViewType(childAdapterPosition + 1) ==
129                 TemplatesRecyclerViewAdapter.ITEM_TYPE_DIVIDER)
130             {
131                 continue;
132             }
133             layoutManager.getDecoratedBoundsWithMargins(child, mBounds);
134             final int right = mBounds.right + Math.round(child.getTranslationX());
135             final int left = right - divider.getIntrinsicWidth();
136             divider.setBounds(left, top, right, bottom);
137             divider.draw(canvas);
138         }
139         canvas.restore();
140     }
141     @Override
142     public void getItemOffsets(Rect outRect, View child, RecyclerView parent,
143                                RecyclerView.State state) {
144         final int childAdapterPosition = parent.getChildAdapterPosition(child);
145         final TemplatesRecyclerViewAdapter adapter =
146                 (TemplatesRecyclerViewAdapter) Objects.requireNonNull(parent.getAdapter());
147         final int itemCount = adapter.getItemCount();
148
149         if (childAdapterPosition == RecyclerView.NO_POSITION ||
150             adapter.getItemViewType(childAdapterPosition) ==
151             TemplatesRecyclerViewAdapter.ITEM_TYPE_DIVIDER ||
152             childAdapterPosition + 1 < itemCount &&
153             adapter.getItemViewType(childAdapterPosition + 1) ==
154             TemplatesRecyclerViewAdapter.ITEM_TYPE_DIVIDER)
155             return;
156
157         super.getItemOffsets(outRect, child, parent, state);
158     }
159 }