Support for sticky date headers

Closes #4696
// FREEBIE
This commit is contained in:
Moxie Marlinspike
2017-01-23 16:44:38 -08:00
parent b677370597
commit 4c815db076
12 changed files with 320 additions and 193 deletions

View File

@@ -18,6 +18,7 @@ package org.thoughtcrime.securesms.util;
import android.content.Context;
import android.os.Build;
import android.support.annotation.NonNull;
import android.text.format.DateFormat;
import java.text.SimpleDateFormat;
@@ -37,6 +38,10 @@ public class DateUtils extends android.text.format.DateUtils {
return System.currentTimeMillis() - millis <= unit.toMillis(span);
}
private static boolean isYesterday(final long when) {
return DateUtils.isToday(when + TimeUnit.DAYS.toMillis(1));
}
private static int convertDelta(final long millis, TimeUnit to) {
return (int) to.convert(System.currentTimeMillis() - millis, TimeUnit.MILLISECONDS);
}
@@ -111,6 +116,19 @@ public class DateUtils extends android.text.format.DateUtils {
return new SimpleDateFormat(dateFormatPattern, locale);
}
public static String getRelativeDate(@NonNull Context context,
@NonNull Locale locale,
long timestamp)
{
if (isToday(timestamp)) {
return context.getString(R.string.DateUtils_today);
} else if (isYesterday(timestamp)) {
return context.getString(R.string.DateUtils_yesterday);
} else {
return getFormattedDateTime(timestamp, "EEE, MMM d, yyyy", locale);
}
}
private static String getLocalizedPattern(String template, Locale locale) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
return DateFormat.getBestDateTimePattern(locale, template);

View File

