2018-12-29 14:19:17 +01:00
|
|
|
/*
|
|
|
|
* Copyright 2018 Stefan Schüller <sschueller@techdroid.com>
|
|
|
|
*
|
|
|
|
* License: GPL-3.0+
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2018-03-03 01:10:13 +01:00
|
|
|
package net.schueller.peertube.adapter;
|
|
|
|
|
|
|
|
import android.content.Context;
|
2018-03-04 01:06:32 +01:00
|
|
|
import android.content.Intent;
|
2019-01-06 15:22:14 +01:00
|
|
|
|
2018-12-23 16:42:01 +01:00
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
import androidx.appcompat.widget.PopupMenu;
|
|
|
|
import androidx.recyclerview.widget.RecyclerView;
|
2018-12-29 22:36:54 +01:00
|
|
|
|
2018-03-03 01:10:13 +01:00
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
import android.widget.ImageView;
|
|
|
|
import android.widget.TextView;
|
|
|
|
|
2018-12-29 22:36:54 +01:00
|
|
|
import com.mikepenz.iconics.Iconics;
|
2018-03-03 01:10:13 +01:00
|
|
|
import com.squareup.picasso.Picasso;
|
|
|
|
|
|
|
|
import net.schueller.peertube.R;
|
2019-01-06 15:22:14 +01:00
|
|
|
import net.schueller.peertube.activity.AccountActivity;
|
2018-07-01 15:11:26 +02:00
|
|
|
import net.schueller.peertube.activity.VideoPlayActivity;
|
2018-03-11 14:51:39 +01:00
|
|
|
import net.schueller.peertube.helper.APIUrlHelper;
|
2018-03-11 17:21:30 +01:00
|
|
|
import net.schueller.peertube.helper.MetaDataHelper;
|
2018-12-15 14:24:11 +01:00
|
|
|
import net.schueller.peertube.intents.Intents;
|
2018-03-14 14:26:36 +01:00
|
|
|
import net.schueller.peertube.model.Avatar;
|
2018-03-03 01:10:13 +01:00
|
|
|
import net.schueller.peertube.model.Video;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
2019-01-06 15:22:14 +01:00
|
|
|
import static net.schueller.peertube.activity.VideoListActivity.EXTRA_ACCOUNTDISPLAYNAME;
|
2018-03-04 01:06:32 +01:00
|
|
|
import static net.schueller.peertube.activity.VideoListActivity.EXTRA_VIDEOID;
|
|
|
|
|
2018-03-03 01:10:13 +01:00
|
|
|
public class VideoAdapter extends RecyclerView.Adapter<VideoAdapter.VideoViewHolder> {
|
|
|
|
|
|
|
|
|
|
|
|
private ArrayList<Video> videoList;
|
|
|
|
private Context context;
|
2018-12-15 21:59:59 +01:00
|
|
|
private String baseUrl;
|
2018-03-03 01:10:13 +01:00
|
|
|
|
|
|
|
public VideoAdapter(ArrayList<Video> videoList, Context context) {
|
|
|
|
this.videoList = videoList;
|
|
|
|
this.context = context;
|
|
|
|
}
|
|
|
|
|
2018-03-03 02:02:00 +01:00
|
|
|
@NonNull
|
2018-03-03 01:10:13 +01:00
|
|
|
@Override
|
2018-03-03 02:02:00 +01:00
|
|
|
public VideoViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
|
2018-03-03 01:10:13 +01:00
|
|
|
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
|
|
|
|
View view = layoutInflater.inflate(R.layout.row_video, parent, false);
|
2018-03-04 20:24:32 +01:00
|
|
|
|
2018-12-15 21:59:59 +01:00
|
|
|
baseUrl = APIUrlHelper.getUrl(context);
|
2018-03-04 20:24:32 +01:00
|
|
|
|
2018-03-03 01:10:13 +01:00
|
|
|
return new VideoViewHolder(view);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-03-03 02:02:00 +01:00
|
|
|
public void onBindViewHolder(@NonNull VideoViewHolder holder, int position) {
|
2018-03-03 01:10:13 +01:00
|
|
|
|
|
|
|
Picasso.with(this.context)
|
2018-12-15 21:59:59 +01:00
|
|
|
.load(baseUrl + videoList.get(position).getPreviewPath())
|
2018-03-03 01:10:13 +01:00
|
|
|
.into(holder.thumb);
|
|
|
|
|
2018-03-14 14:26:36 +01:00
|
|
|
|
|
|
|
Avatar avatar = videoList.get(position).getAccount().getAvatar();
|
|
|
|
if (avatar != null) {
|
|
|
|
String avatarPath = avatar.getPath();
|
|
|
|
Picasso.with(this.context)
|
2018-12-15 21:59:59 +01:00
|
|
|
.load(baseUrl + avatarPath)
|
2018-03-14 14:26:36 +01:00
|
|
|
.into(holder.avatar);
|
|
|
|
}
|
|
|
|
|
2019-01-06 03:23:18 +01:00
|
|
|
// set Name
|
2018-03-03 01:10:13 +01:00
|
|
|
holder.name.setText(videoList.get(position).getName());
|
2018-03-04 20:24:32 +01:00
|
|
|
|
2019-01-06 03:23:18 +01:00
|
|
|
// set duration
|
2019-01-06 15:22:14 +01:00
|
|
|
holder.videoDuration.setText(MetaDataHelper.getDuration(videoList.get(position).getDuration().longValue()));
|
2019-01-06 03:23:18 +01:00
|
|
|
|
2018-03-04 20:24:32 +01:00
|
|
|
// set age and view count
|
2018-03-11 17:21:30 +01:00
|
|
|
holder.videoMeta.setText(
|
|
|
|
MetaDataHelper.getMetaString(videoList.get(position).getCreatedAt(),
|
|
|
|
videoList.get(position).getViews(),
|
|
|
|
context
|
|
|
|
)
|
|
|
|
);
|
2018-03-04 01:06:32 +01:00
|
|
|
|
2018-03-04 20:24:32 +01:00
|
|
|
// set owner
|
2019-01-06 15:22:14 +01:00
|
|
|
String displayNameAndHost = MetaDataHelper.getOwnerString(videoList.get(position).getAccount().getName(),
|
|
|
|
videoList.get(position).getAccount().getHost(),
|
|
|
|
context
|
2018-03-11 17:21:30 +01:00
|
|
|
);
|
2018-03-04 20:24:32 +01:00
|
|
|
|
2019-01-06 15:22:14 +01:00
|
|
|
holder.videoOwner.setText(displayNameAndHost);
|
|
|
|
|
|
|
|
holder.videoOwner.setOnClickListener(v -> {
|
|
|
|
Intent intent = new Intent(context, AccountActivity.class);
|
|
|
|
intent.putExtra(EXTRA_ACCOUNTDISPLAYNAME, displayNameAndHost);
|
|
|
|
context.startActivity(intent);
|
|
|
|
});
|
|
|
|
|
2018-03-04 01:06:32 +01:00
|
|
|
holder.mView.setOnClickListener(v -> {
|
|
|
|
|
|
|
|
// Log.v("VideoAdapter", "click: " + videoList.get(position).getName());
|
|
|
|
|
2019-01-06 15:22:14 +01:00
|
|
|
Intent intent = new Intent(context, VideoPlayActivity.class);
|
2018-03-04 01:06:32 +01:00
|
|
|
intent.putExtra(EXTRA_VIDEOID, videoList.get(position).getUuid());
|
|
|
|
context.startActivity(intent);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2018-12-29 22:36:54 +01:00
|
|
|
holder.moreButton.setText(R.string.video_more_icon);
|
|
|
|
new Iconics.IconicsBuilder().ctx(context).on(holder.moreButton).build();
|
|
|
|
|
2018-12-15 14:24:11 +01:00
|
|
|
holder.moreButton.setOnClickListener(v -> {
|
|
|
|
|
|
|
|
PopupMenu popup = new PopupMenu(context, v);
|
|
|
|
popup.setOnMenuItemClickListener(menuItem -> {
|
|
|
|
switch (menuItem.getItemId()) {
|
|
|
|
case R.id.menu_share:
|
|
|
|
Intents.Share(context, videoList.get(position));
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
popup.inflate(R.menu.menu_video_row_mode);
|
|
|
|
popup.show();
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2018-03-03 01:10:13 +01:00
|
|
|
}
|
|
|
|
|
2018-03-03 02:02:00 +01:00
|
|
|
public void setData(ArrayList<Video> data) {
|
|
|
|
videoList.addAll(data);
|
|
|
|
this.notifyDataSetChanged();
|
|
|
|
}
|
|
|
|
|
2018-03-03 23:08:18 +01:00
|
|
|
public void clearData() {
|
|
|
|
videoList.clear();
|
|
|
|
this.notifyDataSetChanged();
|
|
|
|
}
|
|
|
|
|
2018-03-03 01:10:13 +01:00
|
|
|
@Override
|
|
|
|
public int getItemCount() {
|
|
|
|
return videoList.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
class VideoViewHolder extends RecyclerView.ViewHolder {
|
|
|
|
|
2019-01-06 03:23:18 +01:00
|
|
|
TextView name, videoMeta, videoOwner, moreButton, videoDuration;
|
2018-03-14 14:26:36 +01:00
|
|
|
ImageView thumb, avatar;
|
2018-03-04 01:06:32 +01:00
|
|
|
View mView;
|
2018-03-03 01:10:13 +01:00
|
|
|
|
|
|
|
VideoViewHolder(View itemView) {
|
|
|
|
super(itemView);
|
|
|
|
name = itemView.findViewById(R.id.name);
|
|
|
|
thumb = itemView.findViewById(R.id.thumb);
|
2018-03-14 14:26:36 +01:00
|
|
|
avatar = itemView.findViewById(R.id.avatar);
|
2018-03-03 23:08:18 +01:00
|
|
|
videoMeta = itemView.findViewById(R.id.videoMeta);
|
2018-03-04 20:24:32 +01:00
|
|
|
videoOwner = itemView.findViewById(R.id.videoOwner);
|
2018-12-15 14:24:11 +01:00
|
|
|
moreButton = itemView.findViewById(R.id.moreButton);
|
2019-01-06 03:23:18 +01:00
|
|
|
videoDuration = itemView.findViewById(R.id.video_duration);
|
2018-03-04 01:06:32 +01:00
|
|
|
mView = itemView;
|
2018-03-03 01:10:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|