package app.fedilab.fedilabtube.drawer; /* Copyright 2020 Thomas Schneider * * This file is a part of TubeLab * * 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. * * TubeLab 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 TubeLab; if not, * see . */ import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; 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 app.fedilab.fedilabtube.PeertubeActivity; import app.fedilab.fedilabtube.PeertubeEditUploadActivity; import app.fedilab.fedilabtube.R; import app.fedilab.fedilabtube.ShowAccountActivity; 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 implements OnActionInterface { private List peertubes; private Context context; public PeertubeAdapter(List 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(); SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, ""); boolean ownVideos = peertube.getAccount().getInstance().compareTo(Helper.getLiveInstance(context)) == 0 && userId != null && peertube.getAccount().getId() != null && peertube.getAccount().getId().compareTo(userId) == 0; if (peertube.getChannel() == null) { holder.peertube_account_name.setText(account.getAcct()); 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); } else { holder.peertube_account_name.setText(peertube.getChannel().getAcct()); if (peertube.getChannel().getAvatar() != null && !peertube.getChannel().getAvatar().equals("null") && !peertube.getChannel().getAvatar().startsWith("http")) { peertube.getChannel().setAvatar("https://" + peertube.getInstance() + peertube.getChannel().getAvatar()); peertube.getChannel().setAvatar_static(peertube.getChannel().getAvatar()); } Helper.loadGiF(context, peertube.getChannel(), holder.peertube_profile); } 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 (!ownVideos) { holder.peertube_profile.setOnClickListener(v -> { Intent intent = new Intent(context, ShowAccountActivity.class); Bundle b = new Bundle(); if (peertube.getChannel() == null) { b.putParcelable("account", peertube.getAccount()); } else { b.putParcelable("account", peertube.getChannel()); b.putBoolean("ischannel", true); } intent.putExtras(b); context.startActivity(intent); }); } 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); } if (!ownVideos) { holder.bottom_container.setOnClickListener(v -> { Intent intent = new Intent(context, PeertubeActivity.class); Bundle b = new Bundle(); b.putString("video_id", peertube.getUuid()); intent.putExtras(b); context.startActivity(intent); }); } else { 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(); b.putString("video_id", peertube.getUuid()); 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); } } }