]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/templates/TemplateListDivider.java
9860e80490946d3316bc2b4147ffcaab62a54f31
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / templates / TemplateListDivider.java
1 /*
2  * Copyright © 2021 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 //
19 // Substantial portions taken from DividerItemDecoration subject to the following license terms:
20 //
21 // Copyright 2018 The Android Open Source Project
22 //
23 // Licensed under the Apache License, Version 2.0 (the "License");
24 // you may not use this file except in compliance with the License.
25 // You may obtain a copy of the License at
26 //
27 //      http://www.apache.org/licenses/LICENSE-2.0
28 //
29 // Unless required by applicable law or agreed to in writing, software
30 // distributed under the License is distributed on an "AS IS" BASIS,
31 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
32 // See the License for the specific language governing permissions and
33 // limitations under the License.
34 //
35 package net.ktnx.mobileledger.ui.templates;
36
37 import android.content.Context;
38 import android.graphics.Canvas;
39 import android.graphics.Rect;
40 import android.graphics.drawable.Drawable;
41 import android.view.View;
42
43 import androidx.recyclerview.widget.DividerItemDecoration;
44 import androidx.recyclerview.widget.RecyclerView;
45
46 import java.util.Objects;
47
48 class TemplateListDivider extends DividerItemDecoration {
49     private final Rect mBounds = new Rect();
50     private int mOrientation;
51     public TemplateListDivider(Context context, int orientation) {
52         super(context, orientation);
53         mOrientation = orientation;
54     }
55     @Override
56     public void setOrientation(int orientation) {
57         super.setOrientation(orientation);
58         mOrientation = orientation;
59     }
60     @Override
61     public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
62         if (parent.getLayoutManager() == null || getDrawable() == null) {
63             return;
64         }
65         if (mOrientation == VERTICAL) {
66             drawVertical(c, parent);
67         }
68         else {
69             drawHorizontal(c, parent);
70         }
71     }
72
73     private void drawVertical(Canvas canvas, RecyclerView parent) {
74         canvas.save();
75         final int left;
76         final int right;
77         //noinspection AndroidLintNewApi - NewApi lint fails to handle overrides.
78         if (parent.getClipToPadding()) {
79             left = parent.getPaddingLeft();
80             right = parent.getWidth() - parent.getPaddingRight();
81             canvas.clipRect(left, parent.getPaddingTop(), right,
82                     parent.getHeight() - parent.getPaddingBottom());
83         }
84         else {
85             left = 0;
86             right = parent.getWidth();
87         }
88
89         final Drawable divider = Objects.requireNonNull(getDrawable());
90         final int childCount = parent.getChildCount();
91         final TemplatesRecyclerViewAdapter adapter =
92                 (TemplatesRecyclerViewAdapter) Objects.requireNonNull(parent.getAdapter());
93         final int itemCount = adapter.getItemCount();
94         for (int i = 0; i < childCount; i++) {
95             final View child = parent.getChildAt(i);
96             final int childAdapterPosition = parent.getChildAdapterPosition(child);
97             if (adapter.getItemViewType(childAdapterPosition) ==
98                 TemplatesRecyclerViewAdapter.ITEM_TYPE_DIVIDER ||
99                 childAdapterPosition + 1 < itemCount &&
100                 adapter.getItemViewType(childAdapterPosition + 1) ==
101                 TemplatesRecyclerViewAdapter.ITEM_TYPE_DIVIDER)
102                 continue;
103             parent.getDecoratedBoundsWithMargins(child, mBounds);
104             final int bottom = mBounds.bottom + Math.round(child.getTranslationY());
105             final int top = bottom - divider.getIntrinsicHeight();
106             divider.setBounds(left, top, right, bottom);
107             divider.draw(canvas);
108         }
109         canvas.restore();
110     }
111
112     private void drawHorizontal(Canvas canvas, RecyclerView parent) {
113         canvas.save();
114         final int top;
115         final int bottom;
116         //noinspection AndroidLintNewApi - NewApi lint fails to handle overrides.
117         if (parent.getClipToPadding()) {
118             top = parent.getPaddingTop();
119             bottom = parent.getHeight() - parent.getPaddingBottom();
120             canvas.clipRect(parent.getPaddingLeft(), top,
121                     parent.getWidth() - parent.getPaddingRight(), bottom);
122         }
123         else {
124             top = 0;
125             bottom = parent.getHeight();
126         }
127
128         final Drawable divider = Objects.requireNonNull(getDrawable());
129         final int childCount = parent.getChildCount();
130         final TemplatesRecyclerViewAdapter adapter =
131                 (TemplatesRecyclerViewAdapter) Objects.requireNonNull(parent.getAdapter());
132         final int itemCount = adapter.getItemCount();
133         for (int i = 0; i < childCount; i++) {
134             final View child = parent.getChildAt(i);
135             final int childAdapterPosition = parent.getChildAdapterPosition(child);
136             if (adapter.getItemViewType(childAdapterPosition) ==
137                 TemplatesRecyclerViewAdapter.ITEM_TYPE_DIVIDER ||
138                 childAdapterPosition + 1 < itemCount &&
139                 adapter.getItemViewType(childAdapterPosition + 1) ==
140                 TemplatesRecyclerViewAdapter.ITEM_TYPE_DIVIDER)
141                 continue;
142             parent.getLayoutManager()
143                   .getDecoratedBoundsWithMargins(child, mBounds);
144             final int right = mBounds.right + Math.round(child.getTranslationX());
145             final int left = right - divider.getIntrinsicWidth();
146             divider.setBounds(left, top, right, bottom);
147             divider.draw(canvas);
148         }
149         canvas.restore();
150     }
151     @Override
152     public void getItemOffsets(Rect outRect, View child, RecyclerView parent,
153                                RecyclerView.State state) {
154         final int childAdapterPosition = parent.getChildAdapterPosition(child);
155         final TemplatesRecyclerViewAdapter adapter =
156                 (TemplatesRecyclerViewAdapter) Objects.requireNonNull(parent.getAdapter());
157         final int itemCount = adapter.getItemCount();
158
159         if (adapter.getItemViewType(childAdapterPosition) ==
160             TemplatesRecyclerViewAdapter.ITEM_TYPE_DIVIDER ||
161             childAdapterPosition + 1 < itemCount &&
162             adapter.getItemViewType(childAdapterPosition + 1) ==
163             TemplatesRecyclerViewAdapter.ITEM_TYPE_DIVIDER)
164             return;
165
166         super.getItemOffsets(outRect, child, parent, state);
167     }
168 }