Showing feed results in search

This commit is contained in:
ByteHamster 2020-03-17 13:45:34 +01:00
parent 4f0de071ec
commit 8d2a188eda
12 changed files with 169 additions and 22 deletions

View File

@ -34,6 +34,7 @@ public class EpisodeItemListAdapter extends RecyclerView.Adapter<EpisodeItemView
public EpisodeItemListAdapter(MainActivity mainActivity) {
super();
this.mainActivityRef = new WeakReference<>(mainActivity);
setHasStableIds(true);
}
public void updateItems(List<FeedItem> items) {

View File

@ -0,0 +1,76 @@
package de.danoeh.antennapod.adapter;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import de.danoeh.antennapod.R;
import de.danoeh.antennapod.activity.MainActivity;
import de.danoeh.antennapod.core.feed.Feed;
import de.danoeh.antennapod.fragment.FeedItemlistFragment;
import de.danoeh.antennapod.view.SquareImageView;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;
public class FeedSearchResultAdapter extends RecyclerView.Adapter<FeedSearchResultAdapter.Holder> {
private final WeakReference<MainActivity> mainActivityRef;
private final List<Feed> data = new ArrayList<>();
public FeedSearchResultAdapter(MainActivity mainActivity) {
this.mainActivityRef = new WeakReference<>(mainActivity);
}
public void updateData(List<Feed> newData) {
data.clear();
data.addAll(newData);
notifyDataSetChanged();
}
@NonNull
@Override
public Holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View convertView = View.inflate(mainActivityRef.get(), R.layout.searchlist_item_feed, null);
return new Holder(convertView);
}
@Override
public void onBindViewHolder(@NonNull Holder holder, int position) {
final Feed podcast = data.get(position);
holder.imageView.setContentDescription(podcast.getTitle());
holder.imageView.setOnClickListener(v ->
mainActivityRef.get().loadChildFragment(FeedItemlistFragment.newInstance(podcast.getId())));
Glide.with(mainActivityRef.get())
.load(podcast.getImageUrl())
.apply(new RequestOptions()
.placeholder(R.color.light_gray)
.fitCenter()
.dontAnimate())
.into(holder.imageView);
}
@Override
public long getItemId(int position) {
return data.get(position).getId();
}
@Override
public int getItemCount() {
return data.size();
}
static class Holder extends RecyclerView.ViewHolder {
SquareImageView imageView;
public Holder(@NonNull View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.discovery_cover);
imageView.setDirection(SquareImageView.DIRECTION_HEIGHT);
}
}
}

View File

