fedilab-Android-App/app/src/main/java/app/fedilab/android/drawers/PeertubeAdapter.java

234 lines
10 KiB
Java
Raw Normal View History

2019-05-18 11:10:30 +02:00
package app.fedilab.android.drawers;
2018-10-06 15:59:00 +02:00
/* Copyright 2018 Thomas Schneider
*
2019-05-18 11:10:30 +02:00
* This file is a part of Fedilab
2018-10-06 15:59:00 +02:00
*
* 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.
*
2019-05-18 11:10:30 +02:00
* Fedilab is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
2018-10-06 15:59:00 +02:00
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
2019-05-18 11:10:30 +02:00
* You should have received a copy of the GNU General Public License along with Fedilab; if not,
2018-10-06 15:59:00 +02:00
* see <http://www.gnu.org/licenses>. */
import android.content.Context;
import android.content.Intent;
2018-10-17 18:20:03 +02:00
import android.content.SharedPreferences;
2018-10-06 15:59:00 +02:00
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;
2019-11-15 16:32:25 +01:00
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
2018-10-06 15:59:00 +02:00
import com.bumptech.glide.Glide;
import java.util.List;
2020-03-07 14:16:28 +01:00
import java.util.Objects;
2018-10-06 15:59:00 +02:00
import java.util.regex.Matcher;
import java.util.regex.Pattern;
2019-05-18 11:10:30 +02:00
import app.fedilab.android.R;
import app.fedilab.android.activities.MainActivity;
import app.fedilab.android.activities.PeertubeActivity;
import app.fedilab.android.activities.PeertubeEditUploadActivity;
import app.fedilab.android.activities.ShowAccountActivity;
import app.fedilab.android.asynctasks.ManageListsAsyncTask;
import app.fedilab.android.asynctasks.UpdateAccountInfoAsyncTask;
2019-11-15 16:32:25 +01:00
import app.fedilab.android.client.APIResponse;
import app.fedilab.android.client.Entities.Account;
import app.fedilab.android.client.Entities.Peertube;
import app.fedilab.android.helper.CrossActions;
import app.fedilab.android.helper.Helper;
2019-05-18 11:10:30 +02:00
import app.fedilab.android.interfaces.OnListActionInterface;
2018-10-06 15:59:00 +02:00
/**
* Created by Thomas on 06/10/2018.
* Adapter for peertube
*/
2020-06-16 18:23:13 +02:00
public class PeertubeAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements OnListActionInterface {
2018-10-06 15:59:00 +02:00
private List<Peertube> peertubes;
private Context context;
2018-10-07 10:45:34 +02:00
private String instance;
2019-01-07 19:16:15 +01:00
private boolean ownVideos;
2018-10-06 15:59:00 +02:00
2019-09-06 17:55:14 +02:00
public PeertubeAdapter(String instance, List<Peertube> peertubes) {
2018-10-06 15:59:00 +02:00
this.peertubes = peertubes;
2018-10-07 10:45:34 +02:00
this.instance = instance;
2019-01-07 19:16:15 +01:00
this.ownVideos = false;
2018-10-06 15:59:00 +02:00
}
2019-09-06 17:55:14 +02:00
public PeertubeAdapter(String instance, boolean ownVideos, List<Peertube> peertubes) {
2019-01-07 19:16:15 +01:00
this.peertubes = peertubes;
this.instance = instance;
this.ownVideos = ownVideos;
}
2018-10-06 15:59:00 +02:00
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
2019-08-19 09:11:23 +02:00
context = parent.getContext();
2020-03-07 14:16:28 +01:00
LayoutInflater layoutInflater = LayoutInflater.from(context);
return new ViewHolder(layoutInflater.inflate(R.layout.drawer_peertube, parent, false));
2018-10-06 15:59:00 +02:00
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position) {
final PeertubeAdapter.ViewHolder holder = (PeertubeAdapter.ViewHolder) viewHolder;
final Peertube peertube = peertubes.get(position);
2019-09-06 17:55:14 +02:00
if (peertube.getInstance() == null)
2019-01-03 14:23:13 +01:00
peertube.setInstance(Helper.getLiveInstance(context));
2018-10-17 18:20:03 +02:00
SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
int theme = sharedpreferences.getInt(Helper.SET_THEME, Helper.THEME_DARK);
2019-09-06 17:55:14 +02:00
if (theme == Helper.THEME_LIGHT) {
2018-10-17 18:20:03 +02:00
holder.main_container.setBackgroundResource(R.color.mastodonC3__);
2019-09-06 17:55:14 +02:00
} else if (theme == Helper.THEME_DARK) {
2018-10-17 18:20:03 +02:00
holder.main_container.setBackgroundResource(R.color.mastodonC1_);
2019-09-06 17:55:14 +02:00
} else if (theme == Helper.THEME_BLACK) {
2018-10-17 18:20:03 +02:00
holder.main_container.setBackgroundResource(R.color.black);
}
2018-10-17 14:34:29 +02:00
Account account = peertube.getAccount();
2018-10-06 15:59:00 +02:00
2018-10-17 14:34:29 +02:00
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())));
2018-10-06 15:59:00 +02:00
2019-01-03 17:11:53 +01:00
2018-10-17 14:34:29 +02:00
Glide.with(holder.peertube_video_image.getContext())
.load("https://" + peertube.getInstance() + peertube.getThumbnailPath())
.into(holder.peertube_video_image);
2020-04-10 18:42:34 +02:00
if (account.getAvatar() != null && !account.getAvatar().equals("null") && !account.getAvatar().startsWith("http")) {
2019-01-03 14:23:13 +01:00
account.setAvatar("https://" + peertube.getInstance() + account.getAvatar());
2020-04-10 18:42:34 +02:00
account.setAvatar_static(account.getAvatar());
}
Helper.loadGiF(context, account, holder.peertube_profile);
2019-01-07 19:16:15 +01:00
2019-09-06 17:55:14 +02:00
if (peertube.getHeaderType() != null && peertube.getHeaderTypeValue() != null) {
2019-01-12 19:04:12 +01:00
String type = peertube.getHeaderType();
2020-03-07 14:16:28 +01:00
if ("tags".equals(type)) {
holder.header_title.setText(String.format("#%s", peertube.getHeaderTypeValue()));
} else {
holder.header_title.setText(peertube.getHeaderTypeValue());
2019-01-12 19:04:12 +01:00
}
holder.header_title.setVisibility(View.VISIBLE);
2019-09-06 17:55:14 +02:00
} else {
2019-01-12 19:04:12 +01:00
holder.header_title.setVisibility(View.GONE);
}
2019-09-06 17:55:14 +02:00
if (!this.ownVideos) {
2020-03-07 14:16:28 +01:00
holder.peertube_profile.setOnClickListener(v -> {
//For remote peertube instance
if (!peertube.getInstance().equals(Helper.getLiveInstance(context)))
CrossActions.doCrossProfile(context, account);
else {
Intent intent = new Intent(context, ShowAccountActivity.class);
2019-01-07 19:16:15 +01:00
Bundle b = new Bundle();
2020-03-07 14:16:28 +01:00
b.putBoolean("peertubeaccount", true);
b.putParcelable("account", peertube.getAccount());
2019-01-07 19:16:15 +01:00
intent.putExtras(b);
context.startActivity(intent);
2018-10-06 15:59:00 +02:00
}
2019-01-07 19:16:15 +01:00
});
2020-03-07 14:16:28 +01:00
holder.main_container.setOnClickListener(v -> {
Intent intent = new Intent(context, PeertubeActivity.class);
Bundle b = new Bundle();
if ((instance == null || instance.trim().length() == 0) && MainActivity.social == UpdateAccountInfoAsyncTask.SOCIAL.PEERTUBE)
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));
2019-01-07 19:16:15 +01:00
}
2020-03-07 14:16:28 +01:00
intent.putExtras(b);
context.startActivity(intent);
2019-01-07 19:16:15 +01:00
});
2020-03-07 14:16:28 +01:00
} 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();
if ((instance == null || instance.trim().length() == 0) && MainActivity.social == UpdateAccountInfoAsyncTask.SOCIAL.PEERTUBE)
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));
2019-11-16 15:49:39 +01:00
}
2020-03-07 14:16:28 +01:00
intent.putExtras(b);
context.startActivity(intent);
2019-11-16 15:49:39 +01:00
});
2019-01-07 19:16:15 +01:00
}
2018-10-06 15:59:00 +02:00
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public int getItemCount() {
2018-10-07 10:45:34 +02:00
return peertubes.size();
2018-10-06 15:59:00 +02:00
}
@Override
public void onActionDone(ManageListsAsyncTask.action actionType, APIResponse apiResponse, int statusCode) {
}
2020-03-07 14:16:28 +01:00
static class ViewHolder extends RecyclerView.ViewHolder {
2019-11-16 15:49:39 +01:00
LinearLayout main_container, bottom_container;
2019-03-19 02:12:29 +01:00
ImageView peertube_profile, peertube_video_image;
2018-10-17 14:34:29 +02:00
TextView peertube_account_name, peertube_views, peertube_duration;
2019-01-12 19:04:12 +01:00
TextView peertube_title, peertube_date, header_title;
2018-10-17 18:20:03 +02:00
2018-10-06 15:59:00 +02:00
public ViewHolder(@NonNull View itemView) {
super(itemView);
2018-10-17 14:34:29 +02:00
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);
2018-10-17 18:20:03 +02:00
main_container = itemView.findViewById(R.id.main_container);
2019-01-12 19:04:12 +01:00
header_title = itemView.findViewById(R.id.header_title);
2019-11-16 15:49:39 +01:00
bottom_container = itemView.findViewById(R.id.bottom_container);
2018-10-06 15:59:00 +02:00
}
}
}