fedilab-Android-App/app/src/main/java/fr/gouv/etalab/mastodon/drawers/PeertubeAdapter.java

168 lines
6.7 KiB
Java
Raw Normal View History

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;
2018-10-14 15:20:05 +02:00
import fr.gouv.etalab.mastodon.activities.PeertubeActivity;
2018-10-06 15:59:00 +02:00
import fr.gouv.etalab.mastodon.asynctasks.ManageListsAsyncTask;
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;
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;
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);
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-06 15:59:00 +02:00
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
2018-10-17 14:34:29 +02:00
holder.peertube_profile.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CrossActions.doCrossProfile(context, account);
}
});
Glide.with(holder.peertube_video_image.getContext())
.load("https://" + peertube.getInstance() + peertube.getThumbnailPath())
.into(holder.peertube_video_image);
Helper.loadGiF(context, account.getAvatar(), holder.peertube_profile);
2018-10-17 18:20:03 +02:00
holder.main_container.setOnClickListener(new View.OnClickListener() {
2018-10-06 15:59:00 +02:00
@Override
public void onClick(View v) {
2018-10-14 15:20:05 +02:00
Intent intent = new Intent(context, PeertubeActivity.class);
2018-10-06 15:59:00 +02:00
Bundle b = new Bundle();
2018-10-07 10:45:34 +02:00
String finalUrl = "https://" + instance + peertube.getEmbedPath();
2018-10-06 15:59:00 +02:00
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);
2018-10-14 16:17:25 +02:00
b.putString("peertube_instance", matcherLink.group(1).replace("https://","").replace("http://",""));
b.putString("video_id", matcherLink.group(2));
2018-10-06 15:59:00 +02:00
}
intent.putExtras(b);
context.startActivity(intent);
}
});
}
@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;
2018-10-17 14:34:29 +02:00
ImageView peertube_profile, peertube_video_image;
TextView peertube_account_name, peertube_views, peertube_duration;
TextView peertube_title, peertube_date;
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);
2018-10-06 15:59:00 +02:00
}
}
}