Some fixes

This commit is contained in:
Thomas 2020-09-17 17:00:12 +02:00
parent b23686ab64
commit 11209a5334
8 changed files with 177 additions and 63 deletions

View File

@ -229,4 +229,9 @@
<string name="help">Help</string>
<string name="sensitive_video"> Sensitive videos</string>
<string name="pickup_categories">Pickup categories</string>
<string name="do_not_list">Do not list</string>
<string name="blur">Blur</string>
<string name="display">Display</string>
<string name="no_opinion">No opinion</string>
</resources>

View File

@ -9,7 +9,10 @@
<string name="toast_error">Oops! An error occurred!</string>
<string name="title_muted">Muted</string>
<string name="title_channel">Channels</string>
<string name="do_not_list">Do not list</string>
<string name="blur">Blur</string>
<string name="display">Display</string>
<string name="no_opinion">No opinion</string>
<string name="instance_choice">Pickup an instance</string>
<string name="not_valide_instance">This instance does not seem to be valid!</string>
<string name="no_videos">No videos!</string>

View File

@ -14,13 +14,21 @@ package app.fedilab.fedilabtube;
* You should have received a copy of the GNU General Public License along with TubeLab; if not,
* see <http://www.gnu.org/licenses>. */
import android.os.Bundle;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.view.MenuItem;
import android.view.View;
import android.widget.CheckBox;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
@ -36,9 +44,12 @@ import app.fedilab.fedilabtube.client.APIResponse;
import app.fedilab.fedilabtube.client.entities.Instance;
import app.fedilab.fedilabtube.client.entities.InstanceParams;
import app.fedilab.fedilabtube.drawer.InstanceAdapter;
import app.fedilab.fedilabtube.helper.Helper;
import app.fedilab.fedilabtube.helper.RoundedBackgroundSpan;
import app.fedilab.fedilabtube.viewmodel.InstancesVM;
import es.dmoral.toasty.Toasty;
import static app.fedilab.fedilabtube.MainActivity.peertubeInformation;
@ -49,6 +60,10 @@ public class InstancePickerActivity extends AppCompatActivity {
private RelativeLayout mainLoader, textviewNoAction;
private List<Instance> instances;
private InstanceAdapter instanceAdapter;
boolean[] checkedItems;
String[] itemsLabel;
InstanceParams instanceParams;
private TextView categories_view;
@Override
protected void onCreate(Bundle savedInstanceState) {
@ -65,19 +80,91 @@ public class InstancePickerActivity extends AppCompatActivity {
mainLoader = findViewById(R.id.loader);
textviewNoAction = findViewById(R.id.no_action);
mainLoader.setVisibility(View.VISIBLE);
Button pickup_categories = findViewById(R.id.pickup_categories);
categories_view = findViewById(R.id.categories_view);
Spinner sensitive = findViewById(R.id.sensitive);
/*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();
String[] channelSensitive = new String[]{"do_not_list", "blur", "display", "no_opinion"};
String[] channelSensitivesLabel = new String[]{getString(R.string.do_not_list), getString(R.string.blur), getString(R.string.display), getString(R.string.no_opinion)};
ArrayAdapter<String> adapterChannel = new ArrayAdapter<>(InstancePickerActivity.this,
android.R.layout.simple_spinner_dropdown_item, channelSensitivesLabel);
sensitive.setAdapter(adapterChannel);
InstancesVM viewModel = new ViewModelProvider(InstancePickerActivity.this).get(InstancesVM.class);
sensitive.setSelection(1, false);
sensitive.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
instanceParams.setNsfwPolicy(channelSensitive[position]);
viewModel.getInstances(instanceParams).observe(InstancePickerActivity.this, apiResponse -> manageVIewInstance(apiResponse));
}
}*/
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
checkedItems = new boolean[peertubeInformation.getCategories().size()];
itemsLabel= new String[peertubeInformation.getCategories().size()];
pickup_categories.setOnClickListener(v->{
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(InstancePickerActivity.this);
LinkedHashMap<Integer, String> categories = peertubeInformation.getCategories();
int i = 0;
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();
itemsLabel[i] = pair.getValue();
checkedItems[i] = false;
it.remove();
i++;
}
}
dialogBuilder.setMultiChoiceItems(itemsLabel, checkedItems, (dialog, which, isChecked) -> {
// The user checked or unchecked a box
checkedItems[which] = isChecked;
});
dialogBuilder.setOnDismissListener(dialogInterface -> {
int j = 0;
SpannableStringBuilder stringBuilder = new SpannableStringBuilder();
String between = "";
stringBuilder.append(between);
List<Integer> cats = new ArrayList<>();
for(boolean itemcheked: checkedItems){
if( itemcheked) {
cats.add(j);
String cat = itemsLabel[j];
if (cat != null && cat.trim().toLowerCase().compareTo("null") != 0) {
if (between.length() == 0) between = " ";
String tag = " " + cat + " ";
stringBuilder.append(tag);
stringBuilder.setSpan(new RoundedBackgroundSpan(InstancePickerActivity.this), stringBuilder.length() - tag.length(), stringBuilder.length() - tag.length() + tag.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
stringBuilder.append(" ");
}
}
j++;
}
instanceParams.setCategoriesOr(cats);
categories_view.setText(stringBuilder, TextView.BufferType.SPANNABLE);
viewModel.getInstances(instanceParams).observe(InstancePickerActivity.this, this::manageVIewInstance);
});
dialogBuilder.setPositiveButton(R.string.validate, (dialog, id) -> dialog.dismiss());
AlertDialog alertDialog = dialogBuilder.create();
alertDialog.setTitle(getString(R.string.action_playlist_create));
alertDialog.show();
});
mainLoader.setVisibility(View.VISIBLE);
@ -88,14 +175,13 @@ public class InstancePickerActivity extends AppCompatActivity {
lv_instances.setLayoutManager(mLayoutManager);
if (getSupportActionBar() != null)
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
setTitle(R.string.instances_picker);
InstancesVM viewModel = new ViewModelProvider(InstancePickerActivity.this).get(InstancesVM.class);
viewModel.getInstances(new InstanceParams()).observe(InstancePickerActivity.this, this::manageVIewInstance);
instanceParams = new InstanceParams();
instanceParams.setNsfwPolicy(channelSensitive[1]);
viewModel.getInstances(instanceParams).observe(InstancePickerActivity.this, this::manageVIewInstance);
}

View File

@ -21,7 +21,6 @@ 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;
@ -600,27 +599,22 @@ public class PeertubeAPI {
if (instanceParams != null) {
if (instanceParams.getCategoriesOr() != null && instanceParams.getCategoriesOr().size() > 0) {
StringBuilder parameters = new StringBuilder();
parameters.append("[]&");
for (Integer category : instanceParams.getCategoriesOr())
parameters.append("categoriesOr=").append(category).append("&");
String strParam = parameters.toString();
strParam = strParam.substring(0, strParam.length() - 1);
params.put("categoriesOr[]", strParam);
for (String lang : instanceParams.getLanguagesOr())
parameters.append("categoriesOr[]=").append(lang).append("&");
parameters = new StringBuilder(parameters.substring(0, parameters.length() - 1).substring(15));
params.put("categoriesOr[]", parameters.toString());
}
if (instanceParams.getLanguagesOr() != null && instanceParams.getLanguagesOr().size() > 0) {
StringBuilder parameters = new StringBuilder();
parameters.append("[]&");
for (String language : instanceParams.getLanguagesOr())
parameters.append("languagesOr=").append(language).append("&");
String strParam = parameters.toString();
strParam = strParam.substring(0, strParam.length() - 1);
params.put("languagesOr[]", strParam);
for (String lang : instanceParams.getLanguagesOr())
parameters.append("languagesOr[]=").append(lang).append("&");
parameters = new StringBuilder(parameters.substring(0, parameters.length() - 1).substring(14));
params.put("languagesOr[]", parameters.toString());
}
params.put("minUserQuota", instanceParams.getMinUserQuota());
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);

View File

@ -14,6 +14,8 @@ package app.fedilab.fedilabtube.client.entities;
* You should have received a copy of the GNU General Public License along with TubeLab; if not,
* see <http://www.gnu.org/licenses>. */
import android.text.SpannableStringBuilder;
import java.util.Date;
import java.util.LinkedHashMap;
@ -41,6 +43,7 @@ public class Instance {
private String userVideoQuota;
private String version;
private boolean isNSFW;
private SpannableStringBuilder spannableStringBuilder;
public boolean isAutoBlacklistUserVideosEnabled() {
@ -210,4 +213,12 @@ public class Instance {
public void setUserVideoQuota(String userVideoQuota) {
this.userVideoQuota = userVideoQuota;
}
public SpannableStringBuilder getSpannableStringBuilder() {
return spannableStringBuilder;
}
public void setSpannableStringBuilder(SpannableStringBuilder spannableStringBuilder) {
this.spannableStringBuilder = spannableStringBuilder;
}
}

View File

@ -76,7 +76,7 @@ public class InstanceAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolde
SpannableStringBuilder stringBuilder = new SpannableStringBuilder();
String between = "";
if (instance.getCategories() != null && instance.getCategories().size() > 0) {
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();
@ -91,8 +91,12 @@ public class InstanceAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolde
}
it.remove();
}
instance.setSpannableStringBuilder(stringBuilder);
}
holder.tags.setText(stringBuilder);
if( instance.getSpannableStringBuilder() != null) {
holder.tags.setText(instance.getSpannableStringBuilder());
}
StringBuilder languages = new StringBuilder();
if (instance.getLanguages() != null && instance.getLanguages().size() > 0) {

View File

@ -25,8 +25,7 @@ public class RoundedBackgroundSpan extends ReplacementSpan {
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);
canvas.drawRoundRect(rect, 8, 8, paint);
paint.setColor(textColor);
canvas.drawText(text, start, end, x, y, paint);
}

View File

@ -18,55 +18,67 @@
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="@dimen/fab_margin"
android:fitsSystemWindows="true">
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_margin="@dimen/fab_margin">
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".InstancePickerActivity">
<LinearLayout
tools:context=".InstancePickerActivity"
>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
android:layout_height="match_parent"
>
<Button
android:id="@+id/pickup_categories"
style="@style/Base.Widget.AppCompat.Button.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/pickup_categories" />
android:text="@string/pickup_categories"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
/>
<TextView
android:id="@+id/categories"
app:layout_constraintTop_toBottomOf="@+id/pickup_categories"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:id="@+id/categories_view"
android:layout_width="match_parent"
android:minHeight="50dp"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="match_parent"
<TextView
android:layout_marginTop="10dp"
android:id="@+id/text_spinner"
app:layout_constraintTop_toBottomOf="@+id/categories_view"
app:layout_constraintStart_toStartOf="parent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:showDividers="end">
android:text="@string/sensitive_video"
android:labelFor="@+id/sensitive"/>
<Spinner
app:layout_constraintTop_toBottomOf="@+id/categories_view"
app:layout_constraintStart_toEndOf="@+id/text_spinner"
android:id="@+id/sensitive"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="14dp" />
<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
app:layout_constraintTop_toBottomOf="@+id/text_spinner"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="10dp"
android:layout_height="0dp"
android:orientation="vertical"
android:padding="@dimen/fab_margin">
>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/lv_instances"
@ -117,6 +129,6 @@
android:indeterminate="true" />
</RelativeLayout>
</LinearLayout>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
</androidx.drawerlayout.widget.DrawerLayout>