fedilab-Android-App/app/src/main/java/app/fedilab/android/drawers/ConversationDecoration.java

117 lines
4.6 KiB
Java
Raw Normal View History

2019-05-18 11:10:30 +02:00
package app.fedilab.android.drawers;
2018-09-08 11:30:58 +02:00
/* Copyright 2018 Thomas Schneider
*
2019-05-18 11:10:30 +02:00
* This file is a part of Fedilab
2018-09-08 11:30:58 +02:00
*
* This program is free software; you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation; either version 3 of the
* License, or (at your option) any later version.
*
2019-05-18 11:10:30 +02:00
* Fedilab is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
2018-09-08 11:30:58 +02:00
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
2019-05-18 11:10:30 +02:00
* You should have received a copy of the GNU General Public License along with Fedilab; if not,
2018-09-08 11:30:58 +02:00
* see <http://www.gnu.org/licenses>. */
import android.content.Context;
import android.content.SharedPreferences;
2018-09-08 11:30:58 +02:00
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
2019-06-11 19:38:26 +02:00
import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;
2018-09-08 11:30:58 +02:00
import android.view.View;
2019-05-18 11:10:30 +02:00
import app.fedilab.android.client.Entities.Status;
import app.fedilab.android.helper.Helper;
import app.fedilab.android.R;
2018-09-08 11:30:58 +02:00
2019-05-18 11:10:30 +02:00
import static app.fedilab.android.drawers.StatusListAdapter.FOCUSED_STATUS;
2018-09-08 11:30:58 +02:00
/**
* Created by Thomas on 08/09/2018.
* Adapter for thread decoration
*/
public class ConversationDecoration extends RecyclerView.ItemDecoration{
private Drawable divider;
private Context context;
2019-02-13 18:36:29 +01:00
private boolean compactMode, consoleMode;
2019-02-13 18:36:29 +01:00
public ConversationDecoration(Context context, int theme){
SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
if( theme == Helper.THEME_BLACK)
divider = ContextCompat.getDrawable(context,R.drawable.line_divider_black);
else if(theme == Helper.THEME_DARK)
divider = ContextCompat.getDrawable(context,R.drawable.line_divider_dark);
else if(theme == Helper.THEME_LIGHT)
divider = ContextCompat.getDrawable(context,R.drawable.line_divider_light);
2018-09-08 11:30:58 +02:00
this.context = context;
2019-02-13 18:36:29 +01:00
this.compactMode = sharedpreferences.getBoolean(Helper.SET_COMPACT_MODE, false);
this.consoleMode = sharedpreferences.getBoolean(Helper.SET_CONSOLE_MODE, false);
2018-09-08 11:30:58 +02:00
}
@Override
2018-10-25 14:09:41 +02:00
public void onDraw(@NonNull Canvas canvas, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
int leftSide;
2019-02-13 18:36:29 +01:00
if( consoleMode)
leftSide = (int) Helper.convertDpToPixel(6, context);
else if( compactMode)
leftSide = (int) Helper.convertDpToPixel(3, context);
else
leftSide = (int) Helper.convertDpToPixel(28, context);
int left = parent.getPaddingLeft() + leftSide;
2018-10-25 14:09:41 +02:00
int right = left + (int)Helper.convertDpToPixel(4, context);
2018-09-08 11:30:58 +02:00
int childCount = parent.getChildCount();
2018-10-25 14:09:41 +02:00
int offSet = (int) Helper.convertDpToPixel(30, context);
2018-09-08 11:30:58 +02:00
for (int i = 0; i < childCount; i++) {
2018-10-25 14:09:41 +02:00
2018-09-08 11:30:58 +02:00
View child = parent.getChildAt(i);
2018-10-25 14:09:41 +02:00
StatusListAdapter adapter = (StatusListAdapter) parent.getAdapter();
2018-09-08 11:30:58 +02:00
int position = parent.getChildAdapterPosition(child);
2018-10-25 14:09:41 +02:00
2018-10-26 17:00:38 +02:00
assert adapter != null;
2018-09-08 11:30:58 +02:00
Status status = adapter.getItem(position);
2018-10-25 14:09:41 +02:00
2018-10-26 17:00:38 +02:00
int top, bottom;
2018-09-08 11:30:58 +02:00
if( status != null){
int itemViewType = status.getItemViewType();
2018-09-08 11:30:58 +02:00
Status statusBefore = null;
2018-11-22 17:53:09 +01:00
if( itemViewType != FOCUSED_STATUS || position == 0){
if( position > 0)
statusBefore = adapter.getItem(position - 1);
top = (statusBefore != null && statusBefore.getId().equals(status.getIn_reply_to_id()))?
child.getTop(): (child.getTop() + offSet);
Status statusAfter = null;
if( adapter.getItemCount() > position+1)
statusAfter = adapter.getItem(position + 1);
bottom = (statusAfter != null && status.getId().equals(statusAfter.getIn_reply_to_id()) )?
child.getBottom():child.getTop()+offSet;
if( position == 0 && childCount > 1)
top = bottom - (int)Helper.convertDpToPixel(14, context);
if( position == 0 && childCount <= 1 )
top = bottom;
}else{
top = child.getTop();
bottom = top + (int)Helper.convertDpToPixel(14, context);
}
2018-10-26 17:00:38 +02:00
divider.setBounds(left, top, right, bottom);
divider.draw(canvas);
2018-09-08 11:30:58 +02:00
}
2018-10-26 17:00:38 +02:00
2018-09-08 11:30:58 +02:00
}
2018-10-25 14:09:41 +02:00
2018-09-08 11:30:58 +02:00
}
}