add checkbox for selecting categories

This commit is contained in:
Thomas 2020-09-16 19:02:04 +02:00
parent 33c7f12180
commit 7f4c6faa1d
8 changed files with 141 additions and 11 deletions

View File

@ -227,4 +227,5 @@
<string name="sensitive_content">Sensitive content: %1$s</string>
<string name="followers_instance">%1$s followers instances</string>
<string name="help">Help</string>
<string name="sensitive_video"> Sensitive videos</string>
</resources>

View File

@ -241,6 +241,7 @@
<string name="action_instance_empty_content">No instances match these criteria</string>
<string name="instances_picker">Instances picker</string>
<string name="pickup_instance">Pickup this instance</string>
<string name="sensitive_video"> Sensitive videos</string>
<string name="sensitive_content">Sensitive content: %1$s</string>
<string name="followers_instance">%1$s followers instances</string>
<string name="help">Help</string>

View File

@ -17,6 +17,7 @@ package app.fedilab.fedilabtube;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.CheckBox;
import android.widget.RelativeLayout;
import android.widget.Toast;
@ -26,7 +27,10 @@ import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import app.fedilab.fedilabtube.client.APIResponse;
import app.fedilab.fedilabtube.client.entities.Instance;
@ -35,6 +39,8 @@ import app.fedilab.fedilabtube.drawer.InstanceAdapter;
import app.fedilab.fedilabtube.viewmodel.InstancesVM;
import es.dmoral.toasty.Toasty;
import static app.fedilab.fedilabtube.MainActivity.peertubeInformation;
public class InstancePickerActivity extends AppCompatActivity {
@ -60,6 +66,18 @@ public class InstancePickerActivity extends AppCompatActivity {
textviewNoAction = findViewById(R.id.no_action);
mainLoader.setVisibility(View.VISIBLE);
RelativeLayout checkbox_container = findViewById(R.id.checkbox_container);
LinkedHashMap<Integer, String> categories = peertubeInformation.getCategories();
if (categories != null && categories.size() > 0) {
Iterator<Map.Entry<Integer, String>> it = categories.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<Integer, String> pair = it.next();
CheckBox cb = new CheckBox(InstancePickerActivity.this);
cb.setText(pair.getValue());
checkbox_container.addView(cb);
it.remove();
}
}
mainLoader.setVisibility(View.VISIBLE);

View File

@ -21,6 +21,7 @@ import android.database.sqlite.SQLiteDatabase;
import android.os.Build;
import android.text.Html;
import android.text.SpannableString;
import android.util.Log;
import org.json.JSONArray;
import org.json.JSONException;
@ -617,8 +618,9 @@ public class PeertubeAPI {
params.put("languagesOr[]", strParam);
}
params.put("minUserQuota", instanceParams.getMinUserQuota());
params.put("nsfwPolicy", instanceParams.getNsfwPolicy());
params.put("nsfwPolicy[]", instanceParams.getNsfwPolicy());
}
Log.v(Helper.TAG, "params: " + params);
try {
String response = new HttpsConnection(context).get("https://instances.joinpeertube.org/api/v1/instances", 30, params, null);
@ -2109,7 +2111,11 @@ public class PeertubeAPI {
instance.setName(resobj.getString("name"));
instance.setVersion(resobj.getString("version"));
instance.setShortDescription(resobj.getString("shortDescription"));
instance.setNSFW(resobj.getBoolean("isNSFW"));
if (resobj.has("isNSFW")) {
instance.setNSFW(resobj.getBoolean("isNSFW"));
} else {
instance.setNSFW(false);
}
instance.setSignupAllowed(resobj.getBoolean("signupAllowed"));
instance.setSupportsIPv6(resobj.getBoolean("supportsIPv6"));
instance.setTotalInstanceFollowers(resobj.getInt("totalInstanceFollowers"));

View File

@ -16,6 +16,8 @@ package app.fedilab.fedilabtube.drawer;
import android.content.Context;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
@ -32,6 +34,7 @@ import java.util.Map;
import app.fedilab.fedilabtube.R;
import app.fedilab.fedilabtube.client.entities.Instance;
import app.fedilab.fedilabtube.helper.RoundedBackgroundSpan;
import static app.fedilab.fedilabtube.MainActivity.peertubeInformation;
@ -52,7 +55,7 @@ public class InstanceAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolde
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
context = parent.getContext();
LayoutInflater layoutInflater = LayoutInflater.from(context);
return new PeertubeAdapter.ViewHolder(layoutInflater.inflate(R.layout.drawer_instance, parent, false));
return new ViewHolder(layoutInflater.inflate(R.layout.drawer_instance, parent, false));
}
@Override
@ -60,33 +63,61 @@ public class InstanceAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolde
final Instance instance = instances.get(position);
final ViewHolder holder = (ViewHolder) viewHolder;
holder.description.setText(instance.getShortDescription());
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);
}
holder.name.setText(instance.getName());
holder.host.setText(instance.getHost());
StringBuilder categories = new StringBuilder();
SpannableStringBuilder stringBuilder = new SpannableStringBuilder();
String between = "";
if (instance.getCategories() != null && instance.getCategories().size() > 0) {
Iterator<Map.Entry<Integer, Integer>> it = instance.getCategories().entrySet().iterator();
while (it.hasNext()) {
Map.Entry<Integer, Integer> pair = it.next();
LinkedHashMap<Integer, String> info_cat = peertubeInformation.getCategories();
categories.append(info_cat.get(pair.getKey())).append(" ");
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();
}
}
holder.tags.setText(categories);
holder.tags.setText(stringBuilder);
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(pair.getKey()).append(" ");
LinkedHashMap<String, String> info_lang = peertubeInformation.getLanguages();
languages.append(info_lang.get(pair.getValue())).append(" ");
it.remove();
}
}
holder.languages.setText(languages);
holder.sensitive_content.setText(context.getString(R.string.sensitive_content, instance.getDefaultNSFWPolicy()));
if (languages.toString().trim().length() == 0) {
holder.languages.setVisibility(View.GONE);
} else {
holder.languages.setText(languages);
holder.languages.setVisibility(View.VISIBLE);
}
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);
}
holder.followers_instance.setText(context.getString(R.string.followers_instance, String.valueOf(instance.getTotalInstanceFollowers())));
}
@ -113,6 +144,7 @@ public class InstanceAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolde
name = itemView.findViewById(R.id.name);
host = itemView.findViewById(R.id.host);
description = itemView.findViewById(R.id.description);
followers_instance = itemView.findViewById(R.id.followers_instance);
tags = itemView.findViewById(R.id.tags);
languages = itemView.findViewById(R.id.languages);
sensitive_content = itemView.findViewById(R.id.sensitive_content);

