2018-10-06 15:59:00 +02:00
|
|
|
package fr.gouv.etalab.mastodon.drawers;
|
|
|
|
/* Copyright 2018 Thomas Schneider
|
|
|
|
*
|
|
|
|
* This file is a part of Mastalab
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* Mastalab 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 Mastalab; if not,
|
|
|
|
* 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.support.annotation.NonNull;
|
|
|
|
import android.support.v7.widget.RecyclerView;
|
|
|
|
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 com.bumptech.glide.Glide;
|
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.regex.Matcher;
|
|
|
|
import java.util.regex.Pattern;
|
|
|
|
|
|
|
|
import fr.gouv.etalab.mastodon.R;
|
2019-01-03 17:11:53 +01:00
|
|
|
import fr.gouv.etalab.mastodon.activities.MainActivity;
|
2018-10-14 15:20:05 +02:00
|
|
|
import fr.gouv.etalab.mastodon.activities.PeertubeActivity;
|
2019-01-07 19:16:15 +01:00
|
|
|
import fr.gouv.etalab.mastodon.activities.PeertubeEditUploadActivity;
|
2019-01-03 17:11:53 +01:00
|
|
|
import fr.gouv.etalab.mastodon.activities.ShowAccountActivity;
|
2018-10-06 15:59:00 +02:00
|
|
|
import fr.gouv.etalab.mastodon.asynctasks.ManageListsAsyncTask;
|
2019-01-03 17:11:53 +01:00
|
|
|
import fr.gouv.etalab.mastodon.asynctasks.UpdateAccountInfoAsyncTask;
|
2018-10-06 15:59:00 +02:00
|
|
|
import fr.gouv.etalab.mastodon.client.APIResponse;
|
2018-10-17 14:34:29 +02:00
|
|
|
import fr.gouv.etalab.mastodon.client.Entities.Account;
|
2018-10-06 15:59:00 +02:00
|
|
|
import fr.gouv.etalab.mastodon.client.Entities.Peertube;
|
2018-10-17 14:34:29 +02:00
|
|
|
import fr.gouv.etalab.mastodon.helper.CrossActions;
|
2018-10-06 15:59:00 +02:00
|
|
|
import fr.gouv.etalab.mastodon.helper.Helper;
|
|
|
|
import fr.gouv.etalab.mastodon.interfaces.OnListActionInterface;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Created by Thomas on 06/10/2018.
|
|
|
|
* Adapter for peertube
|
|
|
|
*/
|
|
|
|
public class PeertubeAdapter extends RecyclerView.Adapter implements OnListActionInterface {
|
|
|
|
|
|
|
|
private List<Peertube> peertubes;
|
|
|
|
private LayoutInflater layoutInflater;
|
|
|
|
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
|
|
|
|
2018-10-07 10:45:34 +02:00
|
|
|
public PeertubeAdapter(Context context, String instance, List<Peertube> peertubes){
|
2018-10-06 15:59:00 +02:00
|
|
|
this.peertubes = peertubes;
|
|
|
|
layoutInflater = LayoutInflater.from(context);
|
|
|
|
this.context = context;
|
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-01-07 19:16:15 +01:00
|
|
|
public PeertubeAdapter(Context context, String instance, boolean ownVideos, List<Peertube> peertubes){
|
|
|
|
this.peertubes = peertubes;
|
|
|
|
layoutInflater = LayoutInflater.from(context);
|
|
|
|
this.context = context;
|
|
|
|
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) {
|
2018-10-17 14:34:29 +02:00
|
|
|
return new PeertubeAdapter.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-01-03 14:23:13 +01:00
|
|
|
if( peertube.getInstance() == null)
|
|
|
|
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);
|
|
|
|
if( theme == Helper.THEME_LIGHT){
|
|
|
|
holder.main_container.setBackgroundResource(R.color.mastodonC3__);
|
|
|
|
}else if (theme == Helper.THEME_DARK){
|
|
|
|
holder.main_container.setBackgroundResource(R.color.mastodonC1_);
|
|
|
|
}else if (theme == Helper.THEME_BLACK){
|
|
|
|
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);
|
2019-01-09 18:26:25 +01: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());
|
2018-10-17 14:34:29 +02:00
|
|
|
Helper.loadGiF(context, account.getAvatar(), holder.peertube_profile);
|
2019-01-07 19:16:15 +01:00
|
|
|
|
|
|
|
|
2019-01-13 11:51:17 +01:00
|
|
|
if( peertube.getHeaderType() != null && peertube.getHeaderTypeValue() != null) {
|
2019-01-12 19:04:12 +01:00
|
|
|
String type = peertube.getHeaderType();
|
|
|
|
switch (type){
|
2019-01-13 11:51:17 +01:00
|
|
|
case "tags":
|
2019-01-13 11:54:28 +01:00
|
|
|
holder.header_title.setText(String.format("#%s", peertube.getHeaderTypeValue()));
|
2019-01-12 19:04:12 +01:00
|
|
|
break;
|
|
|
|
default:
|
2019-01-13 11:51:17 +01:00
|
|
|
holder.header_title.setText(peertube.getHeaderTypeValue());
|
2019-01-12 19:04:12 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
holder.header_title.setVisibility(View.VISIBLE);
|
|
|
|
}else {
|
|
|
|
holder.header_title.setVisibility(View.GONE);
|
|
|
|
}
|
|
|
|
|
2019-01-07 19:16:15 +01:00
|
|
|
if( !this.ownVideos) {
|
|
|
|
holder.peertube_profile.setOnClickListener(new View.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
|
|
|
//For remote peertube instance
|
|
|
|
if (!peertube.getInstance().equals(Helper.getLiveInstance(context)))
|
|
|
|
CrossActions.doCrossProfile(context, account);
|
|
|
|
else {
|
|
|
|
Intent intent = new Intent(context, ShowAccountActivity.class);
|
|
|
|
Bundle b = new Bundle();
|
|
|
|
b.putBoolean("peertubeaccount", true);
|
|
|
|
b.putParcelable("account", peertube.getAccount());
|
|
|
|
|
|
|
|
intent.putExtras(b);
|
|
|
|
context.startActivity(intent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
holder.main_container.setOnClickListener(new View.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View 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", matcherLink.group(1).replace("https://", "").replace("http://", ""));
|
|
|
|
b.putString("video_id", matcherLink.group(2));
|
|
|
|
}
|
|
|
|
intent.putExtras(b);
|
|
|
|
context.startActivity(intent);
|
2018-10-06 15:59:00 +02:00
|
|
|
}
|
2019-01-07 19:16:15 +01:00
|
|
|
});
|
|
|
|
}else{
|
|
|
|
holder.main_container.setOnClickListener(new View.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
|
|
|
Intent intent = new Intent(context, PeertubeEditUploadActivity.class);
|
|
|
|
Bundle b = new Bundle();
|
2019-01-12 17:36:24 +01:00
|
|
|
b.putString("video_id",peertube.getUuid());
|
2019-01-07 19:16:15 +01:00
|
|
|
intent.putExtras(b);
|
|
|
|
context.startActivity(intent);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
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) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
class ViewHolder extends RecyclerView.ViewHolder{
|
2018-10-17 18:20:03 +02:00
|
|
|
LinearLayout main_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);
|
2018-10-06 15:59:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|