@@ -0,0 +1,201 @@
package org.thoughtcrime.securesms.util;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.os.Build.VERSION;
import android.support.v4.view.ViewCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.ViewHolder;
import android.view.View;
import android.view.ViewGroup;
import java.util.HashMap;
import java.util.Map;
/**
* A sticky header decoration for android's RecyclerView.
* Currently only supports LinearLayoutManager in VERTICAL orientation.
*/
public class StickyHeaderDecoration extends RecyclerView.ItemDecoration {
private final Map<Long, ViewHolder> headerCache;
private final StickyHeaderAdapter adapter;
private final boolean renderInline;
/**
* @param adapter the sticky header adapter to use
*/
public StickyHeaderDecoration(StickyHeaderAdapter adapter, boolean renderInline) {
this.adapter = adapter;
this.headerCache = new HashMap<>();
this.renderInline = renderInline;
}
/**
* {@inheritDoc}
*/
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent,
RecyclerView.State state)
{
int position = parent.getChildAdapterPosition(view);
int headerHeight = 0;
if (position != RecyclerView.NO_POSITION && hasHeader(parent, position)) {
View header = getHeader(parent, position).itemView;
headerHeight = getHeaderHeightForLayout(header);
}
outRect.set(0, headerHeight, 0, 0);
}
private boolean hasHeader(RecyclerView parent, int adapterPos) {
boolean isReverse = isReverseLayout(parent);
if (isReverse && adapterPos == parent.getAdapter().getItemCount() - 1 ||
!isReverse && adapterPos == 0) {
return true;
}
int previous = adapterPos + (isReverse ? 1 : -1);
return adapter.getHeaderId(adapterPos) != adapter.getHeaderId(previous);
}
private ViewHolder getHeader(RecyclerView parent, int position) {
final long key = adapter.getHeaderId(position);
if (headerCache.containsKey(key)) {
return headerCache.get(key);
} else {
final ViewHolder holder = adapter.onCreateHeaderViewHolder(parent);
final View header = holder.itemView;
//noinspection unchecked
adapter.onBindHeaderViewHolder(holder, position);
int widthSpec = View.MeasureSpec.makeMeasureSpec(parent.getWidth(), View.MeasureSpec.EXACTLY);
int heightSpec = View.MeasureSpec.makeMeasureSpec(parent.getHeight(), View.MeasureSpec.UNSPECIFIED);
int childWidth = ViewGroup.getChildMeasureSpec(widthSpec,
parent.getPaddingLeft() + parent.getPaddingRight(), header.getLayoutParams().width);
int childHeight = ViewGroup.getChildMeasureSpec(heightSpec,
parent.getPaddingTop() + parent.getPaddingBottom(), header.getLayoutParams().height);
header.measure(childWidth, childHeight);
header.layout(0, 0, header.getMeasuredWidth(), header.getMeasuredHeight());
headerCache.put(key, holder);
return holder;
}
}
/**
* {@inheritDoc}
*/
@Override
public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
final int count = parent.getChildCount();
for (int layoutPos = 0; layoutPos < count; layoutPos++) {
final View child = parent.getChildAt(translatedChildPosition(parent, layoutPos));
final int adapterPos = parent.getChildAdapterPosition(child);
if (adapterPos != RecyclerView.NO_POSITION && (layoutPos == 0 || hasHeader(parent, adapterPos))) {
View header = getHeader(parent, adapterPos).itemView;
c.save();
final int left = child.getLeft();
final int top = getHeaderTop(parent, child, header, adapterPos, layoutPos);
c.translate(left, top);
header.draw(c);
c.restore();
}
}
}
private int getHeaderTop(RecyclerView parent, View child, View header, int adapterPos,
int layoutPos)
{
int headerHeight = getHeaderHeightForLayout(header);
int top = getChildY(parent, child) - headerHeight;
if (layoutPos == 0) {
final int count = parent.getChildCount();
final long currentId = adapter.getHeaderId(adapterPos);
// find next view with header and compute the offscreen push if needed
for (int i = 1; i < count; i++) {
int adapterPosHere = parent.getChildAdapterPosition(parent.getChildAt(translatedChildPosition(parent, i)));
if (adapterPosHere != RecyclerView.NO_POSITION) {
long nextId = adapter.getHeaderId(adapterPosHere);
if (nextId != currentId) {
final View next = parent.getChildAt(translatedChildPosition(parent, i));
final int offset = getChildY(parent, next) - (headerHeight + getHeader(parent, adapterPosHere).itemView.getHeight());
if (offset < 0) {
return offset;
} else {
break;
}
}
}
}
top = Math.max(0, top);
}
return top;
}
private int translatedChildPosition(RecyclerView parent, int position) {
return isReverseLayout(parent) ? parent.getChildCount() - 1 - position : position;
}
private int getChildY(RecyclerView parent, View child) {
if (VERSION.SDK_INT < 11) {
Rect rect = new Rect();
parent.getChildVisibleRect(child, rect, null);
return rect.top;
} else {
return (int)ViewCompat.getY(child);
}
}
private int getHeaderHeightForLayout(View header) {
return renderInline ? 0 : header.getHeight();
}
private boolean isReverseLayout(final RecyclerView parent) {
return (parent.getLayoutManager() instanceof LinearLayoutManager) &&
((LinearLayoutManager)parent.getLayoutManager()).getReverseLayout();
}
/**
* The adapter to assist the {@link StickyHeaderDecoration} in creating and binding the header views.
*
* @param <T> the header view holder
*/
public interface StickyHeaderAdapter<T extends ViewHolder> {
/**
* Returns the header id for the item at the given position.
*
* @param position the item position
* @return the header id
*/
long getHeaderId(int position);
/**
* Creates a new header ViewHolder.
*
* @param parent the header's view parent
* @return a view holder for the created view
*/
T onCreateHeaderViewHolder(ViewGroup parent);
/**
* Updates the header view to reflect the header data for the given position
* @param viewHolder the header view holder
* @param position the header's item position
*/
void onBindHeaderViewHolder(T viewHolder, int position);
}
}