2020-09-16 17:27:07 +02:00
|
|
|
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 <http://www.gnu.org/licenses>. */
|
|
|
|
|
|
|
|
|
2020-09-17 19:01:31 +02:00
|
|
|
import android.app.Activity;
|
2020-09-16 17:27:07 +02:00
|
|
|
import android.content.Context;
|
2020-09-17 19:01:31 +02:00
|
|
|
import android.content.Intent;
|
|
|
|
import android.net.Uri;
|
2020-09-16 19:02:04 +02:00
|
|
|
import android.text.SpannableStringBuilder;
|
|
|
|
import android.text.Spanned;
|
2020-09-16 17:27:07 +02:00
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
import android.widget.Button;
|
|
|
|
import android.widget.TextView;
|
|
|
|
|
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
import androidx.recyclerview.widget.RecyclerView;
|
|
|
|
|
|
|
|
import java.util.Iterator;
|
|
|
|
import java.util.LinkedHashMap;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
import app.fedilab.fedilabtube.R;
|
|
|
|
import app.fedilab.fedilabtube.client.entities.Instance;
|
2020-09-16 19:02:04 +02:00
|
|
|
import app.fedilab.fedilabtube.helper.RoundedBackgroundSpan;
|
2020-09-16 17:27:07 +02:00
|
|
|
|
2020-09-17 19:01:31 +02:00
|
|
|
import static android.app.Activity.RESULT_OK;
|
2020-09-16 17:27:07 +02:00
|
|
|
import static app.fedilab.fedilabtube.MainActivity.peertubeInformation;
|
|
|
|
|
|
|
|
|
|
|
|
public class InstanceAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
|
|
|
|
|
|
|
|
private List<Instance> instances;
|
|
|
|
|
|
|
|
private Context context;
|
|
|
|
|
|
|
|
public InstanceAdapter(List<Instance> instances) {
|
|
|
|
this.instances = instances;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@NonNull
|
|
|
|
@Override
|
|
|
|
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
|
|
|
|
context = parent.getContext();
|
|
|
|
LayoutInflater layoutInflater = LayoutInflater.from(context);
|
2020-09-16 19:02:04 +02:00
|
|
|
return new ViewHolder(layoutInflater.inflate(R.layout.drawer_instance, parent, false));
|
2020-09-16 17:27:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position) {
|
|
|
|
final Instance instance = instances.get(position);
|
|
|
|
final ViewHolder holder = (ViewHolder) viewHolder;
|
|
|
|
|
2020-09-16 19:02:04 +02:00
|
|
|
if (instance.getShortDescription() != null && instance.getShortDescription().trim().length() > 0) {
|
|
|
|
holder.description.setText(instance.getShortDescription());
|
|
|
|
holder.description.setVisibility(View.VISIBLE);
|
|
|
|
} else {
|
|
|
|
holder.description.setVisibility(View.GONE);
|
|
|
|
}
|
|
|
|
|
2020-09-16 17:27:07 +02:00
|
|
|
holder.name.setText(instance.getName());
|
|
|
|
holder.host.setText(instance.getHost());
|
|
|
|
|
2020-09-16 19:02:04 +02:00
|
|
|
|
|
|
|
SpannableStringBuilder stringBuilder = new SpannableStringBuilder();
|
|
|
|
String between = "";
|
2020-09-18 16:08:52 +02:00
|
|
|
if (peertubeInformation != null && peertubeInformation.getCategories() != null) {
|
|
|
|
LinkedHashMap<Integer, String> info_cat = new LinkedHashMap<>(peertubeInformation.getCategories());
|
|
|
|
if (instance.getCategories() != null && instance.getCategories().size() > 0 && instance.getSpannableStringBuilder() == null) {
|
|
|
|
Iterator<Map.Entry<Integer, Integer>> it = instance.getCategories().entrySet().iterator();
|
|
|
|
while (it.hasNext()) {
|
|
|
|
Map.Entry<Integer, Integer> pair = it.next();
|
|
|
|
String cat = info_cat.get(pair.getKey());
|
|
|
|
stringBuilder.append(between);
|
|
|
|
if (cat != null && cat.trim().toLowerCase().compareTo("null") != 0) {
|
|
|
|
if (between.length() == 0) between = " ";
|
|
|
|
String tag = " " + cat + " ";
|
|
|
|
stringBuilder.append(tag);
|
|
|
|
stringBuilder.setSpan(new RoundedBackgroundSpan(context), stringBuilder.length() - tag.length(), stringBuilder.length() - tag.length() + tag.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
|
|
|
|
}
|
|
|
|
it.remove();
|
2020-09-16 19:02:04 +02:00
|
|
|
}
|
2020-09-18 16:08:52 +02:00
|
|
|
instance.setSpannableStringBuilder(stringBuilder);
|
2020-09-16 17:27:07 +02:00
|
|
|
}
|
|
|
|
}
|
2020-09-18 16:08:52 +02:00
|
|
|
if (instance.getSpannableStringBuilder() != null) {
|
2020-09-17 17:00:12 +02:00
|
|
|
holder.tags.setText(instance.getSpannableStringBuilder());
|
|
|
|
}
|
|
|
|
|
2020-09-18 16:08:52 +02:00
|
|
|
if (peertubeInformation != null && peertubeInformation.getLanguages() != null) {
|
|
|
|
LinkedHashMap<String, String> info_lang = new LinkedHashMap<>(peertubeInformation.getLanguages());
|
|
|
|
StringBuilder languages = new StringBuilder();
|
|
|
|
if (instance.getLanguages() != null && instance.getLanguages().size() > 0) {
|
|
|
|
Iterator<Map.Entry<Integer, String>> it = instance.getLanguages().entrySet().iterator();
|
|
|
|
while (it.hasNext()) {
|
|
|
|
Map.Entry<Integer, String> pair = it.next();
|
|
|
|
languages.append(info_lang.get(pair.getValue())).append(" ");
|
|
|
|
it.remove();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (languages.toString().trim().length() == 0) {
|
|
|
|
holder.languages.setVisibility(View.GONE);
|
|
|
|
} else {
|
|
|
|
holder.languages.setText(languages);
|
|
|
|
holder.languages.setVisibility(View.VISIBLE);
|
2020-09-16 17:27:07 +02:00
|
|
|
}
|
|
|
|
}
|
2020-09-16 19:02:04 +02:00
|
|
|
if (instance.getDefaultNSFWPolicy().compareTo("do_not_list") != 0) {
|
|
|
|
holder.sensitive_content.setText(context.getString(R.string.sensitive_content, instance.getDefaultNSFWPolicy()));
|
|
|
|
holder.sensitive_content.setVisibility(View.VISIBLE);
|
|
|
|
} else {
|
|
|
|
holder.sensitive_content.setVisibility(View.GONE);
|
|
|
|
}
|
2020-09-16 17:27:07 +02:00
|
|
|
holder.followers_instance.setText(context.getString(R.string.followers_instance, String.valueOf(instance.getTotalInstanceFollowers())));
|
2020-09-17 19:01:31 +02:00
|
|
|
|
|
|
|
holder.pickup.setOnClickListener(v -> {
|
|
|
|
Intent data = new Intent();
|
|
|
|
String instanceHost = instance.getHost();
|
|
|
|
data.setData(Uri.parse(instanceHost));
|
|
|
|
((Activity) context).setResult(RESULT_OK, data);
|
|
|
|
((Activity) context).finish();
|
|
|
|
});
|
2020-09-16 17:27:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public long getItemId(int position) {
|
|
|
|
return position;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getItemCount() {
|
|
|
|
return instances.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static class ViewHolder extends RecyclerView.ViewHolder {
|
|
|
|
|
|
|
|
Button pickup;
|
|
|
|
TextView name, host, description;
|
|
|
|
TextView tags, followers_instance, languages, sensitive_content;
|
|
|
|
|
|
|
|
public ViewHolder(@NonNull View itemView) {
|
|
|
|
super(itemView);
|
|
|
|
pickup = itemView.findViewById(R.id.pickup);
|
|
|
|
name = itemView.findViewById(R.id.name);
|
|
|
|
host = itemView.findViewById(R.id.host);
|
|
|
|
description = itemView.findViewById(R.id.description);
|
2020-09-16 19:02:04 +02:00
|
|
|
followers_instance = itemView.findViewById(R.id.followers_instance);
|
2020-09-16 17:27:07 +02:00
|
|
|
tags = itemView.findViewById(R.id.tags);
|
|
|
|
languages = itemView.findViewById(R.id.languages);
|
|
|
|
sensitive_content = itemView.findViewById(R.id.sensitive_content);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|