157 lines
6.2 KiB
Java
157 lines
6.2 KiB
Java
package app.fedilab.fedilabtube.drawer;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
import android.content.Intent;
|
|
import android.os.Bundle;
|
|
import android.view.LayoutInflater;
|
|
import android.view.View;
|
|
import android.view.ViewGroup;
|
|
import android.widget.ImageView;
|
|
import android.widget.LinearLayout;
|
|
import android.widget.TextView;
|
|
|
|
import androidx.annotation.NonNull;
|
|
import androidx.recyclerview.widget.RecyclerView;
|
|
|
|
import com.bumptech.glide.Glide;
|
|
|
|
import java.util.List;
|
|
import java.util.Objects;
|
|
import java.util.regex.Matcher;
|
|
import java.util.regex.Pattern;
|
|
|
|
import app.fedilab.fedilabtube.PeertubeActivity;
|
|
import app.fedilab.fedilabtube.PeertubeEditUploadActivity;
|
|
import app.fedilab.fedilabtube.R;
|
|
import app.fedilab.fedilabtube.client.APIResponse;
|
|
import app.fedilab.fedilabtube.client.entities.Account;
|
|
import app.fedilab.fedilabtube.client.entities.Peertube;
|
|
import app.fedilab.fedilabtube.helper.Helper;
|
|
import app.fedilab.fedilabtube.interfaces.OnActionInterface;
|
|
|
|
|
|
public class PeertubeAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements OnActionInterface {
|
|
|
|
private List<Peertube> peertubes;
|
|
private Context context;
|
|
|
|
|
|
public PeertubeAdapter(List<Peertube> peertubes) {
|
|
this.peertubes = peertubes;
|
|
|
|
}
|
|
|
|
@NonNull
|
|
@Override
|
|
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
|
|
context = parent.getContext();
|
|
LayoutInflater layoutInflater = LayoutInflater.from(context);
|
|
return new ViewHolder(layoutInflater.inflate(R.layout.drawer_peertube, parent, false));
|
|
}
|
|
|
|
@Override
|
|
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position) {
|
|
|
|
|
|
final PeertubeAdapter.ViewHolder holder = (PeertubeAdapter.ViewHolder) viewHolder;
|
|
final Peertube peertube = peertubes.get(position);
|
|
if (peertube.getInstance() == null)
|
|
peertube.setInstance(Helper.getLiveInstance(context));
|
|
Account account = peertube.getAccount();
|
|
|
|
holder.peertube_account_name.setText(account.getAcct());
|
|
holder.peertube_title.setText(peertube.getName());
|
|
holder.peertube_duration.setText(context.getString(R.string.duration_video, Helper.secondsToString(peertube.getDuration())));
|
|
holder.peertube_date.setText(String.format(" - %s", Helper.dateDiff(context, peertube.getCreated_at())));
|
|
holder.peertube_views.setText(context.getString(R.string.number_view_video, Helper.withSuffix(peertube.getView())));
|
|
|
|
|
|
Glide.with(holder.peertube_video_image.getContext())
|
|
.load("https://" + peertube.getInstance() + peertube.getThumbnailPath())
|
|
.into(holder.peertube_video_image);
|
|
if (account.getAvatar() != null && !account.getAvatar().equals("null") && !account.getAvatar().startsWith("http")) {
|
|
account.setAvatar("https://" + peertube.getInstance() + account.getAvatar());
|
|
account.setAvatar_static(account.getAvatar());
|
|
}
|
|
Helper.loadGiF(context, account, holder.peertube_profile);
|
|
|
|
|
|
if (peertube.getHeaderType() != null && peertube.getHeaderTypeValue() != null) {
|
|
String type = peertube.getHeaderType();
|
|
if ("tags".equals(type)) {
|
|
holder.header_title.setText(String.format("#%s", peertube.getHeaderTypeValue()));
|
|
} else {
|
|
holder.header_title.setText(peertube.getHeaderTypeValue());
|
|
}
|
|
holder.header_title.setVisibility(View.VISIBLE);
|
|
} else {
|
|
holder.header_title.setVisibility(View.GONE);
|
|
}
|
|
|
|
holder.bottom_container.setOnClickListener(v -> {
|
|
Intent intent = new Intent(context, PeertubeEditUploadActivity.class);
|
|
Bundle b = new Bundle();
|
|
b.putString("video_id", peertube.getUuid());
|
|
intent.putExtras(b);
|
|
context.startActivity(intent);
|
|
});
|
|
holder.peertube_video_image.setOnClickListener(v -> {
|
|
Intent intent = new Intent(context, PeertubeActivity.class);
|
|
Bundle b = new Bundle();
|
|
String instance = Helper.getLiveInstance(context);
|
|
String finalUrl = "https://" + instance + peertube.getEmbedPath();
|
|
Pattern link = Pattern.compile("(https?://[\\da-z.-]+\\.[a-z.]{2,10})/videos/embed/(\\w{8}-\\w{4}-\\w{4}-\\w{4}-\\w{12})$");
|
|
Matcher matcherLink = link.matcher(finalUrl);
|
|
if (matcherLink.find()) {
|
|
String url = matcherLink.group(1) + "/videos/watch/" + matcherLink.group(2);
|
|
b.putString("peertubeLinkToFetch", url);
|
|
b.putString("peertube_instance", Objects.requireNonNull(matcherLink.group(1)).replace("https://", "").replace("http://", ""));
|
|
b.putString("video_id", matcherLink.group(2));
|
|
}
|
|
intent.putExtras(b);
|
|
context.startActivity(intent);
|
|
});
|
|
|
|
}
|
|
|
|
@Override
|
|
public long getItemId(int position) {
|
|
return position;
|
|
}
|
|
|
|
@Override
|
|
public int getItemCount() {
|
|
return peertubes.size();
|
|
}
|
|
|
|
|
|
@Override
|
|
public void onActionDone(APIResponse apiResponse, int statusCode) {
|
|
|
|
}
|
|
|
|
static class ViewHolder extends RecyclerView.ViewHolder {
|
|
LinearLayout main_container, bottom_container;
|
|
ImageView peertube_profile, peertube_video_image;
|
|
TextView peertube_account_name, peertube_views, peertube_duration;
|
|
TextView peertube_title, peertube_date, header_title;
|
|
|
|
public ViewHolder(@NonNull View itemView) {
|
|
super(itemView);
|
|
peertube_account_name = itemView.findViewById(R.id.peertube_account_name);
|
|
peertube_title = itemView.findViewById(R.id.peertube_title);
|
|
peertube_video_image = itemView.findViewById(R.id.peertube_video_image);
|
|
peertube_profile = itemView.findViewById(R.id.peertube_profile);
|
|
peertube_date = itemView.findViewById(R.id.peertube_date);
|
|
peertube_views = itemView.findViewById(R.id.peertube_views);
|
|
peertube_duration = itemView.findViewById(R.id.peertube_duration);
|
|
main_container = itemView.findViewById(R.id.main_container);
|
|
header_title = itemView.findViewById(R.id.header_title);
|
|
bottom_container = itemView.findViewById(R.id.bottom_container);
|
|
}
|
|
}
|
|
|
|
|
|
} |