View File

@ -0,0 +1,42 @@
package app.fedilab.fedilabtube.helper;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.text.style.ReplacementSpan;
import org.jetbrains.annotations.NotNull;
import app.fedilab.fedilabtube.R;
public class RoundedBackgroundSpan extends ReplacementSpan {
private int backgroundColor;
private int textColor;
public RoundedBackgroundSpan(Context context) {
super();
backgroundColor = context.getResources().getColor(R.color.colorAccent_full);
textColor = context.getResources().getColor(R.color.browser_actions_bg_grey);
}
@Override
public void draw(Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, @NotNull Paint paint) {
RectF rect = new RectF(x, top, x + measureText(paint, text, start, end), bottom);
paint.setColor(backgroundColor);
int CORNER_RADIUS = 8;
canvas.drawRoundRect(rect, CORNER_RADIUS, CORNER_RADIUS, paint);
paint.setColor(textColor);
canvas.drawText(text, start, end, x, y, paint);
}
@Override
public int getSize(Paint paint, CharSequence text, int start, int end, Paint.FontMetricsInt fm) {
return Math.round(paint.measureText(text, start, end));
}
private float measureText(Paint paint, CharSequence text, int start, int end) {
return paint.measureText(text, start, end);
}
}

View File

@ -25,6 +25,35 @@
android:layout_height="match_parent"
tools:context=".InstancePickerActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:id="@+id/checkbox_container"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/sensitive_video" />
<Spinner
android:id="@+id/sensitive"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp" />
</LinearLayout>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"

View File

@ -42,7 +42,8 @@
android:id="@+id/host"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="@+id/name"
android:layout_marginStart="10dp"
app:layout_constraintStart_toEndOf="@+id/name"
app:layout_constraintTop_toTopOf="parent" />
<TextView