mirror of https://github.com/TeamNewPipe/NewPipe
167 lines
5.5 KiB
Java
167 lines
5.5 KiB
Java
package org.schabi.newpipe.search_fragment;
|
|
|
|
import android.app.Activity;
|
|
import android.content.Context;
|
|
import android.support.v7.widget.RecyclerView;
|
|
import android.view.LayoutInflater;
|
|
import android.view.View;
|
|
import android.view.ViewGroup;
|
|
|
|
import com.nostra13.universalimageloader.core.DisplayImageOptions;
|
|
import com.nostra13.universalimageloader.core.ImageLoader;
|
|
|
|
import org.schabi.newpipe.ImageErrorLoadingListener;
|
|
import org.schabi.newpipe.R;
|
|
import org.schabi.newpipe.StreamInfoItemHolder;
|
|
import org.schabi.newpipe.StreamInfoItemViewCreator;
|
|
import org.schabi.newpipe.extractor.AbstractVideoInfo;
|
|
import org.schabi.newpipe.extractor.StreamPreviewInfo;
|
|
|
|
import java.util.List;
|
|
import java.util.Vector;
|
|
|
|
/**
|
|
* Created by the-scrabi on 01.08.16.
|
|
*/
|
|
public class StreamInfoListAdapter extends RecyclerView.Adapter<StreamInfoItemHolder> {
|
|
|
|
private Activity activity = null;
|
|
private View rootView = null;
|
|
private List<StreamPreviewInfo> streamList = new Vector<>();
|
|
private ImageLoader imageLoader = ImageLoader.getInstance();
|
|
private DisplayImageOptions displayImageOptions =
|
|
new DisplayImageOptions.Builder().cacheInMemory(true).build();
|
|
|
|
StreamInfoListAdapter(Activity a, View rootView) {
|
|
activity = a;
|
|
this.rootView = rootView;
|
|
}
|
|
|
|
public void addVideoList(List<StreamPreviewInfo> videos) {
|
|
streamList.addAll(videos);
|
|
notifyDataSetChanged();
|
|
}
|
|
|
|
public void clearVideoList() {
|
|
streamList = new Vector<>();
|
|
notifyDataSetChanged();
|
|
}
|
|
|
|
@Override
|
|
public int getItemCount() {
|
|
return streamList.size();
|
|
}
|
|
|
|
@Override
|
|
public StreamInfoItemHolder onCreateViewHolder(ViewGroup parent, int i) {
|
|
View itemView = LayoutInflater.from(parent.getContext())
|
|
.inflate(R.layout.video_item, parent, false);
|
|
|
|
return new StreamInfoItemHolder(itemView);
|
|
}
|
|
|
|
@Override
|
|
public void onBindViewHolder(StreamInfoItemHolder holder, int i) {
|
|
StreamPreviewInfo info = streamList.get(i);
|
|
// fill holder with information
|
|
holder.itemVideoTitleView.setText(info.title);
|
|
if(info.uploader != null && !info.uploader.isEmpty()) {
|
|
holder.itemUploaderView.setText(info.uploader);
|
|
} else {
|
|
holder.itemUploaderView.setVisibility(View.INVISIBLE);
|
|
}
|
|
if(info.duration > 0) {
|
|
holder.itemDurationView.setText(StreamInfoItemViewCreator.getDurationString(info.duration));
|
|
} else {
|
|
if(info.stream_type == AbstractVideoInfo.StreamType.LIVE_STREAM) {
|
|
holder.itemDurationView.setText(R.string.duration_live);
|
|
} else {
|
|
holder.itemDurationView.setVisibility(View.GONE);
|
|
}
|
|
}
|
|
if(info.view_count >= 0) {
|
|
holder.itemViewCountView.setText(shortViewCount(info.view_count));
|
|
} else {
|
|
holder.itemViewCountView.setVisibility(View.GONE);
|
|
}
|
|
if(info.upload_date != null && !info.upload_date.isEmpty()) {
|
|
holder.itemUploadDateView.setText(info.upload_date + " • ");
|
|
}
|
|
|
|
holder.itemThumbnailView.setImageResource(R.drawable.dummy_thumbnail);
|
|
if(info.thumbnail_url != null && !info.thumbnail_url.isEmpty()) {
|
|
imageLoader.displayImage(info.thumbnail_url,
|
|
holder.itemThumbnailView,
|
|
displayImageOptions,
|
|
new ImageErrorLoadingListener(activity, rootView, info.service_id));
|
|
}
|
|
}
|
|
|
|
|
|
public static String shortViewCount(Long viewCount){
|
|
if(viewCount >= 1000000000){
|
|
return Long.toString(viewCount/1000000000)+"B views";
|
|
}else if(viewCount>=1000000){
|
|
return Long.toString(viewCount/1000000)+"M views";
|
|
}else if(viewCount>=1000){
|
|
return Long.toString(viewCount/1000)+"K views";
|
|
}else {
|
|
return Long.toString(viewCount)+" views";
|
|
}
|
|
}
|
|
|
|
public static String getDurationString(int duration) {
|
|
String output = "";
|
|
int days = duration / (24 * 60 * 60); /* greater than a day */
|
|
duration %= (24 * 60 * 60);
|
|
int hours = duration / (60 * 60); /* greater than an hour */
|
|
duration %= (60 * 60);
|
|
int minutes = duration / 60;
|
|
int seconds = duration % 60;
|
|
|
|
//handle days
|
|
if(days > 0) {
|
|
output = Integer.toString(days) + ":";
|
|
}
|
|
// handle hours
|
|
if(hours > 0 || !output.isEmpty()) {
|
|
if(hours > 0) {
|
|
if(hours >= 10 || output.isEmpty()) {
|
|
output += Integer.toString(hours);
|
|
} else {
|
|
output += "0" + Integer.toString(hours);
|
|
}
|
|
} else {
|
|
output += "00";
|
|
}
|
|
output += ":";
|
|
}
|
|
//handle minutes
|
|
if(minutes > 0 || !output.isEmpty()) {
|
|
if(minutes > 0) {
|
|
if(minutes >= 10 || output.isEmpty()) {
|
|
output += Integer.toString(minutes);
|
|
} else {
|
|
output += "0" + Integer.toString(minutes);
|
|
}
|
|
} else {
|
|
output += "00";
|
|
}
|
|
output += ":";
|
|
}
|
|
|
|
//handle seconds
|
|
if(output.isEmpty()) {
|
|
output += "0:";
|
|
}
|
|
|
|
if(seconds >= 10) {
|
|
output += Integer.toString(seconds);
|
|
} else {
|
|
output += "0" + Integer.toString(seconds);
|
|
}
|
|
|
|
return output;
|
|
}
|
|
}
|