@ -344,7 +344,6 @@ public abstract class EpisodesListFragment extends Fragment {
private void createRecycleAdapter(RecyclerView recyclerView, EmptyViewHandler emptyViewHandler) {
MainActivity mainActivity = (MainActivity) getActivity();
listAdapter = new EpisodeItemListAdapter(mainActivity);
listAdapter.setHasStableIds(true);
listAdapter.updateItems(episodes);
recyclerView.setAdapter(listAdapter);
emptyViewHandler.updateAdapter(listAdapter);

View File

@ -586,7 +586,6 @@ public class QueueFragment extends Fragment {
if (recyclerAdapter == null) {
MainActivity activity = (MainActivity) getActivity();
recyclerAdapter = new QueueRecyclerAdapter(activity, itemTouchHelper);
recyclerAdapter.setHasStableIds(true);
recyclerView.setAdapter(recyclerAdapter);
emptyView.updateAdapter(recyclerAdapter);
}

View File

@ -3,6 +3,7 @@ package de.danoeh.antennapod.fragment;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.util.Pair;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
@ -21,12 +22,14 @@ import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration;
import de.danoeh.antennapod.R;
import de.danoeh.antennapod.activity.MainActivity;
import de.danoeh.antennapod.adapter.EpisodeItemListAdapter;
import de.danoeh.antennapod.adapter.FeedSearchResultAdapter;
import de.danoeh.antennapod.core.event.DownloadEvent;
import de.danoeh.antennapod.core.event.DownloaderUpdate;
import de.danoeh.antennapod.core.event.FeedItemEvent;
import de.danoeh.antennapod.core.event.PlaybackPositionEvent;
import de.danoeh.antennapod.core.event.PlayerStatusEvent;
import de.danoeh.antennapod.core.event.UnreadItemsUpdateEvent;
import de.danoeh.antennapod.core.feed.Feed;
import de.danoeh.antennapod.core.feed.FeedItem;
import de.danoeh.antennapod.core.storage.FeedSearcher;
import de.danoeh.antennapod.core.util.FeedItemUtil;
@ -52,10 +55,12 @@ public class SearchFragment extends Fragment {
private static final String ARG_FEED = "feed";
private EpisodeItemListAdapter adapter;
private FeedSearchResultAdapter adapterFeeds;
private Disposable disposable;
private ProgressBar progressBar;
private EmptyViewHandler emptyViewHandler;
private RecyclerView recyclerView;
private RecyclerView recyclerViewFeeds;
private List<FeedItem> results;
/**
@ -110,6 +115,7 @@ public class SearchFragment extends Fragment {
View layout = inflater.inflate(R.layout.search_fragment, container, false);
((AppCompatActivity) getActivity()).setSupportActionBar(layout.findViewById(R.id.toolbar));
progressBar = layout.findViewById(R.id.progressBar);
recyclerView = layout.findViewById(R.id.recyclerView);
LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(layoutManager);
@ -119,6 +125,14 @@ public class SearchFragment extends Fragment {
adapter = new EpisodeItemListAdapter((MainActivity) getActivity());
recyclerView.setAdapter(adapter);
recyclerViewFeeds = layout.findViewById(R.id.recyclerViewFeeds);
LinearLayoutManager layoutManagerFeeds = new LinearLayoutManager(getActivity());
layoutManagerFeeds.setOrientation(RecyclerView.HORIZONTAL);
recyclerViewFeeds.setLayoutManager(layoutManagerFeeds);
recyclerViewFeeds.setHasFixedSize(true);
adapterFeeds = new FeedSearchResultAdapter((MainActivity) getActivity());
recyclerViewFeeds.setAdapter(adapterFeeds);
emptyViewHandler = new EmptyViewHandler(getContext());
emptyViewHandler.attachToRecyclerView(recyclerView);
emptyViewHandler.setIcon(R.attr.action_search);
@ -249,19 +263,20 @@ public class SearchFragment extends Fragment {
.observeOn(AndroidSchedulers.mainThread())
.subscribe(results -> {
progressBar.setVisibility(View.GONE);
this.results = results;
adapter.updateItems(results);
this.results = results.first;
adapter.updateItems(results.first);
adapterFeeds.updateData(results.second);
String query = getArguments().getString(ARG_QUERY);
emptyViewHandler.setMessage(getString(R.string.no_results_for_query, query));
}, error -> Log.e(TAG, Log.getStackTraceString(error)));
}
@NonNull
private List<FeedItem> performSearch() {
Bundle args = getArguments();
String query = args.getString(ARG_QUERY);
long feed = args.getLong(ARG_FEED);
Context context = getActivity();
return FeedSearcher.searchFeedItems(context, query, feed);
private Pair<List<FeedItem>, List<Feed>> performSearch() {
String query = getArguments().getString(ARG_QUERY);
long feed = getArguments().getLong(ARG_FEED);
List<FeedItem> items = FeedSearcher.searchFeedItems(getContext(), query, feed);
List<Feed> feeds = FeedSearcher.searchFeeds(getContext(), query);
return new Pair<>(items, feeds);
}
}

View File

@ -10,7 +10,11 @@ import de.danoeh.antennapod.core.R;
* From http://stackoverflow.com/a/19449488/6839
*/
public class SquareImageView extends AppCompatImageView {
private boolean useMinimum = false;
public static final int DIRECTION_WIDTH = 0;
public static final int DIRECTION_HEIGHT = 1;
public static final int DIRECTION_MINIMUM = 2;
private int direction = DIRECTION_WIDTH;
public SquareImageView(Context context) {
super(context);
@ -27,20 +31,32 @@ public class SquareImageView extends AppCompatImageView {
}
private void loadAttrs(Context context, AttributeSet attrs) {
TypedArray a = context.obtainStyledAttributes(attrs, new int[]{R.styleable.SquareImageView_useMinimum});
useMinimum = a.getBoolean(0, false);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.SquareImageView);
direction = a.getInt(R.styleable.SquareImageView_direction, DIRECTION_WIDTH);
a.recycle();
}
public void setDirection(int direction) {
this.direction = direction;
requestLayout();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int size = getMeasuredWidth();
if (useMinimum) {
size = Math.min(getMeasuredWidth(), getMeasuredHeight());
switch (direction) {
case DIRECTION_MINIMUM:
int size = Math.min(getMeasuredWidth(), getMeasuredHeight());
setMeasuredDimension(size, size);
break;
case DIRECTION_HEIGHT:
setMeasuredDimension(getMeasuredHeight(), getMeasuredHeight());
break;
default:
setMeasuredDimension(getMeasuredWidth(), getMeasuredWidth());
break;
}
setMeasuredDimension(size, size);
}
}

View File

@ -20,7 +20,7 @@
android:transitionName="coverTransition"
tools:src="@android:drawable/sym_def_app_icon"
android:foreground="?attr/selectableItemBackgroundBorderless"
squareImageView:useMinimum="true" />
squareImageView:direction="minimum" />
<TextView
android:id="@+id/txtvPodcastTitle"

View File

@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:squareImageView="http://schemas.android.com/apk/de.danoeh.antennapod"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="4dp"
@ -12,7 +13,8 @@
android:layout_height="match_parent"
android:elevation="4dp"
android:outlineProvider="bounds"
android:foreground="?android:attr/selectableItemBackground"/>
android:foreground="?android:attr/selectableItemBackground"
squareImageView:direction="width" />
</LinearLayout>

View File

@ -24,7 +24,19 @@
<androidx.recyclerview.widget.RecyclerView
android:layout_below="@id/toolbar"
android:id="@+id/recyclerViewFeeds"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="12dp"
android:paddingRight="12dp"
android:clipToPadding="false"/>
<androidx.recyclerview.widget.RecyclerView
android:layout_below="@id/recyclerViewFeeds"
android:id="@+id/recyclerView"
android:layout_marginTop="-4dp"
android:paddingTop="12dp"
android:clipToPadding="false"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>

View File

@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:squareImageView="http://schemas.android.com/apk/de.danoeh.antennapod"
android:layout_width="match_parent"
android:layout_height="96dp"
android:padding="4dp"
android:clipToPadding="false">
<de.danoeh.antennapod.view.SquareImageView
android:id="@+id/discovery_cover"
android:layout_width="match_parent"
android:layout_height="96dp"
android:elevation="4dp"
android:outlineProvider="bounds"
android:foreground="?android:attr/selectableItemBackground"
squareImageView:direction="height" />
</LinearLayout>

View File

@ -1,7 +1,9 @@
<?xml version='1.0' encoding='utf-8'?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:squareImageView="http://schemas.android.com/apk/de.danoeh.antennapod"
android:layout_width="match_parent"
android:layout_height="match_parent">
@ -10,7 +12,8 @@
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="centerCrop"
tools:src="@mipmap/ic_launcher_round" />
tools:src="@mipmap/ic_launcher_round"
squareImageView:direction="width"/>
<com.joanzapata.iconify.widget.IconTextView
android:id="@+id/txtvTitle"

View File

@ -57,6 +57,10 @@
<attr name="action_icon_color" format="color"/>
<declare-styleable name="SquareImageView">
<attr name="useMinimum" format="boolean" />
<attr name="direction" format="enum">
<enum name="width" value="0"/>
<enum name="height" value="1"/>
<enum name="minimum" value="2"/>
</attr>
</declare-styleable>
</resources>