22
33import android .content .Context ;
44import android .util .Log ;
5+ import android .view .LayoutInflater ;
56import android .view .View ;
67import android .view .ViewGroup ;
8+ import android .widget .TextView ;
79
810import androidx .annotation .NonNull ;
911import androidx .annotation .Nullable ;
1012import androidx .recyclerview .widget .GridLayoutManager ;
1113import androidx .recyclerview .widget .RecyclerView ;
1214
15+ import org .schabi .newpipe .R ;
1316import org .schabi .newpipe .database .LocalItem ;
17+ import org .schabi .newpipe .database .history .model .DateHeaderItem ;
1418import org .schabi .newpipe .database .stream .model .StreamStateEntity ;
1519import org .schabi .newpipe .info_list .ItemViewMode ;
1620import org .schabi .newpipe .local .history .HistoryRecordManager ;
@@ -66,6 +70,8 @@ public class LocalItemListAdapter extends RecyclerView.Adapter<RecyclerView.View
6670 private static final int HEADER_TYPE = 0 ;
6771 private static final int FOOTER_TYPE = 1 ;
6872
73+ private static final int DATE_HEADER_HOLDER_TYPE = 0x4000 ;
74+
6975 private static final int STREAM_STATISTICS_HOLDER_TYPE = 0x1000 ;
7076 private static final int STREAM_PLAYLIST_HOLDER_TYPE = 0x1001 ;
7177 private static final int STREAM_STATISTICS_GRID_HOLDER_TYPE = 0x1002 ;
@@ -311,6 +317,8 @@ public int getItemViewType(int position) {
311317 } else {
312318 return STREAM_STATISTICS_HOLDER_TYPE ;
313319 }
320+ case DATE_HEADER :
321+ return DATE_HEADER_HOLDER_TYPE ;
314322 default :
315323 Log .e (TAG , "No holder type has been considered for item: ["
316324 + item .getLocalItemType () + "]" );
@@ -331,6 +339,9 @@ public RecyclerView.ViewHolder onCreateViewHolder(@NonNull final ViewGroup paren
331339 return new HeaderFooterHolder (headerSupplier .get ());
332340 case FOOTER_TYPE :
333341 return new HeaderFooterHolder (footer );
342+ case DATE_HEADER_HOLDER_TYPE :
343+ return new DateHeaderHolder (LayoutInflater .from (parent .getContext ())
344+ .inflate (R .layout .list_date_header_item , parent , false ));
334345 case LOCAL_PLAYLIST_HOLDER_TYPE :
335346 return new LocalPlaylistItemHolder (localItemBuilder , parent );
336347 case LOCAL_PLAYLIST_GRID_HOLDER_TYPE :
@@ -374,7 +385,18 @@ public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder holder, int
374385 + "position = [" + position + "]" );
375386 }
376387
377- if (holder instanceof LocalItemHolder ) {
388+ if (holder instanceof DateHeaderHolder ) {
389+ // If header isn't null, offset the items by -1
390+ if (hasHeader ()) {
391+ position --;
392+ }
393+
394+ final LocalItem localItem = localItems .get (position );
395+ if (localItem instanceof DateHeaderItem ) {
396+ ((DateHeaderHolder ) holder ).dateHeaderText .setText (
397+ dateTimeFormatter .format (((DateHeaderItem ) localItem ).getDate ()));
398+ }
399+ } else if (holder instanceof LocalItemHolder ) {
378400 // If header isn't null, offset the items by -1
379401 if (hasHeader ()) {
380402 position --;
@@ -408,12 +430,23 @@ public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder holder, fina
408430 }
409431 }
410432
433+ private static class DateHeaderHolder extends RecyclerView .ViewHolder {
434+ private final TextView dateHeaderText ;
435+
436+ DateHeaderHolder (@ NonNull final View itemView ) {
437+ super (itemView );
438+ dateHeaderText = itemView .findViewById (R .id .dateHeaderText );
439+ }
440+ }
441+
442+
411443 public GridLayoutManager .SpanSizeLookup getSpanSizeLookup (final int spanCount ) {
412444 return new GridLayoutManager .SpanSizeLookup () {
413445 @ Override
414446 public int getSpanSize (final int position ) {
415447 final int type = getItemViewType (position );
416- return type == HEADER_TYPE || type == FOOTER_TYPE ? spanCount : 1 ;
448+ return type == HEADER_TYPE || type == FOOTER_TYPE
449+ || type == DATE_HEADER_HOLDER_TYPE ? spanCount : 1 ;
417450 }
418451 };
419452 }
0 commit comments