Use item decorator for date grouping

pull/153/head
M66B 6 years ago
parent 3445f79465
commit e770f00c58

@ -101,7 +101,6 @@ import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
@ -129,10 +128,6 @@ import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;
import static android.text.format.DateUtils.DAY_IN_MILLIS;
import static android.text.format.DateUtils.FORMAT_SHOW_DATE;
import static android.text.format.DateUtils.FORMAT_SHOW_WEEKDAY;
public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHolder> {
private Context context;
private LayoutInflater inflater;
@ -180,7 +175,6 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
public class ViewHolder extends RecyclerView.ViewHolder implements
View.OnClickListener, View.OnLongClickListener, BottomNavigationView.OnNavigationItemSelectedListener {
private View view;
private TextView tvDay;
private View vwColor;
private ImageView ivExpander;
private ImageView ivFlagged;
@ -252,7 +246,6 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
private RecyclerView rvImage;
private Group grpDay;
private Group grpHeaders;
private Group grpAttachments;
private Group grpExpanded;
@ -267,7 +260,6 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
super(itemView);
view = itemView.findViewById(R.id.clItem);
tvDay = itemView.findViewById(R.id.tvDay);
vwColor = itemView.findViewById(R.id.vwColor);
ivExpander = itemView.findViewById(R.id.ivExpander);
ivFlagged = itemView.findViewById(R.id.ivFlagged);
@ -353,7 +345,6 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
adapterImage = new AdapterImage(context, owner);
rvImage.setAdapter(adapterImage);
grpDay = itemView.findViewById(R.id.grpDay);
grpHeaders = itemView.findViewById(R.id.grpHeaders);
grpAttachments = itemView.findViewById(R.id.grpAttachments);
grpExpanded = itemView.findViewById(R.id.grpExpanded);
@ -367,16 +358,6 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
super.itemView.getBottom());
}
void setDisplacement(float dx) {
ViewGroup group = (ViewGroup) view;
for (int i = 0; i < group.getChildCount(); i++) {
View child = group.getChildAt(i);
int id = child.getId();
if (id != R.id.tvDay && id != R.id.vSeparatorDay)
child.setTranslationX(dx);
}
}
private void wire() {
final View touch = (viewType == ViewType.THREAD && threading ? ivExpander : vwColor);
touch.setOnClickListener(this);
@ -455,13 +436,10 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
pbLoading.setVisibility(View.VISIBLE);
clearExpanded();
grpDay.setVisibility(View.GONE);
}
@SuppressLint("WrongConstant")
private void bindTo(final TupleMessageEx message) {
setDisplacement(0);
pbLoading.setVisibility(View.GONE);
if (viewType == ViewType.THREAD)
@ -469,7 +447,6 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
// Text size
if (textSize != 0) {
tvDay.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
tvFrom.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
tvSubject.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize * 0.9f);
tvBody.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
@ -488,31 +465,6 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
}
}
// Date header
if (message.day) {
Calendar cal = Calendar.getInstance();
cal.setTime(new Date());
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
cal.add(Calendar.DAY_OF_MONTH, -2);
if (message.received <= cal.getTimeInMillis())
tvDay.setText(
DateUtils.formatDateRange(
context,
message.received,
message.received,
FORMAT_SHOW_WEEKDAY | FORMAT_SHOW_DATE));
else
tvDay.setText(
DateUtils.getRelativeTimeSpanString(
message.received,
new Date().getTime(),
DAY_IN_MILLIS, 0));
}
grpDay.setVisibility(message.day ? View.VISIBLE : View.GONE);
// Selected / disabled
view.setActivated(selectionTracker != null && selectionTracker.isSelected(message.id));
view.setAlpha(
@ -3040,28 +2992,6 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
}
void submitList(PagedList<TupleMessageEx> list) {
if (date && "time".equals(sort)) {
TupleMessageEx prev = null;
for (int i = 0; i < list.size(); i++) {
TupleMessageEx message = list.get(i);
if (message != null)
if (i == 0)
message.day = true;
else if (prev != null) {
Calendar cal0 = Calendar.getInstance();
Calendar cal1 = Calendar.getInstance();
cal0.setTimeInMillis(prev.received);
cal1.setTimeInMillis(message.received);
int year0 = cal0.get(Calendar.YEAR);
int year1 = cal1.get(Calendar.YEAR);
int day0 = cal0.get(Calendar.DAY_OF_YEAR);
int day1 = cal1.get(Calendar.DAY_OF_YEAR);
message.day = (year0 != year1 || day0 != day1);
}
prev = message;
}
}
differ.submitList(list);
}
@ -3086,6 +3016,10 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
}
}
int getZoom() {
return this.zoom;
}
void setSort(String sort) {
if (!sort.equals(this.sort)) {
this.sort = sort;
@ -3093,6 +3027,10 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
}
}
String getSort() {
return this.sort;
}
void setDuplicates(boolean duplicates) {
if (this.duplicates != duplicates) {
this.duplicates = duplicates;

@ -35,7 +35,6 @@ import javax.mail.Address;
import androidx.annotation.NonNull;
import androidx.room.Entity;
import androidx.room.ForeignKey;
import androidx.room.Ignore;
import androidx.room.Index;
import androidx.room.PrimaryKey;
@ -137,9 +136,6 @@ public class EntityMessage implements Serializable {
public String error; // volatile
public Long last_attempt; // send
@Ignore
boolean day = false;
static String generateMessageId() {
StringBuilder sb = new StringBuilder();
sb.append('<')
@ -229,9 +225,9 @@ public class EntityMessage implements Serializable {
this.ui_browsed.equals(other.ui_browsed) &&
Objects.equals(this.ui_snoozed, other.ui_snoozed) &&
Objects.equals(this.warning, other.warning) &&
Objects.equals(this.error, other.error) &&
Objects.equals(this.error, other.error)
// last_attempt
this.day == other.day);
);
}
return false;
}

@ -38,7 +38,9 @@ import android.os.Parcel;
import android.os.Parcelable;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.format.DateUtils;
import android.util.LongSparseArray;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
@ -62,8 +64,10 @@ import java.text.Collator;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
@ -96,6 +100,10 @@ import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import static android.text.format.DateUtils.DAY_IN_MILLIS;
import static android.text.format.DateUtils.FORMAT_SHOW_DATE;
import static android.text.format.DateUtils.FORMAT_SHOW_WEEKDAY;
public class FragmentMessages extends FragmentBase {
private ViewGroup view;
private SwipeRefreshLayout swipeRefresh;
@ -124,6 +132,7 @@ public class FragmentMessages extends FragmentBase {
private String search;
private boolean pane;
private boolean date;
private boolean threading;
private boolean pull;
private boolean actionbar;
@ -222,6 +231,7 @@ public class FragmentMessages extends FragmentBase {
else
pull = false;
date = prefs.getBoolean("date", true);
threading = prefs.getBoolean("threading", true);
actionbar = prefs.getBoolean("actionbar", true);
autoexpand = prefs.getBoolean("autoexpand", true);
@ -322,6 +332,93 @@ public class FragmentMessages extends FragmentBase {
itemDecorator.setDrawable(getContext().getDrawable(R.drawable.divider));
rvMessage.addItemDecoration(itemDecorator);
DividerItemDecoration dateDecorator = new DividerItemDecoration(getContext(), llm.getOrientation()) {
@Override
public void onDraw(Canvas canvas, RecyclerView parent, RecyclerView.State state) {
for (int i = 0; i < parent.getChildCount(); i++) {
View view = parent.getChildAt(i);
int pos = parent.getChildAdapterPosition(view);
View header = getView(parent, pos);
if (header != null) {
canvas.save();
canvas.translate(0, parent.getChildAt(i).getTop() - header.getMeasuredHeight());
header.draw(canvas);
canvas.restore();
}
}
}
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
int pos = parent.getChildAdapterPosition(view);
View header = getView(parent, pos);
if (header == null)
outRect.setEmpty();
else
outRect.top = header.getMeasuredHeight();
}
private View getView(RecyclerView parent, int pos) {
if (!date || !"time".equals(adapter.getSort()))
return null;
if (pos == RecyclerView.NO_POSITION)
return null;
TupleMessageEx prev = (pos > 0 ? adapter.getCurrentList().get(pos - 1) : null);
TupleMessageEx message = adapter.getCurrentList().get(pos);
if (pos > 0 && prev == null)
return null;
if (message == null)
return null;
if (pos > 0) {
Calendar cal0 = Calendar.getInstance();
Calendar cal1 = Calendar.getInstance();
cal0.setTimeInMillis(prev.received);
cal1.setTimeInMillis(message.received);
int year0 = cal0.get(Calendar.YEAR);
int year1 = cal1.get(Calendar.YEAR);
int day0 = cal0.get(Calendar.DAY_OF_YEAR);
int day1 = cal1.get(Calendar.DAY_OF_YEAR);
if (year0 == year1 && day0 == day1)
return null;
}
View header = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_message_date, parent, false);
TextView tvDate = header.findViewById(R.id.tvDate);
tvDate.setTextSize(TypedValue.COMPLEX_UNIT_PX, Helper.getTextSize(parent.getContext(), adapter.getZoom()));
Calendar cal = Calendar.getInstance();
cal.setTime(new Date());
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
cal.add(Calendar.DAY_OF_MONTH, -2);
if (message.received <= cal.getTimeInMillis())
tvDate.setText(
DateUtils.formatDateRange(
parent.getContext(),
message.received,
message.received,
FORMAT_SHOW_WEEKDAY | FORMAT_SHOW_DATE));
else
tvDate.setText(
DateUtils.getRelativeTimeSpanString(
message.received,
new Date().getTime(),
DAY_IN_MILLIS, 0));
header.measure(View.MeasureSpec.makeMeasureSpec(parent.getWidth(), View.MeasureSpec.EXACTLY),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
header.layout(0, 0, header.getMeasuredWidth(), header.getMeasuredHeight());
return header;
}
};
rvMessage.addItemDecoration(dateDecorator);
boolean compact = prefs.getBoolean("compact", false);
int zoom = prefs.getInt("zoom", compact ? 0 : 1);
String sort = prefs.getString("sort", "time");
@ -729,8 +826,7 @@ public class FragmentMessages extends FragmentBase {
public void onChildDraw(
@NonNull Canvas canvas, @NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder,
float dX, float dY, int actionState, boolean isCurrentlyActive) {
AdapterMessage.ViewHolder holder = ((AdapterMessage.ViewHolder) viewHolder);
holder.setDisplacement(dX);
super.onChildDraw(canvas, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
if (selectionPredicate != null) {
handler.removeCallbacks(enableSelection);
@ -748,6 +844,7 @@ public class FragmentMessages extends FragmentBase {
if (swipes == null)
return;
AdapterMessage.ViewHolder holder = ((AdapterMessage.ViewHolder) viewHolder);
Rect rect = holder.getItemRect();
int margin = Helper.dp2pixels(getContext(), 12);
int size = Helper.dp2pixels(getContext(), 24);

@ -13,29 +13,6 @@
android:focusable="true"
android:focusableInTouchMode="true">
<TextView
android:id="@+id/tvDay"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="?android:attr/windowBackground"
android:gravity="center_horizontal"
android:padding="6dp"
android:text="Today"
android:textAppearance="@style/TextAppearance.AppCompat.Small"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<View
android:id="@+id/vSeparatorDay"
android:layout_width="0dp"
android:layout_height="1dp"
android:background="?attr/colorSeparator"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tvDay" />
<View
android:id="@+id/vwColor"
android:layout_width="6dp"
@ -43,7 +20,7 @@
android:background="@color/colorAccent"
app:layout_constraintBottom_toTopOf="@+id/vSeparatorAddress"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/vSeparatorDay" />
app:layout_constraintTop_toTopOf="parent" />
<View
android:id="@+id/paddingTop"
@ -52,7 +29,7 @@
android:visibility="gone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/vSeparatorDay" />
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/ivExpander"
@ -304,7 +281,7 @@
app:layout_constraintBottom_toTopOf="@+id/vSeparatorAddress"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/vSeparatorDay" />
app:layout_constraintTop_toTopOf="parent" />
<View
android:id="@+id/vwRipple"
@ -314,7 +291,7 @@
app:layout_constraintBottom_toTopOf="@+id/vSeparatorAddress"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/vSeparatorDay" />
app:layout_constraintTop_toTopOf="parent" />
<View
android:id="@+id/vSeparatorAddress"
@ -581,12 +558,6 @@
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/vwBody" />
<androidx.constraintlayout.widget.Group
android:id="@+id/grpDay"
android:layout_width="0dp"
android:layout_height="0dp"
app:constraint_referenced_ids="tvDay,vSeparatorDay" />
<androidx.constraintlayout.widget.Group
android:id="@+id/grpHeaders"
android:layout_width="0dp"

@ -0,0 +1,29 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tvDate"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="?android:attr/windowBackground"
android:gravity="center_horizontal"
android:padding="6dp"
android:text="Today"
android:textAppearance="@style/TextAppearance.AppCompat.Small"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<View
android:id="@+id/vSeparatorDate"
android:layout_width="0dp"
android:layout_height="1dp"
android:background="?attr/colorSeparator"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tvDate" />
</androidx.constraintlayout.widget.ConstraintLayout>

@ -13,29 +13,6 @@
android:focusable="true"
android:focusableInTouchMode="true">
<TextView
android:id="@+id/tvDay"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="?android:attr/windowBackground"
android:gravity="center_horizontal"
android:padding="6dp"
android:text="Today"
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<View
android:id="@+id/vSeparatorDay"
android:layout_width="0dp"
android:layout_height="1dp"
android:background="?attr/colorSeparator"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tvDay" />
<View
android:id="@+id/vwColor"
android:layout_width="6dp"
@ -43,7 +20,7 @@
android:background="@color/colorAccent"
app:layout_constraintBottom_toTopOf="@+id/vSeparatorAddress"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/vSeparatorDay" />
app:layout_constraintTop_toTopOf="parent" />
<View
android:id="@+id/paddingTop"
@ -51,7 +28,7 @@
android:layout_height="3dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/vSeparatorDay" />
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/ivExpander"
@ -297,7 +274,7 @@
app:layout_constraintBottom_toTopOf="@+id/vSeparatorAddress"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/vSeparatorDay" />
app:layout_constraintTop_toTopOf="parent" />
<View
android:id="@+id/vwRipple"
@ -307,7 +284,7 @@
app:layout_constraintBottom_toTopOf="@+id/vSeparatorAddress"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/vSeparatorDay" />
app:layout_constraintTop_toTopOf="parent" />
<View
android:id="@+id/vSeparatorAddress"
@ -573,12 +550,6 @@
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/vwBody" />
<androidx.constraintlayout.widget.Group
android:id="@+id/grpDay"
android:layout_width="0dp"
android:layout_height="0dp"
app:constraint_referenced_ids="tvDay,vSeparatorDay" />
<androidx.constraintlayout.widget.Group
android:id="@+id/grpHeaders"
android:layout_width="0dp"

Loading…
Cancel
Save